Skip to content

Commit 1901890

Browse files
committed
Complete auto-scaling in RadioButtonCollection, .rescaleHeightTo and .rescaleWidthTo
1 parent d451235 commit 1901890

2 files changed

Lines changed: 96 additions & 19 deletions

File tree

guiComponents.ts

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace microgui {
1010
import Cursor = user_interface_base.Cursor
1111
import CursorDir = user_interface_base.CursorDir
1212
import Picker = user_interface_base.Picker
13-
1413
import Bounds = user_interface_base.Bounds
1514
import Screen = user_interface_base.Screen
1615
import Button = user_interface_base.Button
@@ -227,6 +226,49 @@ namespace microgui {
227226
}
228227

229228

229+
/**
230+
*
231+
* @param xScaling 1.0 is default; affects this.bounds.width: width = (this.xScaling * this.unscaledWidth)
232+
* @param yScaling 1.0 is default; affects this.bounds.height: height = (this.yScaling * this.unscaledHeight)
233+
*/
234+
public rescaleWidthTo(newWidth: number): void {
235+
if (this.bounds != null) {
236+
this.xScaling = newWidth / this.unscaledWidth;
237+
this.bounds.left = this.getLeftAndTop()[0];
238+
this.bounds.top = this.getLeftAndTop()[1];
239+
240+
this.bounds = new Bounds({
241+
width: this.unscaledWidth * this.xScaling,
242+
height: this.unscaledHeight * this.yScaling,
243+
left: this.bounds.left,
244+
top: this.bounds.top
245+
})
246+
}
247+
}
248+
249+
250+
/**
251+
*
252+
* @param xScaling 1.0 is default; affects this.bounds.width: width = (this.xScaling * this.unscaledWidth)
253+
* @param yScaling 1.0 is default; affects this.bounds.height: height = (this.yScaling * this.unscaledHeight)
254+
*/
255+
public rescaleHeightTo(newHeight: number): void {
256+
if (this.bounds != null) {
257+
this.yScaling = newHeight / this.unscaledHeight
258+
259+
this.bounds.left = this.getLeftAndTop()[0];
260+
this.bounds.top = this.getLeftAndTop()[1];
261+
262+
this.bounds = new Bounds({
263+
width: this.unscaledWidth * this.xScaling,
264+
height: this.unscaledHeight * this.yScaling,
265+
left: this.bounds.left,
266+
top: this.bounds.top
267+
})
268+
}
269+
}
270+
271+
230272
/**
231273
* Invoked by parent, see Window.
232274
*/
@@ -240,11 +282,10 @@ namespace microgui {
240282
15
241283
)
242284

243-
this.bounds.fillRect(this.backgroundColour)
285+
this.bounds.fillRect(this.backgroundColour);
244286
}
245287
}
246288

247-
248289
public get width() { return this.unscaledWidth * this.xScaling }
249290
public get height() { return this.unscaledHeight * this.yScaling }
250291
public get active() { return this.isActive }
@@ -354,6 +395,7 @@ namespace microgui {
354395
}
355396
}
356397

398+
357399
export class GUISlider extends TextBox {
358400
private maximum: number;
359401
private minimum: number;
@@ -1158,7 +1200,7 @@ namespace microgui {
11581200

11591201

11601202
export class RadioButton {
1161-
private text: string;
1203+
public text: string;
11621204
private textColour: number;
11631205
private x: number;
11641206
private y: number;
@@ -1205,11 +1247,15 @@ namespace microgui {
12051247
}
12061248

12071249

1250+
12081251
export class RadioButtonCollection extends GUIComponentAbstract {
12091252
private title: string;
12101253
private btns: RadioButton[]
12111254
private selectedTextBtnIndex: number;
12121255

1256+
private xBorder = 12
1257+
private static MINIMUM_BUTTON_Y_SPACING: number = 2;
1258+
12131259
constructor(opts: {
12141260
alignment: GUIComponentAlignment,
12151261
isActive: boolean,
@@ -1239,21 +1285,54 @@ namespace microgui {
12391285
this.title = (opts.title != null) ? opts.title : "";
12401286
this.btns = (opts.btns != null) ? opts.btns : [];
12411287

1242-
const xBorder = this.bounds.width * 0.15;
1243-
const yBorder = this.bounds.height * 0.05;
1288+
const originalWidth = this.bounds.width
1289+
const originalHeight = this.bounds.height
1290+
this.autoScale()
1291+
1292+
const yBorder = this.bounds.height * 0.10
12441293
const ySpacing = (this.bounds.height - yBorder) / (this.btns.length + 1);
12451294

12461295
for (let i = 0; i < this.btns.length; i++) {
12471296
this.btns[i].setPosition(
1248-
xBorder + this.bounds.left + this.bounds.width,
1249-
this.bounds.top + this.bounds.height + ((i + 1) * ySpacing) - 3
1297+
this.xBorder + this.bounds.left + originalWidth,
1298+
yBorder + this.bounds.top + originalHeight + ((i + 1) * ySpacing) - 3
12501299
);
12511300
this.btns[i].setSelected(false)
12521301
}
12531302

12541303
if (this.btns.length > 0) {
1304+
// Choose the current active button:
12551305
this.selectedTextBtnIndex = 0
12561306
this.btns[this.selectedTextBtnIndex].setSelected(true)
1307+
};
1308+
1309+
super.makeActive();
1310+
this.setupButtonBindings();
1311+
}
1312+
1313+
private autoScale() {
1314+
const yBorder = 0;
1315+
const ySpacing =
1316+
font.charHeight *
1317+
RadioButtonCollection.MINIMUM_BUTTON_Y_SPACING;
1318+
1319+
// AutoScaling height:
1320+
const maxComponentHeight = yBorder + (ySpacing * this.btns.length) - 3
1321+
if (this.bounds.height < maxComponentHeight)
1322+
this.rescaleHeightTo(maxComponentHeight)
1323+
1324+
// AutoScaling width:
1325+
const titleWidth = (this.title.length + 1) * font.charWidth;
1326+
const btnTextWidthFn = (btn: RadioButton) =>
1327+
this.xBorder + ((btn.text.length + 1) * font.charWidth);
1328+
1329+
const maxComponentWidth = this.btns.reduce((acc, btn) => (
1330+
btnTextWidthFn(btn) > acc ? btnTextWidthFn(btn) : acc),
1331+
titleWidth
1332+
);
1333+
1334+
if (this.bounds.width < maxComponentWidth) {
1335+
this.rescaleWidthTo(maxComponentWidth)
12571336
}
12581337
}
12591338

@@ -1302,7 +1381,7 @@ namespace microgui {
13021381
this.title,
13031382
(screen().width >> 1) + this.bounds.left + (this.width >> 1) - titleOffset,
13041383
(screen().height >> 1) + this.bounds.top + 2,
1305-
)
1384+
);
13061385

13071386
this.btns.forEach(btn => btn.draw())
13081387
}
@@ -1644,7 +1723,6 @@ namespace microgui {
16441723
}
16451724
}
16461725

1647-
16481726
export class KeyboardComponent extends ButtonCollection {
16491727
constructor(opts: {
16501728
alignment: GUIComponentAlignment,
@@ -1671,4 +1749,3 @@ namespace microgui {
16711749
}
16721750
}
16731751
}
1674-

main.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,20 @@ namespace microcode {
216216
// Example 6: RadioButtons
217217

218218
// const rbc = new RadioButtonCollection({
219-
// alignment: GUIComponentAlignment.BOT,
219+
// alignment: GUIComponentAlignment.CENTRE,
220220
// btns: [
221221
// new RadioButton({ text: "hi", onClick: () => { basic.showString("a") } }),
222-
// new RadioButton({ text: "hiya", onClick: () => { basic.showString("b") } }),
222+
// new RadioButton({ text: "hiyaaaaaaaaaa", onClick: () => { basic.showString("b") } }),
223223
// new RadioButton({ text: "hello", onClick: () => { basic.showString("c") } }),
224-
// new RadioButton({ text: "howdy", onClick: () => { basic.showString("d") } })
224+
// new RadioButton({ text: "a", onClick: () => { basic.showString("d") } }),
225+
// new RadioButton({ text: "b", onClick: () => { basic.showString("e") } })
225226
// ],
226227
// isActive: true,
227-
// yScaling: 1.1,
228228
// title: "The title",
229-
// colour: 3,
230-
// })
229+
// colour: 3
230+
// });
231231

232-
// const gcs = new GUIComponentScene({ app, components: [rbc] })
233-
// app.pushScene(gcs)
232+
// const gcs = new GUIComponentScene({ app, components: [rbc] });
233+
// app.pushScene(gcs);
234234
}
235235

0 commit comments

Comments
 (0)