Skip to content

Commit f7fc63f

Browse files
committed
refactor to function to allow update
1 parent 7b21d49 commit f7fc63f

1 file changed

Lines changed: 74 additions & 81 deletions

File tree

inputMethods.ts

Lines changed: 74 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ namespace microgui {
204204
QWERTY,
205205
NUMERIC,
206206
NUMERIC_POSITIVE_ONLY,
207-
NUMERIC_WITH_DELETE
208207
}
209208

210209
interface IKeyboard {
@@ -222,11 +221,9 @@ namespace microgui {
222221
type KeyboardBtnFn = (btn: Button, kb: IKeyboard) => void;
223222
type SpecialBtnData = { btnRow: number, btnCol: number, behaviour: KeyboardBtnFn };
224223
type KeyboardLayoutData = {
225-
[id: number]: {
226224
btnTexts: (string | Bitmap)[][],
227225
defaultBtnBehaviour: KeyboardBtnFn,
228226
specialBtnBehaviours: SpecialBtnData[]
229-
}
230227
};
231228

232229
const __kbBehaviourNumericDefault: KeyboardBtnFn = (btn: Button, kb: IKeyboard) => { // Default Behaviour: Prevent leading zeroes
@@ -291,83 +288,79 @@ namespace microgui {
291288
}
292289
} // END OF: ENTER
293290

294-
const __keyboardLayout: KeyboardLayoutData = {
295-
[KeyboardLayouts.QWERTY]: {
296-
btnTexts: [
297-
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
298-
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
299-
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
300-
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"],
301-
["<-", "^", " _______ ", "ENTER"]
302-
],
303-
defaultBtnBehaviour: (btn: Button, kb: IKeyboard) => {
304-
kb.appendText(btn.state[0])
305-
},
306-
specialBtnBehaviours: [
307-
{ btnRow: 4, btnCol: 0, behaviour: (btn: Button, kb: IKeyboard) => kb.deletePriorCharacters(1) }, // Backspace
308-
{ btnRow: 4, btnCol: 1, behaviour: (btn: Button, kb: IKeyboard) => kb.swapCase() }, // Change case
309-
{ btnRow: 4, btnCol: 2, behaviour: (btn: Button, kb: IKeyboard) => kb.appendText(" ") }, // Spacebar
310-
{ btnRow: 4, btnCol: 3, behaviour: (btn: Button, kb: IKeyboard) => kb.nextScene() } // ENTER
311-
]
312-
},
313-
314-
/**
315-
* Ensures that the user inputs result in a valid number.
316-
* E.g: prevents two decimal places, - only at start, etc
317-
*/
318-
[KeyboardLayouts.NUMERIC]: {
319-
btnTexts: [
320-
["1", "2", "3", "<-"],
321-
["4", "5", "6", ".", "-"],
322-
["7", "8", "9", "0", "ENTER"]
323-
],
324-
defaultBtnBehaviour: __kbBehaviourNumericDefault,
325-
specialBtnBehaviours: [
326-
{ btnRow: 0, btnCol: 3, behaviour: (btn: Button, kb: IKeyboard) => kb.deletePriorCharacters(1) }, // Backspace
327-
{ btnRow: 1, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericMinus(b, kb) },
328-
{ btnRow: 1, btnCol: 3, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericDeimcal(b, kb) },
329-
{ btnRow: 2, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericEnter(b, kb) }
330-
]
331-
},
332-
333-
/**
334-
* Ensures that the user inputs result in a valid number.
335-
* E.g: prevents two decimal places, - only at start, etc
336-
*/
337-
[KeyboardLayouts.NUMERIC_POSITIVE_ONLY]: {
338-
btnTexts: [
339-
["1", "2", "3", "<-"],
340-
["4", "5", "6", "."],
341-
["7", "8", "9", "0", "ENTER"]
342-
],
343-
defaultBtnBehaviour: __kbBehaviourNumericDefault,
344-
specialBtnBehaviours: [
345-
{ btnRow: 0, btnCol: 3, behaviour: (b: Button, kb: IKeyboard) => kb.deletePriorCharacters(1) }, // Backspace
346-
{ btnRow: 1, btnCol: 3, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericDeimcal(b, kb) }, // Decimal point
347-
{ btnRow: 2, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericEnter(b, kb) } // Enter
348-
]
349-
},
350-
351-
/**
352-
* Same as above, but we have a Trashcan bitmap for a custom delete fn.
353-
* This is used by MicroCode; so its DigitWidget (this keyboard) can be deleted like other elements.
354-
*/
355-
[KeyboardLayouts.NUMERIC_WITH_DELETE]: {
356-
btnTexts: [
357-
["1", "2", "3", "<-", btn_delete],
358-
["4", "5", "6", ".", "-"],
359-
["7", "8", "9", "0", "ENTER"]
360-
],
361-
defaultBtnBehaviour: __kbBehaviourNumericDefault,
362-
specialBtnBehaviours: [
363-
{ btnRow: 0, btnCol: 3, behaviour: (b: Button, kb: IKeyboard) => kb.deletePriorCharacters(1) }, // Backspace
364-
{ btnRow: 0, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => kb.deleteFn() }, // btn_delete
365-
{ btnRow: 1, btnCol: 3, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericDeimcal(b, kb) },// Decimal point
366-
{ btnRow: 1, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericMinus(b, kb) }, // Minus
367-
{ btnRow: 2, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericEnter(b, kb) } // Enter
368-
]
291+
function __keyboardLayout(layout: KeyboardLayouts, del = false): KeyboardLayoutData {
292+
switch (layout) {
293+
case KeyboardLayouts.QWERTY: {
294+
const ret: KeyboardLayoutData = {
295+
btnTexts: [
296+
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
297+
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
298+
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
299+
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"],
300+
["<-", "^", " _______ ", "ENTER"]
301+
],
302+
defaultBtnBehaviour: (btn: Button, kb: IKeyboard) => {
303+
kb.appendText(btn.state[0])
304+
},
305+
specialBtnBehaviours: [
306+
{ btnRow: 4, btnCol: 0, behaviour: (btn: Button, kb: IKeyboard) => kb.deletePriorCharacters(1) }, // Backspace
307+
{ btnRow: 4, btnCol: 1, behaviour: (btn: Button, kb: IKeyboard) => kb.swapCase() }, // Change case
308+
{ btnRow: 4, btnCol: 2, behaviour: (btn: Button, kb: IKeyboard) => kb.appendText(" ") }, // Spacebar
309+
{ btnRow: 4, btnCol: 3, behaviour: (btn: Button, kb: IKeyboard) => kb.nextScene() } // ENTER
310+
]
311+
}
312+
return ret
313+
}
314+
315+
/**
316+
* Ensures that the user inputs result in a valid number.
317+
* E.g: prevents two decimal places, - only at start, etc
318+
*/
319+
case KeyboardLayouts.NUMERIC: {
320+
const ret: KeyboardLayoutData = {
321+
btnTexts: [
322+
["1", "2", "3", "<-"],
323+
["4", "5", "6", ".", "-"],
324+
["7", "8", "9", "0", "ENTER"]
325+
],
326+
defaultBtnBehaviour: __kbBehaviourNumericDefault,
327+
specialBtnBehaviours: [
328+
{ btnRow: 0, btnCol: 3, behaviour: (btn: Button, kb: IKeyboard) => kb.deletePriorCharacters(1) }, // Backspace
329+
{ btnRow: 1, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericMinus(b, kb) },
330+
{ btnRow: 1, btnCol: 3, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericDeimcal(b, kb) },
331+
{ btnRow: 2, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericEnter(b, kb) }
332+
]
333+
}
334+
if (del) {
335+
ret.btnTexts[0].push(btn_delete)
336+
ret.specialBtnBehaviours.insertAt(1,
337+
{ btnRow: 0, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => kb.deleteFn() }) // btn_delete
338+
}
339+
return ret
340+
}
341+
342+
/**
343+
* Ensures that the user inputs result in a valid number.
344+
* E.g: prevents two decimal places, - only at start, etc
345+
*/
346+
case KeyboardLayouts.NUMERIC_POSITIVE_ONLY: {
347+
const ret: KeyboardLayoutData = {
348+
btnTexts: [
349+
["1", "2", "3", "<-"],
350+
["4", "5", "6", "."],
351+
["7", "8", "9", "0", "ENTER"]
352+
],
353+
defaultBtnBehaviour: __kbBehaviourNumericDefault,
354+
specialBtnBehaviours: [
355+
{ btnRow: 0, btnCol: 3, behaviour: (b: Button, kb: IKeyboard) => kb.deletePriorCharacters(1) }, // Backspace
356+
{ btnRow: 1, btnCol: 3, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericDeimcal(b, kb) }, // Decimal point
357+
{ btnRow: 2, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericEnter(b, kb) } // Enter
358+
]
359+
}
360+
return ret
361+
}
369362
}
370-
};
363+
}
371364

372365
const KEYBOARD_FRAME_COUNTER_CURSOR_ON = 20;
373366
export class Keyboard extends CursorScene implements IKeyboard {
@@ -439,7 +432,7 @@ namespace microgui {
439432
startup() {
440433
super.startup()
441434

442-
const data = __keyboardLayout[this.keyboardLayout];
435+
const data = __keyboardLayout(this.keyboardLayout);
443436
this.btns = data.btnTexts.map(_ => []);
444437

445438
const charWidth = bitmaps.font8.charWidth
@@ -550,7 +543,7 @@ namespace microgui {
550543
: (t: string) => { return t.toLowerCase() }
551544

552545

553-
const specialBtnData: SpecialBtnData[] = __keyboardLayout[this.keyboardLayout].specialBtnBehaviours;
546+
const specialBtnData: SpecialBtnData[] = __keyboardLayout(this.keyboardLayout).specialBtnBehaviours;
554547
const specialBtnRows: number[] = specialBtnData.map((sbd: SpecialBtnData) => sbd.btnRow);
555548
const specialBtnCols: number[] = specialBtnData.map((sbd: SpecialBtnData) => sbd.btnCol);
556549

0 commit comments

Comments
 (0)