pxl8/src/gui/pxl8_gui.c
2026-04-15 00:53:03 -05:00

283 lines
9.1 KiB
C

#include "pxl8_gui.h"
#include <stdlib.h>
#include <string.h>
#include "pxl8_gfx.h"
#include "pxl8_math.h"
#include "pxl8_gui_palette.h"
#include "pxl8_mem.h"
pxl8_gui_state* pxl8_gui_state_create(void) {
pxl8_gui_state* state = (pxl8_gui_state*)pxl8_malloc(sizeof(pxl8_gui_state));
if (!state) return NULL;
memset(state, 0, sizeof(pxl8_gui_state));
return state;
}
void pxl8_gui_state_destroy(pxl8_gui_state* state) {
if (!state) return;
pxl8_free(state);
}
void pxl8_gui_begin_frame(pxl8_gui_state* state, pxl8_gfx* gfx) {
(void)gfx;
if (!state) return;
state->hot_id = 0;
}
void pxl8_gui_end_frame(pxl8_gui_state* state, pxl8_gfx* gfx) {
(void)gfx;
if (!state) return;
if (!state->cursor_down) {
state->active_id = 0;
}
state->cursor_clicked = false;
}
void pxl8_gui_cursor_move(pxl8_gui_state* state, i32 x, i32 y) {
if (!state) return;
state->cursor_x = x;
state->cursor_y = y;
}
void pxl8_gui_cursor_down(pxl8_gui_state* state) {
if (!state) return;
state->cursor_down = true;
}
void pxl8_gui_cursor_up(pxl8_gui_state* state) {
if (!state) return;
state->cursor_down = false;
state->cursor_clicked = true;
}
static bool is_cursor_over(const pxl8_gui_state* state, i32 x, i32 y, i32 w, i32 h) {
return state->cursor_x >= x && state->cursor_x < (x + w) &&
state->cursor_y >= y && state->cursor_y < (y + h);
}
bool pxl8_gui_button(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id, i32 x, i32 y, i32 w, i32 h, const char* label) {
if (!state || !gfx || !label) return false;
bool cursor_over = is_cursor_over(state, x, y, w, h);
bool is_hot = (state->hot_id == id);
bool is_active = (state->active_id == id);
if (cursor_over) {
state->hot_id = id;
}
if (cursor_over && state->cursor_down && state->active_id == 0) {
state->active_id = id;
}
bool clicked = is_active && state->cursor_clicked && cursor_over;
if (clicked) {
state->active_id = 0;
}
u8 bg_color;
u8 border_color;
i32 offset_x = 0;
i32 offset_y = 0;
if (is_active) {
bg_color = pxl8_gui_color(gfx, PXL8_UI_BG3);
border_color = pxl8_gui_color(gfx, PXL8_UI_BG2);
offset_x = 1;
offset_y = 1;
} else if (is_hot || cursor_over) {
bg_color = pxl8_gui_color(gfx, PXL8_UI_BG3);
border_color = pxl8_gui_color(gfx, PXL8_UI_FG0);
} else {
bg_color = pxl8_gui_color(gfx, PXL8_UI_BG2);
border_color = pxl8_gui_color(gfx, PXL8_UI_BG3);
}
pxl8_2d_rect_fill(gfx, x, y, w, h, bg_color);
pxl8_2d_rect(gfx, x, y, w, h, border_color);
i32 text_len = (i32)strlen(label);
i32 text_x = x + (w / 2) - ((text_len * 8) / 2) + offset_x;
i32 text_y = y + (h / 2) - 5 + offset_y;
pxl8_2d_text(gfx, label, text_x, text_y, pxl8_gui_color(gfx, PXL8_UI_FG0));
return clicked;
}
u8 pxl8_gui_color(pxl8_gfx* gfx, u8 index) {
if (!gfx || index >= PXL8_UI_PALETTE_SIZE) return 0;
u32 abgr = pxl8_ui_palette[index];
u8 r = (abgr >> 0) & 0xFF;
u8 g = (abgr >> 8) & 0xFF;
u8 b = (abgr >> 16) & 0xFF;
return pxl8_gfx_find_closest_color(gfx, r, g, b);
}
bool pxl8_gui_slider(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id, i32 x, i32 y, i32 w, i32 h, f32* value, f32 min_val, f32 max_val) {
if (!state || !gfx || !value) return false;
bool cursor_over = is_cursor_over(state, x, y, w, h);
bool is_active = (state->active_id == id);
bool changed = false;
if (cursor_over) {
state->hot_id = id;
}
if (cursor_over && state->cursor_down && state->active_id == 0) {
state->active_id = id;
}
if (is_active && state->cursor_down) {
f32 t = pxl8_clamp((f32)(state->cursor_x - x) / (f32)w, 0.0f, 1.0f);
f32 new_val = min_val + t * (max_val - min_val);
if (new_val != *value) {
*value = new_val;
changed = true;
}
}
u8 bg_color = pxl8_gui_color(gfx, PXL8_UI_BG1);
u8 fill_color = pxl8_gui_color(gfx, is_active ? PXL8_UI_FG0 : PXL8_UI_BG3);
u8 handle_color = pxl8_gui_color(gfx, PXL8_UI_FG0);
pxl8_2d_rect_fill(gfx, x, y, w, h, bg_color);
f32 t = (*value - min_val) / (max_val - min_val);
i32 fill_w = (i32)(t * (f32)w);
if (fill_w > 0) {
pxl8_2d_rect_fill(gfx, x, y, fill_w, h, fill_color);
}
i32 handle_x = x + fill_w - 2;
if (handle_x < x) handle_x = x;
if (handle_x > x + w - 4) handle_x = x + w - 4;
pxl8_2d_rect_fill(gfx, handle_x, y, 4, h, handle_color);
return changed;
}
bool pxl8_gui_slider_int(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id, i32 x, i32 y, i32 w, i32 h, i32* value, i32 min_val, i32 max_val) {
if (!state || !gfx || !value) return false;
f32 fval = (f32)*value;
bool changed = pxl8_gui_slider(state, gfx, id, x, y, w, h, &fval, (f32)min_val, (f32)max_val);
if (changed) {
*value = (i32)(fval + 0.5f);
}
return changed;
}
i32 pxl8_gui_toolbar(pxl8_gui_state* state, pxl8_gfx* gfx, u32 base_id,
i32 x, i32 y, i32 btn_w, i32 btn_h,
const char** labels, i32 count, i32 selected) {
i32 result = -1;
for (i32 i = 0; i < count; i++) {
i32 bx = x + i * (btn_w + 2);
bool clicked = pxl8_gui_button(state, gfx, base_id + (u32)i, bx, y, btn_w, btn_h, labels[i]);
if (clicked) result = i;
if (i == selected) {
u8 sel_color = pxl8_gui_color(gfx, PXL8_UI_FG0);
pxl8_2d_rect(gfx, bx, y + btn_h - 2, btn_w, 2, sel_color);
}
}
return result;
}
void pxl8_gui_window(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, const char* title) {
if (!gfx || !title) return;
u8 title_bg = pxl8_gui_color(gfx, PXL8_UI_BG1);
u8 body_bg = pxl8_gui_color(gfx, PXL8_UI_BG2);
u8 border = pxl8_gui_color(gfx, PXL8_UI_BG3);
u8 title_fg = pxl8_gui_color(gfx, PXL8_UI_FG0);
pxl8_2d_rect_fill(gfx, x, y, w, 28, title_bg);
pxl8_2d_rect_fill(gfx, x, y + 28, w, h - 28, body_bg);
pxl8_2d_rect(gfx, x, y, w, h, border);
pxl8_2d_rect_fill(gfx, x, y + 28, w, 1, border);
i32 title_x = x + 10;
i32 title_y = y + (28 / 2) - 5;
pxl8_2d_text(gfx, title, title_x, title_y, title_fg);
}
void pxl8_gui_label(pxl8_gfx* gfx, i32 x, i32 y, const char* text, u8 color) {
if (!gfx || !text) return;
pxl8_2d_text(gfx, text, x, y, color);
}
bool pxl8_gui_is_hovering(const pxl8_gui_state* state) {
if (!state) return false;
return state->hot_id != 0;
}
void pxl8_gui_get_cursor_pos(const pxl8_gui_state* state, i32* x, i32* y) {
if (!state) return;
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;
}