diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-09-23 04:12:07 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-09-23 04:12:07 +0200 |
| commit | 9d3e754f3660e3bd6d13475a7d26e6d1f1e29fbe (patch) | |
| tree | 8bb4281acdb9648f987f77179f9cbde79747d4bb | |
| parent | 467f09e9521e509295211560b57ca9a0119837bd (diff) | |
Implement checkbox GUI element
| -rw-r--r-- | res/CMakeLists.txt | 9 | ||||
| -rw-r--r-- | res/LICENSE | 2 | ||||
| -rw-r--r-- | res/checkbox.bmp | bin | 0 -> 382 bytes | |||
| -rw-r--r-- | res/checkbox_24.bmp | bin | 0 -> 1230 bytes | |||
| -rw-r--r-- | src/game/src/res.c | 10 | ||||
| -rw-r--r-- | src/gui/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/gui/inc/gui/checkbox.h | 34 | ||||
| -rw-r--r-- | src/gui/src/checkbox.c | 71 |
8 files changed, 127 insertions, 0 deletions
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 Binary files differnew file mode 100644 index 0000000..a5678a5 --- /dev/null +++ b/res/checkbox.bmp diff --git a/res/checkbox_24.bmp b/res/checkbox_24.bmp Binary files differnew file mode 100644 index 0000000..3b001a4 --- /dev/null +++ b/res/checkbox_24.bmp 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 <gui/button.h> #include <gui/line_edit.h> #include <gui/rounded_rect.h> +#include <gui/checkbox.h> #include <resource.h> #include <terrain.h> #include <unit.h> @@ -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 <gui.h> +#include <gfx.h> +#include <util.h> +#include <stdbool.h> +#include <stddef.h> + +#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 <gui.h> +#include <gui/checkbox.h> +#include <gui_private.h> +#include <gfx.h> +#include <stdbool.h> + +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 + } + }; +} |
