Mercurial > molko
changeset 409:6011ad866e99
ui: delete action functions
The handling of actions is done without taking care of the return value which
make it impossible to detect UI elements since it's a immediate mode like
implementation.
Users are encouraged to write code like instead:
```
if (button_handle(&b, &ev)) {
// Do something.
}
```
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 09 Sep 2022 09:27:17 +0200 |
parents | 0ea90751a62d |
children | 1bf7d6669f0a |
files | examples/example-ui/main.c src/libmlk-ui/ui/button.c src/libmlk-ui/ui/button.h src/libmlk-ui/ui/checkbox.c src/libmlk-ui/ui/checkbox.h src/libmlk-ui/ui/frame.c src/libmlk-ui/ui/frame.h src/libmlk-ui/ui/label.c src/libmlk-ui/ui/label.h |
diffstat | 9 files changed, 13 insertions(+), 156 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/example-ui/main.c Fri Apr 08 15:59:08 2022 +0200 +++ b/examples/example-ui/main.c Fri Sep 09 09:27:17 2022 +0200 @@ -16,8 +16,6 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include <core/action.h> -#include <core/action-stack.h> #include <core/core.h> #include <core/event.h> #include <core/game.h> @@ -63,9 +61,6 @@ * +---------------------------------------------+ */ static struct { - struct action *actions[16]; - struct action_stack st; - struct { int active; int x; @@ -74,24 +69,19 @@ struct { struct frame frame; - struct action act; } panel; struct { struct label label; - struct action act; } header; struct { struct checkbox cb; - struct action cb_act; struct label label; - struct action label_act; } autosave; struct { struct button button; - struct action act; } quit; } ui = { .panel = { @@ -137,8 +127,6 @@ panic(); if (window_open("Example - UI", W, H) < 0) panic(); - - action_stack_init(&ui.st, ui.actions, UTIL_SIZE(ui.actions)); } static void @@ -195,30 +183,6 @@ resize_button(); } -static void -prepare(void) -{ - /* Frame. */ - frame_action(&ui.panel.frame, &ui.panel.act); - - /* Header title. */ - label_action(&ui.header.label, &ui.header.act); - - /* Button quit. */ - button_action(&ui.quit.button, &ui.quit.act); - - /* Autosave. */ - checkbox_action(&ui.autosave.cb, &ui.autosave.cb_act); - label_action(&ui.autosave.label, &ui.autosave.label_act); - - /* Add all UI elements. */ - action_stack_add(&ui.st, &ui.panel.act); - action_stack_add(&ui.st, &ui.header.act); - action_stack_add(&ui.st, &ui.autosave.cb_act); - action_stack_add(&ui.st, &ui.autosave.label_act); - action_stack_add(&ui.st, &ui.quit.act); -} - static int headerclick(int x, int y) { @@ -257,28 +221,19 @@ ui.motion.y = ev->click.y; window_set_cursor(WINDOW_CURSOR_SIZE); } - else - action_stack_handle(&ui.st, ev); break; case EVENT_CLICKUP: ui.motion.active = 0; window_set_cursor(WINDOW_CURSOR_ARROW); - /* Fallthrough. */ + break; default: - action_stack_handle(&ui.st, ev); break; } -} + + checkbox_handle(&ui.autosave.cb, ev); -static void -update(struct state *st, unsigned int ticks) -{ - (void)st; - - if (ui.quit.button.state == BUTTON_STATE_ACTIVATED) + if (button_handle(&ui.quit.button, ev)) game_quit(); - else - action_stack_update(&ui.st, ticks); } static void @@ -288,7 +243,11 @@ painter_set_color(0xffffffff); painter_clear(); - action_stack_draw(&ui.st); + frame_draw(&ui.panel.frame); + label_draw(&ui.header.label); + checkbox_draw(&ui.autosave.cb); + label_draw(&ui.autosave.label); + button_draw(&ui.quit.button); painter_present(); } @@ -297,11 +256,9 @@ { struct state state = { .handle = handle, - .update = update, .draw = draw }; - prepare(); resize(); game_init(states, UTIL_SIZE(states));
--- a/src/libmlk-ui/ui/button.c Fri Apr 08 15:59:08 2022 +0200 +++ b/src/libmlk-ui/ui/button.c Fri Sep 09 09:27:17 2022 +0200 @@ -19,7 +19,6 @@ #include <assert.h> #include <string.h> -#include <core/action.h> #include <core/event.h> #include <core/maths.h> #include <core/painter.h> @@ -42,18 +41,6 @@ click->x, click->y); } -static void -handle(struct action *act, const union event *ev) -{ - button_handle(act->data, ev); -} - -static void -draw(struct action *act) -{ - button_draw(act->data); -} - void button_draw_default(const struct theme *t, const struct button *button) { @@ -83,7 +70,7 @@ label_draw(&label); } -void +int button_handle(struct button *button, const union event *ev) { assert(button); @@ -108,6 +95,8 @@ default: break; } + + return button->state == BUTTON_STATE_ACTIVATED; } void @@ -125,15 +114,3 @@ theme_draw_button(button->theme, button); } - -void -button_action(struct button *button, struct action *act) -{ - assert(button); - assert(act); - - memset(act, 0, sizeof (*act)); - act->data = button; - act->handle = handle; - act->draw = draw; -}
--- a/src/libmlk-ui/ui/button.h Fri Apr 08 15:59:08 2022 +0200 +++ b/src/libmlk-ui/ui/button.h Fri Sep 09 09:27:17 2022 +0200 @@ -23,7 +23,6 @@ union event; -struct action; struct theme; enum button_state { @@ -44,7 +43,7 @@ CORE_BEGIN_DECLS -void +int button_handle(struct button *, const union event *); void @@ -56,9 +55,6 @@ void button_draw(const struct button *); -void -button_action(struct button *, struct action *); - CORE_END_DECLS #endif /* !MLK_UI_BUTTON_H */
--- a/src/libmlk-ui/ui/checkbox.c Fri Apr 08 15:59:08 2022 +0200 +++ b/src/libmlk-ui/ui/checkbox.c Fri Sep 09 09:27:17 2022 +0200 @@ -19,7 +19,6 @@ #include <assert.h> #include <string.h> -#include <core/action.h> #include <core/event.h> #include <core/maths.h> #include <core/painter.h> @@ -37,18 +36,6 @@ return maths_is_boxed(cb->x, cb->y, cb->w, cb->h, click->x, click->y); } -static void -handle(struct action *act, const union event *ev) -{ - checkbox_handle(act->data, ev); -} - -static void -draw(struct action *act) -{ - checkbox_draw(act->data); -} - void checkbox_draw_default(const struct theme *t, const struct checkbox *cb) { @@ -90,15 +77,3 @@ { theme_draw_checkbox(cb->theme, cb); } - -void -checkbox_action(struct checkbox *cb, struct action *act) -{ - assert(cb); - assert(act); - - memset(act, 0, sizeof (*act)); - act->data = cb; - act->handle = handle; - act->draw = draw; -}
--- a/src/libmlk-ui/ui/checkbox.h Fri Apr 08 15:59:08 2022 +0200 +++ b/src/libmlk-ui/ui/checkbox.h Fri Sep 09 09:27:17 2022 +0200 @@ -23,7 +23,6 @@ union event; -struct action; struct theme; struct checkbox { @@ -46,9 +45,6 @@ void checkbox_draw(const struct checkbox *); -void -checkbox_action(struct checkbox *, struct action *); - CORE_END_DECLS #endif /* !MLK_UI_CHECKBOX_H */
--- a/src/libmlk-ui/ui/frame.c Fri Apr 08 15:59:08 2022 +0200 +++ b/src/libmlk-ui/ui/frame.c Fri Sep 09 09:27:17 2022 +0200 @@ -19,18 +19,11 @@ #include <assert.h> #include <string.h> -#include <core/action.h> #include <core/painter.h> #include "frame.h" #include "theme.h" -static void -draw(struct action *act) -{ - frame_draw(act->data); -} - void frame_draw_default(const struct theme *t, const struct frame *frame) { @@ -54,14 +47,3 @@ theme_draw_frame(frame->theme, frame); } - -void -frame_action(struct frame *frame, struct action *act) -{ - assert(frame); - assert(act); - - memset(act, 0, sizeof (*act)); - act->data = frame; - act->draw = draw; -}
--- a/src/libmlk-ui/ui/frame.h Fri Apr 08 15:59:08 2022 +0200 +++ b/src/libmlk-ui/ui/frame.h Fri Sep 09 09:27:17 2022 +0200 @@ -21,7 +21,6 @@ #include <core/core.h> -struct action; struct theme; enum frame_style { @@ -46,9 +45,6 @@ void frame_draw(const struct frame *); -void -frame_action(struct frame *, struct action *); - CORE_END_DECLS #endif /* !MLK_UI_FRAME_H */
--- a/src/libmlk-ui/ui/label.c Fri Apr 08 15:59:08 2022 +0200 +++ b/src/libmlk-ui/ui/label.c Fri Sep 09 09:27:17 2022 +0200 @@ -19,7 +19,6 @@ #include <assert.h> #include <string.h> -#include <core/action.h> #include <core/font.h> #include <core/panic.h> #include <core/texture.h> @@ -27,12 +26,6 @@ #include "label.h" #include "theme.h" -static void -draw(struct action *act) -{ - label_draw(act->data); -} - void label_draw_default(const struct theme *t, const struct label *label) { @@ -96,14 +89,3 @@ theme_draw_label(label->theme, label); } - -void -label_action(struct label *label, struct action *act) -{ - assert(label); - assert(act); - - memset(act, 0, sizeof (*act)); - act->data = label; - act->draw = draw; -}
--- a/src/libmlk-ui/ui/label.h Fri Apr 08 15:59:08 2022 +0200 +++ b/src/libmlk-ui/ui/label.h Fri Sep 09 09:27:17 2022 +0200 @@ -21,7 +21,6 @@ #include <core/core.h> -struct action; struct theme; enum label_flags { @@ -53,9 +52,6 @@ void label_draw(const struct label *); -void -label_action(struct label *, struct action *); - CORE_END_DECLS #endif /* !MLK_UI_LABEL_H */