new ui idea

This commit is contained in:
Krzosa Karol
2025-01-16 11:44:23 +01:00
parent c315d50a52
commit 1b40ab9a8e
5 changed files with 116 additions and 484 deletions

View File

@@ -1,8 +1,10 @@
fn s8_t ui_tprint_loc(ui_code_loc_t loc) {
return s8_printf(tcx.temp, "%s(%d)", loc.file, loc.line);
}
fn 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;
}
s8_seek(result, s8_lit("##"), s8_seek_none, &result.len);
return result;
}
@@ -14,27 +16,27 @@ fn s8_t ui_get_hash_string(s8_t string) {
return string;
}
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;
}
return ui_null_id;
fn ui_id_t ui_id_from_string(s8_t string) {
u64 value = hash_data(string);
ui_id_t result = {value};
return result;
}
fn ui_id_t ui_find_top_id(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 u64 ui_hash_code_loc(ui_code_loc_t loc) {
u64 result = 4242843;
u64 file_hash = hash_data(s8_from_char(loc.file));
u64 line_hash = hash_data(s8_struct(loc.line));
u64 cont_hash = hash_data(s8_struct(loc.counter));
result = hash_mix(result, file_hash);
result = hash_mix(result, line_hash);
result = hash_mix(result, cont_hash);
return result;
}
fn ui_box_t *ui_find_box(ui_id_t id) {
if (id.value == 0) return NULL;
for (ui_box_t *it = ui->hash_first; it; it = it->hash_next) {
if (it->id.value == id.value) {
return it;
}
}
return NULL;
fn ui_id_t ui_id_from_code_loc(ui_code_loc_t loc) {
u64 id = ui_hash_code_loc(loc);
ui_id_t result = {id};
return result;
}
fn ui_box_t *ui_alloc_box(void) {
@@ -48,20 +50,24 @@ fn ui_box_t *ui_alloc_box(void) {
return box;
}
fn void ui_push(ui_box_t *parent, ui_box_t *box) {
fn ui_box_t *ui_find_box(ui_id_t id) {
if (id.value == 0) return NULL;
for (ui_box_t *it = ui->hash_first; it; it = it->hash_next) {
if (it->id.value == id.value) {
return it;
}
}
return NULL;
}
fn void ui_push_box(ui_box_t *parent, ui_box_t *box) {
box->parent = parent;
DLLQ_APPEND(parent->first, parent->last, box);
parent->node_count += 1;
}
fn void ui_set_semantic_size(ui_box_t *box, ui_size_t x, ui_size_t y) {
box->semantic_size[0] = x;
box->semantic_size[1] = y;
}
fn s8_t ui_tprint_loc(ui_code_loc_t loc) {
return s8_printf(tcx.temp, "%s(%d)", loc.file, loc.line);
}
fn void ui_push_top(ui_box_t *new_top) { ui->top = new_top; }
fn void ui_pop_top(void) { ui->top = ui->top->parent; }
fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flags_t flags, ui_id_t id) {
ui_box_t *box = ui_find_box(id);
@@ -79,34 +85,12 @@ fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flags_t flags, ui_id
box->last_touched_event_id = ui->event->id;
box->id = id;
box->flags = flags;
ui_push(ui->top, box);
ui_push_box(ui->top, box);
return box;
}
fn ui_id_t ui_gen_id(ui_id_flags_t flags, ui_code_loc_t loc, s8_t string) {
u64 result = 42523423493;
if (flags.use_string) {
u64 string_hash = hash_data(string);
result = hash_mix(string_hash, result);
}
if (flags.use_hierarchy) {
ui_id_t top_id = ui_find_top_id();
result = hash_mix(top_id.value, result);
}
if (flags.use_code_loc) {
u64 file_hash = hash_data(s8_from_char(loc.file));
u64 line_hash = hash_data(s8_struct(loc.line));
u64 cont_hash = hash_data(s8_struct(loc.counter));
result = hash_mix(result, file_hash);
result = hash_mix(result, line_hash);
result = hash_mix(result, cont_hash);
}
ui_id_t id = {result};
return id;
}
fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, ui_box_flags_t flags, s8_t string) {
ui_id_t id = ui_gen_id(ui->id_flags, loc, ui_get_hash_string(string));
ui_id_t id = ui_id_from_string(ui_get_hash_string(string));
ui_box_t *box = ui_build_box_from_id(loc, flags, id);
box->string = ui_get_display_string(string);
return box;
@@ -141,20 +125,18 @@ fn ui_signal_t ui_signal_from_box(ui_box_t *box) {
return result;
}
fn void ui_init(ma_arena_t *arena) {
assert(arena != tcx.temp);
ui = ma_push_type(arena, ui_t);
ui->box_arena = arena;
ui->root = ma_push_type(arena, ui_box_t);
ui->id_flags = (ui_id_flags_t){.use_string = true, .use_code_loc = true};
ui->box_array = ma_push_type(arena, ui_box_t);
SLLS_PUSH(ui->free_first, ui->box_array);
fn ui_signal_t ui_button(ui_code_loc_t loc, char *str, ...) {
S8_FMT(tcx.temp, str, string);
ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){ .draw_rect = true, .draw_text = true }, string);
{
box->full_rect = box->rect = r2f32_cut_top(&ui->top->rect, rn_state.main_font.size);
}
ui_signal_t signal = ui_signal_from_box(box);
return signal;
}
fn void ui_begin_build(ui_code_loc_t loc, app_event_t *event) {
ui->event = event;
fn void ui_begin_build(ui_code_loc_t loc, app_event_t *ev, r2f32_t window_rect) {
ui->event = ev;
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
next = box->hash_next;
@@ -165,13 +147,14 @@ fn void ui_begin_build(ui_code_loc_t loc, app_event_t *event) {
}
}
zero_struct(ui->root);
ui->top = ui->root;
ui->root->loc = UI_CODE_LOC;
zero_struct(&ui->root);
ui->root.full_rect = ui->root.rect = window_rect;
ui->top = &ui->root;
ui->root.loc = loc;
}
fn void ui_end_build(void) {
assert(ui->top == ui->root);
assert(ui->top == &ui->root);
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
next = box->hash_next;
@@ -184,189 +167,10 @@ fn void ui_end_build(void) {
}
}
fn void ui_begin_frame(app_frame_t *frame) {
ui->frame = frame;
}
fn void ui_push_parent(ui_box_t *box) {
ui->top = box;
}
fn ui_box_t *ui_pop_parent(void) {
ui_box_t *top = ui->top;
ui->top = ui->top->parent;
return top;
}
fn ui_box_t *ui_spacer(ui_code_loc_t loc, ui_size_t x, ui_size_t y) {
ui_box_t *box = ui_build_box_from_id(loc, (ui_box_flags_t){.draw_rect = true, .draw_border = true}, ui_null_id);
ui_set_semantic_size(box, x, y);
return box;
}
fn ui_signal_t ui_scroller_button(ui_code_loc_t loc, ui_size_t x, ui_size_t y) {
ui_id_t id = ui_gen_id(ui->id_flags, loc, s8_lit("spacer"));
ui_box_t *box = ui_build_box_from_id(loc, (ui_box_flags_t){.draw_rect = true, .draw_border = true}, id);
ui_set_semantic_size(box, x, y);
ui_signal_t signal = ui_signal_from_box(box);
return signal;
}
fn ui_box_t *ui_push_container(ui_code_loc_t loc, ui_size_t x, ui_size_t y) {
ui_box_t *box = ui_build_box_from_id(loc, (ui_box_flags_t){.draw_rect = true}, ui_null_id);
ui_push_parent(box);
ui_set_semantic_size(box, x, y);
box->grow_axis = ui_axis2_y;
return box;
}
fn ui_box_t *ui_push_xcontainer(ui_code_loc_t loc, ui_size_t x, ui_size_t y) {
ui_box_t *box = ui_push_container(loc, x, y);
box->grow_axis = ui_axis2_x;
return box;
}
fn ui_box_t *ui_push_list_container(ui_code_loc_t loc, ui_size_t size) {
return ui_push_container(loc, size, ui_children_sum());
}
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);
ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){.draw_text = true}, string);
ui_set_semantic_size(box, ui_percent(1), ui_text());
if (box->created_new) box->expanded = true;
ui_signal_t signal = ui_signal_from_box(box);
if (signal.clicked) box->expanded = !box->expanded;
signal.clicked = box->expanded;
if (signal.clicked) {
box->string = s8_printf(tcx.temp, "* %S", box->string); // ▼
} else {
box->string = s8_printf(tcx.temp, "> %S", box->string); // ►
}
ui_set_indented_string(box, box->string);
if (signal.clicked) ui->indent_stack += 1;
return signal;
}
fn void ui_pop_exp(void) {
ui->indent_stack -= 1;
}
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_box_flags_t){.draw_text = true}, ui_null_id);
ui_set_indented_string(box, string);
ui_set_semantic_size(box, ui_text(), ui_text());
return 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_null_id;
}
}
}
fn void ui_layout(void) {
rn_font_t *font = &rn_state.main_font;
// compute standalone sizes: (pixels, text_content)
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;
if (box == ui->root) continue; // @todo: remove somehow
for (ui_axis2_t axis = 0; axis < ui_axis2_count; axis += 1) {
ui_size_t sem = box->semantic_size[axis];
assert(sem.kind != ui_size_kind_null);
if (sem.kind == ui_size_kind_pixels) {
box->computed_size[axis] = sem.value;
} else if (sem.kind == ui_size_kind_text_content) {
box->computed_size[axis] = rn_measure_string(font, box->string).e[axis];
}
}
}
// compute: (percent_of_parent)
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;
ui_box_t *parent = box->parent; // @todo: I'm not sure why Ryan uses a loop to find a parent with fixed size_kind
for (ui_axis2_t axis = 0; axis < ui_axis2_count; axis += 1) {
ui_size_t sem = box->semantic_size[axis];
if (sem.kind == ui_size_kind_percent_of_parent) {
assert(parent->semantic_size[axis].kind == ui_size_kind_pixels ||
parent->semantic_size[axis].kind == ui_size_kind_text_content ||
parent->semantic_size[axis].kind == ui_size_kind_percent_of_parent);
box->computed_size[axis] = (sem.value) * parent->computed_size[axis];
}
}
}
// compute: (children_sum)
for (ui_postorder_iter_t it = ui_iterate_postorder(ui->root); ui_postorder_iter_is_valid(it); ui_iter_advance_postorder(&it)) {
ui_box_t *box = it.box;
for (ui_axis2_t axis = 0; axis < ui_axis2_count; axis += 1) {
ui_size_t sem = box->semantic_size[axis];
if (sem.kind != ui_size_kind_children_sum) continue;
for (ui_box_t *child = box->first; child; child = child->next) {
assert(child->computed_size[axis] != 0.f);
if (box->grow_axis == axis) {
box->computed_size[axis] += child->computed_size[axis];
} else {
box->computed_size[axis] = MAX(box->computed_size[axis], child->computed_size[axis]);
}
}
}
}
// solve violations
// compute relative positions
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;
ui_box_t *parent = box->parent;
if (ui->root == box) continue; // @todo: how to remove this?
for (ui_axis2_t axis = 0; axis < ui_axis2_count; axis += 1) {
f32 *pos = &box->computed_rel_pos[axis];
f32 size = box->computed_size[axis];
f32 parent_pos = parent->computed_rel_pos[axis];
f32 *iter_pos = &parent->iter_pos[axis];
*pos = parent_pos + *iter_pos;
if (parent->flags.scroll) {
*pos -= parent->view_offset.e[axis];
}
if (parent->grow_axis == axis) *iter_pos += size;
}
v2f32_t pos = v2f32(box->computed_rel_pos[0], box->computed_rel_pos[1]);
v2f32_t size = v2f32(box->computed_size[0], box->computed_size[1]);
box->rect = r2f32_mindim(pos, size);
}
}
fn void ui_draw(void) {
rn_font_t *font = &rn_state.main_font;
ui_test_iterator();
ui_layout();
// actually draw
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
fn void ui_draw(app_frame_t *frame) {
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;
r2f32_t clip_rect = box->rect;
if (box->parent) {
clip_rect = r2f32_intersect(clip_rect, box->parent->rect);
}
v4f32_t rect_color = primary_color_global;
v4f32_t text_color = black_color_global;
@@ -387,7 +191,32 @@ fn void ui_draw(void) {
rn_draw_rect_border(box->rect, accent2_color_global);
}
if (box->flags.draw_text) {
rn_draw_string(font, box->rect.min, text_color, box->string);
rn_draw_string(&rn_state.main_font, box->rect.min, text_color, box->string);
}
}
}
fn void ui_demo_init(ma_arena_t *arena) {
ui = ma_push_type(arena, ui_t);
ui->box_arena = ma_push_arena(arena, mib(1));
}
fn void ui_demo_update(app_frame_t *frame) {
for (app_event_t *ev = frame->first_event; ev; ev = ev->next) {
ui_begin_build(UILOC, ev, r2f32_mindim(v2f32(0.f, 0.f), frame->window_size));
ui_button(UILOC, "thing itself##id32");
ui_end_build();
}
rn_begin();
ui_draw(frame);
rn_end(frame->window_size, white_color_global);
// End frame
for (app_event_t *ev = frame->first_event; ev; ev = ev->next) {
if (ev_left_up(ev)) {
ui->active = ui_null_id;
}
}
}