forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor.ts
252 lines (234 loc) · 8.51 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
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
namespace microcode {
export type CursorCancelHandler = () => void
const SEARCH_INCR = 8
const SEARCH_MAX = 160
const SEARCH_SLOPE = 1.1
export class Cursor extends Component implements IPlaceable {
private xfrm_: Affine
stylus: Sprite
private hitbox_: Bounds
quadtree: QuadTree
cancelHandlerStack: CursorCancelHandler[]
anim: Animation
dest: Vec2
public get xfrm() {
return this.xfrm_
}
public get hitbox() {
return Bounds.Translate(this.hitbox_, this.xfrm.worldPos)
}
constructor() {
super("cursor")
this.xfrm_ = new Affine()
this.stylus = new Sprite({
parent: this,
img: icons.get("cursor"),
})
// Small hitbox around the pointer.
this.hitbox_ = new Bounds({
width: 3,
height: 3,
left: -3,
top: -2,
})
this.cancelHandlerStack = []
this.dest = new Vec2()
this.anim = new Animation((p: Vec2) => this.animCallback(p))
}
private animCallback(p: Vec2) {
this.xfrm.localPos = p
}
public moveTo(pos: Vec2) {
this.dest.copyFrom(pos)
this.anim.clearFrames()
this.anim.addFrame(
new EaseFrame({
duration: 0.05,
//curve: curves.easeOut(curves.easing.sq2),
curve: curves.linear(),
startValue: this.xfrm.localPos,
endValue: this.dest,
})
)
this.anim.start()
}
public snapTo(x: number, y: number) {
this.dest.x = this.xfrm.localPos.x = x
this.dest.y = this.xfrm.localPos.y = y
}
private query(opts: {
boundsFn: (dist: number) => Bounds
filterFn: (value: Button, dist: number) => boolean
}): Button {
if (this.anim && this.anim.playing) {
return null
}
let dist = SEARCH_INCR
let candidates: Button[]
let overlapping = this.getOverlapping()
while (true) {
const bounds = opts.boundsFn(dist)
candidates = this.quadtree
.query(bounds)
// filter and map to Button type
.filter(comp => comp.kind === "button")
.map(comp => comp as Button)
candidates = candidates
// Filter buttons overlapping the cursor.
.filter(btn => overlapping.indexOf(btn) < 0)
// Filter buttons per caller.
.filter(btn => opts.filterFn(btn, dist))
// Sort by distance from cursor.
.sort((a, b) => {
const apos = a.xfrm.worldPos
const bpos = b.xfrm.worldPos
return (
Vec2.DistSq(this.xfrm.worldPos, a.xfrm.worldPos) -
Vec2.DistSq(this.xfrm.worldPos, b.xfrm.worldPos)
)
})
if (candidates.length) {
break
}
// No candidates found, widen the search area.
dist += SEARCH_INCR
if (dist > SEARCH_MAX) {
break
}
}
return candidates.shift()
}
public queryUp(): Button {
return this.query({
boundsFn: dist => {
return new Bounds({
left: this.xfrm.worldPos.x - (dist >> 1),
top: this.xfrm.worldPos.y - dist,
width: dist,
height: dist,
})
},
filterFn: (btn, dist) => {
const pos = btn.xfrm.worldPos
// Filter to upward buttons that are more up than left or right from us.
return (
pos.y < this.xfrm.worldPos.y &&
Math.abs(pos.y - this.xfrm.worldPos.y) /
Math.abs(pos.x - this.xfrm.worldPos.x) >
SEARCH_SLOPE
)
},
})
}
public queryDown(): Button {
return this.query({
boundsFn: dist => {
return new Bounds({
left: this.xfrm.worldPos.x - (dist >> 1),
top: this.xfrm.worldPos.y,
width: dist,
height: dist,
})
},
filterFn: (btn, dist) => {
const pos = btn.xfrm.worldPos
// Filter to downward buttons that are more down than left or right from us.
return (
pos.y > this.xfrm.worldPos.y &&
Math.abs(pos.y - this.xfrm.worldPos.y) /
Math.abs(pos.x - this.xfrm.worldPos.x) >
SEARCH_SLOPE
)
},
})
}
public queryLeft(): Button {
return this.query({
boundsFn: dist => {
return new Bounds({
left: this.xfrm.worldPos.x - dist,
top: this.xfrm.worldPos.y - (dist >> 1),
width: dist,
height: dist,
})
},
filterFn: (btn, dist) => {
const pos = btn.xfrm.worldPos
// Filter to leftward buttons that are more left than up or down from us.
return (
pos.x < this.xfrm.worldPos.x &&
Math.abs(pos.x - this.xfrm.worldPos.x) /
Math.abs(pos.y - this.xfrm.worldPos.y) >
SEARCH_SLOPE
)
},
})
}
public queryRight(): Button {
return this.query({
boundsFn: dist => {
return new Bounds({
left: this.xfrm.worldPos.x,
top: this.xfrm.worldPos.y - (dist >> 1),
width: dist,
height: dist,
})
},
filterFn: (btn, dist) => {
const pos = btn.xfrm.worldPos
// Filter to rightward buttons that are to more right than up or down from us.
return (
pos.x > this.xfrm.worldPos.x &&
Math.abs(pos.x - this.xfrm.worldPos.x) /
Math.abs(pos.y - this.xfrm.worldPos.y) >
SEARCH_SLOPE
)
},
})
}
public click(): boolean {
let overlapping = this.getOverlapping() //.sort((a, b) => a.z - b.z);
if (overlapping.length) {
const btn = overlapping.shift()
btn.click()
return true
}
return false
}
public cancel(): boolean {
if (this.cancelHandlerStack.length) {
this.cancelHandlerStack[this.cancelHandlerStack.length - 1]()
return true
}
return false
}
private getOverlapping(): Button[] {
const crsb = this.hitbox
// Query for neary items.
const btns = this.quadtree
.query(crsb)
// filter and map to Button type
.filter(comp => comp.kind === "button")
.map(comp => comp as Button)
// filter to intersecting buttons
.filter(btn => {
return Bounds.Intersects(crsb, btn.hitbox)
})
return btns
}
/* override */ destroy() {
this.quadtree = undefined
super.destroy()
}
/* override */ update() {
super.update()
if (this.anim) {
this.anim.update()
}
}
/* override */ draw() {
this.stylus.draw()
//this.hitbox.dbgRect(15);
}
}
}