From e8c8c1bf5c919070316d64efe96a7c55775daac1 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 11 Jan 2025 12:32:29 +0100 Subject: [PATCH] Thinking about ids --- src/wasm_app/main.c | 83 ++++++------------ src/wasm_app/ui.c | 205 +++++++++++++++++++++++++++++--------------- 2 files changed, 161 insertions(+), 127 deletions(-) diff --git a/src/wasm_app/main.c b/src/wasm_app/main.c index 7c4d050..60aa0c8 100644 --- a/src/wasm_app/main.c +++ b/src/wasm_app/main.c @@ -47,30 +47,31 @@ fn b32 app_update(app_frame_t *frame) { assert(frame->first_event); for (app_event_t *ev = frame->first_event; ev; ev = ev->next) { - defer_block(ui_begin_build(FILE_AND_LINE, ev), ui_end_build()) { - defer_block(ui_push_list_container(FILE_AND_LINE), ui_pop_parent()) { - defer_if (ui_push_expander(FILE_AND_LINE, "app_event_t").clicked, ui_pop_expander()) { - defer_if (ui_push_expander(FILE_AND_LINE, "mouse_wheel_delta: v3f64_t").clicked, ui_pop_expander()) { - ui_label(FILE_AND_LINE, "x: f64 = value"); - ui_label(FILE_AND_LINE, "y: f64 = value"); - ui_label(FILE_AND_LINE, "z: f64 = value"); + defer_block(ui_begin_build(UI_CODE_LOC, ev), ui_end_build()) { + defer_block(ui_push_list_container(UI_CODE_LOC), ui_pop_parent()) { + defer_if (ui_push_exp(UI_CODE_LOC, "app_event_t").clicked, ui_pop_exp()) { + defer_if (ui_push_exp(UI_CODE_LOC, "mouse_wheel_delta: v3f64_t").clicked, ui_pop_exp()) { + ui_label(UI_CODE_LOC, "x: f64 = value"); + ui_label(UI_CODE_LOC, "y: f64 = value"); + ui_label(UI_CODE_LOC, "z: f64 = value"); } - ui_label(FILE_AND_LINE, "kind: app_event_kind_t = value"); - ui_label(FILE_AND_LINE, "ctrl: b8 = value"); - ui_label(FILE_AND_LINE, "shift: b8 = value"); - defer_if (ui_push_expander(FILE_AND_LINE, "pos: v2f64_t").clicked, ui_pop_expander()) { - defer_if (ui_push_expander(FILE_AND_LINE, "inner_pos: v2f64_t").clicked, ui_pop_expander()) { - ui_label(FILE_AND_LINE, "x_: f64 = value"); - ui_label(FILE_AND_LINE, "y_: f64 = value"); - } - ui_label(FILE_AND_LINE, "y__: f64 = value"); - defer_if (ui_push_expander(FILE_AND_LINE, "_inner_pos: v2f64_t").clicked, ui_pop_expander()) { - ui_label(FILE_AND_LINE, "x____: f64 = value"); - ui_label(FILE_AND_LINE, "y____: f64 = value"); - } + defer_if (ui_push_exp(UI_CODE_LOC, "inner_pos: v2f64_t").clicked, ui_pop_exp()) { + ui_label(UI_CODE_LOC, "x: f64 = value"); + ui_label(UI_CODE_LOC, "y: f64 = value"); } - ui_label(FILE_AND_LINE, "alt: b8 = value"); - ui_label(FILE_AND_LINE, "meta: b8 = value"); + ui_label(UI_CODE_LOC, "kind: app_event_kind_t = value"); + ui_label(UI_CODE_LOC, "ctrl: b8 = value"); + ui_label(UI_CODE_LOC, "shift: b8 = value"); + defer_if (ui_push_exp(UI_CODE_LOC, "pos: v2f64_t").clicked, ui_pop_exp()) { + defer_if (ui_push_exp(UI_CODE_LOC, "inner_pos: v2f64_t").clicked, ui_pop_exp()) { + ui_label(UI_CODE_LOC, "x: f64 = value"); + ui_label(UI_CODE_LOC, "y: f64 = value"); + + } + ui_label(UI_CODE_LOC, "y: f64 = value"); + } + ui_label(UI_CODE_LOC, "alt: b8 = value"); + ui_label(UI_CODE_LOC, "meta: b8 = value"); } } } @@ -79,45 +80,9 @@ fn b32 app_update(app_frame_t *frame) { rn_begin(); ui_draw(); - rn_draw_stringf(&rn_state.main_font, v2f32(0,frame->window_size.y - 100), black_color_global, "ui_boxes: %d delta: %f update: %f", ui->allocated_boxes, frame->delta, frame->update); + rn_draw_stringf(&rn_state.main_font, v2f32(0,frame->window_size.y - 100), black_color_global, "ui_boxes: %d delta: %f update: %f event_count: %d", ui->allocated_boxes, frame->delta, frame->update, frame->event_count); rn_end(frame->window_size, white_color_global); -#if 0 - // These steps should be totally optional!! - { - app_event_t *ev = events.last; - f64 delta = app_get_anim_delta_time(); - f64 time = app_get_anim_time(); - f64 font_height = get_font_height(); - - // animate - // render - gfx_begin(globals->gfx, ev); - gfx_clear(globals->gfx, white_color_global); - gfx_textf(globals->gfx, (v2f64_t){0,ev->window_size.y - font_height}, black_color_global, - "delta: %f, time: %f widget_count: %d", delta, time, (i32)ui->allocated_boxes); - - - // ► - type_t *ti = type(app_event_t); - f64 xoffset = measure_text("--"); - - - v2f64_t p = {0, 0}; - gfx_textf(globals->gfx, p, black_color_global, "▼ %S: struct", ti->name); - p.x += xoffset; p.y += font_height + 5; - - for (type_member_t *tm = ti->members; tm < ti->members + ti->count; tm += 1) { - s8_t value = ti__serial_data(tcx.temp, ti_extract_member(&globals->event, tm), tm->type); - gfx_textf(globals->gfx, p, black_color_global, "%-20S: %-20S = %S", tm->name, ti_serial_type(tcx.temp, tm->type), value); - p.y += font_height + 5; - } - - ui_draw(globals->gfx); - gfx_end(globals->gfx); - } -#endif - ui_end_frame(); return true; } diff --git a/src/wasm_app/ui.c b/src/wasm_app/ui.c index 2878244..2aba6db 100644 --- a/src/wasm_app/ui.c +++ b/src/wasm_app/ui.c @@ -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); } }