pxl8/src/gui/pxl8_gui.c

199 lines
5.7 KiB
C
Raw Normal View History

#include "pxl8_gui.h"
#include <stdlib.h>
#include <string.h>
#include "pxl8_gfx.h"
2026-01-21 23:19:50 -06:00
#include "pxl8_mem.h"
pxl8_gui_state* pxl8_gui_state_create(void) {
2026-01-21 23:19:50 -06:00
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;
2026-01-21 23:19:50 -06:00
pxl8_free(state);
}
2026-01-21 23:19:50 -06:00
void pxl8_gui_begin_frame(pxl8_gui_state* state, pxl8_gfx* gfx) {
(void)gfx;
if (!state) return;
state->hot_id = 0;
}
2026-01-21 23:19:50 -06:00
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) {
2026-01-21 23:19:50 -06:00
bg_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
border_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG2);
offset_x = 1;
offset_y = 1;
} else if (is_hot || cursor_over) {
2026-01-21 23:19:50 -06:00
bg_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
border_color = pxl8_gfx_ui_color(gfx, PXL8_UI_FG0);
} else {
2026-01-21 23:19:50 -06:00
bg_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG2);
border_color = pxl8_gfx_ui_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;
2026-01-21 23:19:50 -06:00
pxl8_2d_text(gfx, label, text_x, text_y, pxl8_gfx_ui_color(gfx, PXL8_UI_FG1));
return clicked;
}
2026-01-31 09:31:17 -06:00
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 = (f32)(state->cursor_x - x) / (f32)w;
if (t < 0.0f) t = 0.0f;
if (t > 1.0f) t = 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_gfx_ui_color(gfx, PXL8_UI_BG1);
u8 fill_color = pxl8_gfx_ui_color(gfx, is_active ? PXL8_UI_FG0 : PXL8_UI_BG3);
u8 handle_color = pxl8_gfx_ui_color(gfx, PXL8_UI_FG1);
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;
}
void pxl8_gui_window(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, const char* title) {
if (!gfx || !title) return;
2026-01-21 23:19:50 -06:00
u8 title_bg = pxl8_gfx_ui_color(gfx, PXL8_UI_BG1);
u8 body_bg = pxl8_gfx_ui_color(gfx, PXL8_UI_BG2);
u8 border = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
u8 title_fg = pxl8_gfx_ui_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;
2026-01-21 23:19:50 -06:00
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;
}