Skip to content

Commit 1cad6d4

Browse files
committed
TextBox overrides addContext, allowing for dynamic text updating, new example in test.ts
1 parent 7793de1 commit 1cad6d4

2 files changed

Lines changed: 63 additions & 27 deletions

File tree

guiComponents.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,20 @@ namespace microgui {
392392
}
393393
}
394394

395+
/**
396+
* This checking and conversion is inadequate.
397+
* Should: check all types of each element in ctx
398+
*/
399+
addContext(ctx: any[]) {
400+
if (ctx == null || ctx.length == 0) {
401+
this.textChunks = [""]
402+
} else if (typeof (ctx[0]) !== 'string') {
403+
// throw ""
404+
} else {
405+
this.textChunks = ctx;
406+
}
407+
}
408+
395409
draw() {
396410
super.draw()
397411
// this.printCenter(this.text)
@@ -1782,4 +1796,3 @@ namespace microgui {
17821796
}
17831797
}
17841798
}
1785-

test.ts

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
namespace microcode {
23
import Scene = user_interface_base.Scene
34
import SceneManager = user_interface_base.SceneManager
@@ -20,37 +21,58 @@ namespace microcode {
2021
control.singleSimulator();
2122
const app = new App();
2223

24+
const simpleTextComponent: microgui.GUIComponentAbstract = new TextBox({
25+
alignment: GUIComponentAlignment.BOT,
26+
isActive: false,
27+
title: "Title Text :)", // optional arg
28+
text: ["Press micro:bit A btn"], // optional arg
29+
colour: 6, // optional arg
30+
xScaling: 1.7, // optional arg
31+
})
32+
33+
34+
let count = 0;
35+
input.onButtonPressed(1, function () {
36+
count++;
37+
simpleTextComponent.clearContext()
38+
simpleTextComponent.addContext(["" + count])
39+
})
40+
41+
const gcs = new GUIComponentScene({ app, components: [simpleTextComponent] })
42+
app.pushScene(gcs)
43+
2344
// Comment out the examples you aren't using:
2445

2546
// Example 0:
26-
const kb = new microgui.Keyboard({
27-
app,
28-
layout: microgui.KeyboardLayouts.NUMERIC,
29-
cb: (txt: string) => { basic.showString(txt) },
30-
foregroundColor: 2, // optional arg
31-
backgroundColor: 6, // optional arg
32-
defaultTxt: "0", // optional arg
33-
maxTxtLength: 6, // optional arg
34-
txtColor: 1, // optional arg
35-
deleteFn: () => basic.showString("Bye"), // optional arg
36-
backBtn: () => basic.showNumber(2)
37-
});
38-
39-
app.popScene();
40-
app.pushScene(kb);
47+
// const kb = new microgui.Keyboard({
48+
// app,
49+
// layout: microgui.KeyboardLayouts.NUMERIC,
50+
// cb: (txt: string) => { basic.showString(txt) },
51+
// foregroundColor: 2, // optional arg
52+
// backgroundColor: 6, // optional arg
53+
// defaultTxt: "0", // optional arg
54+
// maxTxtLength: 6, // optional arg
55+
// txtColor: 1, // optional arg
56+
// deleteFn: () => basic.showString("Bye"), // optional arg
57+
// backBtn: () => basic.showNumber(2)
58+
// });
59+
60+
// app.popScene();
61+
// app.pushScene(kb);
62+
4163
// Example 1a:
4264

43-
// const simpleTextComponent = new TextBox({
44-
// alignment: GUIComponentAlignment.BOT,
45-
// isActive: false,
46-
// title: "Title Text :)", // optional arg
47-
// text: ["Hello there,", "I hope you are well.", "Isn't this neat?"], // optional arg
48-
// colour: 6, // optional arg
49-
// xScaling: 1.7, // optional arg
50-
// })
65+
// const simpleTextComponent = new TextBox({
66+
// alignment: GUIComponentAlignment.BOT,
67+
// isActive: false,
68+
// title: "Title Text :)", // optional arg
69+
// text: ["Hello there,", "I hope you are well.", "Isn't this neat?"], // optional arg
70+
// colour: 6, // optional arg
71+
// xScaling: 1.7, // optional arg
72+
// })
5173

52-
// const gcs = new GUIComponentScene({ app, components: [simpleTextComponent] })
53-
// app.pushScene(gcs)
74+
// const gcs = new GUIComponentScene({ app, components: [simpleTextComponent] })
75+
// app.pushScene(gcs)
5476

5577

5678

@@ -138,7 +160,7 @@ namespace microcode {
138160

139161

140162

141-
163+
142164
// const buttonCollection = new ButtonCollection({
143165
// alignment: GUIComponentAlignment.TOP,
144166
// btns: [
@@ -249,3 +271,4 @@ namespace microcode {
249271
// app.pushScene(gcs);
250272
}
251273

274+

0 commit comments

Comments
 (0)