Skip to content

Commit 514a44c

Browse files
committed
Implement onclick handling and color toggling in layout example
1 parent 49deb46 commit 514a44c

1 file changed

Lines changed: 59 additions & 22 deletions

File tree

examples/layout.psh

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,22 @@ class UINode
5353
self.children[i].redraw(fb);
5454
}
5555
}
56-
}
5756

58-
// Box with a fixed size
59-
class Box extends UINode
60-
{
61-
init(self, width, height)
57+
onclick(self, x, y)
6258
{
63-
UINode.init(self);
64-
self.width = width;
65-
self.height = height;
59+
for (let var i = 0; i < self.children.len; ++i)
60+
{
61+
let child = self.children[i];
62+
63+
let on_child = (
64+
x >= child.x && x <= child.x + child.width &&
65+
y >= child.y && y <= child.y + child.height
66+
);
67+
68+
if (on_child) {
69+
child.onclick(x, y);
70+
}
71+
}
6672
}
6773
}
6874

@@ -198,8 +204,32 @@ class Center extends UINode
198204
}
199205
}
200206

207+
// Box with a fixed size
208+
class Box extends UINode
209+
{
210+
init(self, width, height)
211+
{
212+
UINode.init(self);
213+
self.width = width;
214+
self.height = height;
215+
}
216+
}
201217

218+
class Cell extends Box
219+
{
220+
init(self, width, height)
221+
{
222+
Box.init(self, width, height);
223+
self.bgcolor = COLOR_GREY;
224+
self.on = false;
225+
}
202226

227+
onclick(self, x, y)
228+
{
229+
self.on = !self.on;
230+
self.bgcolor = self.on? COLOR_WHITE:COLOR_GREY;
231+
}
232+
}
203233

204234
let fb = Image(550, 300);
205235
let window = $window_create(fb.width, fb.height, "Layout Engine Test", 0);
@@ -222,33 +252,40 @@ for (let var j = 0; j < 4; ++j)
222252
// For each cell
223253
for (let var i = 0; i < 16; ++i)
224254
{
225-
let box = Box(24, 24);
226-
box.bgcolor = COLOR_GREY;
227-
box.margin = 6;
228-
row.add_child(box);
255+
let cell = Cell(24, 24);
256+
cell.margin = 6;
257+
row.add_child(cell);
229258
}
230259

231260
col.add_child(row);
232261
}
233262

234-
center.compute_size();
235-
center.compute_pos();
236-
center.redraw(fb);
237-
$window_draw_frame(window, fb.bytes);
238-
239-
240-
263+
fun redraw()
264+
{
265+
let start_time = $time_current_ms();
266+
center.compute_size();
267+
center.compute_pos();
268+
center.redraw(fb);
269+
let dt = $time_current_ms() - start_time;
270+
$println("Redraw time: " + dt.to_s() + "ms");
271+
272+
$window_draw_frame(window, fb.bytes);
273+
}
241274

275+
redraw();
242276

243277
loop
244278
{
245279
let msg = $actor_recv();
280+
246281
if (msg instanceof UIEvent) {
247282
if (msg.kind == 'CLOSE_WINDOW' || (msg.kind == 'KEY_DOWN' && msg.key == 'ESCAPE')) {
248283
break;
249284
}
250-
}
251-
252-
253285

286+
if (msg.kind == 'MOUSE_DOWN') {
287+
center.onclick(msg.x, msg.y);
288+
redraw();
289+
}
290+
}
254291
}

0 commit comments

Comments
 (0)