cepheid

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

commit 2470ba0930d8c9340859dac432fb7831ebc8f974
parent 5fdf004438aa58f4d4b8fd71782d5c31e913c99d
Author: hhvn <dev@hhvn.uk>
Date:   Sun, 20 Nov 2022 00:15:30 +0000

Improve Input/Button integration

Diffstat:
Msrc/gui.c | 27++++++++++++++++++++++-----
Msrc/main.h | 4+++-
Msrc/struct.h | 16+++++++++-------
3 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/src/gui.c b/src/gui.c @@ -203,7 +203,7 @@ gui_button(int x, int y, int w, Button *b) { int h = BUTTON_HEIGHT; ui_draw_border_around(x, y, w, h, 1); - ui_print(x + (w - ui_textsize(b->label)) / 2, y + PAD, + ui_print(x + (w - ui_textsize(b->label)) / 2, y + PAD/2, b->enabled ? col_fg : col_altfg, "%s", b->label); @@ -215,8 +215,12 @@ static void gui_click_button(MouseButton button, Geom *geom, void *elem) { Button *b = elem; - if (button == MOUSE_BUTTON_LEFT) - b->func(b->arg); + if (button == MOUSE_BUTTON_LEFT) { + if (b->submit && b->submit->onenter) + b->submit->onenter(b->submit); + else if (b->func) + b->func(b->arg); + } } void @@ -291,7 +295,7 @@ gui_input(int x, int y, int w, Input *in) { else if (!focused && in->placeholder) ui_print(x + TPX, y + TPY, col_info, "%s", in->placeholder); if (focused) { - ui_draw_rect(x + TPX + charpx * in->cur, y + TPY, 1, FONT_SIZE, col_fg); + ui_draw_rect(x + TPX + charpx * in->cur, y + TPY, 1, FONT_SIZE - 2, col_fg); } gui_click_register(RECT(x, y, w, h), GUI_INPUT, in); } @@ -329,11 +333,24 @@ gui_key_input(void *elem, int *fcount) { in->cur--; } else if (ui_keyboard_check(KEY_RIGHT, fcount) && in->cur != in->len) { in->cur++; - } else if (c && in->len < INPUT_MAX) { + } else if (ui_keyboard_check(KEY_TAB, fcount) && in->onenter == gui_input_next) { + gui_input_next(in); + } else if (c && (!in->accept || in->accept(c)) && in->len < INPUT_MAX) { editins(in->wstr, &in->len, &in->cur, INPUT_MAX, c); } } +int +gui_input_next(Input *in) { + ui_focus(GUI_INPUT, in + sizeof(Input)); + return 0; +} + +void +gui_input_clear(Input *in) { + edittrunc(in->wstr, &in->len, &in->cur); +} + /* This function is an intermediate filter that checks whether an item is * collapsed before running the filter defined in the Treeview struct */ static int diff --git a/src/main.h b/src/main.h @@ -127,7 +127,7 @@ void ui_draw_view_design(void); void ui_draw_view_settings(void); /* gui.c */ -#define BUTTON_HEIGHT (PAD * 2 + FONT_SIZE) +#define BUTTON_HEIGHT (PAD + FONT_SIZE) extern void (*gui_key_handlers[GUI_ELEMS])(void *elem, int *fcount); int gui_click_handle(void); void gui_tabs(int x, int y, int w, int h, Tabs *tabs); @@ -135,6 +135,8 @@ int gui_checkbox(int x, int y, Checkbox *checkbox); /* returns width */ void gui_button(int x, int y, int w, Button *b); void gui_dropdown(int x, int y, int w, Dropdown *d); void gui_input(int x, int y, int w, Input *in); +int gui_input_next(Input *in); /* if inputs are contained in an array, this .onenter advances to the next */ +void gui_input_clear(Input *in); void gui_treeview(int x, int y, int w, int h, Treeview *tv); /* views/main.c */ diff --git a/src/struct.h b/src/struct.h @@ -178,13 +178,6 @@ typedef struct { char *label; } Checkbox; -typedef struct { - int enabled; - char *label; - void (*func)(int); - int arg; -} Button; - #define INPUT_MAX 512 typedef struct Input Input; struct Input { @@ -194,8 +187,17 @@ struct Input { int len; int cur; int (*onenter)(Input *); /* return positive to clear */ + int (*accept)(wchar_t); }; +typedef struct { + int enabled; + char *label; + void (*func)(int); + int arg; + Input *submit; /* run the onenter in an Input instead */ +} Button; + #define DROPDOWN_MAX 64 typedef struct { int n;