This commit is contained in:
Krzosa Karol
2025-01-11 20:52:44 +01:00
parent b81297450b
commit 16125f5605
3 changed files with 37 additions and 28 deletions

View File

@@ -2,8 +2,8 @@
** [ ] Choosing a keying strategy from user code
** [ ] Using parents
** [ ] Using file and line
**
**
** [ ] Keyboard friendliness
** [ ] ui_em size
**
*/
@@ -117,10 +117,10 @@ gb ui_t *ui = NULL;
gb_read_only ui_id_t ui_id_null;
gb_read_only ui_box_t ui_box_null;
fn b32 ui_null_id(ui_id_t id) { return id.value == 0; }
fn b32 ui_null(ui_box_t *box) { return box->id.value == 0; }
fn b32 ui_hot(ui_box_t *box) { return !ui_null(box) && box->id.value == ui->hot.value; }
fn b32 ui_active(ui_box_t *box) { return !ui_null(box) && box->id.value == ui->active.value; }
fn b32 ui_is_null_id(ui_id_t id) { return id.value == 0; }
fn b32 ui_is_null_box(ui_box_t *box) { return box->id.value == 0; }
fn b32 ui_is_hot_box(ui_box_t *box) { return !ui_is_null_box(box) && box->id.value == ui->hot.value; }
fn b32 ui_is_active_box(ui_box_t *box) { return !ui_is_null_box(box) && box->id.value == ui->active.value; }
#define ev_left(ev) ((ev)->mouse_button == app_mouse_button_left)
#define ev_left_up(ev) ((ev)->kind == app_event_kind_mouse_up && ev_left(ev))
@@ -149,20 +149,23 @@ s8_t ui_get_hash_string(s8_t string) {
return string;
}
// @todo: we are not building a hierarchy, it's flat ! so this doesn't work!!
fn ui_id_t ui_id_from_string(s8_t string) {
u64 value = ht_hash_data(ui_get_hash_string(string));
ui_id_t id = {value};
return id;
}
fn ui_id_t ui_id_from_id_stack(void) {
u64 value = 134;
for (i32 i = 0; i < ui->id_stack.len; i += 1) {
value = ui_hash_mix(value, ui->id_stack.data[i].value);
fn ui_id_t ui_find_valid_id(ui_box_t *box) {
for (ui_box_t *it = box; it; it = it->parent) {
if (!ui_is_null_box(it)) return it->id;
}
ui_id_t id = {value};
return id;
return ui_id_null;
}
fn ui_id_t ui_id_from_parent(void) {
ui_id_t parent_id = ui_find_valid_id(ui->top);
if (ui_is_null_id(parent_id)) parent_id.value = 1423;
return parent_id;
}
fn ui_box_t *ui_find_box(ui_id_t id) {
@@ -191,13 +194,19 @@ fn void ui_push_box(ui_box_t *box) {
DLLQ_APPEND(ui->top->first, ui->top->last, box);
}
fn s8_t ui_tprint_loc(ui_code_loc_t loc) {
return s8_printf(tcx.temp, "%s(%d)", loc.file, loc.line);
}
fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_id_t id) {
ui_box_t *box = ui_find_box(id);
if (box) {
expect (box->last_touched_event_id != ui->event->id) {
fatalf("likely ui id collision between: %S and %S", ui_tprint_loc(loc), ui_tprint_loc(box->loc));
}
memory_zero(box, offsetof(ui_box_t, id));
} else {
box = ui_alloc_box();
// @todo: we can't really make sure ids don't repeat can't we?
DLLQ_APPEND_EX(ui->hash_first, ui->hash_last, box, hash_next, hash_prev);
}
box->loc = loc;
@@ -217,7 +226,7 @@ fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, s8_t string) {
id.value = ui_hash_mix(file_hash, line_hash);
id.value = ui_hash_mix(id.value, cont_hash);
} else if (ui->id_strategy == ui_id_strategy_hierarchy) {
ui_id_t id_from_stack = ui_id_from_id_stack();
ui_id_t id_from_stack = ui_id_from_parent();
id.value = ui_hash_mix(string_id.value, id_from_stack.value);
} else_is_invalid;
@@ -233,21 +242,21 @@ fn ui_signal_t ui_signal_from_box(ui_box_t *box) {
b32 move = ev->kind == app_event_kind_mouse_move;
b32 inside = r2f32_contains(box->rect, ev->mouse_pos);
if (ui_active(box)) {
if (ui_is_active_box(box)) {
if (ev_left_up(ev)) {
if (ui_hot(box)) {
if (ui_is_hot_box(box)) {
result.clicked = true;
} else {
ui->active.value = 0;
}
}
} else if (ui_hot(box) && ev_left_down(ev)) {
} else if (ui_is_hot_box(box) && ev_left_down(ev)) {
ui->active = box->id;
}
if (inside) {
ui->hot.value = box->id.value;
} else if (!inside && ui_hot(box)) {
} else if (!inside && ui_is_hot_box(box)) {
ui->hot = ui_id_null;
}
@@ -352,14 +361,14 @@ fn ui_signal_t ui_push_exp(ui_code_loc_t loc, char *str, ...) {
if (signal.clicked) {
ui->indent_stack += 1;
STACK_PUSH(ui->id_stack, box->id);
ui_push_indent();
}
return signal;
}
fn void ui_pop_exp(void) {
ui_pop_indent();
ui->indent_stack -= 1;
STACK_POP(ui->id_stack);
}
@@ -549,10 +558,10 @@ fn void ui_draw(void) {
ui_box_t *box = it.box;
v4f32_t rect_color = primary_color_global;
if (ui_hot(box)) {
if (ui_is_hot_box(box)) {
rect_color = secondary_color_global;
}
if (ui_active(box)) {
if (ui_is_active_box(box)) {
rect_color = accent1_color_global;
}