Skip to content

Commit 0a83fb6

Browse files
committed
TextButton & TextButtonCollection: added cursor, scrolling, modified callbacks
1 parent 69a2e53 commit 0a83fb6

2 files changed

Lines changed: 107 additions & 25 deletions

File tree

guiComponents.ts

Lines changed: 103 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
namespace microcode {
2+
function unbindShieldButtons() {
3+
control.onEvent(ControllerButtonEvent.Pressed, controller.A.id, () => { })
4+
control.onEvent(ControllerButtonEvent.Pressed, controller.A.id + keymap.PLAYER_OFFSET, () => { })
5+
control.onEvent(ControllerButtonEvent.Pressed, controller.B.id, () => { })
6+
control.onEvent(ControllerButtonEvent.Pressed, controller.up.id, () => { })
7+
control.onEvent(ControllerButtonEvent.Pressed, controller.down.id, () => { })
8+
control.onEvent(ControllerButtonEvent.Pressed, controller.left.id, () => { })
9+
control.onEvent(ControllerButtonEvent.Pressed, controller.right.id, () => { })
10+
}
11+
212
/**
313
* Passed to the constructor of a GUIComponent to quickly align it.
414
* Alignment may be further adjusted by an xOffset & yOffset.
@@ -860,34 +870,64 @@ namespace microcode {
860870

861871
export class TextButton {
862872
public bounds: Bounds;
873+
private shadowBounds: Bounds;
874+
875+
public callback: () => void;
876+
863877
private text: string;
864-
private callback: (x: any) => void;
865878
private colour: number;
866879
private textColour: number;
867880

868881
constructor(opts: {
869-
text: string,
870-
callback: (x: any) => void,
871-
x?: number,
872-
y?: number
873-
colour?: number,
874-
textColour?: number
882+
text: string,
883+
callback: () => void,
884+
x?: number,
885+
y?: number
886+
colour?: number,
887+
textColour?: number
875888
}) {
876889
this.text = opts.text;
877890

878891
const x = (opts.x != null) ? opts.x : 0;
879892
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});
893+
this.bounds = new Bounds({
894+
top: y,
895+
left: x,
896+
width: (font.charWidth * (this.text.length + 1)),
897+
height: font.charHeight + 2
898+
});
899+
this.shadowBounds = new Bounds({
900+
top: this.bounds.top,
901+
left: this.bounds.left,
902+
width: this.bounds.width + 1,
903+
height: this.bounds.height + 1
904+
});
905+
881906
this.callback = opts.callback;
907+
882908
this.colour = (opts.colour != null) ? opts.colour : 6;
883909
this.textColour = (opts.textColour != null) ? opts.textColour : 1;
884910
}
885911

912+
public shiftBounds(leftShift: number, topShift: number, widthShift: number, heightShift: number) {
913+
this.bounds.left += leftShift;
914+
this.bounds.top += topShift;
915+
this.bounds.width += widthShift;
916+
this.bounds.height += heightShift;
917+
918+
this.shadowBounds.left += leftShift;
919+
this.shadowBounds.top += topShift;
920+
this.shadowBounds.width += widthShift;
921+
this.shadowBounds.height += heightShift;
922+
}
923+
886924
draw() {
925+
this.shadowBounds.fillRect(15);
887926
this.bounds.fillRect(this.colour);
927+
888928
screen().print(
889929
this.text,
890-
this.bounds.left + (screen().width >> 1) + 1,
930+
this.bounds.left + (screen().width >> 1) + 3,
891931
this.bounds.top + (screen().height >> 1) + 1,
892932
this.textColour
893933
)
@@ -896,7 +936,10 @@ namespace microcode {
896936

897937

898938
export class TextButtonCollection extends GUIComponentAbstract {
939+
private title: string;
899940
private textBtns: TextButton[];
941+
private selectedTextBtnIndex: number;
942+
private cursorBounds: Bounds;
900943

901944
constructor(opts: {
902945
alignment: GUIComponentAlignment,
@@ -926,18 +969,65 @@ namespace microcode {
926969
border: opts.border
927970
});
928971

972+
this.title = (opts.title != null) ? opts.title : "";
929973
this.textBtns = (opts.textBtns != null) ? opts.textBtns : [];
974+
this.selectedTextBtnIndex = 0;
930975

976+
const titleYOffset = (this.title != "") ? 13 : 0;
931977
this.textBtns.forEach(btn => {
932-
btn.bounds.left += this.bounds.left;
933-
btn.bounds.top += this.bounds.top;
978+
btn.shiftBounds(this.bounds.left, this.bounds.top + titleYOffset, 0, 0)
934979
})
980+
981+
// Cursor:
982+
if (this.textBtns.length > 0) {
983+
this.cursorBounds = new Bounds({
984+
top: this.textBtns[0].bounds.top - 1,
985+
left: this.textBtns[0].bounds.left - 1,
986+
width: this.textBtns[0].bounds.width + 2,
987+
height: this.textBtns[0].bounds.height + 2,
988+
})
989+
}
990+
991+
this.setupButtonBindings();
992+
}
993+
994+
setupButtonBindings() {
995+
unbindShieldButtons();
996+
997+
control.onEvent(ControllerButtonEvent.Pressed, controller.A.id, () => {
998+
if (this.textBtns.length > 0) {
999+
this.textBtns[this.selectedTextBtnIndex].callback();
1000+
}
1001+
})
1002+
1003+
control.onEvent(ControllerButtonEvent.Pressed, controller.B.id, () => { })
1004+
1005+
control.onEvent(ControllerButtonEvent.Pressed, controller.up.id, () => {
1006+
this.selectedTextBtnIndex = Math.max(this.selectedTextBtnIndex - 1, 0);
1007+
})
1008+
control.onEvent(ControllerButtonEvent.Pressed, controller.down.id, () => {
1009+
this.selectedTextBtnIndex = (this.selectedTextBtnIndex + 1) % this.textBtns.length;
1010+
})
1011+
control.onEvent(ControllerButtonEvent.Pressed, controller.left.id, () => { })
1012+
control.onEvent(ControllerButtonEvent.Pressed, controller.right.id, () => { })
9351013
}
9361014

9371015
draw() {
9381016
super.draw();
9391017

940-
this.textBtns.forEach(btn => btn.draw())
1018+
const titleOffset = (font.charWidth * this.title.length) >> 1;
1019+
1020+
screen().print(
1021+
this.title,
1022+
(screen().width >> 1) + this.bounds.left + (this.width >> 1) - titleOffset,
1023+
(screen().height >> 1) + this.bounds.top + 2,
1024+
)
1025+
1026+
this.textBtns.forEach(btn => btn.draw());
1027+
1028+
if (this.textBtns.length > 0) {
1029+
this.cursorBounds.drawRect(9);
1030+
}
9411031
}
9421032
}
9431033

@@ -1165,15 +1255,6 @@ namespace microcode {
11651255
)
11661256
}
11671257

1168-
unbindShieldButtons() {
1169-
control.onEvent(ControllerButtonEvent.Pressed, controller.A.id, () => { })
1170-
control.onEvent(ControllerButtonEvent.Pressed, controller.A.id + keymap.PLAYER_OFFSET, () => { })
1171-
control.onEvent(ControllerButtonEvent.Pressed, controller.B.id, () => { })
1172-
control.onEvent(ControllerButtonEvent.Pressed, controller.up.id, () => { })
1173-
control.onEvent(ControllerButtonEvent.Pressed, controller.down.id, () => { })
1174-
control.onEvent(ControllerButtonEvent.Pressed, controller.left.id, () => { })
1175-
control.onEvent(ControllerButtonEvent.Pressed, controller.right.id, () => { })
1176-
}
11771258

11781259
makeActive() {
11791260
this.isActive = true
@@ -1182,7 +1263,7 @@ namespace microcode {
11821263

11831264
unmakeActive() {
11841265
this.isActive = false
1185-
this.unbindShieldButtons()
1266+
unbindShieldButtons()
11861267
}
11871268

11881269
click() {

main.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ namespace microcode {
106106
const txtBtnComp = new TextButtonCollection({
107107
alignment: GUIComponentAlignment.CENTRE,
108108
isActive: true,
109-
textBtns: [new TextButton({text: "hi", callback: () => "hi", })],
110-
xOffset: 10
109+
textBtns: [new TextButton({ text: "Text Btn 1", callback: () => basic.showString("hi"), textColour: 1})],
110+
xOffset: 10,
111+
title: "Title :)"
111112
})
112113
const window = new Window({ app, components: [txtBtnComp] })
113114
app.pushScene(window)
114-
}
115+
}

0 commit comments

Comments
 (0)