Skip to content

Commit b2f0eea

Browse files
committed
inputMethods: Keyboard: now using Grid, code improvements
1 parent 4112726 commit b2f0eea

File tree

1 file changed

+55
-43
lines changed

1 file changed

+55
-43
lines changed

inputMethods.ts

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
namespace microgui {
23
import AppInterface = user_interface_base.AppInterface
34
import Scene = user_interface_base.Scene
@@ -183,29 +184,30 @@ namespace microgui {
183184

184185
export class KeyboardMenu extends CursorSceneWithPriorPage {
185186
private static WIDTHS: number[] = [10, 10, 10, 10, 4]
186-
private btns: Button[]
187-
private btnText: string[]
187+
private btns: Button[][]
188+
private btnsText: string[][]
188189
private text: string;
189-
private upperCase: boolean;
190+
private isUpperCase: boolean;
190191
private next: (arg0: string) => void
191192
private frameCounter: number;
192193
private shakeText: boolean
193194
private shakeTextCounter: number
194195

195196
constructor(app: AppInterface, next: (arg0: string) => void) {
196-
super(app, function() { }, new GridNavigator())//, 5, 5, KeyboardMenu.WIDTHS, new GridNavigator(5, 10))
197+
super(app, function() { }, new GridNavigator())
197198
this.text = ""
198-
this.upperCase = true
199-
200-
this.btns = []
201-
this.btnText = [
202-
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
203-
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
204-
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
205-
"U", "V", "W", "X", "Y", "Z", ",", ".", "?", "!",
206-
"<-", "^", " _______ ", "ENTER"
199+
this.isUpperCase = true
200+
201+
this.btnsText = [
202+
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
203+
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
204+
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
205+
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"],
206+
["<-", "^", " _______ ", "ENTER"]
207207
];
208208

209+
this.btns = this.btnsText.map((row: string[]) => [])
210+
209211
this.next = next
210212
this.frameCounter = 0;
211213
this.shakeText = false;
@@ -217,18 +219,18 @@ namespace microgui {
217219

218220
const defaultBehaviour = (btn: Button) => {
219221
if (this.text.length < KEYBOARD_MAX_TEXT_LENGTH) {
220-
this.text += this.btnText[btn.state[0]]
222+
this.text += this.btnsText[btn.state[0]][btn.state[1]]
221223
this.frameCounter = KEYBOARD_FRAME_COUNTER_CURSOR_ON
222224
}
223225
else {
224226
this.shakeText = true
225227
}
226228
}
227229

228-
for (let i = 0; i < 4; i++) {
230+
for (let i = 0; i < this.btns.length; i++) {
229231
const xDiff = screen().width / (KeyboardMenu.WIDTHS[i] + 1);
230-
for (let j = 0; j < 10; j++) {
231-
this.btns.push(
232+
for (let j = 0; j < this.btnsText[i].length; j++) {
233+
this.btns[i][j] =
232234
new Button({
233235
parent: null,
234236
style: ButtonStyles.Transparent,
@@ -237,9 +239,8 @@ namespace microgui {
237239
x: (xDiff * (j + 1)) - (screen().width / 2),
238240
y: (13 * (i + 1)) - 18,
239241
onClick: defaultBehaviour,
240-
state: [(i * 10) + j]
242+
state: [i, j] // Coords of the button; used to lookup this.btnsText
241243
})
242-
)
243244
}
244245
}
245246

@@ -250,8 +251,8 @@ namespace microgui {
250251
? this.text.substr(0, this.text.length - 1)
251252
: this.text
252253
this.frameCounter = KEYBOARD_FRAME_COUNTER_CURSOR_ON
253-
},
254-
(btn: Button) => { this.changeCase() },
254+
}, // BACKSPACE
255+
(btn: Button) => { this.changeCase() }, // CHANGE CASE
255256
(btn: Button) => {
256257
if (this.text.length < KEYBOARD_MAX_TEXT_LENGTH) {
257258
this.text += " ";
@@ -260,41 +261,46 @@ namespace microgui {
260261
else {
261262
this.shakeText = true
262263
}
263-
},
264-
(btn: Button) => { this.next(this.text) }
264+
}, // SPACEBAR
265+
(btn: Button) => { this.next(this.text) } // ENTER
265266
]
266267

267268
const icons = [bitmaps.create(16, 10), bitmaps.create(10, 10), bitmaps.create(55, 10), bitmaps.create(33, 10)]
268269
const x = [22, 38, 74, 124]
269-
for (let i = 0; i < 4; i++) {
270-
this.btns.push(
270+
271+
const specialBtnRow = this.btns.length - 1;
272+
for (let j = 0; j < this.btns[specialBtnRow].length; j++) {
273+
this.btns[specialBtnRow][j] =
271274
new Button({
272275
parent: null,
273276
style: ButtonStyles.Transparent,
274-
icon: icons[i],
277+
icon: icons[j],
275278
ariaId: "",
276-
x: x[i] - (screen().width / 2),
279+
x: x[j] - (screen().width / 2),
277280
y: (13 * 5) - 18,
278-
onClick: botRowBehaviours[i]
281+
onClick: botRowBehaviours[j]
279282
})
280-
)
281283
}
282284

283285
this.changeCase()
284-
this.navigator.setBtns([this.btns])
286+
this.navigator.setBtns(this.btns)
285287
}
286288

289+
287290
private changeCase() {
288-
this.upperCase = !this.upperCase;
291+
this.isUpperCase = !this.isUpperCase;
289292

290-
if (this.upperCase)
291-
this.btnText = this.btnText.map((btn, i) =>
292-
btn = (i < 40) ? btn.toUpperCase() : btn
293-
)
294-
else
295-
this.btnText = this.btnText.map((btn, i) =>
296-
btn = (i < 40) ? btn.toLowerCase() : btn
297-
)
293+
const toUpperCase = (btnText: string) => { return btnText.toUpperCase(); }
294+
const toLowerCase = (btnText: string) => { return btnText.toLowerCase(); }
295+
const swapCase = (this.isUpperCase) ? toUpperCase : toLowerCase;
296+
297+
// Don't do special char row:
298+
for (let i = 0; i < this.btns.length - 1; i++) {
299+
for (let j = 0; j < this.btns[i].length; j++) {
300+
const btnText = this.btnsText[i][j]
301+
this.btnsText[i][j] = (j < 40) ? swapCase(btnText) : btnText
302+
}
303+
}
298304
}
299305

300306
draw() {
@@ -399,11 +405,16 @@ namespace microgui {
399405
)
400406

401407
for (let i = 0; i < this.btns.length; i++) {
402-
this.btns[i].draw()
408+
for (let j = 0; j < this.btns[i].length; j++) {
409+
const btn = this.btns[i][j];
410+
const btnText = this.btnsText[i][j];
403411

404-
const x = (screen().width / 2) + this.btns[i].xfrm.localPos.x - (this.btns[i].icon.width / 2) + 2
405-
const y = (screen().height / 2) + this.btns[i].xfrm.localPos.y + font.charHeight - 12
406-
screen().print(this.btnText[i], x, y, 1) // White text
412+
const x = (screen().width / 2) + btn.xfrm.localPos.x - (btn.icon.width / 2) + 2
413+
const y = (screen().height / 2) + btn.xfrm.localPos.y + font.charHeight - 12
414+
415+
btn.draw()
416+
screen().print(btnText, x, y, 1) // White text
417+
}
407418
}
408419

409420
super.draw()
@@ -707,3 +718,4 @@ namespace microgui {
707718
}
708719
}
709720
}
721+

0 commit comments

Comments
 (0)