Skip to content

Commit b52d01a

Browse files
authored
Merge pull request #1 from microbit-apps/refactorDelete
Refactor delete
2 parents 7b21d49 + e2c9d27 commit b52d01a

File tree

1 file changed

+68
-85
lines changed

1 file changed

+68
-85
lines changed

inputMethods.ts

Lines changed: 68 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ namespace microgui {
203203
export enum KeyboardLayouts {
204204
QWERTY,
205205
NUMERIC,
206-
NUMERIC_POSITIVE_ONLY,
207-
NUMERIC_WITH_DELETE
206+
NUMERIC_POSITIVE_INTEGER,
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
@@ -264,7 +261,7 @@ namespace microgui {
264261
} // END OF: Minus symbol: Toggle "-" at the start.
265262

266263

267-
const __kbBehaviourNumericDeimcal: KeyboardBtnFn = (btn: Button, kb: IKeyboard) => { // Decimal point
264+
const __kbBehaviourNumericDecimal: KeyboardBtnFn = (btn: Button, kb: IKeyboard) => { // Decimal point
268265
const txt = kb.getText();
269266
const len = txt.length;
270267
const decimalAlreadyPresent = txt.includes(".")
@@ -291,83 +288,68 @@ 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+
if (del) {
313+
ret.btnTexts[4].insertAt(0,btn_delete)
314+
ret.specialBtnBehaviours.push({ btnRow: 4, btnCol: 0, behaviour: (b: Button, kb: IKeyboard) => kb.deleteFn() })
315+
}
316+
return ret
317+
}
318+
319+
/**
320+
* Ensures that the user inputs result in a valid number.
321+
* E.g: prevents two decimal places, - only at start, etc
322+
*/
323+
case KeyboardLayouts.NUMERIC_POSITIVE_INTEGER:
324+
case KeyboardLayouts.NUMERIC: {
325+
const ret: KeyboardLayoutData = {
326+
btnTexts: [
327+
["1", "2", "3", "<-"],
328+
["4", "5", "6" ],
329+
["7", "8", "9", "0", "ENTER"]
330+
],
331+
defaultBtnBehaviour: __kbBehaviourNumericDefault,
332+
specialBtnBehaviours: [
333+
{ btnRow: 0, btnCol: 3, behaviour: (btn: Button, kb: IKeyboard) => kb.deletePriorCharacters(1) }, // Backspace
334+
{ btnRow: 2, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericEnter(b, kb) }
335+
]
336+
}
337+
if (layout == KeyboardLayouts.NUMERIC) {
338+
ret.btnTexts[1].push(".")
339+
ret.btnTexts[1].push("-")
340+
ret.specialBtnBehaviours.push(
341+
{ btnRow: 1, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericMinus(b, kb) })
342+
ret.specialBtnBehaviours.push(
343+
{ btnRow: 1, btnCol: 3, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericDecimal(b, kb) })
344+
}
345+
if (del) {
346+
ret.btnTexts[0].push(btn_delete)
347+
ret.specialBtnBehaviours.push({ btnRow: 0, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => kb.deleteFn() })
348+
}
349+
return ret
350+
}
369351
}
370-
};
352+
}
371353

372354
const KEYBOARD_FRAME_COUNTER_CURSOR_ON = 20;
373355
export class Keyboard extends CursorScene implements IKeyboard {
@@ -432,14 +414,14 @@ namespace microgui {
432414
this.backgroundColor = (opts.backgroundColor) ? opts.backgroundColor : 6; // Default to blue
433415

434416
this.txtColor = (opts.txtColor) ? opts.txtColor : 1;
435-
this.passedDeleteFn = (opts.deleteFn) ? opts.deleteFn : () => { };
417+
this.passedDeleteFn = opts.deleteFn
436418
this.passedBackBtn = (opts.backBtn) ? opts.backBtn : () => { };
437419
}
438420

439421
startup() {
440422
super.startup()
441423

442-
const data = __keyboardLayout[this.keyboardLayout];
424+
const data = __keyboardLayout(this.keyboardLayout, this.passedDeleteFn !== undefined);
443425
this.btns = data.btnTexts.map(_ => []);
444426

445427
const charWidth = bitmaps.font8.charWidth
@@ -550,7 +532,7 @@ namespace microgui {
550532
: (t: string) => { return t.toLowerCase() }
551533

552534

553-
const specialBtnData: SpecialBtnData[] = __keyboardLayout[this.keyboardLayout].specialBtnBehaviours;
535+
const specialBtnData: SpecialBtnData[] = __keyboardLayout(this.keyboardLayout, this.passedDeleteFn !== undefined).specialBtnBehaviours;
554536
const specialBtnRows: number[] = specialBtnData.map((sbd: SpecialBtnData) => sbd.btnRow);
555537
const specialBtnCols: number[] = specialBtnData.map((sbd: SpecialBtnData) => sbd.btnCol);
556538

@@ -571,7 +553,8 @@ namespace microgui {
571553
}
572554

573555
public deleteFn(): void {
574-
this.passedDeleteFn(this.text);
556+
if (this.passedDeleteFn)
557+
this.passedDeleteFn(this.text);
575558
}
576559

577560
public getText() {

0 commit comments

Comments
 (0)