Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
393 lines (358 loc) · 14.9 KB
/
Copy pathmain.ts
File metadata and controls
393 lines (358 loc) · 14.9 KB
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
//-------------------------------------------------
// README samples
//-------------------------------------------------
namespace ui.controls.samples {
const STACK_SETUP_SCOPE = "stack-setup"
const MIXER_SCOPE = "mixer"
class SettingsScreen extends ui.UiScreen {
private speed: number
private speedLabel: ui.UiLabel
constructor(runtime: ui.UiRuntime) {
super(runtime)
this.speed = 5
this.backgroundColor = 8
this.add(new ui.UiLabel("Speed", 1), { x: 8, y: 8 })
this.speedLabel = new ui.UiLabel("" + this.speed, 7)
this.add(this.speedLabel, { x: 8, y: 24 })
this.add(new ui.UiLabel("Press A to Edit", 1), {
centerX: 80,
y: 108,
})
}
public handleInput(event: ui.UiInputEvent): boolean | undefined {
if (event.action == "activate" && event.phase != "released") {
this.openSpeedEditor()
return true
}
return undefined
}
private openSpeedEditor(): void {
this.openModal(
new ui.UiNumericEntryModal(
"speed-editor",
this.speed,
value => {
this.speed = value
this.speedLabel.setText("" + value)
},
),
)
}
}
class NameEntryScreen extends ui.UiScreen {
private name: string
private nameLabel: ui.UiLabel
constructor(runtime: ui.UiRuntime) {
super(runtime)
this.name = ""
this.backgroundColor = 8
this.add(new ui.UiLabel("Player", 1), { x: 8, y: 8 })
this.nameLabel = new ui.UiLabel("No name", 7)
this.add(this.nameLabel, { x: 8, y: 24 })
this.add(new ui.UiLabel("Press A to enter", 1), {
centerX: 80,
y: 108,
})
}
public handleInput(event: ui.UiInputEvent): boolean | undefined {
if (event.action == "activate" && event.phase != "released") {
this.openNameEditor()
return true
}
return undefined
}
private openNameEditor(): void {
this.openModal(
new ui.UiTextEntryModal({
modalScopeId: "name-editor",
title: loc("Enter your name:"),
initialText: this.name,
allowWhitespace: true,
allowSymbols: true,
maxLength: 16,
onResult: result => {
if (result.kind == "completed") {
this.name = result.text
this.nameLabel.setText(
this.name.length ? this.name : "No name",
)
}
},
}),
)
}
}
class DataGraphScreen extends ui.UiScreen {
private values: number[]
private tick: number
private graphRect: ui.Rect
private valueLabel: ui.UiLabel
private toggleButton: ui.UiButton
private running: boolean
constructor(runtime: ui.UiRuntime) {
super(runtime)
this.backgroundColor = 0
this.tick = 0
this.running = true
this.graphRect = new ui.Rect(8, 22, 144, 70)
this.add(new ui.UiLabel("Signal", 1), { x: 8, y: 6 })
this.toggleButton = new ui.UiButton("toggle", "Stop", () => {
this.running = !this.running
this.toggleButton.setText(this.running ? "Stop" : "Start")
})
this.values = [
24, 28, 35, 40, 46, 52, 58, 63, 68, 72, 70, 66, 60, 54, 48, 42,
36, 31, 27, 25,
]
this.valueLabel = new ui.UiLabel(
"" + this.values[this.values.length - 1],
7,
)
this.add(this.valueLabel, { x: 128, y: 6 })
this.add(this.toggleButton, { centerX: 80, centerY: 107 })
}
public update(): void {
if (!this.running) return
this.tick += 1
if (this.tick % 6 != 0) return
const phase = Math.idiv(this.tick, 6) % 20
const wave = phase < 10 ? phase : 20 - phase
this.values.removeAt(0)
this.values.push(25 + wave * 6)
this.valueLabel.setText("" + this.values[this.values.length - 1])
}
public render(surface: ui.DrawSurface): void {
surface.drawRect(this.graphRect, 1)
surface.drawLine(
this.graphRect.x + 1,
this.graphRect.y + Math.idiv(this.graphRect.height, 2),
this.graphRect.x + this.graphRect.width - 2,
this.graphRect.y + Math.idiv(this.graphRect.height, 2),
13,
)
let previousX = 0
let previousY = 0
for (let i = 0; i < this.values.length; i++) {
const x =
this.graphRect.x +
2 +
Math.idiv(
i * (this.graphRect.width - 4),
this.values.length - 1,
)
const y =
this.graphRect.y +
this.graphRect.height -
3 -
Math.idiv(this.values[i] * (this.graphRect.height - 6), 100)
if (i > 0) surface.drawLine(previousX, previousY, x, y, 7)
previousX = x
previousY = y
}
super.render(surface)
}
}
// A column stack that owns one focus scope. Rows of different widths each
// center on their own, labels sit between them, and up and down move from
// row to row without any navigation code in the screen.
class StackSetupScreen extends ui.UiScreen {
private status: ui.UiLabel
constructor(runtime: ui.UiRuntime) {
super(runtime)
this.backgroundColor = 6
this.status = new ui.UiLabel("Choose a size", 15)
const stack = new ui.UiStack({
orientation: "column",
// One scope for every row in the stack. The stack registers the
// rows under it and answers directional movement itself, which
// is what makes up and down cross from one row to the next.
scopeId: STACK_SETUP_SCOPE,
// Cross-axis placement of each child. Rows of two, three, and
// one control all end up on the same center line. A single
// child can override it with `alignment` on its record.
alignment: "center",
wrap: true,
gap: 0,
children: [
// Every child is a record: the view plus whatever spacing
// and alignment belong to it rather than to the stack.
// Passive views such as labels contribute no focus targets
// and are skipped by movement.
{ view: this.status },
{ view: new ui.UiLabel("Size", 15), gapBefore: 4 },
{ view: this.createRow("size", ["S", "M", "L"], 34), gapBefore: 3 },
{ view: new ui.UiLabel("Speed", 15), gapBefore: 4 },
{ view: this.createRow("speed", ["Slow", "Fast"], 40), gapBefore: 3 },
// For a child pinned to the far edge instead, give the stack
// `justify: "spaceBetween"` and a placement that stretches it
// along its axis.
{ view: this.createRow("done", ["Done"], 44), gapBefore: 10 },
],
})
this.add(stack, {
x: 0,
y: 4,
width: ui.STANDARD_DISPLAY_WIDTH,
horizontalAlignment: "center",
})
}
// Rows built for a stack need no scope of their own. The stack assigns
// its scope to each of them, so all the controls navigate as a single
// ragged grid.
private createRow(
id: string,
texts: string[],
controlWidth: number,
): ui.UiRow<string> {
const controls: ui.UiControl<string>[] = []
for (let i = 0; i < texts.length; i++) {
const text = texts[i]
controls.push(
ui.button<string>(id + "-" + i, text, () =>
this.status.setText(text + " selected"),
),
)
}
return new ui.UiRow<string>({
controls,
controlSize: { width: controlWidth, height: 20 },
controlStyle: ui.UiButtonStyles.LightShadowedWhite,
gap: 4,
})
}
}
// The same stack put to work differently: stretched to the display so that
// `justify` spreads the children instead of hand-written gaps, one child
// aligned against the edge rather than centered, and a grid, a plain button
// and a row all sharing the stack's scope. The transport strip is a nested
// row stack, which merges its children into a single navigation row.
class MixerScreen extends ui.UiScreen {
private status: ui.UiLabel
constructor(runtime: ui.UiRuntime) {
super(runtime)
this.backgroundColor = 12
this.status = new ui.UiLabel("Mixer", 1)
// A grid contributes one navigation row per grid row, so the pads
// navigate internally exactly as they would on their own.
const pads = new ui.UiGrid<string>({
controls: [
this.pad("1"),
this.pad("2"),
this.pad("3"),
this.pad("4"),
this.pad("5"),
this.pad("6"),
],
columnCount: 3,
controlSize: { width: 34, height: 20 },
controlStyle: ui.UiButtonStyles.LightShadowedWhite,
rowGap: 4,
columnGap: 4,
})
// A nested stack takes the scope of the stack that holds it, and
// passes it on to its own children. Being a row stack, it reports
// one merged navigation row, so the button and the pair of volume
// controls read as a single strip.
const transport = new ui.UiStack({
orientation: "row",
alignment: "center",
gap: 4,
children: [
{
view: new ui.UiButton<string>({
id: "play",
text: "Play",
size: { width: 40, height: 20 },
onActivate: () => this.status.setText("Playing"),
}),
},
{
view: new ui.UiRow<string>({
controls: [
// The type argument keeps the control's value
// type as `string`. Without it the id is
// inferred as its own literal type, which will
// not fit a row of `UiControl<string>`.
ui.button<string>("volume-down", "-", () =>
this.status.setText("Quieter"),
),
ui.button<string>("volume-up", "+", () =>
this.status.setText("Louder"),
),
],
controlSize: { width: 24, height: 20 },
controlStyle: ui.UiButtonStyles.LightShadowedWhite,
gap: 4,
}),
},
],
})
const stack = new ui.UiStack({
orientation: "column",
scopeId: MIXER_SCOPE,
alignment: "center",
// Spreads the leftover space between the children, which needs a
// placement that stretches the stack along its axis. The pads
// end up centered and the transport strip pinned to the bottom
// without a single hand-tuned gap.
justify: "spaceBetween",
wrap: true,
children: [
// Overrides the stack's centering for this child only.
{ view: this.status, alignment: "start" },
{ view: pads },
{ view: transport },
],
})
this.add(stack, {
x: 0,
y: 4,
width: ui.STANDARD_DISPLAY_WIDTH,
height: ui.STANDARD_DISPLAY_HEIGHT - 8,
horizontalAlignment: "stretch",
verticalAlignment: "stretch",
})
}
private pad(name: string): ui.UiControl<string> {
return ui.button<string>("pad-" + name, name, () =>
this.status.setText("Pad " + name),
)
}
}
// Selects a locale by assigning the localization seams directly. A
// consuming app normally assigns these from its generated per-language
// file; any code that runs before the UI is constructed can do the same,
// which is how a sample or test drives a locale.
function applySpanishLocale(): void {
_loc.table = {
"space": "espacio",
"DEL": "SUP",
"Enter your name:": "Escribe tu nombre:",
}
_loc.alphabetLower = "abcdefghijklmnñopqrstuvwxyz"
_loc.alphabetUpper = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ"
_loc.accentsLower = "áéíóúü"
_loc.accentsUpper = "ÁÉÍÓÚÜ"
_loc.symbols = "-_.,!?@#¡¿"
}
// French keeps the default alphabet and symbols and adds an accent set,
// so it exercises the accents keyboard page.
function applyFrenchLocale(): void {
_loc.table = {
"space": "espace",
"DEL": "SUP",
"Enter your name:": "Entre ton nom :",
}
_loc.accentsLower = "àâäçéèêëîïôöùûüÿœ"
_loc.accentsUpper = "ÀÂÄÇÉÈÊËÎÏÔÖÙÛÜŸŒ"
}
//applySpanishLocale()
applyFrenchLocale()
const runtime = new ui.UiRuntime(new ui.DisplayShieldFrameAdapter())
//runtime.push(new SettingsScreen(runtime))
//runtime.push(new NameEntryScreen(runtime))
//runtime.push(new DataGraphScreen(runtime))
//runtime.push(new StackSetupScreen(runtime))
runtime.push(new MixerScreen(runtime))
runtime.start()
}