@@ -9,40 +9,32 @@ import {
99 TextureAtlas ,
1010 UIBaseElement ,
1111 UISpriteElement ,
12- video ,
12+ UITextButton ,
1313} from "melonjs" ;
1414import { createExampleComponent } from "../utils" ;
1515
1616const base = `${ import . meta. env . BASE_URL } assets/ui/` ;
1717
1818let texture : TextureAtlas ;
1919
20- class ButtonUI extends UISpriteElement {
20+ class ButtonUI extends UITextButton {
2121 private unclicked_region : object ;
2222 private clicked_region : object ;
23- private font : Text ;
24- private label : string ;
2523
2624 constructor ( x : number , y : number , color : string , label : string ) {
2725 super ( x , y , {
2826 image : texture ,
2927 region : `${ color } _button04` ,
28+ font : "kenpixel" ,
29+ size : 12 ,
30+ text : label ,
3031 } ) ;
3132
3233 this . unclicked_region = texture . getRegion ( `${ color } _button04` ) ;
3334 this . clicked_region = texture . getRegion ( `${ color } _button05` ) ;
3435 this . anchorPoint . set ( 0 , 0 ) ;
3536 this . setOpacity ( 0.5 ) ;
36- this . label = label ;
3737 this . floating = false ;
38-
39- this . font = new Text ( 0 , 0 , {
40- font : "kenpixel" ,
41- size : 12 ,
42- fillStyle : "black" ,
43- textAlign : "center" ,
44- textBaseline : "middle" ,
45- } ) ;
4638 }
4739
4840 override onOver ( ) {
@@ -70,29 +62,15 @@ class ButtonUI extends UISpriteElement {
7062 ) ;
7163 return false ;
7264 }
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- }
8765}
8866
8967class CheckBoxUI extends UISpriteElement {
9068 private on_icon_region : object ;
9169 private off_icon_region : object ;
92- private font : Text ;
9370 private isSelected : boolean ;
9471 private label_on : string ;
9572 private label_off : string ;
73+ label : Text ;
9674
9775 constructor (
9876 x : number ,
@@ -116,16 +94,15 @@ class CheckBoxUI extends UISpriteElement {
11694 this . label_off = offLabel ;
11795 this . floating = false ;
11896
119- this . font = new Text ( 0 , 0 , {
97+ // create label as a sibling — added to the parent by the caller
98+ this . label = new Text ( x + this . width , y + this . height / 2 , {
12099 font : "kenpixel" ,
121100 size : 12 ,
122101 fillStyle : "black" ,
123102 textAlign : "left" ,
124103 textBaseline : "middle" ,
125- text : offLabel ,
104+ text : onLabel ,
126105 } ) ;
127-
128- this . getBounds ( ) . width += this . font . measureText ( ) . width ;
129106 }
130107
131108 override onOver ( ) {
@@ -140,26 +117,18 @@ class CheckBoxUI extends UISpriteElement {
140117 if ( selected ) {
141118 this . setRegion ( this . on_icon_region ) ;
142119 this . isSelected = true ;
120+ this . label . setText ( this . label_on ) ;
143121 } else {
144122 this . setRegion ( this . off_icon_region ) ;
145123 this . isSelected = false ;
124+ this . label . setText ( this . label_off ) ;
146125 }
147126 }
148127
149128 override onClick ( ) {
150129 this . setSelected ( ! this . isSelected ) ;
151130 return false ;
152131 }
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- }
163132}
164133
165134class UIContainer extends UIBaseElement {
@@ -192,7 +161,6 @@ class UIContainer extends UIBaseElement {
192161 fillStyle : "black" ,
193162 textAlign : "center" ,
194163 textBaseline : "top" ,
195- bold : true ,
196164 text : label ,
197165 } ) ,
198166 ) ;
@@ -213,28 +181,29 @@ class PlayScreen extends Stage {
213181
214182 const cbPanel = new UIBaseElement ( 125 , 75 , 100 , 100 ) ;
215183
216- cbPanel . addChild (
217- new CheckBoxUI (
218- 0 ,
219- 0 ,
220- texture ,
221- "green_boxCheckmark" ,
222- "grey_boxCheckmark" ,
223- "Music ON" ,
224- "Music OFF" ,
225- ) ,
184+ const cb1 = new CheckBoxUI (
185+ 0 ,
186+ 0 ,
187+ texture ,
188+ "green_boxCheckmark" ,
189+ "grey_boxCheckmark" ,
190+ "Music ON" ,
191+ "Music OFF" ,
226192 ) ;
227- cbPanel . addChild (
228- new CheckBoxUI (
229- 0 ,
230- 50 ,
231- texture ,
232- "green_boxCheckmark" ,
233- "grey_boxCheckmark" ,
234- "Sound FX ON" ,
235- "Sound FX OFF" ,
236- ) ,
193+ cbPanel . addChild ( cb1 ) ;
194+ cbPanel . addChild ( cb1 . label ) ;
195+
196+ const cb2 = new CheckBoxUI (
197+ 0 ,
198+ 50 ,
199+ texture ,
200+ "green_boxCheckmark" ,
201+ "grey_boxCheckmark" ,
202+ "Sound FX ON" ,
203+ "Sound FX OFF" ,
237204 ) ;
205+ cbPanel . addChild ( cb2 ) ;
206+ cbPanel . addChild ( cb2 . label ) ;
238207
239208 panel . addChild ( cbPanel ) ;
240209
@@ -247,7 +216,7 @@ class PlayScreen extends Stage {
247216}
248217
249218const createGame = ( ) => {
250- const app = new App ( 800 , 600 , {
219+ new App ( 800 , 600 , {
251220 parent : "screen" ,
252221 scale : "auto" ,
253222 scaleMethod : "flex-width" ,
0 commit comments