Skip to content

Commit 69a2e53

Browse files
committed
TextButton is aligned with TextButtonComponents.
1 parent 568081e commit 69a2e53

2 files changed

Lines changed: 115 additions & 20 deletions

File tree

guiComponents.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,90 @@ namespace microcode {
858858
}
859859

860860

861+
export class TextButton {
862+
public bounds: Bounds;
863+
private text: string;
864+
private callback: (x: any) => void;
865+
private colour: number;
866+
private textColour: number;
867+
868+
constructor(opts: {
869+
text: string,
870+
callback: (x: any) => void,
871+
x?: number,
872+
y?: number
873+
colour?: number,
874+
textColour?: number
875+
}) {
876+
this.text = opts.text;
877+
878+
const x = (opts.x != null) ? opts.x : 0;
879+
const y = (opts.y != null) ? opts.y : 0;
880+
this.bounds = new Bounds({top: y, left: x, width: (font.charWidth * this.text.length), height: font.charHeight + 2});
881+
this.callback = opts.callback;
882+
this.colour = (opts.colour != null) ? opts.colour : 6;
883+
this.textColour = (opts.textColour != null) ? opts.textColour : 1;
884+
}
885+
886+
draw() {
887+
this.bounds.fillRect(this.colour);
888+
screen().print(
889+
this.text,
890+
this.bounds.left + (screen().width >> 1) + 1,
891+
this.bounds.top + (screen().height >> 1) + 1,
892+
this.textColour
893+
)
894+
}
895+
}
896+
897+
898+
export class TextButtonCollection extends GUIComponentAbstract {
899+
private textBtns: TextButton[];
900+
901+
constructor(opts: {
902+
alignment: GUIComponentAlignment,
903+
isActive: boolean,
904+
textBtns: TextButton[],
905+
isHidden?: boolean,
906+
xOffset?: number,
907+
yOffset?: number,
908+
xScaling?: number,
909+
yScaling?: number,
910+
colour?: number,
911+
border?: boolean,
912+
title?: string,
913+
text?: string | string[]
914+
}) {
915+
super({
916+
alignment: opts.alignment,
917+
xOffset: (opts.xOffset != null) ? opts.xOffset : 0,
918+
yOffset: (opts.yOffset != null) ? opts.yOffset : 0,
919+
width: GUIComponentAbstract.DEFAULT_WIDTH,
920+
height: GUIComponentAbstract.DEFAULT_HEIGHT,
921+
isActive: opts.isActive,
922+
isHidden: opts.isHidden,
923+
xScaling: opts.xScaling,
924+
yScaling: opts.yScaling,
925+
colour: opts.colour,
926+
border: opts.border
927+
});
928+
929+
this.textBtns = (opts.textBtns != null) ? opts.textBtns : [];
930+
931+
this.textBtns.forEach(btn => {
932+
btn.bounds.left += this.bounds.left;
933+
btn.bounds.top += this.bounds.top;
934+
})
935+
}
936+
937+
draw() {
938+
super.draw();
939+
940+
this.textBtns.forEach(btn => btn.draw())
941+
}
942+
}
943+
944+
861945
/**
862946
* Holds other components,
863947
* One component is active at a time

main.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,38 @@ namespace microcode {
7777
// const window = new Window({app, components: [comp1, comp2, comp3] })
7878
// app.pushScene(window)
7979

80-
let count = 0;
81-
const comp1 = new ButtonCollection({
82-
alignment: GUIComponentAlignment.TOP,
83-
btns: [
84-
[new Button({ icon: "pin_0", ariaId: "+1", x: 10, y: 10, onClick: () => {
85-
count += 1;
86-
}})],
87-
],
88-
isActive: true,
89-
colour: 2,
90-
xScaling: 0.5,
91-
yScaling: 0.7,
92-
xOffset: -10
93-
})
80+
// let count = 0;
81+
// const comp1 = new ButtonCollection({
82+
// alignment: GUIComponentAlignment.TOP,
83+
// btns: [
84+
// [new Button({
85+
// icon: "pin_0", ariaId: "+1", x: 10, y: 10, onClick: () => {
86+
// count += 1;
87+
// }
88+
// })],
89+
// ],
90+
// isActive: true,
91+
// colour: 2,
92+
// xScaling: 0.5,
93+
// yScaling: 0.7,
94+
// xOffset: -10
95+
// })
9496

95-
const comp2 = new GUIGraph({
96-
alignment: GUIComponentAlignment.BOT,
97-
graphableFns: [new GraphableFunction((_) => count)],
98-
isActive: false,
99-
})
97+
// const comp2 = new GUIGraph({
98+
// alignment: GUIComponentAlignment.BOT,
99+
// graphableFns: [new GraphableFunction((_) => count)],
100+
// isActive: false,
101+
// })
100102

101-
const window = new Window({ app, components: [comp1, comp2] })
103+
// const window = new Window({ app, components: [comp1, comp2] })
104+
// app.pushScene(window)
105+
106+
const txtBtnComp = new TextButtonCollection({
107+
alignment: GUIComponentAlignment.CENTRE,
108+
isActive: true,
109+
textBtns: [new TextButton({text: "hi", callback: () => "hi", })],
110+
xOffset: 10
111+
})
112+
const window = new Window({ app, components: [txtBtnComp] })
102113
app.pushScene(window)
103114
}

0 commit comments

Comments
 (0)