cepheid

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

commit 1851992f949b891f1c7e0bba417cc9f3f24cea43
parent 0e0ba467caee941cd30b6b6a956019fa7208360c
Author: hhvn <dev@hhvn.uk>
Date:   Sat, 19 Nov 2022 16:03:52 +0000

Buttons

Diffstat:
Msrc/gui.c | 24+++++++++++++++++++++++-
Msrc/main.h | 2++
Msrc/struct.h | 1+
Msrc/style.h | 1+
Mstyles/aurora.c | 1+
Mstyles/hhvn.c | 1+
6 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/src/gui.c b/src/gui.c @@ -6,6 +6,7 @@ static int clickablei = 0; static void gui_click_tabs(MouseButton button, Geom *geom, void *elem); static void gui_click_checkbox(MouseButton button, Geom *geom, void *elem); +static void gui_click_button(MouseButton button, Geom *geom, void *elem); static void gui_click_input(MouseButton button, Geom *geom, void *elem); static void gui_click_dropdown(MouseButton button, Geom *geom, void *elem); static void gui_click_treeview(MouseButton button, Geom *geom, void *elem); @@ -17,7 +18,7 @@ static void (*click_handlers[GUI_ELEMS])( Geom *geom, void *elem) = { [GUI_TAB] = gui_click_tabs, [GUI_CHECKBOX] = gui_click_checkbox, - [GUI_BUTTON] = NULL, + [GUI_BUTTON] = gui_click_button, [GUI_INPUT] = gui_click_input, [GUI_DROPDOWN] = gui_click_dropdown, [GUI_TREEVIEW] = gui_click_treeview, @@ -197,6 +198,27 @@ gui_click_checkbox(MouseButton button, Geom *geom, void *elem) { } void +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, + b->enabled ? col_fg : col_altfg, + "%s", b->label); + + if (b->enabled) + gui_click_register(RECT(x, y, w, h), GUI_BUTTON, b); +} + +static void +gui_click_button(MouseButton button, Geom *geom, void *elem) { + Button *b = elem; + + if (button == MOUSE_BUTTON_LEFT) + b->func(b->arg); +} + +void gui_dropdown(int x, int y, int w, Dropdown *d) { int h = FONT_SIZE; int fh, ph; diff --git a/src/main.h b/src/main.h @@ -126,10 +126,12 @@ void ui_draw_view_design(void); void ui_draw_view_settings(void); /* gui.c */ +#define BUTTON_HEIGHT (PAD * 2 + 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); 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); void gui_treeview(int x, int y, int w, int h, Treeview *tv); diff --git a/src/struct.h b/src/struct.h @@ -178,6 +178,7 @@ typedef struct { } Checkbox; typedef struct { + int enabled; char *label; void (*func)(int); int arg; diff --git a/src/style.h b/src/style.h @@ -28,6 +28,7 @@ /* colours */ extern const Color col_fg; +extern const Color col_altfg; extern const Color col_bg; extern const Color col_altbg; extern const Color col_border; diff --git a/styles/aurora.c b/styles/aurora.c @@ -8,6 +8,7 @@ #define AURORA_GREEN { 0x64, 0x96, 0x96, 0xff } const Color col_fg = WHITE; +const Color col_altfg = { 0x66, 0x66, 0x66, 0xff }; const Color col_bg = { 0x00, 0x00, 0x3c, 0xff }; const Color col_altbg = { 0x00, 0x00, 0x2c, 0xff }; const Color col_border = { 0x1e, 0x1e, 0x1e, 0xff }; diff --git a/styles/hhvn.c b/styles/hhvn.c @@ -4,6 +4,7 @@ #define GREY { 0x99, 0x99, 0x99, 0xff } const Color col_fg = WHITE; +const Color col_altfg = { 0x66, 0x66, 0x66, 0xff }; const Color col_bg = { 0x05, 0x0a, 0x10, 0xff }; const Color col_altbg = { 0x0c, 0x17, 0x26, 0xff }; const Color col_border = { 0x1b, 0x36, 0x4b, 0xff };