Skip to content

Commit 191b992

Browse files
obiotclaude
andcommitted
Fix UI example: replace standalone Text.draw() with preDraw/draw/postDraw
Text.draw(renderer, text, x, y) standalone pattern was removed in 19.0. Use pos.set + setText + preDraw/draw/postDraw instead. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d333fc6 commit 191b992

1 file changed

Lines changed: 11 additions & 33 deletions

File tree

packages/examples/src/examples/ui/ExampleUI.tsx

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
TextureAtlas,
1010
UIBaseElement,
1111
UISpriteElement,
12-
video,
1312
} from "melonjs";
1413
import { createExampleComponent } from "../utils";
1514

@@ -20,8 +19,6 @@ let texture: TextureAtlas;
2019
class ButtonUI extends UISpriteElement {
2120
private unclicked_region: object;
2221
private clicked_region: object;
23-
private font: Text;
24-
private label: string;
2522

2623
constructor(x: number, y: number, color: string, label: string) {
2724
super(x, y, {
@@ -33,16 +30,18 @@ class ButtonUI extends UISpriteElement {
3330
this.clicked_region = texture.getRegion(`${color}_button05`);
3431
this.anchorPoint.set(0, 0);
3532
this.setOpacity(0.5);
36-
this.label = label;
3733
this.floating = false;
3834

39-
this.font = new Text(0, 0, {
35+
// add label as child — drawn automatically by the container
36+
const font = new Text(this.width / 2, this.height / 2, {
4037
font: "kenpixel",
4138
size: 12,
4239
fillStyle: "black",
4340
textAlign: "center",
4441
textBaseline: "middle",
42+
text: label,
4543
});
44+
this.addChild(font);
4645
}
4746

4847
override onOver() {
@@ -70,20 +69,6 @@ class ButtonUI extends UISpriteElement {
7069
);
7170
return false;
7271
}
73-
74-
override draw(renderer: Parameters<UISpriteElement["draw"]>[0]) {
75-
super.draw(renderer);
76-
this.font.draw(
77-
renderer,
78-
this.label,
79-
this.pos.x + this.width / 2,
80-
this.pos.y + this.height / 2,
81-
);
82-
}
83-
84-
override onDestroyEvent() {
85-
this.font.destroy();
86-
}
8772
}
8873

8974
class CheckBoxUI extends UISpriteElement {
@@ -116,14 +101,16 @@ class CheckBoxUI extends UISpriteElement {
116101
this.label_off = offLabel;
117102
this.floating = false;
118103

119-
this.font = new Text(0, 0, {
104+
// add label as child — positioned to the right of the checkbox
105+
this.font = new Text(this.width, this.height / 2, {
120106
font: "kenpixel",
121107
size: 12,
122108
fillStyle: "black",
123109
textAlign: "left",
124110
textBaseline: "middle",
125-
text: offLabel,
111+
text: onLabel,
126112
});
113+
this.addChild(this.font);
127114

128115
this.getBounds().width += this.font.measureText().width;
129116
}
@@ -140,26 +127,18 @@ class CheckBoxUI extends UISpriteElement {
140127
if (selected) {
141128
this.setRegion(this.on_icon_region);
142129
this.isSelected = true;
130+
this.font.setText(this.label_on);
143131
} else {
144132
this.setRegion(this.off_icon_region);
145133
this.isSelected = false;
134+
this.font.setText(this.label_off);
146135
}
147136
}
148137

149138
override onClick() {
150139
this.setSelected(!this.isSelected);
151140
return false;
152141
}
153-
154-
override draw(renderer: Parameters<UISpriteElement["draw"]>[0]) {
155-
super.draw(renderer);
156-
this.font.draw(
157-
renderer,
158-
` ${this.isSelected ? this.label_on : this.label_off}`,
159-
this.pos.x + this.width,
160-
this.pos.y + this.height / 2,
161-
);
162-
}
163142
}
164143

165144
class UIContainer extends UIBaseElement {
@@ -192,7 +171,6 @@ class UIContainer extends UIBaseElement {
192171
fillStyle: "black",
193172
textAlign: "center",
194173
textBaseline: "top",
195-
bold: true,
196174
text: label,
197175
}),
198176
);
@@ -247,7 +225,7 @@ class PlayScreen extends Stage {
247225
}
248226

249227
const createGame = () => {
250-
const app = new App(800, 600, {
228+
const _app = new App(800, 600, {
251229
parent: "screen",
252230
scale: "auto",
253231
scaleMethod: "flex-width",

0 commit comments

Comments
 (0)