-
Notifications
You must be signed in to change notification settings - Fork 18
/
navigator.ts
401 lines (356 loc) · 12.4 KB
/
navigator.ts
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
397
398
399
400
401
namespace microcode {
export interface INavigator {
clear: () => void
addButtons: (btns: Button[]) => void
move: (dir: CursorDir) => Button
getCurrent: () => Button
screenToButton: (x: number, y: number) => Button
initialCursor: (row: number, col: number) => Button
updateAria: () => void
}
export const BACK_BUTTON_ERROR_KIND = "back_button"
export const FORWARD_BUTTON_ERROR_KIND = "forward_button"
export class NavigationError {
kind: string
constructor(kind: string) {
this.kind = kind
}
}
// ragged rows of buttons
export class RowNavigator implements INavigator {
protected buttonGroups: Button[][]
protected row: number
protected col: number
constructor() {
this.buttonGroups = []
}
public clear() {
this.buttonGroups = []
}
public getRow() {
return this.row
}
public addButtons(btns: Button[]) {
this.buttonGroups.push(btns)
}
public screenToButton(x: number, y: number): Button {
const p = new Vec2(x, y)
for (let row = 0; row < this.buttonGroups.length; row++) {
const buttons = this.buttonGroups[row]
const target = buttons.find(btn =>
Bounds.Translate(btn.bounds, btn.xfrm.worldPos).contains(p)
)
if (target) {
this.row = row
this.col = buttons.indexOf(target)
return target
}
}
return undefined
}
public move(dir: CursorDir) {
this.makeGood()
switch (dir) {
case CursorDir.Up: {
if (this.row == 0)
throw new NavigationError(BACK_BUTTON_ERROR_KIND)
this.row--
// because the column in new row may be out of bounds
this.makeGood()
break
}
case CursorDir.Down: {
if (this.row == this.buttonGroups.length - 1)
throw new NavigationError(FORWARD_BUTTON_ERROR_KIND)
this.row++
// because the column in new row may be out of bounds
this.makeGood()
break
}
case CursorDir.Left: {
if (this.col == 0) {
if (this.row > 0) {
this.row--
} else {
this.row = this.buttonGroups.length - 1
}
this.col = this.buttonGroups[this.row].length - 1
} else this.col--
break
}
case CursorDir.Right: {
if (this.col == this.buttonGroups[this.row].length - 1) {
if (this.row < this.buttonGroups.length - 1) {
this.row++
} else {
this.row = 0
}
this.col = -1
}
this.col++
break
}
case CursorDir.Back: {
if (this.col > 0) this.col = 0
else if (this.row > 0) this.row--
else return undefined
break
}
}
const btn = this.buttonGroups[this.row][this.col]
this.reportAria(btn)
return btn
}
public updateAria() {
this.reportAria(this.getCurrent())
}
protected reportAria(btn: Button) {
if (btn) btn.reportAria(true)
}
public getCurrent(): Button {
return this.buttonGroups[this.row][this.col]
}
protected makeGood() {
if (this.row >= this.buttonGroups.length)
this.row = this.buttonGroups.length - 1
if (this.col >= this.buttonGroups[this.row].length)
this.col = this.buttonGroups[this.row].length - 1
}
public initialCursor(row: number = 0, col: number = 0) {
const rows = this.buttonGroups.length
while (row < 0) row = (row + rows) % rows
const cols = this.buttonGroups[row].length
while (col < 0) col = (col + cols) % cols
this.row = row
this.col = col
return this.buttonGroups[row][col]
}
}
// this adds accessibility for rule
export class RuleRowNavigator extends RowNavigator {
private rules: RuleDefn[]
constructor() {
super()
this.rules = []
}
/* overrides */
public clear() {
super.clear()
this.rules = []
}
public addRule(rule: RuleDefn) {
this.rules.push(rule)
}
public atRuleStart() {
return this.row >= 1 && this.col == 0
}
protected reportAria(ret: Button) {
if (!ret) {
return
}
let accessibilityMessage: accessibility.AccessibilityMessage
if (this.row > 0 && this.col == 0) {
const ruleDef = this.rules[this.row - 1]
const whens = ruleDef.sensors
.concat(ruleDef.filters)
.map(s => tidToString(s))
const dos: string[] = ruleDef.actuators
.concat(ruleDef.modifiers.map(t => getTid(t)))
.map(s => tidToString(s))
accessibilityMessage = <accessibility.RuleAccessibilityMessage>{
type: "rule",
whens,
dos,
}
} else {
accessibilityMessage = <accessibility.TileAccessibilityMessage>{
type: "tile",
value: (ret ? ret.ariaId : "") || "",
force: true,
}
}
accessibility.setLiveContent(accessibilityMessage)
}
}
// mostly a matrix, except for last row, which may be ragged
// also supports delete button
// add support for aria
export class PickerNavigator implements INavigator {
protected deleteButton: Button
protected row: number
protected col: number
constructor(private picker: Picker) {}
private get width() {
return this.picker.width
}
private get length() {
return this.picker.group.defs.length
}
get hasDelete() {
return !!this.deleteButton
}
moveToIndex(index: number) {
assert(index < this.length, "index out of bounds")
this.row = Math.idiv(index, this.width)
this.col = index % this.width
this.reportAria()
return this.picker.group.getButtonAtIndex(index)
}
private height() {
return Math.ceil(this.length / this.width)
}
private currentRowWidth() {
assert(this.row >= 0, "row out of bounds")
return this.row < this.height() - 1
? this.width
: this.length - this.width * (this.height() - 1)
}
public initialCursor(row: number = 0, col: number = 0): Button {
this.row = row
this.col = col
const btn = this.getCurrent()
if (btn) {
this.reportAria()
return undefined // TODO
}
return undefined
}
clear() {
this.deleteButton = undefined
}
addButtons(btns: ButtonBase[]) {}
addDelete(btn: Button) {
this.deleteButton = btn
}
getCurrent() {
// console.log(`row: ${this.row}, col: ${this.col}`)
if (this.row == -1) {
return this.deleteButton
} else {
const index = this.row * this.width + this.col
if (index < this.length)
return this.picker.group.getButtonAtIndex(index)
}
return undefined
}
screenToButton(x: number, y: number): Button {
const p = new Vec2(x, y)
const btn = this.deleteButton
if (
btn &&
Bounds.Translate(btn.bounds, btn.xfrm.worldPos).contains(p)
)
return btn
const np = this.picker.group.getButtonAtScreen(x, y)
if (np) {
this.row = np.y
this.col = np.x
if (this.col >= this.currentRowWidth())
this.col = this.currentRowWidth() - 1
return this.getCurrent()
}
return undefined
}
move(dir: CursorDir) {
switch (dir) {
case CursorDir.Up: {
if (this.row == -1 || (!this.deleteButton && this.row == 0))
throw new NavigationError(BACK_BUTTON_ERROR_KIND)
if (this.row > 0) this.row--
else if (this.deleteButton) this.row = -1
break
}
case CursorDir.Down: {
if (this.row < this.height() - 1) {
this.row++
if (this.col >= this.currentRowWidth()) {
this.col = this.currentRowWidth() - 1
}
} else throw new NavigationError(FORWARD_BUTTON_ERROR_KIND)
break
}
case CursorDir.Left: {
if (this.col > 0) this.col--
else if (this.row > 0) {
this.row--
this.col = this.width - 1
} else if (this.deleteButton) {
this.row = -1
}
break
}
case CursorDir.Right: {
if (this.row == -1) {
this.row = 0
this.col = 0
} else if (this.col < this.currentRowWidth() - 1) this.col++
else if (this.row < this.height() - 1) {
this.row++
this.col = 0
}
break
}
}
this.reportAria()
return this.getCurrent()
}
public updateAria() {
this.reportAria()
}
protected reportAria() {
if (this.row == -1) {
accessibility.setLiveContent(<
accessibility.TextAccessibilityMessage
>{
type: "text",
value: "delete_tile",
force: true,
})
}
}
}
// accessibility for LEDs
export class LEDNavigator extends PickerNavigator {
constructor(picker: Picker) {
super(picker)
this.row = 2
this.col = 2
}
protected reportAria() {
super.reportAria()
if (this.row == -1) return
const on = true // TODO: btn.getIcon() == "solid_red"
accessibility.setLiveContent(<
accessibility.LEDAccessibilityMessage
>{
type: "led",
on,
x: this.col,
y: this.row,
force: true,
})
}
}
// accessibility for melody
export class MelodyNavigator extends PickerNavigator {
constructor(picker: Picker) {
super(picker)
this.row = 2
this.col = 2
}
protected reportAria() {
super.reportAria()
if (this.row == -1) return
const on = true // TODO btn.getIcon() === "note_on"
const index = this.hasDelete ? this.row - 1 : this.row
accessibility.setLiveContent(<
accessibility.NoteAccessibilityMessage
>{
type: "note",
on,
index,
force: true,
})
}
}
}