-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton.ts
More file actions
396 lines (343 loc) · 10 KB
/
button.ts
File metadata and controls
396 lines (343 loc) · 10 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
namespace user_interface_base {
export class Borders {
constructor(
public top: number,
public bottom: number,
public left: number,
public right: number
) { }
}
export class ButtonStyle {
constructor(
public fill: number,
public borders: Borders,
public shadow: boolean
) { }
}
/**
* Parameters for a Button constructor, which controls the:
* background behind the bitmap in the button; whether or not it has a shadow effect;
* and the style of the borders around the button.
*/
export namespace ButtonStyles {
export const ShadowedWhite = new ButtonStyle(
1,
new Borders(1, 12, 1, 1),
true
)
export const LightShadowedWhite = new ButtonStyle(
1,
new Borders(1, 11, 1, 1),
true
)
export const FlatWhite = new ButtonStyle(
1,
new Borders(1, 1, 1, 1),
false
)
/*
export const RectangleWhite = new ButtonStyle(
1,
new Borders(0, 0, 0, 0),
false
)
*/
export const BorderedPurple = new ButtonStyle(
11,
new Borders(12, 12, 12, 12),
false
)
export const RedBorderedWhite = new ButtonStyle(
1,
new Borders(2, 2, 2, 2),
false
)
export const Transparent = new ButtonStyle(
0,
new Borders(0, 0, 0, 0),
false
)
}
export function borderLeft(style: ButtonStyle) {
return style.borders.left ? 1 : 0
}
export function borderTop(style: ButtonStyle) {
return style.borders.top ? 1 : 0
}
export function borderRight(style: ButtonStyle) {
return style.borders.right ? 1 : 0
}
export function borderBottom(style: ButtonStyle) {
return style.borders.bottom ? 1 : 0
}
export function borderWidth(style: ButtonStyle) {
return borderLeft(style) + borderRight(style)
}
export function borderHeight(style: ButtonStyle) {
return borderTop(style) + borderBottom(style)
}
/**
* This is currently only extended by Button.
* Some navigators and pickers choose to operate on this instead of buttons for generality.
* See the Button class below.
*/
export class ButtonBase implements IComponent, ISizable, IPlaceable {
public icon: Sprite
private xfrm_: Affine
private style: ButtonStyle
constructor(x: number, y: number, style: ButtonStyle, parent: Affine) {
this.xfrm_ = new Affine()
this.xfrm.localPos.x = x
this.xfrm.localPos.y = y
this.style = style
this.xfrm.parent = parent
}
public get xfrm() {
return this.xfrm_
}
public get width() {
return this.bounds.width
}
public get height() {
return this.bounds.height
}
public get bounds() {
// Returns bounds in local space
return Bounds.GrowXY(
this.icon.bounds,
borderLeft(this.style),
borderTop(this.style)
)
}
public get rootXfrm(): Affine {
let xfrm = this.xfrm
while (xfrm.parent) {
xfrm = xfrm.parent
}
return xfrm
}
public buildSprite(img: Bitmap) {
this.icon = new Sprite({
parent: this,
img,
})
this.icon.xfrm.parent = this.xfrm
}
public getImage() {
return this.icon.image
}
public occlusions(bounds: Bounds) {
return this.icon.occlusions(bounds)
}
public setVisible(visible: boolean) {
this.icon.invisible = !visible
if (!visible) {
this.hover(false)
}
}
public visible() {
return !this.icon.invisible
}
public hover(hov: boolean) { }
public update() { }
isOffScreenX(): boolean {
return this.icon.isOffScreenX()
}
draw() {
this.drawStyle()
this.drawIcon()
}
private drawIcon() {
this.icon.draw()
}
private drawStyle() {
if (this.style.fill)
Screen.fillBoundsXfrm(
this.xfrm,
this.icon.bounds,
this.style.fill
)
if (this.style.borders)
Screen.outlineBoundsXfrm4(
this.xfrm,
this.icon.bounds,
1,
this.style.borders
)
if (this.style.shadow) {
Screen.setPixelXfrm(
this.xfrm,
this.icon.bounds.left - 1,
this.icon.bounds.bottom,
this.style.borders.bottom
)
Screen.setPixelXfrm(
this.xfrm,
this.icon.bounds.right + 1,
this.icon.bounds.bottom,
this.style.borders.bottom
)
}
}
}
/**
* GUI Button with a bitmap sprite, border colour, bitmap background colour,
* text can be optoinally displayed below the button.
* These buttons are typically used by navigators,
* which map physical button presses over a row or grid of these buttons.
*
* ALL arguments for the Button constructor are optional and have defaults.
*/
export class Button extends ButtonBase {
/** The bitmap to be displayed by the button, if it is a string it will be looked up; see coreAssets.ts */
private iconId: string | number | Bitmap
/** The text to be displayed below the button, formerly used as a lookup for the text to be displayed. */
private _ariaId: string
/**
* The action the button should take, typically used to transition between scenes.
* The button parameter is optional. It may be useful if you want to change the boundaryColor, or state, when pressed.
*/
public onClick?: (button: Button) => void
/**
* Used by draw to change boundary colour to .boundaryColor when this is true, and .dynamicBoundaryColorsOn
*/
public selected: boolean
/**
* Do you want the buttons boundary to change to .boundaryColor when pressed?
*/
private dynamicBoundaryColorsOn: boolean
/**
* used by .dynamicBoundaryColorsOn and .selected, see .draw()
*/
private boundaryColor: number
/**
* Seldom used, you can store arbitrary data here.
* This might be useful when you choose to pass button with onClick().
*/
public state: any[]
/**
* Used to disable a button.
*/
public pressable: boolean
public get ariaId(): string {
return this._ariaId
}
public set ariaId(value: string) {
this._ariaId = value
}
get getLocalX() { return this.xfrm.localPos.x }
get getLocalY() { return this.xfrm.localPos.y }
set setLocalX(x: number) { this.xfrm.localPos.x = x }
set setLocalY(y: number) { this.xfrm.localPos.y = y }
reportAria(force = false) {
// const msg: accessibility.TileAccessibilityMessage = {
// type: "tile",
// value: this.ariaId,
// force,
// }
// accessibility.setLiveContent(msg)
}
constructor(opts: {
parent?: IPlaceable
style?: ButtonStyle
icon?: string | number | Bitmap
ariaId?: string
x?: number
y?: number
onClick?: (button: Button) => void,
dynamicBoundaryColorsOn?: boolean
boundaryColor?: number,
flipIcon?: boolean,
state?: any[]
}) {
super(
(opts.x != null) ? opts.x : 0,
(opts.y != null) ? opts.y : 0,
opts.style || ButtonStyles.Transparent,
opts.parent && opts.parent.xfrm
)
this.iconId = opts.icon
this._ariaId = (opts.ariaId != null) ? opts.ariaId : ""
this.onClick = opts.onClick
this.buildSprite(this.image_())
if (opts.flipIcon) {
this.icon.image = this.icon.image.clone()
this.icon.image.flipY()
}
this.selected = false
this.pressable = true
// Setup dynamic boundary colours.
// This is used by .draw()
// If the button is hovered it is blue, it it is selected (A pressed once) it is green, other button boundaries are red.
// This remains the case even if you are no longer hovering over the button.
// This means that all buttons with dynamicBoundaryColorsOn will have boundaries.
// MicroData/sensorSelect.ts uses this to select multiple buttons.
if (opts.dynamicBoundaryColorsOn == null) {
opts.dynamicBoundaryColorsOn = false
}
else {
this.dynamicBoundaryColorsOn = opts.dynamicBoundaryColorsOn
this.boundaryColor = 2 // Red
}
if (opts.boundaryColor != null) {
this.dynamicBoundaryColorsOn = true
this.boundaryColor = opts.boundaryColor
}
// This is null if not passed in.
this.state = opts.state
}
public getIcon() {
return this.iconId
}
public toggleDynamicBoundaryColors() {
this.dynamicBoundaryColorsOn = !this.dynamicBoundaryColorsOn
}
public toggleSelected(): void {
this.selected = !this.selected
}
private image_() {
return typeof this.iconId == "string" || typeof this.iconId == "number"
? getIcon(this.iconId, false)
: this.iconId
}
public setIcon(iconId: string | number, img?: Bitmap) {
this.iconId = iconId
if (img) this.icon.setImage(img)
else this.buildSprite(this.image_())
}
/**
* Is it visible and pressable?
*/
public clickable() {
return this.visible() && this.pressable
}
/**
* invokes .onClick(), but only if it is clickable and non-null.
*/
public click() {
if (!this.clickable()) {
return
}
if (this.onClick && this.onClick != null) {
this.onClick(this)
}
}
public draw() {
super.draw()
// Draw a boundary colour if requested, this allows you to change a buttons border color based upon whether or not it has been clicked.
if (this.dynamicBoundaryColorsOn) {
const boundaryColour = (this.selected && this.pressable) ? 7 : this.boundaryColor // 7 = green
// Draw the outline multiple times for a thicker border.
// TODO: optimise this, and the underlying outlineBoundsXfrm, outlineBoundsXfrm invokes drawLine between 4 and 8 times each!
for (let dist = 1; dist <= 3; dist++) {
Screen.outlineBoundsXfrm(
this.xfrm,
this.icon.bounds,
dist,
boundaryColour
)
}
}
}
}
}