feat(gui): add grid_select, toggle, panel, status_bar, image widgets
This commit is contained in:
parent
b42bf74472
commit
ab0d2a4b04
3 changed files with 79 additions and 0 deletions
|
|
@ -222,3 +222,63 @@ void pxl8_gui_get_cursor_pos(const pxl8_gui_state* state, i32* x, i32* y) {
|
|||
if (x) *x = state->cursor_x;
|
||||
if (y) *y = state->cursor_y;
|
||||
}
|
||||
|
||||
i32 pxl8_gui_grid_select(pxl8_gui_state* state, pxl8_gfx* gfx, u32 base_id,
|
||||
i32 x, i32 y, i32 cell_w, i32 cell_h,
|
||||
i32 cols, i32 rows, const u8* colors, i32 selected) {
|
||||
i32 result = -1;
|
||||
for (i32 r = 0; r < rows; r++) {
|
||||
for (i32 c = 0; c < cols; c++) {
|
||||
i32 cx = x + c * (cell_w + 1);
|
||||
i32 cy = y + r * (cell_h + 1);
|
||||
i32 idx = r * cols + c;
|
||||
u32 id = base_id + (u32)idx;
|
||||
bool over = is_cursor_over(state, cx, cy, cell_w, cell_h);
|
||||
if (over) state->hot_id = id;
|
||||
if (over && state->cursor_down && state->active_id == 0)
|
||||
state->active_id = id;
|
||||
bool clicked = (state->active_id == id) && state->cursor_clicked && over;
|
||||
if (clicked) { result = idx; state->active_id = 0; }
|
||||
pxl8_2d_rect_fill(gfx, cx, cy, cell_w, cell_h, colors[idx]);
|
||||
if (idx == selected) {
|
||||
u8 sel = pxl8_gui_color(gfx, PXL8_UI_FG0);
|
||||
pxl8_2d_rect(gfx, cx - 1, cy - 1, cell_w + 2, cell_h + 2, sel);
|
||||
} else if (over) {
|
||||
u8 hov = pxl8_gui_color(gfx, PXL8_UI_BG3);
|
||||
pxl8_2d_rect(gfx, cx, cy, cell_w, cell_h, hov);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void pxl8_gui_image(pxl8_gfx* gfx, u32 texture_id, i32 sx, i32 sy, i32 sw, i32 sh,
|
||||
i32 dx, i32 dy, i32 dw, i32 dh) {
|
||||
pxl8_2d_sprite(gfx, texture_id, dx, dy, dw, dh, false, false);
|
||||
(void)sx; (void)sy; (void)sw; (void)sh;
|
||||
}
|
||||
|
||||
void pxl8_gui_panel(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h) {
|
||||
u8 bg = pxl8_gui_color(gfx, PXL8_UI_BG1);
|
||||
u8 border = pxl8_gui_color(gfx, PXL8_UI_BG3);
|
||||
pxl8_2d_rect_fill(gfx, x, y, w, h, bg);
|
||||
pxl8_2d_rect(gfx, x, y, w, h, border);
|
||||
}
|
||||
|
||||
void pxl8_gui_status_bar(pxl8_gfx* gfx, i32 y, i32 screen_w, i32 h, const char* text) {
|
||||
u8 bg = pxl8_gui_color(gfx, PXL8_UI_BG1);
|
||||
u8 fg = pxl8_gui_color(gfx, PXL8_UI_FG0);
|
||||
pxl8_2d_rect_fill(gfx, 0, y, screen_w, h, bg);
|
||||
pxl8_2d_text(gfx, text, 4, y + (h / 2) - 5, fg);
|
||||
}
|
||||
|
||||
bool pxl8_gui_toggle(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id,
|
||||
i32 x, i32 y, i32 w, i32 h, const char* label, bool active) {
|
||||
bool clicked = pxl8_gui_button(state, gfx, id, x, y, w, h, label);
|
||||
if (active) {
|
||||
u8 sel = pxl8_gui_color(gfx, PXL8_UI_FG0);
|
||||
pxl8_2d_rect(gfx, x, y, w, 2, sel);
|
||||
pxl8_2d_rect(gfx, x, y + h - 2, w, 2, sel);
|
||||
}
|
||||
return clicked ? !active : active;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,6 +39,20 @@ i32 pxl8_gui_toolbar(pxl8_gui_state* state, pxl8_gfx* gfx, u32 base_id,
|
|||
const char** labels, i32 count, i32 selected);
|
||||
void pxl8_gui_window(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, const char* title);
|
||||
|
||||
i32 pxl8_gui_grid_select(pxl8_gui_state* state, pxl8_gfx* gfx, u32 base_id,
|
||||
i32 x, i32 y, i32 cell_w, i32 cell_h,
|
||||
i32 cols, i32 rows, const u8* colors, i32 selected);
|
||||
|
||||
void pxl8_gui_image(pxl8_gfx* gfx, u32 texture_id, i32 sx, i32 sy, i32 sw, i32 sh,
|
||||
i32 dx, i32 dy, i32 dw, i32 dh);
|
||||
|
||||
void pxl8_gui_panel(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h);
|
||||
|
||||
void pxl8_gui_status_bar(pxl8_gfx* gfx, i32 y, i32 screen_w, i32 h, const char* text);
|
||||
|
||||
bool pxl8_gui_toggle(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id,
|
||||
i32 x, i32 y, i32 w, i32 h, const char* label, bool active);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -469,6 +469,11 @@ static const char* pxl8_ffi_cdefs =
|
|||
"u8 pxl8_gui_color(pxl8_gfx* gfx, u8 index);\n"
|
||||
"bool pxl8_gui_is_hovering(const pxl8_gui_state* state);\n"
|
||||
"void pxl8_gui_get_cursor_pos(const pxl8_gui_state* state, i32* x, i32* y);\n"
|
||||
"i32 pxl8_gui_grid_select(pxl8_gui_state* state, pxl8_gfx* gfx, u32 base_id, i32 x, i32 y, i32 cell_w, i32 cell_h, i32 cols, i32 rows, const u8* colors, i32 selected);\n"
|
||||
"void pxl8_gui_image(pxl8_gfx* gfx, u32 texture_id, i32 sx, i32 sy, i32 sw, i32 sh, i32 dx, i32 dy, i32 dw, i32 dh);\n"
|
||||
"void pxl8_gui_panel(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h);\n"
|
||||
"void pxl8_gui_status_bar(pxl8_gfx* gfx, i32 y, i32 screen_w, i32 h, const char* text);\n"
|
||||
"bool pxl8_gui_toggle(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id, i32 x, i32 y, i32 w, i32 h, const char* label, bool active);\n"
|
||||
"\n"
|
||||
"typedef struct pxl8_save pxl8_save;\n"
|
||||
"pxl8_save* pxl8_save_create(const char* game_name, u32 magic, u32 version);\n"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue