From 03633cde2a8e16f813f08e8cdd85ddded51f87ae Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Sat, 10 Aug 2024 19:58:37 +0200 Subject: Buttons --- examples/ui.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'examples/ui.c') diff --git a/examples/ui.c b/examples/ui.c index 95a3235..9f31550 100755 --- a/examples/ui.c +++ b/examples/ui.c @@ -366,6 +366,22 @@ void draw_text_area(u32 color, f64 x0, f64 y0, f64 width, f64 height, i64 num_ch print_text(color, x0, y0, scale_x, scale_y, num_chars, text); } +void draw_panel(u32 color, f64 x0, f64 y0, f64 width, f64 height) { + i64 i0 = (i64) floor(x0 + .5); + i64 j0 = (i64) floor(y0 + .5); + i64 i1 = (i64) floor(x0 + width + .5); + i64 j1 = (i64) floor(y0 + height + .5); + + if (i0 < 0) i0 = 0; + if (j0 < 0) j0 = 0; + if (i1 >= platform.frame_width) i1 = platform.frame_width - 1; + if (j1 >= platform.frame_height) j1 = platform.frame_height - 1; + + for (i64 j = j0; j < j1; ++j) + for (i64 i = i0; i < i1; ++i) + platform.pixels[j * platform.frame_width + i] = color; +} + i32 main(i32 argc, c8 **argv) { (void) argc; (void) argv; @@ -378,6 +394,10 @@ i32 main(i32 argc, c8 **argv) { p_init(); + b8 button_0_down = 0; + b8 button_1_down = 0; + b8 button_1_checked = 0; + while (!platform.done) { p_wait_events(); @@ -385,6 +405,35 @@ i32 main(i32 argc, c8 **argv) { for (i32 i = 0; i < platform.frame_width; ++i) platform.pixels[j * platform.frame_width + i] = 0x302000; + if (platform.cursor_x >= 40 && platform.cursor_x < 100 && platform.cursor_y >= 40 && platform.cursor_y < 100) { + button_0_down = platform.key_down[BUTTON_LEFT]; + if (button_0_down) + draw_panel(0xffffff, 40, 40, 60, 60); + else + draw_panel(0x00ff00, 40, 40, 60, 60); + } else { + button_0_down = 0; + draw_panel(0x208020, 40, 40, 60, 60); + } + + if (platform.cursor_x >= 40 && platform.cursor_x < 100 && platform.cursor_y >= 120 && platform.cursor_y < 180) { + button_1_down = platform.key_down[BUTTON_LEFT]; + if (platform.key_pressed[BUTTON_LEFT]) + button_1_checked = !button_1_checked; + if (button_1_down) + draw_panel(0xffffff, 40, 120, 60, 60); + else if (button_1_checked) + draw_panel(0xff8080, 40, 120, 60, 60); + else + draw_panel(0x80ff80, 40, 120, 60, 60); + } else { + button_1_down = 0; + if (button_1_checked) + draw_panel(0xff0000, 40, 120, 60, 60); + else + draw_panel(0x00ff00, 40, 120, 60, 60); + } + i64 w = platform.frame_width / 2; i64 h = platform.frame_height / 2; i64 x0 = w / 2; -- cgit v1.2.3