Skip to content

Commit e663a97

Browse files
committed
TextButton & TextButtonCollection: improved cursor, added fixed issue where the cursor would jut out by 1 pixel.
1 parent 0a83fb6 commit e663a97

2 files changed

Lines changed: 45 additions & 11 deletions

File tree

guiComponents.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@ namespace microcode {
876876

877877
private text: string;
878878
private colour: number;
879+
private shadowColour: number;
879880
private textColour: number;
880881

881882
constructor(opts: {
@@ -893,19 +894,20 @@ namespace microcode {
893894
this.bounds = new Bounds({
894895
top: y,
895896
left: x,
896-
width: (font.charWidth * (this.text.length + 1)),
897+
width: (font.charWidth * (this.text.length + 1)) + 1, // + 1 for the cursor in ButtonCollection to draw on top of.
897898
height: font.charHeight + 2
898899
});
899900
this.shadowBounds = new Bounds({
900901
top: this.bounds.top,
901-
left: this.bounds.left,
902-
width: this.bounds.width + 1,
902+
left: this.bounds.left + 1,
903+
width: this.bounds.width + 1, // + 1 for the cursor in ButtonCollection to draw on top of.
903904
height: this.bounds.height + 1
904905
});
905906

906907
this.callback = opts.callback;
907908

908909
this.colour = (opts.colour != null) ? opts.colour : 6;
910+
this.shadowColour = 15;
909911
this.textColour = (opts.textColour != null) ? opts.textColour : 1;
910912
}
911913

@@ -921,8 +923,12 @@ namespace microcode {
921923
this.shadowBounds.height += heightShift;
922924
}
923925

926+
public setShadowColour(newColour: number) {
927+
this.shadowColour = newColour;
928+
}
929+
924930
draw() {
925-
this.shadowBounds.fillRect(15);
931+
this.shadowBounds.fillRect(this.shadowColour);
926932
this.bounds.fillRect(this.colour);
927933

928934
screen().print(
@@ -974,18 +980,23 @@ namespace microcode {
974980
this.selectedTextBtnIndex = 0;
975981

976982
const titleYOffset = (this.title != "") ? 13 : 0;
983+
const btnXOffset = (this.textBtns.length > 0) ? (this.bounds.width / (this.textBtns.length + 1)) : 0;
984+
985+
let cumulativeXOffset = 0;
977986
this.textBtns.forEach(btn => {
978-
btn.shiftBounds(this.bounds.left, this.bounds.top + titleYOffset, 0, 0)
987+
btn.shiftBounds(this.bounds.left, this.bounds.top + titleYOffset + cumulativeXOffset, 0, 0);
988+
cumulativeXOffset += btnXOffset
979989
})
980990

981991
// Cursor:
982992
if (this.textBtns.length > 0) {
983993
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-
})
994+
top: this.textBtns[this.selectedTextBtnIndex].bounds.top - 1,
995+
left: this.textBtns[this.selectedTextBtnIndex].bounds.left,
996+
width: this.textBtns[this.selectedTextBtnIndex].bounds.width + 2,
997+
height: this.textBtns[this.selectedTextBtnIndex].bounds.height + 2,
998+
});
999+
this.textBtns[this.selectedTextBtnIndex].setShadowColour(6);
9891000
}
9901001

9911002
this.setupButtonBindings();
@@ -1003,11 +1014,31 @@ namespace microcode {
10031014
control.onEvent(ControllerButtonEvent.Pressed, controller.B.id, () => { })
10041015

10051016
control.onEvent(ControllerButtonEvent.Pressed, controller.up.id, () => {
1017+
this.textBtns[this.selectedTextBtnIndex].setShadowColour(15);
1018+
10061019
this.selectedTextBtnIndex = Math.max(this.selectedTextBtnIndex - 1, 0);
1020+
this.cursorBounds = new Bounds({
1021+
top: this.textBtns[this.selectedTextBtnIndex].bounds.top - 1,
1022+
left: this.textBtns[this.selectedTextBtnIndex].bounds.left,
1023+
width: this.textBtns[this.selectedTextBtnIndex].bounds.width + 2,
1024+
height: this.textBtns[this.selectedTextBtnIndex].bounds.height + 2,
1025+
})
1026+
this.textBtns[this.selectedTextBtnIndex].setShadowColour(6);
10071027
})
1028+
10081029
control.onEvent(ControllerButtonEvent.Pressed, controller.down.id, () => {
1030+
this.textBtns[this.selectedTextBtnIndex].setShadowColour(15);
1031+
10091032
this.selectedTextBtnIndex = (this.selectedTextBtnIndex + 1) % this.textBtns.length;
1033+
this.cursorBounds = new Bounds({
1034+
top: this.textBtns[this.selectedTextBtnIndex].bounds.top - 1,
1035+
left: this.textBtns[this.selectedTextBtnIndex].bounds.left,
1036+
width: this.textBtns[this.selectedTextBtnIndex].bounds.width + 2,
1037+
height: this.textBtns[this.selectedTextBtnIndex].bounds.height + 2,
1038+
})
1039+
this.textBtns[this.selectedTextBtnIndex].setShadowColour(6);
10101040
})
1041+
10111042
control.onEvent(ControllerButtonEvent.Pressed, controller.left.id, () => { })
10121043
control.onEvent(ControllerButtonEvent.Pressed, controller.right.id, () => { })
10131044
}

main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ namespace microcode {
106106
const txtBtnComp = new TextButtonCollection({
107107
alignment: GUIComponentAlignment.CENTRE,
108108
isActive: true,
109-
textBtns: [new TextButton({ text: "Text Btn 1", callback: () => basic.showString("hi"), textColour: 1})],
109+
textBtns: [
110+
new TextButton({ text: "Text Btn 1", callback: () => basic.showString("hi") }),
111+
new TextButton({ text: "Text Btn 2", callback: () => basic.showString("yo") })
112+
],
110113
xOffset: 10,
111114
title: "Title :)"
112115
})

0 commit comments

Comments
 (0)