-
Notifications
You must be signed in to change notification settings - Fork 18
/
fieldeditors.ts
379 lines (352 loc) · 11.6 KB
/
fieldeditors.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
namespace microcode {
export function getFieldEditor(tile: Tile): FieldEditor {
if (tile instanceof ModifierEditor) return tile.fieldEditor
return undefined
}
class FieldEditor {
init(): any {
return undefined
}
clone(field: any): any {
return undefined
}
editor(
field: any,
picker: Picker,
onHide: () => void,
onDelete?: () => void
): void {}
toImage(field: any): Image {
return undefined
}
toBuffer(field: any): Buffer {
return undefined
}
fromBuffer(buf: BufferReader): any {
return undefined
}
}
export class ModifierEditor {
constructor(public tid: number) {
this.firstInstance = false
}
fieldEditor: FieldEditor
firstInstance: boolean
getField(): any {
return null
}
getIcon(): string | Image {
return null
}
getNewInstance(field: any = null): ModifierEditor {
return null
}
serviceCommandArg(): Buffer {
return null
}
}
class IconFieldEditor extends FieldEditor {
init() {
return img`
. . . . .
. 1 . 1 .
. . . . .
1 . . . 1
. 1 1 1 .
`
}
clone(img: Image) {
return img.clone()
}
editor(
field: any,
picker: Picker,
onHide: () => void,
onDelete?: () => void
) {
iconEditor(field, picker, onHide, onDelete)
}
toImage(field: any) {
return icondb.renderMicrobitLEDs(field)
}
toBuffer(img: Image) {
const ret = Buffer.create(4)
for (let index = 0; index < 25; index++) {
let byte = index >> 3
let bit = index & 7
let col = index % 5
let row = Math.idiv(index, 5)
ret[byte] |= img.getPixel(col, row) << bit
}
return ret
}
fromBuffer(br: BufferReader) {
const buf = br.readBuffer(4)
const img = image.create(5, 5)
for (let index = 0; index < 25; index++) {
let byte = index >> 3
let bit = index & 7
let col = index % 5
let row = Math.idiv(index, 5)
img.setPixel(col, row, (buf[byte] >> bit) & 1)
}
return img
}
}
export class IconEditor extends ModifierEditor {
field: Image
constructor(field: Image = null) {
super(Tid.TID_MODIFIER_ICON_EDITOR)
this.fieldEditor = new IconFieldEditor()
this.field = this.fieldEditor.clone(
field ? field : this.fieldEditor.init()
)
}
getField() {
return this.field
}
getIcon(): string | Image {
return this.firstInstance
? getIcon(Tid.TID_MODIFIER_ICON_EDITOR)
: this.fieldEditor.toImage(this.field)
}
getNewInstance(field: any = null) {
return new IconEditor(field ? field : this.field.clone())
}
serviceCommandArg() {
const buf = Buffer.create(5)
for (let col = 0; col < 5; ++col) {
let v = 0
for (let row = 0; row < 5; ++row) {
if (this.field.getPixel(col, row)) v |= 1 << row
}
buf[col] = v
}
return buf
}
}
export interface Melody {
notes: string
tempo: number
}
export const MELODY_LENGTH = 4
export const NUM_NOTES = 5
//export const noteNames = ["C", "D", "E", "F", "G", "A", "B", "C", "D"]
function setNote(buf: Buffer, offset: number, note: string) {
const noteToFreq: { [note: string]: number } = {
"0": 261.63, // C4
"1": 293.66, // D4
"2": 329.63, // E4
"3": 349.23, // F4
"4": 392.0, // G4
"5": 440.0, // A4
"6": 493.88, // B4
"7": 523.25, // C5
"8": 587.33, // D5
}
const period = 1000000 / (note !== "." ? noteToFreq[note] : 1000)
const duty = note === "." ? 0 : (period * 0.5) / 2
const duration = 250
buf.setNumber(NumberFormat.UInt16LE, offset + 0, period)
buf.setNumber(NumberFormat.UInt16LE, offset + 2, duty)
buf.setNumber(NumberFormat.UInt16LE, offset + 4, duration)
}
class MelodyFieldEditor extends FieldEditor {
init() {
return { notes: `0240`, tempo: 120 }
}
clone(melody: Melody) {
return { notes: melody.notes.slice(0), tempo: melody.tempo }
}
editor(
field: any,
picker: Picker,
onHide: () => void,
onDelete?: () => void
) {
melodyEditor(field, picker, onHide, onDelete)
}
toImage(field: any) {
return icondb.melodyToImage(field)
}
toBuffer(melody: Melody) {
const buf = Buffer.create(3)
buf.setUint8(0, melody.tempo)
// convert the melody notes into list of integers
const notes = melody.notes.split("")
// fill the buffer with the notes, 4 bits for each note
for (let i = 0; i < MELODY_LENGTH; i++) {
const byte = i >> 1
const bit = (i & 1) << 2
if (notes[i] != ".") {
const note = (parseInt(notes[i]) || 0) + 1
buf.setUint8(
byte + 1,
buf.getUint8(byte + 1) | (note << bit)
)
}
}
return buf
}
fromBuffer(br: BufferReader) {
const buf = br.readBuffer(3)
const tempo = buf[0]
let notes = ""
// read the notes from the buffer
for (let i = 0; i < MELODY_LENGTH; i++) {
const byte = i >> 1
const bit = (i & 1) << 2
const note = (buf[byte + 1] >> bit) & 0xf
notes += note == 0 ? "." : (note - 1).toString()
}
return { tempo, notes }
}
}
export class MelodyEditor extends ModifierEditor {
field: Melody
constructor(field: Melody = null) {
super(Tid.TID_MODIFIER_MELODY_EDITOR)
this.firstInstance = false
this.fieldEditor = new MelodyFieldEditor()
this.field = this.fieldEditor.clone(
field ? field : this.fieldEditor.init()
)
}
getField() {
return this.field
}
getIcon(): string | Image {
return this.firstInstance
? getIcon(Tid.TID_MODIFIER_MELODY_EDITOR)
: this.fieldEditor.toImage(this.field)
}
getNewInstance(field: any = null) {
return new MelodyEditor(
field ? field : this.fieldEditor.clone(this.field)
)
}
serviceCommandArg() {
const buf = Buffer.create(6 * 8)
for (let i = 0; i < MELODY_LENGTH; i++) {
setNote(buf, i * 6, this.field.notes[i])
}
return buf
}
}
let iconEditorTile: ModifierEditor = undefined
let melodyEditorTile: ModifierEditor = undefined
export function getEditor(tid: Tid): ModifierEditor {
if (tid == Tid.TID_MODIFIER_ICON_EDITOR) {
if (!iconEditorTile) {
iconEditorTile = new IconEditor()
iconEditorTile.firstInstance = true
}
return iconEditorTile
} else if (tid == Tid.TID_MODIFIER_MELODY_EDITOR) {
if (!melodyEditorTile) {
melodyEditorTile = new MelodyEditor()
melodyEditorTile.firstInstance = true
}
return melodyEditorTile
}
return undefined
}
function iconEditor(
image5x5: Image,
picker: Picker,
onHide: () => void,
onDelete?: () => void
) {
const getColor = (col: number, row: number) => {
return image5x5.getPixel(col, row) ? "solid_red" : "solid_black"
}
// TODO: replace this with a function from index to colo
let defs: PickerButtonDef[] = []
for (let row = 0; row < 5; row++) {
for (let col = 0; col < 5; col++) {
defs.push({
icon: getColor(col, row),
})
}
}
picker.setGroup(defs)
const red = icons.get("solid_red")
const black = icons.get("solid_black")
picker.show(
{
width: 5,
title: accessibility.ariaToTooltip(TID_MODIFIER_ICON_EDITOR),
onClick: (index: number) => {
let row = Math.idiv(index, 5)
let col = index % 5
const on = image5x5.getPixel(col, row)
image5x5.setPixel(col, row, on ? 0 : 1)
defs[index].icon = getColor(col, row)
picker.draw()
},
onHide,
onDelete,
navigator: () => new LEDNavigator(picker),
style: ButtonStyles.Transparent,
},
false
)
}
function melodyEditor(
melody: Melody,
picker: Picker,
onHide: () => void,
onDelete?: () => void
) {
const getIcon = (col: number, row: number) => {
const note_icon =
melody.notes[col] === "."
? "note_off"
: parseInt(melody.notes[col]) === NUM_NOTES - 1 - row
? "note_on"
: "note_off"
return note_icon
}
let defs: PickerButtonDef[] = []
for (let row = 0; row < NUM_NOTES; row++) {
for (let col = 0; col < MELODY_LENGTH; col++) {
defs.push({
icon: getIcon(col, row),
})
}
}
picker.setGroup(defs)
picker.show(
{
width: MELODY_LENGTH,
title: accessibility.ariaToTooltip(TID_MODIFIER_MELODY_EDITOR),
onClick: index => {
let row = Math.idiv(index, MELODY_LENGTH)
let col = index % MELODY_LENGTH
if (getIcon(col, row) !== "note_on") {
const note = (NUM_NOTES - 1 - row).toString()
const buf = Buffer.create(6)
setNote(buf, 0, note)
new jacs.TopWriter().deployFreq(buf)
}
melody.notes =
melody.notes.slice(0, col) +
(getIcon(col, row) === "note_on"
? "."
: (NUM_NOTES - 1 - row).toString()) +
melody.notes.slice(col + 1)
for (row = 0; row < NUM_NOTES; row++) {
defs[row * MELODY_LENGTH + col].icon = getIcon(col, row)
}
picker.draw()
picker.navigator.updateAria()
},
onHide,
onDelete,
navigator: () => new MelodyNavigator(picker),
style: ButtonStyles.Transparent,
},
false
)
}
}