381 lines
13 KiB
C
381 lines
13 KiB
C
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;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
fn 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;
|
|
}
|
|
|
|
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_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 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_box_t *ui_alloc_box(void) {
|
|
ui_box_t *box = NULL;
|
|
if (ui->free_first) {
|
|
SLLS_POP_AND_STORE(ui->free_first, box);
|
|
} else {
|
|
box = ma_push_type(ui->box_arena, ui_box_t);
|
|
ui->allocated_boxes += 1;
|
|
}
|
|
return box;
|
|
}
|
|
|
|
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_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 ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_id_t id, ui_box_flag_t flags) {
|
|
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();
|
|
box->created_new = true;
|
|
DLLQ_APPEND_EX(ui->hash_first, ui->hash_last, box, hash_next, hash_prev);
|
|
}
|
|
box->loc = loc;
|
|
box->last_touched_event_id = ui->event->id;
|
|
box->id = id;
|
|
box->flags = flags;
|
|
ui_push_box(box);
|
|
return box;
|
|
}
|
|
|
|
fn ui_id_t ui_gen_id(ui_id_strategy_t strat, ui_code_loc_t loc, s8_t string) {
|
|
assert(strat != 0);
|
|
u64 result = 42523423493;
|
|
if (is_flag_set(strat, ui_id_strategy_use_string)) {
|
|
u64 string_hash = hash_data(string);
|
|
result = hash_mix(string_hash, result);
|
|
}
|
|
if (is_flag_set(strat, ui_id_strategy_use_hierarchy)) {
|
|
ui_id_t top_id = ui_find_top_id();
|
|
result = hash_mix(top_id.value, result);
|
|
}
|
|
if (is_flag_set(strat, ui_id_strategy_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, s8_t string, ui_box_flag_t flags) {
|
|
ui_id_t id = ui_gen_id(ui->id_strategy, loc, ui_get_hash_string(string));
|
|
ui_box_t *box = ui_build_box_from_id(loc, id, flags);
|
|
box->string = ui_get_display_string(string);
|
|
return box;
|
|
}
|
|
|
|
fn ui_signal_t ui_signal_from_box(ui_box_t *box) {
|
|
ui_signal_t result = {box};
|
|
app_event_t *ev = ui->event;
|
|
|
|
b32 move = ev->kind == app_event_kind_mouse_move;
|
|
b32 inside = r2f32_contains(box->rect, ev->mouse_pos);
|
|
|
|
if (ui_is_active_box(box)) {
|
|
result.dragging = true;
|
|
if (ev_left_up(ev)) {
|
|
if (ui_is_hot_box(box)) {
|
|
result.clicked = true;
|
|
} else {
|
|
ui->active.value = 0;
|
|
}
|
|
}
|
|
} 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_is_hot_box(box)) {
|
|
ui->hot = ui_null_id;
|
|
}
|
|
|
|
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_strategy = flag2(ui_id_strategy_use_string, ui_id_strategy_use_code_loc);
|
|
|
|
ui->box_array = ma_push_type(arena, ui_box_t);
|
|
SLLS_PUSH(ui->free_first, ui->box_array);
|
|
}
|
|
|
|
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;
|
|
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);
|
|
}
|
|
}
|
|
|
|
zero_struct(ui->root);
|
|
ui->top = ui->root;
|
|
ui->root->loc = UI_CODE_LOC;
|
|
}
|
|
|
|
fn void ui_end_build(void) {
|
|
assert(ui->top == ui->root);
|
|
|
|
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
|
|
next = box->hash_next;
|
|
b32 touched_box_during_update = ui->event->id <= box->last_touched_event_id;
|
|
if (!touched_box_during_update) {
|
|
DLLQ_REMOVE_EX(ui->hash_first, ui->hash_last, box, hash_next, hash_prev);
|
|
zero_struct(box);
|
|
SLLS_PUSH(ui->free_first, box);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn void ui_begin_frame(app_frame_t *frame) {
|
|
ui->frame = frame;
|
|
}
|
|
|
|
fn void ui_push_top(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_null_id, flag2(ui_box_flag_draw_rect, ui_box_flag_draw_border));
|
|
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(flag2(ui_id_strategy_use_code_loc, ui_id_strategy_use_string), loc, s8_lit("spacer"));
|
|
ui_box_t *box = ui_build_box_from_id(loc, id, flag3(ui_box_flag_draw_scroller, ui_box_flag_draw_rect, ui_box_flag_draw_border));
|
|
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_null_id, flag2(ui_box_flag_draw_rect, ui_box_flag_draw_border));
|
|
ui_push_top(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, string, flag2(ui_box_flag_draw_rect, ui_box_flag_draw_text));
|
|
ui_set_semantic_size(box, ui_percent(100), 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_null_id, flag2(ui_box_flag_draw_rect, ui_box_flag_draw_text));
|
|
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_draw(void) {
|
|
rn_font_t *font = &rn_state.main_font;
|
|
ui_test_iterator();
|
|
|
|
// 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(sem.value >= 0 && sem.value <= 100.0);
|
|
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 / 100.0f) * 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->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);
|
|
}
|
|
|
|
// actually draw
|
|
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;
|
|
|
|
v4f32_t rect_color = primary_color_global;
|
|
if (ui_is_hot_box(box)) {
|
|
rect_color = secondary_color_global;
|
|
}
|
|
if (ui_is_active_box(box)) {
|
|
rect_color = accent1_color_global;
|
|
}
|
|
|
|
if (is_flag_set(box->flags, ui_box_flag_draw_rect)) {
|
|
rn_draw_rect(box->rect, rect_color);
|
|
}
|
|
if (is_flag_set(box->flags, ui_box_flag_draw_border)) {
|
|
rn_draw_rect_border(box->rect, accent2_color_global);
|
|
}
|
|
if (is_flag_set(box->flags, ui_box_flag_draw_text)) {
|
|
rn_draw_string(font, box->rect.min, black_color_global, box->string);
|
|
}
|
|
if (is_flag_set(box->flags, ui_box_flag_draw_scroller)) {
|
|
rn_draw_string(font, box->rect.min, black_color_global, s8_lit("s"));
|
|
}
|
|
}
|
|
}
|