diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-09-20 13:43:18 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-09-20 16:56:30 +0200 |
| commit | 87b4ef3a15af505f5ed5150ee1dadd2e2bc94c17 (patch) | |
| tree | f3d317088514089d0f84fb553cf9e307dffca549 /src/gui | |
| parent | 14df82ee4db71509f4ec4968df439d4659ca1ac3 (diff) | |
| download | jancity-87b4ef3a15af505f5ed5150ee1dadd2e2bc94c17.tar.gz | |
Remap calls to pad/mouse/keyboard to input
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/gui/inc/gui.h | 5 | ||||
| -rw-r--r-- | src/gui/privinc/gui_private.h | 7 | ||||
| -rw-r--r-- | src/gui/src/button.c | 42 | ||||
| -rw-r--r-- | src/gui/src/container.c | 4 | ||||
| -rw-r--r-- | src/gui/src/gui.c | 84 |
6 files changed, 103 insertions, 41 deletions
diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index b7fe2c1..6ff3a71 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -9,4 +9,4 @@ add_library(gui "src/rounded_rect.c" ) target_include_directories(gui PUBLIC "inc" PRIVATE "privinc") -target_link_libraries(gui PUBLIC camera gfx peripheral font) +target_link_libraries(gui PUBLIC camera gfx input peripheral font) diff --git a/src/gui/inc/gui.h b/src/gui/inc/gui.h index 84a6c66..9093572 100644 --- a/src/gui/inc/gui.h +++ b/src/gui/inc/gui.h @@ -3,6 +3,7 @@ #include <camera.h> #include <gfx.h> +#include <input.h> #include <peripheral.h> #ifdef __cplusplus @@ -16,7 +17,7 @@ struct gui_common { void (*add_child)(struct gui_common *parent, struct gui_common *child); int (*update)(struct gui_common *, const union peripheral *, - const struct camera *); + const struct camera *, struct input *); int (*render)(const struct gui_common *); void (*get_dim)(const struct gui_common *, short *w, short *h); } *cb; @@ -29,7 +30,7 @@ struct gui_common void gui_add_child(struct gui_common *parent, struct gui_common *child); void gui_add_sibling(struct gui_common *g, struct gui_common *sibling); int gui_update(struct gui_common *g, const union peripheral *p, - const struct camera *c); + const struct camera *c, struct input *in); int gui_render(const struct gui_common *g); #ifdef __cplusplus diff --git a/src/gui/privinc/gui_private.h b/src/gui/privinc/gui_private.h index af18dcd..c03d04c 100644 --- a/src/gui/privinc/gui_private.h +++ b/src/gui/privinc/gui_private.h @@ -2,6 +2,9 @@ #define GUI_PRIVATE_H #include <gui.h> +#include <input.h> +#include <peripheral.h> +#include <camera.h> #ifdef __cplusplus extern "C" @@ -9,6 +12,10 @@ extern "C" #endif void gui_coords(const struct gui_common *g, short *x, short *y); +bool gui_pressed(const struct gui_common *g, const struct input *in, + const union peripheral *p, const struct camera *cam, short w, short h); +bool gui_released(const struct gui_common *g, const union peripheral *p, + const struct camera *cam, short w, short h); #ifdef __cplusplus } diff --git a/src/gui/src/button.c b/src/gui/src/button.c index f4f94e0..cf49679 100644 --- a/src/gui/src/button.c +++ b/src/gui/src/button.c @@ -4,6 +4,7 @@ #include <gui_private.h> #include <camera.h> #include <gfx.h> +#include <input.h> #include <mouse.h> #include <pad.h> #include <peripheral.h> @@ -37,47 +38,22 @@ static void get_dim(const struct gui_common *const g, static bool pressed(const struct gui_button *const b, const union peripheral *const p, - const struct camera *const cam) + const struct camera *const cam, + const struct input *const in) { - bool check = false; + short w, h; - switch (p->common.type) - { - case PERIPHERAL_TYPE_PAD: - check = pad_justpressed(&p->pad.pad, PAD_KEY_A); - break; - - case PERIPHERAL_TYPE_KEYBOARD_MOUSE: - check = mouse_justreleased(&p->kbm.mouse, MOUSE_BUTTON_LEFT); - break; - - case PERIPHERAL_TYPE_TOUCH: - check = mouse_justpressed(&p->kbm.mouse, MOUSE_BUTTON_LEFT); - break; - } - - if (check) - { - short x, y; - struct util_rect d; - - gui_coords(&b->common, &x, &y); - get_dim(&b->common, &d.w, &d.h); - d.x = x; - d.y = y; - - return cursor_collision(cam, &d); - } - - return false; + get_dim(&b->common, &w, &h); + return gui_pressed(&b->common, in, p, cam, w, h); } static int update(struct gui_common *const g, - const union peripheral *const p, const struct camera *const c) + const union peripheral *const p, const struct camera *const c, + struct input *const in) { struct gui_button *const b = (struct gui_button *)g; - if (pressed(b, p, c) && b->on_pressed) + if (pressed(b, p, c, in) && b->on_pressed) b->on_pressed(b->arg); return 0; diff --git a/src/gui/src/container.c b/src/gui/src/container.c index ad3b901..e7d4049 100644 --- a/src/gui/src/container.c +++ b/src/gui/src/container.c @@ -1,6 +1,7 @@ #include <gui/container.h> #include <gui.h> #include <camera.h> +#include <input.h> #include <peripheral.h> static void add_child(struct gui_common *const parent, @@ -52,7 +53,8 @@ static void get_dim(const struct gui_common *const g, } static int update(struct gui_common *const g, - const union peripheral *const p, const struct camera *const cam) + const union peripheral *const p, const struct camera *const cam, + struct input *const in) { struct gui_container *const c = (struct gui_container *)g; diff --git a/src/gui/src/gui.c b/src/gui/src/gui.c index 022ce73..b99300b 100644 --- a/src/gui/src/gui.c +++ b/src/gui/src/gui.c @@ -1,4 +1,7 @@ #include <gui.h> +#include <camera.h> +#include <input.h> +#include <peripheral.h> static void get_centered(const struct gui_common *const g, short *const x, short *const y) @@ -51,6 +54,79 @@ void gui_coords(const struct gui_common *const g, short *const x, get_centered(g, x, y); } + +static bool check_collision(const struct gui_common *const g, + const union peripheral *const p, + const struct camera *cam, const short w, const short h) +{ + short x, y; + + gui_coords(g, &x, &y); + + const struct util_rect d = + { + .x = x, + .y = y, + .w = w, + .h = h + }; + + return cursor_collision(cam, &d); +} + +bool gui_pressed(const struct gui_common *const g, + const struct input *const in, + const union peripheral *const p, + const struct camera *cam, const short w, const short h) +{ + bool check = false; + + switch (p->common.type) + { + case PERIPHERAL_TYPE_PAD: + check = input_pad_justpressed(in, &p->pad.pad, PAD_KEY_A); + break; + + case PERIPHERAL_TYPE_KEYBOARD_MOUSE: + /* Fall through. */ + case PERIPHERAL_TYPE_TOUCH: + check = input_mouse_justreleased(in, &p->kbm.mouse, + MOUSE_BUTTON_LEFT); + break; + } + + if (check) + return check_collision(g, p, cam, w, h); + + return false; +} + +bool gui_released(const struct gui_common *const g, + const union peripheral *const p, + const struct camera *cam, const short w, const short h) +{ + bool check = false; + + switch (p->common.type) + { + case PERIPHERAL_TYPE_PAD: + check = pad_justpressed(&p->pad.pad, PAD_KEY_A); + break; + + case PERIPHERAL_TYPE_KEYBOARD_MOUSE: + /* Fall through. */ + case PERIPHERAL_TYPE_TOUCH: + check = mouse_justreleased(&p->kbm.mouse, + MOUSE_BUTTON_LEFT); + break; + } + + if (check) + return !check_collision(g, p, cam, w, h); + + return false; +} + void gui_add_sibling(struct gui_common *const g, struct gui_common *const s) { @@ -77,16 +153,16 @@ void gui_add_child(struct gui_common *const p, } int gui_update(struct gui_common *const g, const union peripheral *const p, - const struct camera *const c) + const struct camera *const c, struct input *const in) { - if (g->child && gui_update(g->child, p, c)) + if (g->child && gui_update(g->child, p, c, in)) return -1; - if (g->cb && g->cb->update && g->cb->update(g, p, c)) + if (g->cb && g->cb->update && g->cb->update(g, p, c, in)) return -1; for (struct gui_common *s = g->sibling; s; s = s->sibling) - if (gui_update(s, p, c)) + if (gui_update(s, p, c, in)) return -1; return 0; |
