cepheid

An Aurora 4X clone
Log | Files | Refs | README

commit 45af01adb87a528cd1e741ddb42ca81793eadf06
parent 38a4c8da499f06d4e67a46781f6d0b649654d4a3
Author: hhvn <dev@hhvn.uk>
Date:   Wed, 30 Nov 2022 13:34:59 +0000

Buttons and escape binding to go back in all smenu entries

Diffstat:
Mdev/config.mk | 2+-
Msrc/gui.c | 3++-
Msrc/main.c | 8++++++--
Msrc/views/smenu.c | 56++++++++++++++++++++++++++++++++++++++------------------
Msrc/views/struct.h | 5++++-
5 files changed, 51 insertions(+), 23 deletions(-)

diff --git a/dev/config.mk b/dev/config.mk @@ -1,4 +1,4 @@ -CFLAGS += -Wall -g3 -O0 +CFLAGS += -Wall -g3 -Og CFLAGS += -DDEBUG CFLAGS += -DCHECK_FRAME_MEM_FREE ARGS = diff --git a/src/gui.c b/src/gui.c @@ -392,7 +392,6 @@ gui_mouse_input(MouseButton button, Geom *geom, void *elem) { /* When enter is pressed in input, or when button submits the input. */ static void gui_enter_input(Input *in) { - wcstombs(in->str, in->wstr, INPUT_MAX); if (in->onenter(GUI_INPUT, in)) edittrunc(in->wstr, &in->len, &in->cur); } @@ -406,6 +405,7 @@ gui_key_input(void *elem, int *fcount) { gui_enter_input(in); } else if (gui_key_check(KEY_BACKSPACE, fcount) && in->len && in->cur) { editrm(in->wstr, &in->len, &in->cur); + wcstombs(in->str, in->wstr, INPUT_MAX); } else if (gui_key_check(KEY_LEFT, fcount) && in->cur) { in->cur--; } else if (gui_key_check(KEY_RIGHT, fcount) && in->cur != in->len) { @@ -414,6 +414,7 @@ gui_key_input(void *elem, int *fcount) { gui_input_next(GUI_INPUT, in); } else if (c && (!in->accept || in->accept(c)) && in->len < INPUT_MAX) { editins(in->wstr, &in->len, &in->cur, INPUT_MAX, c); + wcstombs(in->str, in->wstr, INPUT_MAX); } } diff --git a/src/main.c b/src/main.c @@ -53,8 +53,12 @@ main(void) { ui_update_screen(); while (ui_loop()) { - if (IsKeyPressed(KEY_ESCAPE)) - view_tabs.sel = VIEW_SMENU; + if (IsKeyPressed(KEY_ESCAPE)) { + if (view_tabs.sel == VIEW_SMENU) + view_smenu.back->func(GUI_BUTTON, view_smenu.back); + else + view_tabs.sel = VIEW_SMENU; + } if (save && (IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT))) { /* AAAAAAAAAAHHHHHHHHHHHH. WHY NOT JUST USE KEY_1, KEY_2..! */ diff --git a/src/views/smenu.c b/src/views/smenu.c @@ -20,7 +20,6 @@ static int savecheck_callback(int type, void *elem); static int buttonhandler(int type, void *elem); static void newhandler(void); static int newhandler_actual(int type, void *elem); -static int newhandler_back(int type, void *elem); static void quithandler(void); static void loadhandler(void); static int loadhandler_actual(int type, void *elem); @@ -51,7 +50,7 @@ View_smenu view_smenu = { .onenter = newhandler_actual, }, .create = {1, "Create", NULL, 0, .submit = &view_smenu.new.name}, - .back = {1, "Back", newhandler_back, 0}, + .back = {1, "Back", newhandler_actual, SMENU_BACK}, }, .cont = { .save = NULL, @@ -60,7 +59,7 @@ View_smenu view_smenu = { .save = { .check = 0, .msg = NULL, - .back = {1, "Back", NULL, -1}, + .back = {1, "Back", NULL, SMENU_BACK}, .save = {1, "Save", NULL, 1}, .discard = {1, "Discard", NULL, 0}, }, @@ -77,6 +76,7 @@ View_smenu view_smenu = { .print = loadprinter, .dclick = loadhandler_actual, }, + .back = {1, "Back", loadhandler_actual, SMENU_BACK }, .delete = {0, "Delete", loadhandler_actual, 1 }, .load = {0, "Load", loadhandler_actual, 0 }, } @@ -90,6 +90,7 @@ savecheck(char *msg, void (*action)(void)) { v->save.discard.func = savecheck_callback; v->save.msg = msg; v->save.func = action; + v->back = &v->save.back; } static int @@ -98,9 +99,10 @@ savecheck_callback(int type, void *elem) { int arg = b->arg; v->save.check = 0; - if (arg) + if (arg == SMENU_BACK) + return 0; + if (arg == 1) save_write(); - if (arg != -1) v->save.func(); return 0; } @@ -145,10 +147,22 @@ buttonhandler(int type, void *elem) { static void newhandler(void) { v->new.disp = 1; + v->back = &v->new.back; } static int newhandler_actual(int type, void *elem) { + Button *b; + + if (type == GUI_BUTTON) { + b = elem; + if (b->arg == SMENU_BACK) { + gui_input_clear(&v->new.name); + v->new.disp = 0; + return 1; + } + } + if (save_create(v->new.name.str) == -1) error(1, "failed to create new save\n"); /* TODO: error handling that doesn't just cause an exit? */ @@ -158,13 +172,6 @@ newhandler_actual(int type, void *elem) { return 1; } -static int -newhandler_back(int type, void *elem) { - gui_input_clear(&v->new.name); - v->new.disp = 0; - return 0; -} - static void quithandler(void) { quit = 1; @@ -173,30 +180,35 @@ quithandler(void) { static void loadhandler(void) { v->load.disp = 1; + v->back = &v->load.back; } static int loadhandler_actual(int type, void *elem) { struct Loadable *l; Button *b; - int delete; - + int action; if (type == GUI_BUTTON) { b = elem; - delete = b->arg == 1; - } else delete = 0; + action = b->arg; + } else action = 0; l = v->load.savelist.sel->data; - if (delete) { + switch (action) { + case 1: save_delete(l->name); tree_delete(&v->load.savelist.sel, loadfree); - } else { + break; + case 0: save_read(l->name); view_tabs.sel = VIEW_MAIN; + /* fallthrough */ + case SMENU_BACK: v->load.disp = 0; } + return 0; } @@ -322,6 +334,9 @@ view_smenu_draw(void) { y += PAD; gui_input(x, y, 300, &v->new.name); + v->new.create.enabled = + v->new.name.str[0] ? 1 : 0; + x += w - 50 - PAD * 2, gui_button(x, y + PAD * 2, 50, &v->new.create); @@ -366,6 +381,9 @@ view_smenu_draw(void) { x -= BUTTON_W + PAD; gui_button(x, y, BUTTON_W, &v->load.delete); + + x -= BUTTON_W + PAD; + gui_button(x, y, BUTTON_W, &v->load.back); } else { ui_draw_rect(EXPLODE_RECT(v->main), bg); ui_draw_border_around(EXPLODE_RECT(v->main), 1); @@ -379,5 +397,7 @@ view_smenu_draw(void) { gui_button(x, y, w, &v->b[i]); y += BUTTON_HEIGHT + PAD; } + + v->back = NULL; } } diff --git a/src/views/struct.h b/src/views/struct.h @@ -76,7 +76,8 @@ enum { SMENU_CONT, SMENU_LOAD, SMENU_QUIT, - SMENU_LAST + SMENU_LAST, + SMENU_BACK = -1, }; struct Loadable { @@ -110,7 +111,9 @@ typedef struct { int disp; Tree saves; Treeview savelist; + Button back; Button delete; Button load; } load; + Button *back; } View_smenu;