forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.ts
162 lines (143 loc) · 4.74 KB
/
cursor.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
namespace microcode {
export type CursorCancelHandler = () => void
export enum CursorDir {
Up,
Down,
Left,
Right,
Back,
}
export interface CursorState {
navigator: INavigator
pos: Vec2
ariaId: string
size: Bounds
}
export class Cursor implements IComponent, IPlaceable {
xfrm: Affine
navigator: INavigator
cancelHandlerStack: CursorCancelHandler[]
moveStartMs: number
moveDest: Vec2
ariaPos: Vec2
ariaId: string
size: Bounds
cycle: number
visible = true
constructor() {
this.xfrm = new Affine()
this.cancelHandlerStack = []
this.moveDest = new Vec2()
this.cycle = 1
this.setSize()
}
public moveTo(pos: Vec2, ariaId: string, sizeHint: Bounds) {
this.setSize(sizeHint)
this.moveDest.copyFrom(pos)
this.moveStartMs = control.millis()
this.setAriaContent(ariaId)
}
public setAriaContent(ariaId: string, ariaPos: Vec2 = null) {
this.ariaId = ariaId || ""
this.ariaPos = ariaPos
}
public snapTo(x: number, y: number, ariaId: string, sizeHint: Bounds) {
this.setSize(
sizeHint ||
new Bounds({ left: 0, top: 0, width: 16, height: 16 })
)
this.moveDest.x = this.xfrm.localPos.x = x
this.moveDest.y = this.xfrm.localPos.y = y
this.setAriaContent(ariaId)
}
public setSize(size?: Bounds) {
size =
size || new Bounds({ left: 0, top: 0, width: 16, height: 16 })
if (this.size) this.size.copyFrom(size)
else this.size = size.clone()
}
public saveState(): CursorState {
return {
navigator: this.navigator,
pos: this.xfrm.localPos.clone(),
ariaId: this.ariaId,
size: this.size.clone(),
}
}
public restoreState(state: CursorState) {
this.navigator = state.navigator
this.xfrm.localPos.copyFrom(state.pos)
this.moveDest.copyFrom(state.pos)
this.ariaId = state.ariaId
this.size.copyFrom(state.size)
}
public move(dir: CursorDir): Button {
return this.navigator.move(dir)
}
public click(): boolean {
let target = this.navigator.getCurrent() //.sort((a, b) => a.z - b.z);
if (target) {
target.click()
if (Options.profiling) control.heapSnapshot()
return true
}
return false
}
public cancel(): boolean {
if (this.cancelHandlerStack.length) {
this.cancelHandlerStack[this.cancelHandlerStack.length - 1]()
return true
}
return false
}
update() {
const currTimeMs = control.millis()
const elapsedTimeMs = currTimeMs - this.moveStartMs
if (elapsedTimeMs < 63) {
Vec2.LerpToRefFix(
this.xfrm.localPos,
this.moveDest,
elapsedTimeMs << 2,
this.xfrm.localPos
)
} else {
this.xfrm.localPos.copyFrom(this.moveDest)
}
this.cycle = currTimeMs % 1000 < 500 ? 1 : 0
}
draw() {
control.enablePerfCounter()
if (!this.visible) return
Screen.outlineBoundsXfrm(
this.xfrm,
this.size,
1,
this.cycle ? 6 : 9
)
Screen.outlineBoundsXfrm(
this.xfrm,
this.size,
2,
this.cycle ? 9 : 6
)
const text = accessibility.ariaToTooltip(this.ariaId)
if (text) {
const pos = this.ariaPos || this.xfrm.localPos
const n = text.length
const font = microcode.font
const w = font.charWidth * n
const h = font.charHeight
const x = Math.max(
Screen.LEFT_EDGE + 1,
Math.min(Screen.RIGHT_EDGE - 1 - w, pos.x - (w >> 1))
)
const y = Math.min(
pos.y + (this.size.width >> 1) + (font.charHeight >> 1) + 1,
Screen.BOTTOM_EDGE - 1 - font.charHeight
)
Screen.fillRect(x - 1, y - 1, w + 1, h + 2, 15)
Screen.print(text, x, y, 1, font)
}
}
}
}