forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.tsx
More file actions
160 lines (135 loc) · 3.95 KB
/
ui.tsx
File metadata and controls
160 lines (135 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/// <reference path="./jsxtypes.d.ts" />
import * as ds from "@devicescript/core"
import { Image, ImageContext } from "@devicescript/graphics"
/**
* Type-safe wrapper for Object.assign()
*/
function setup<T, S extends T>(self: S, props: T) {
Object.assign(self, props)
}
export abstract class BaseElement implements JSX.Element {
abstract render(ctx: ImageContext): void
}
export abstract class PositionElement extends BaseElement {
x: number
y: number
}
export abstract class ParentElement extends BaseElement {
children?: JSX.Element | JSX.Element[]
renderChildren(ctx: ImageContext) {
if (!this.children) return
if (Array.isArray(this.children))
for (const ch of this.children) ch.render(ctx)
else this.children.render(ctx)
}
}
export interface PositionProps {
x?: number
y?: number
}
export class Translate extends ParentElement {
x: number
y: number
constructor(props: PositionProps & JSX.BaseProps) {
super()
setup(this, props)
}
render(ctx: ImageContext): void {
if (this.x || this.y) {
ctx.save()
ctx.translate(this.x ?? 0, this.y ?? 0)
this.renderChildren(ctx)
ctx.restore()
} else this.renderChildren(ctx)
}
}
export class Style extends ParentElement {
strokeColor: number
fillColor: number
constructor(
props: {
strokeColor?: number
fillColor?: number
} & JSX.BaseProps
) {
super()
setup(this, props)
}
render(ctx: ImageContext): void {
ctx.save()
if (this.strokeColor !== undefined) ctx.strokeColor = this.strokeColor
if (this.fillColor !== undefined) ctx.fillColor = this.fillColor
this.renderChildren(ctx)
ctx.restore()
}
}
export class Text extends PositionElement {
children: string
constructor(props: PositionProps & { children: string }) {
super()
setup(this, props)
}
render(ctx: ImageContext): void {
ctx.fillText(this.children, this.x ?? 0, this.y ?? 0)
}
}
export class Rect extends PositionElement {
width: number
height: number
constructor(props: PositionProps & { width: number; height: number }) {
super()
setup(this, props)
}
render(ctx: ImageContext): void {
ctx.strokeRect(this.x ?? 0, this.y ?? 0, this.width, this.height)
}
}
export class FillRect extends Rect {
override render(ctx: ImageContext): void {
ctx.fillRect(this.x ?? 0, this.y ?? 0, this.width, this.height)
}
}
export class Fragment extends ParentElement {
constructor(props: JSX.BaseProps) {
super()
setup(this, props)
}
render(ctx: ImageContext): void {
this.renderChildren(ctx)
}
}
export function HorizontalGauge(props: {
x?: number
y?: number
width: number
height: number
value: number
showPercent?: boolean
}): JSX.Element {
const { x, y, width, height, value, showPercent } = props
const w = Math.round((width - 4) * value)
const fc = value > 0.5 ? 0 : 1
return (
<Translate x={x} y={y}>
<Rect width={width} height={height}></Rect>
<FillRect x={2} y={2} width={w} height={height - 4}></FillRect>
{!!showPercent && (
<Style fillColor={fc}>
<Text x={width >> 1} y={2}>
{Math.round(value * 100) + "%"}
</Text>
</Style>
)}
</Translate>
)
}
;(ds as typeof ds)._jsx = (element, props) => {
if (element === "") element = Fragment
if (typeof element === "string") throw new Error(`invalid ${element}`)
// in DeviceScript class ctors can be called with or without new
else return (element as JSX.FunctionComponent<JSX.BaseProps>)(props)
}
export function renderOnImage(elt: JSX.Element, image: Image) {
image.fill(0)
elt.render(image.allocContext())
}