Thinking about ids

This commit is contained in:
Krzosa Karol
2025-01-11 12:32:29 +01:00
parent 3acc1c2ca4
commit e8c8c1bf5c
2 changed files with 161 additions and 127 deletions

View File

@@ -1,8 +1,25 @@
/*
** [ ] Choosing a keying strategy from user code
** [ ] Using parents
** [ ] Using file and line
**
**
**
*/
typedef struct ui_id_t ui_id_t;
struct ui_id_t {
u64 value;
};
typedef struct ui_code_loc_t ui_code_loc_t;
struct ui_code_loc_t {
char *file;
int line;
int counter;
};
#define UI_CODE_LOC (ui_code_loc_t){.file = __FILE__, .line = __LINE__, .counter = __COUNTER__}
typedef enum {
ui_axis2_x,
ui_axis2_y,
@@ -24,6 +41,10 @@ struct ui_size_t {
f32 strictness;
};
typedef enum {
ui_box_flag_none = 0,
} ui_box_flag_t;
typedef struct ui_box_t ui_box_t;
struct ui_box_t {
// recreated every frame in building code
@@ -35,9 +56,7 @@ struct ui_box_t {
s8_t string;
ui_size_t semantic_size[ui_axis2_count];
b32 grow_axis[ui_axis2_count];
// debug info
char *file_and_line;
ui_code_loc_t loc;
// preserving state
ui_id_t id; // important position!: offset(id) used for partial zeroing
@@ -83,10 +102,46 @@ struct ui_t {
ui_id_t hot;
ui_id_t active;
STACK(ui_id_t, 256) id_stack;
int indent_stack;
};
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; }
#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))
#define ev_left_down(ev) ((ev)->kind == app_event_kind_mouse_down && ev_left(ev))
fn u64 ui_hash_mix(u64 x, u64 y) {
x ^= y;
x *= 0xff51afd7ed558ccd;
x ^= x >> 32;
return x;
}
// @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(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);
}
ui_id_t id = {value};
return id;
}
fn ui_box_t *ui_find_box(ui_id_t id) {
if (id.value == 0) return NULL;
@@ -99,57 +154,71 @@ fn ui_box_t *ui_find_box(ui_id_t id) {
}
fn ui_box_t *ui_alloc_box(void) {
ui_box_t *result = NULL;
ui_box_t *box = NULL;
if (ui->free_first) {
SLLS_POP_AND_STORE(ui->free_first, result);
SLLS_POP_AND_STORE(ui->free_first, box);
} else {
result = ma_push_type(ui->box_arena, ui_box_t);
box = ma_push_type(ui->box_arena, ui_box_t);
ui->allocated_boxes += 1;
}
return result;
return box;
}
fn ui_box_t *ui_build_box(char *file_and_line, ui_id_t id) {
fn void ui_push_box(ui_box_t *box) {
box->parent = ui->top;
DLLQ_APPEND(ui->top->first, ui->top->last, box);
}
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) {
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->file_and_line = file_and_line;
box->loc = loc;
box->last_touched_event_id = ui->event->id;
box->id = id;
ui_push_box(box);
return box;
}
#define ui_hot(box) (box->id.value == ui->hot.value)
#define ui_active(box) (box->id.value == ui->active.value)
fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, s8_t string) {
ui_id_t string_id = ui_id_from_string(string);
ui_id_t id_from_stack = ui_id_from_id_stack();
u64 value = ui_hash_mix(string_id.value, id_from_stack.value);
ui_id_t id = {value};
ui_box_t *box = ui_build_box_from_id(loc, id);
box->string = string;
return box;
}
fn ui_signal_t ui_signal_from_box(ui_box_t *box) {
ui_signal_t result = {0};
app_event_t *ev = ui->event;
b32 left = ev->mouse_button == app_mouse_button_left;
b32 left_up = ev->kind == app_event_kind_mouse_up && left;
b32 left_down = ev->kind == app_event_kind_mouse_down && left;
b32 move = ev->kind == app_event_kind_mouse_move;
b32 inside = r2f32_contains(box->rect, ev->mouse_pos);
if (ui_active(box)) {
if (left_up) {
if (ui_hot(box)) result.clicked = true;
else ui->active.value = 0;
if (ev_left_up(ev)) {
if (ui_hot(box)) {
result.clicked = true;
} else {
ui->active.value = 0;
}
}
} else if (ui_hot(box) && left_down) {
} else if (ui_hot(box) && ev_left_down(ev)) {
ui->active = box->id;
}
if (inside) {
ui->hot.value = box->id.value;
} else if (!inside && ui_hot(box)) {
ui->hot.value = 0;
ui->hot = ui_id_null;
}
return result;
@@ -160,25 +229,27 @@ fn void ui_init(ma_arena_t *arena) {
ui = ma_push_type(arena, ui_t);
ui->box_arena = arena;
ui->root = ma_push_type(arena, ui_box_t);
ui->box_array = ma_push_type(arena, ui_box_t);
SLLS_PUSH(ui->free_first, ui->box_array);
}
fn void ui_begin_build(char *file_and_line, app_event_t *event) {
fn void ui_begin_build(ui_code_loc_t loc, app_event_t *event) {
ui->event = event;
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
next = box->hash_next;
b32 is_null = box->id.value == 0;
if (is_null) {
if (box->id.value == 0) {
DLLQ_REMOVE_EX(ui->hash_first, ui->hash_last, box, hash_next, hash_prev);
zero_struct(box);
SLLS_PUSH(ui->free_first, box);
}
}
ui->top = ui->root = ui_build_box(file_and_line, ui_id_null);
zero_struct(ui->root);
ui->top = ui->root;
ui->root->loc = UI_CODE_LOC;
}
fn void ui_end_build(void) {
@@ -193,26 +264,13 @@ fn void ui_end_build(void) {
SLLS_PUSH(ui->free_first, box);
}
}
}
fn void ui_begin_frame(app_frame_t *frame) {
ui->frame = frame;
}
fn ui_id_t ui_id_from_string(s8_t string) {
u64 value = ht_hash_data(string);
ui_id_t id = {value};
return id;
}
fn void ui_push_box(ui_box_t *box) {
box->parent = ui->top;
DLLQ_APPEND(ui->top->first, ui->top->last, box);
}
fn void ui_push_parent(ui_box_t *box) {
ui_push_box(box);
fn void ui_push_top(ui_box_t *box) {
ui->top = box;
}
@@ -222,34 +280,32 @@ fn ui_box_t *ui_pop_parent(void) {
return top;
}
fn ui_box_t *ui_spacer(char *file_and_line, char *spacing_char) {
ui_id_t id = ui_id_null;
ui_box_t *box = ui_build_box(file_and_line, id);
ui_push_box(box);
fn ui_box_t *ui_spacer(ui_code_loc_t loc, char *spacing_char) {
ui_box_t *box = ui_build_box_from_id(loc, ui_id_null);
v2f32_t spacing = rn_measure_string(&rn_state.main_font, s8_from_char(spacing_char));
box->semantic_size[0] = (ui_size_t){ui_size_kind_pixels, spacing.x, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_pixels, 10, 0.5};
return box;
}
fn ui_box_t *ui_push_list_container(char *file_and_line) {
ui_id_t id = ui_id_null;
ui_box_t *box = ui_build_box(file_and_line, id);
ui_push_parent(box);
fn ui_box_t *ui_push_list_container(ui_code_loc_t loc) {
ui_box_t *box = ui_build_box_from_id(loc, ui_id_null);
ui_push_top(box);
box->semantic_size[0] = (ui_size_t){ui_size_kind_children_sum, 0, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_children_sum, 0, 0.5};
box->grow_axis[1] = true;
return box;
}
fn ui_signal_t ui_push_expander(char *file_and_line, char *str, ...) {
fn void ui_push_indent(void) { ui->indent_stack += 1; }
fn void ui_pop_indent(void) { ui->indent_stack -= 1; }
fn void ui_set_string(ui_box_t *box, s8_t string) { box->string = s8_printf(tcx.temp, "%.*s%S", ui->indent_stack, " ", string); }
fn ui_signal_t ui_push_exp(ui_code_loc_t loc, char *str, ...) {
S8_FMT(tcx.temp, str, string);
ui_id_t id = ui_id_from_string(string);
ui_box_t *box = ui_build_box(file_and_line, id);
box->string = string;
ui_box_t *box = ui_build_box_from_string(loc, string);
box->semantic_size[0] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
ui_push_box(box);
ui_signal_t signal = ui_signal_from_box(box);
if (signal.clicked) {
@@ -258,28 +314,29 @@ fn ui_signal_t ui_push_expander(char *file_and_line, char *str, ...) {
signal.clicked = box->expanded;
if (signal.clicked) {
{
ui_box_t *container = ui_push_list_container(file_and_line);
container->grow_axis[0] = 1;
container->grow_axis[1] = 0;
ui_spacer(file_and_line, "-");
ui_box_t *c = ui_push_list_container(file_and_line);
}
string = s8_printf(tcx.temp, "* %S", string); // ▼
} else {
string = s8_printf(tcx.temp, "> %S", string); // ►
}
ui_set_string(box, string);
if (signal.clicked) {
STACK_PUSH(ui->id_stack, box->id);
ui_push_indent();
}
return signal;
}
fn void ui_pop_expander() {
ui_pop_parent();
ui_pop_parent();
fn void ui_pop_exp(void) {
ui_pop_indent();
STACK_POP(ui->id_stack);
}
fn ui_box_t *ui_label(char *file_and_line, char *fmt, ...) {
fn ui_box_t *ui_label(ui_code_loc_t loc, char *fmt, ...) {
S8_FMT(tcx.temp, fmt, string);
ui_id_t id = ui_id_from_string(string);
ui_box_t *box = ui_build_box(file_and_line, id);
ui_push_box(box);
box->string = string;
ui_box_t *box = ui_build_box_from_id(loc, ui_id_null);
ui_set_string(box, string);
box->semantic_size[0] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
return box;
@@ -359,7 +416,11 @@ fn void ui_test_stringify_postorder(sb8_t *sb, ui_box_t *box) {
}
fn void ui_end_frame(void) {
for (app_event_t *ev = ui->frame->first_event; ev; ev = ev->next) {
if (ev_left_up(ev)) {
ui->active = ui_id_null;
}
}
}
fn void ui_draw(void) {
@@ -457,7 +518,15 @@ fn void ui_draw(void) {
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
ui_box_t *box = it.box;
rn_draw_rect(box->rect, primary_color_global);
v4f32_t rect_color = primary_color_global;
if (ui_hot(box)) {
rect_color = secondary_color_global;
}
if (ui_active(box)) {
rect_color = accent1_color_global;
}
rn_draw_rect(box->rect, rect_color);
rn_draw_string(font, box->rect.min, black_color_global, box->string);
}
}