ui id strategies, display and hash string

This commit is contained in:
Krzosa Karol
2025-01-11 12:58:28 +01:00
parent e8c8c1bf5c
commit b81297450b
2 changed files with 46 additions and 15 deletions

View File

@@ -46,6 +46,7 @@ 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(UI_CODE_LOC, ev), ui_end_build()) {
defer_block(ui_push_list_container(UI_CODE_LOC), ui_pop_parent()) {
@@ -55,15 +56,15 @@ fn b32 app_update(app_frame_t *frame) {
ui_label(UI_CODE_LOC, "y: f64 = value");
ui_label(UI_CODE_LOC, "z: 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(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()) {
defer_if (ui_push_exp(UI_CODE_LOC, "inner_pos: v2f64_t##asd").clicked, ui_pop_exp()) {
ui_label(UI_CODE_LOC, "x: f64 = value");
ui_label(UI_CODE_LOC, "y: f64 = value");
}
defer_if (ui_push_exp(UI_CODE_LOC, "inner_pos: v2f64_t##qwe").clicked, ui_pop_exp()) {
ui_label(UI_CODE_LOC, "x: f64 = value");
ui_label(UI_CODE_LOC, "y: f64 = value");

View File

@@ -85,6 +85,11 @@ struct ui_signal_t {
b8 hovering;
};
typedef enum {
ui_id_strategy_hierarchy,
ui_id_strategy_code_loc,
} ui_id_strategy_t;
typedef struct ui_t ui_t;
struct ui_t {
ma_arena_t *box_arena; // required to be only used for boxes
@@ -105,6 +110,7 @@ struct ui_t {
STACK(ui_id_t, 256) id_stack;
int indent_stack;
ui_id_strategy_t id_strategy;
};
gb ui_t *ui = NULL;
@@ -127,9 +133,25 @@ fn u64 ui_hash_mix(u64 x, u64 y) {
return x;
}
s8_t ui_get_display_string(s8_t string) {
s8_t result = string;
if (s8_seek(result, s8_lit("##"), s8_seek_none, &result.len)) {
int a = 10;
}
return result;
}
s8_t ui_get_hash_string(s8_t string) {
i64 len = 0;
if (s8_seek(string, s8_lit("##"), s8_seek_none, &len)) {
string = s8_skip(string, len + 2);
}
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(string);
u64 value = ht_hash_data(ui_get_hash_string(string));
ui_id_t id = {value};
return id;
}
@@ -186,13 +208,21 @@ fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_id_t id) {
}
fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, s8_t string) {
ui_id_t id = {32};
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};
if (ui->id_strategy == ui_id_strategy_code_loc) {
u64 file_hash = ht_hash_data(s8_from_char(loc.file));
u64 line_hash = ht_hash_data(s8((char *)&loc.line, sizeof(loc.line)));
u64 cont_hash = ht_hash_data(s8((char *)&loc.counter, sizeof(loc.counter)));
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();
id.value = ui_hash_mix(string_id.value, id_from_stack.value);
} else_is_invalid;
ui_box_t *box = ui_build_box_from_id(loc, id);
box->string = string;
box->string = ui_get_display_string(string);
return box;
}
@@ -299,7 +329,7 @@ fn ui_box_t *ui_push_list_container(ui_code_loc_t loc) {
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 void ui_set_indented_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);
@@ -314,11 +344,11 @@ fn ui_signal_t ui_push_exp(ui_code_loc_t loc, char *str, ...) {
signal.clicked = box->expanded;
if (signal.clicked) {
string = s8_printf(tcx.temp, "* %S", string); // ▼
box->string = s8_printf(tcx.temp, "* %S", box->string); // ▼
} else {
string = s8_printf(tcx.temp, "> %S", string); // ►
box->string = s8_printf(tcx.temp, "> %S", box->string); // ►
}
ui_set_string(box, string);
ui_set_indented_string(box, box->string);
if (signal.clicked) {
@@ -336,7 +366,7 @@ fn void ui_pop_exp(void) {
fn ui_box_t *ui_label(ui_code_loc_t loc, char *fmt, ...) {
S8_FMT(tcx.temp, fmt, string);
ui_box_t *box = ui_build_box_from_id(loc, ui_id_null);
ui_set_string(box, string);
ui_set_indented_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;