forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigator.ts
444 lines (396 loc) · 14.3 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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
finished: () => void
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
}
}
export class RowNavigator implements INavigator {
protected buttonGroups: Button[][]
protected buttons: Button[]
protected row: number
protected col: number
constructor() {
this.buttonGroups = []
this.buttons = []
}
public clear() {
this.buttonGroups = []
this.buttons = []
}
public getRow() {
return this.row
}
public addButtons(btns: Button[]) {
this.buttonGroups.push(btns)
this.buttons = this.buttons.concat(btns)
}
public finished() {}
// this is inefficient
public screenToButton(x: number, y: number): Button {
const p = new Vec2(x, y)
const target = this.buttons.find(btn =>
Bounds.Translate(btn.bounds, btn.xfrm.worldPos).contains(p)
)
if (target) {
for (let row = 0; row < this.buttonGroups.length; row++) {
for (
let col = 0;
col < this.buttonGroups[row].length;
col++
) {
if (this.buttonGroups[row][col] === target) {
this.row = row
this.col = col
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 add 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: string[] = ruleDef.sensors
.map(s => s.tid)
.concat(ruleDef.filters.map(s => s.tid))
const dos: string[] = ruleDef.actuators
.map(s => s.tid)
.concat(ruleDef.modifiers.map(s => s.tid))
accessibilityMessage = <accessibility.RuleAccessibilityMessage>{
type: "rule",
whens,
dos,
}
} else {
accessibilityMessage = <accessibility.TileAccessibilityMessage>{
type: "tile",
value: (ret ? ret.ariaId : "") || "",
force: true,
}
}
accessibility.setLiveContent(accessibilityMessage)
}
}
class MatrixNavigator extends RowNavigator {
protected hasDelete: boolean
public initialCursor(row: number = 0, col: number = 0) {
this.hasDelete = this.buttonGroups[0].length == 1
this.row = 2 + (this.hasDelete ? 1 : 0)
this.col = 2
const btn = this.buttonGroups[this.row][this.col]
this.reportAria(btn)
return btn
}
protected reportAria(btn: Button) {
if (!btn) {
return null
}
if (this.hasDelete && this.row == 0 && this.col == 0) {
accessibility.setLiveContent(<
accessibility.TextAccessibilityMessage
>{
type: "text",
value: "delete_tile",
force: true,
})
return null
}
return btn
}
}
// accessibility for LEDs
export class LEDNavigator extends MatrixNavigator {
protected reportAria(b: Button): Button {
const btn = super.reportAria(b)
if (!btn) return null
const on = btn.getIcon() == "solid_red"
accessibility.setLiveContent(<
accessibility.LEDAccessibilityMessage
>{
type: "led",
on,
x: this.col,
y: this.row,
force: true,
})
return null
}
}
// accessibility for melody
export class MelodyNavigator extends MatrixNavigator {
protected reportAria(b: Button): Button {
let btn = super.reportAria(b)
if (!btn) return null
const on = btn.getIcon() === "note_on"
const index = this.hasDelete ? this.row - 1 : this.row
accessibility.setLiveContent(<
accessibility.NoteAccessibilityMessage
>{
type: "note",
on,
index,
force: true,
})
return null
}
}
export class SimpleGridNavigator implements INavigator {
buttons: Button[]
curr: Button
private sortedButtons: Button[][]
constructor() {
this.buttons = []
}
public clear() {
this.buttons = []
this.curr = undefined
}
public addButtons(btns: Button[]) {
this.buttons = this.buttons.concat(btns)
}
private getRow() {
for (let row = 0; row < this.sortedButtons.length; row++) {
if (
this.curr.xfrm.worldPos.y ==
this.sortedButtons[row][0].xfrm.worldPos.y
)
return row
}
return -1
}
// call this when finished adding buttons
public finished() {
this.sortedButtons = []
// we now need to optimize a bit by sorting and creating rows
let currRow: Button[] = []
this.buttons.sort((a, b) => a.xfrm.worldPos.y - b.xfrm.worldPos.y)
this.buttons.forEach(btn => {
if (
currRow.length == 0 ||
btn.xfrm.worldPos.y == currRow[0].xfrm.worldPos.y
)
currRow.push(btn)
else {
this.sortedButtons.push(currRow)
currRow = [btn]
}
})
if (currRow.length) this.sortedButtons.push(currRow)
// sort each row by x coordinate
this.sortedButtons.forEach(btns =>
btns.sort((a, b) => a.xfrm.worldPos.x - b.xfrm.worldPos.x)
)
}
public move(dir: CursorDir): Button {
const findNearInX = (btns: Button[], col: number) => {
const nearInX = btns
.filter(
btn =>
Math.abs(
btn.xfrm.worldPos.x - this.curr.xfrm.worldPos.x
) <
btn.width >> 1
)
.shift()
if (!nearInX) return btns[col]
return nearInX
}
let btn: Button
if (!this.curr) {
btn = this.buttons[0]
} else {
const row = this.getRow()
const col = this.sortedButtons[row].indexOf(this.curr)
switch (dir) {
case CursorDir.Up: {
if (row > 0) {
const prevRow = this.sortedButtons[row - 1]
if (col < prevRow.length)
btn = findNearInX(prevRow, col)
else {
btn = prevRow[prevRow.length - 1]
}
} else {
throw new NavigationError(BACK_BUTTON_ERROR_KIND)
}
break
}
case CursorDir.Down: {
if (row < this.sortedButtons.length - 1) {
const nextRow = this.sortedButtons[row + 1]
if (col < nextRow.length)
btn = findNearInX(nextRow, col)
else {
btn = nextRow[nextRow.length - 1]
}
}
break
}
case CursorDir.Left: {
if (col > 0) btn = this.sortedButtons[row][col - 1]
else if (row > 0) {
const prevRow = this.sortedButtons[row - 1]
btn = prevRow[prevRow.length - 1]
} else {
const prevRow =
this.sortedButtons[
this.sortedButtons.length - 1
]
btn = prevRow[prevRow.length - 1]
}
break
}
case CursorDir.Right: {
if (col < this.sortedButtons[row].length - 1)
btn = this.sortedButtons[row][col + 1]
else if (row < this.sortedButtons.length - 1) {
const nextRow = this.sortedButtons[row + 1]
btn = nextRow[0]
} else {
const nextRow = this.sortedButtons[0]
btn = nextRow[0]
}
break
}
}
}
if (btn) {
btn.reportAria(true)
this.curr = btn
}
return this.curr
}
public updateAria() {
this.curr.reportAria(true)
}
public screenToButton(x: number, y: number): Button {
const p = new Vec2(x, y)
const target = this.buttons.find(btn =>
Bounds.Translate(btn.bounds, btn.xfrm.worldPos).contains(p)
)
if (target) this.curr = target
return target
}
public getCurrent(): Button {
return this.curr
}
public initialCursor(row: number, col: number): Button {
this.curr = this.buttons[row]
return this.curr
}
}
}