From 9d3e754f3660e3bd6d13475a7d26e6d1f1e29fbe Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Fri, 23 Sep 2022 04:12:07 +0200 Subject: [PATCH] Implement checkbox GUI element --- res/CMakeLists.txt | 9 +++++ res/LICENSE | 2 ++ res/checkbox.bmp | Bin 0 -> 382 bytes res/checkbox_24.bmp | Bin 0 -> 1230 bytes src/game/src/res.c | 10 ++++++ src/gui/CMakeLists.txt | 1 + src/gui/inc/gui/checkbox.h | 34 ++++++++++++++++++ src/gui/src/checkbox.c | 71 +++++++++++++++++++++++++++++++++++++ 8 files changed, 127 insertions(+) create mode 100644 res/checkbox.bmp create mode 100644 res/checkbox_24.bmp create mode 100644 src/gui/inc/gui/checkbox.h create mode 100644 src/gui/src/checkbox.c diff --git a/res/CMakeLists.txt b/res/CMakeLists.txt index d77daaf..88069af 100644 --- a/res/CMakeLists.txt +++ b/res/CMakeLists.txt @@ -208,6 +208,14 @@ sprite(NAME line_edit_right CY 491 TRANSPARENT FALSE) +sprite(NAME checkbox + X 376 + Y 132 + BPP 4 + CX 368 + CY 490 + TRANSPARENT TRUE) + sound(NAME acknowledge_01) sound(NAME acknowledge_02) sound(NAME selected_01) @@ -218,6 +226,7 @@ container(NAME rts btn_left btn_mid btn_right + checkbox worker_n worker_ne worker_e diff --git a/res/LICENSE b/res/LICENSE index e252ace..86a891a 100644 --- a/res/LICENSE +++ b/res/LICENSE @@ -57,6 +57,8 @@ line_edit_mid.bmp: line_edit_mid_24.bmp: line_edit_right.bmp: line_edit_right_24.bmp: +checkbox_24.bmp: +checkbox.bmp: Derived works from ui_sheet.png font.bmp: diff --git a/res/checkbox.bmp b/res/checkbox.bmp new file mode 100644 index 0000000000000000000000000000000000000000..a5678a5337f84204c0d0f063708ed5d373415e0f GIT binary patch literal 382 zcmZ?rtz%>WgLyzQ2Z&{Wm=A~<8CZZM1H%I#-oOvRoIo}RJGloH11XXK6PjvnJ|PBO zLn{UgCl7}IKLSvYy%LS(3{?460n4M217ZtSWk5kRm6)jrE<&D4T=pW10}BvjE*>^eB>-yZ8@d1h literal 0 HcmV?d00001 diff --git a/src/game/src/res.c b/src/game/src/res.c index da07f96..23103a0 100644 --- a/src/game/src/res.c +++ b/src/game/src/res.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -273,6 +274,15 @@ static const struct container c[] = { .sprite = &gui_line_edit_sprites[GUI_LINE_EDIT_RIGHT] } + }, + + { + .path = "checkbox", + .type = CONTAINER_TYPE_SPRITE, + .data = + { + .sprite = &gui_checkbox_sprite + } } }; diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 504c02f..bd5b004 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -2,6 +2,7 @@ add_library(gui "src/bar.c" "src/button.c" "src/button_type1.c" + "src/checkbox.c" "src/container.c" "src/gui.c" "src/label.c" diff --git a/src/gui/inc/gui/checkbox.h b/src/gui/inc/gui/checkbox.h new file mode 100644 index 0000000..bf37f34 --- /dev/null +++ b/src/gui/inc/gui/checkbox.h @@ -0,0 +1,34 @@ +#ifndef GUI_CHECKBOX_H +#define GUI_CHECKBOX_H + +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct gui_checkbox +{ + struct gui_common common; + bool active; + void *arg; + void (*on_pressed)(bool active, void *arg); +}; + +void gui_checkbox_init(struct gui_checkbox *c); + +extern struct sprite gui_checkbox_sprite; + +UTIL_STATIC_ASSERT(!offsetof(struct gui_checkbox, common), + "unexpected offset for struct gui_checkbox"); + +#ifdef __cplusplus +} +#endif + +#endif /* GUI_CHECKBOX_H */ diff --git a/src/gui/src/checkbox.c b/src/gui/src/checkbox.c new file mode 100644 index 0000000..128423d --- /dev/null +++ b/src/gui/src/checkbox.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include + +struct sprite gui_checkbox_sprite; + +static int render(const struct gui_common *const g) +{ + const struct gui_checkbox *const c = (const struct gui_checkbox *)g; + + sprite_get_or_ret(s, -1); + + if (sprite_clone(&gui_checkbox_sprite, s)) + return -1; + + gui_coords(g, &s->x, &s->y); + s->w /= 2; + + if (c->active) + s->u += s->w; + + sprite_sort(s); + return 0; +} + +static void get_dim(const struct gui_common *const g, + short *const w, short *const h) +{ + *w = gui_checkbox_sprite.w / 2; + *h = gui_checkbox_sprite.h; +} + +static int update(struct gui_common *const g, + const union peripheral *const p, const struct camera *const cam, + struct input *const in) +{ + struct gui_checkbox *const c = (struct gui_checkbox *)g; + short w, h; + + get_dim(g, &w, &h); + + if (gui_pressed(g, in, p, cam, w, h)) + { + c->active ^= true; + + if (c->on_pressed) + c->on_pressed(c->active, c->arg); + } + + return 0; +} + +void gui_checkbox_init(struct gui_checkbox *const c) +{ + static const struct gui_common_cb cb = + { + .get_dim = get_dim, + .update = update, + .render = render + }; + + *c = (const struct gui_checkbox) + { + .common = + { + .cb = &cb + } + }; +}