-
Notifications
You must be signed in to change notification settings - Fork 18
/
picker.ts
267 lines (241 loc) · 8.65 KB
/
picker.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
namespace microcode {
// TODO: functionalize the Picker to reduce memory pressure
// 1. a function to get image from col, row
// 2. a function to set image from col, row
// 2. the number of rows and columns (supported ragged)
export type PickerButtonDef = {
icon: string
ariaId?: string
}
export interface IPicker {
size: number
getPickerButtonDef(index: number): PickerButtonDef
}
// the picker group only needs to access the PickerButtonDefs list,
// which should be functionalized to reduce memory pressure, no
// need for buttons here
class PickerGroup {
public xfrm: Affine
public bounds: Bounds
private cell: Bounds
constructor(public picker: Picker, public defs: PickerButtonDef[]) {
this.xfrm = new Affine()
this.xfrm.parent = picker.xfrm
}
// TODO: on click
public buttonHeight() {
return this.cell.height
}
public getButtonAtIndex(idx: number): Button {
const def = this.defs[idx]
const btn = new Button({
parent: this.picker,
style: this.picker.style,
icon: def.icon,
ariaId: def.ariaId,
x: 0,
y: 0,
onClick: () => {
this.picker.onButtonClicked(idx)
},
})
btn.xfrm.parent = this.xfrm
this.setButtonCoords(idx, btn)
return btn
}
public getButtonAtScreen(x: number, y: number): Vec2 {
const p = new Vec2(x, y)
const b = Bounds.Translate(this.bounds, this.xfrm.worldPos)
if (!b.contains(p)) return undefined
const row = Math.idiv(y - b.top, this.cell.height)
const col = Math.idiv(x - b.left, this.cell.width)
return new Vec2(col, row)
}
public layout(maxPerRow: number) {
// first compute bounds of biggest button
this.cell = new Bounds()
this.defs.forEach(def => {
const btn = new ButtonBase(
0,
0,
this.picker.style,
this.picker.xfrm
)
btn.buildSprite(icons.get(def.icon))
this.cell.add(btn.bounds)
})
this.layoutDraw()
}
private setButtonCoords(idx: number, btn: ButtonBase) {
btn.buildSprite(icons.get(this.defs[idx].icon))
const row = Math.idiv(idx, this.picker.width)
btn.xfrm.localPos.x =
(this.cell.width >> 1) +
(idx % this.picker.width) * this.cell.width +
(idx % this.picker.width)
btn.xfrm.localPos.y = row * this.cell.height
}
private layoutDraw(draw: boolean = false) {
// matrix layout of buttons
this.bounds = new Bounds()
this.defs.forEach((def, idx) => {
const btn = new ButtonBase(0, 0, this.picker.style, this.xfrm)
this.setButtonCoords(idx, btn)
this.bounds.add(Bounds.Translate(btn.bounds, btn.xfrm.localPos))
if (draw) btn.draw()
})
}
public draw() {
this.layoutDraw(true)
}
}
export class Picker implements IPlaceable {
public group: PickerGroup
private start: number
public navigator: PickerNavigator
public visible: boolean
public style: ButtonStyle
public width: number
private xfrm_: Affine
private prevState: CursorState
private deleteBtn: Button
private panel: Bounds
private onClick: (index: number) => void
private onHide: () => void
private onDelete: () => void
private hideOnClick: boolean
private title: string
public get xfrm() {
return this.xfrm_
}
constructor(private cursor: Cursor) {
this.xfrm_ = new Affine()
this.group = undefined
this.navigator = new PickerNavigator(this)
}
public setGroup(defs: PickerButtonDef[]) {
this.group = new PickerGroup(this, defs)
}
public onButtonClicked(index: number) {
const onClick = this.onClick
if (this.hideOnClick) {
this.cursor.cancelHandlerStack.pop()
this.hide()
}
if (onClick) {
onClick(index)
}
}
private cancelClicked() {
this.cursor.cancelHandlerStack.pop()
this.hide()
}
show(
opts: {
width?: number
title?: string
onClick?: (index: number) => void
onHide?: () => void
onDelete?: () => void
navigator?: () => PickerNavigator
selected?: number
style?: ButtonStyle
},
hideOnClick: boolean = true
) {
this.start = opts.selected ? opts.selected : -1
this.onClick = opts.onClick
this.onHide = opts.onHide
this.onDelete = opts.onDelete
if (opts.navigator) {
this.navigator.clear()
this.navigator = opts.navigator()
} else {
this.navigator.clear()
this.navigator = new PickerNavigator(this)
}
this.hideOnClick = hideOnClick
this.title = opts.title
this.style = opts.style || ButtonStyles.LightShadowedWhite
this.width = opts.width || 5
this.prevState = this.cursor.saveState()
this.cursor.navigator = this.navigator
this.cursor.cancelHandlerStack.push(() => this.cancelClicked())
if (this.onDelete) {
this.deleteBtn = new Button({
parent: this,
style: ButtonStyles.RedBorderedWhite,
icon: "delete",
x: 0,
y: 0,
onClick: () => {
this.hide()
this.onDelete()
},
})
}
this.layout(this.width)
this.visible = true
}
hide() {
this.visible = false
this.navigator.clear()
this.cursor.restoreState(this.prevState)
this.deleteBtn = undefined
this.group = undefined
if (this.onHide) {
this.onHide()
}
}
draw() {
control.enablePerfCounter()
if (!this.visible) return
Screen.fillBoundsXfrm(this.xfrm, this.panel, 12)
Screen.outlineBoundsXfrm(this.xfrm, this.panel, 1, 15)
if (this.title) {
const w = this.xfrm.worldPos
Screen.print(
this.title,
w.x + this.panel.left + 2,
w.y + this.panel.top + 4,
1,
microcode.font
)
}
if (this.group) this.group.draw()
if (this.deleteBtn) this.deleteBtn.draw()
}
private layout(maxPerRow: number) {
this.panel = new Bounds()
const padding = 2
let top = padding
if (this.deleteBtn || this.title) {
top += this.deleteBtn ? this.deleteBtn.height : HEADER
}
if (this.deleteBtn) {
this.navigator.addDelete(this.deleteBtn)
}
if (this.group) {
const group = this.group
group.layout(maxPerRow)
top += group.buttonHeight() >> 1
group.xfrm.localPos.y = top
this.panel.add(Bounds.Translate(group.bounds, new Vec2(0, top)))
top += group.bounds.height
}
if (this.deleteBtn) {
this.deleteBtn.xfrm.localPos.x =
this.panel.right - (this.deleteBtn.width >> 1) + 1
this.deleteBtn.xfrm.localPos.y =
this.panel.top + (this.deleteBtn.height >> 1)
}
this.panel.grow(padding)
this.xfrm.localPos.x = padding - (this.panel.width >> 1)
this.xfrm.localPos.y = padding - (this.panel.height >> 1)
if (this.start < 0) this.start = 0
const btn = this.navigator.moveToIndex(this.start)
this.cursor.moveTo(btn.xfrm.worldPos, btn.ariaId, btn.bounds)
}
}
const HEADER = 16
}