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; s8_seek(result, s8_lit("##"), s8_seek_none, &result.len); 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_id_from_string(s8_t string) { u64 value = hash_data(string); ui_id_t result = {value}; return result; } 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_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) { 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 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 r2f32_t ui_next_rect(ui_op_t op, r2f32_t *rect, v2f32_t required_size) { switch (op) { case ui_op_cut_top: return r2f32_cut_top_no_squash(rect, required_size.y); break; case ui_op_cut_bottom: return r2f32_cut_bottom_no_squash(rect, required_size.y); break; case ui_op_cut_left: return r2f32_cut_left_no_squash(rect, required_size.x); break; case ui_op_cut_right: return r2f32_cut_right_no_squash(rect, required_size.x); break; case ui_op_idle: break; default_is_invalid; } return (r2f32_t){0}; } 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); 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(ui->top, box); return box; } 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_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); v2f32_t string_size = rn_measure_string(&rn_state.main_font, box->string); string_size.x += 50; r2f32_t rect = ui_next_rect(ui->top->op, &ui->top->rect, string_size); ui_set_rect(box, rect); return box; } #define ui_boxf(flags, ...) ui__boxf(UILOC, flags, __VA_ARGS__) fn ui_box_t *ui__boxf(ui_code_loc_t loc, ui_box_flags_t flags, char *str, ...) { S8_FMT(tcx.temp, str, string); ui_box_t *box = ui_build_box_from_string(loc, flags, 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->final_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; } #define ui_button(...) ui__button(UILOC, __VA_ARGS__) 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, .fully_center_text = true }, string); ui_signal_t signal = ui_signal_from_box(box); return signal; } #define ui_label(...) ui__label(UILOC, __VA_ARGS__) fn ui_box_t *ui__label(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); return box; } #define ui_begin_expander(...) ui__begin_expander(UILOC, __VA_ARGS__) fn ui_signal_t ui__begin_expander(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); 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 ( box->expanded) box->string = s8_printf(tcx.temp, "* %S", box->string); if (!box->expanded) box->string = s8_printf(tcx.temp, "> %S", box->string); if (box->expanded) r2f32_cut_left(&ui->top->rect, ui_em(0.5f)); return signal; } fn void ui_end_expander(void) { r2f32_add_left(&ui->top->rect, ui_em(0.5f)); } 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; 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->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); 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 r2f32_t window_rect_from_frame(app_frame_t *frame) { r2f32_t window_rect = r2f32(0, 0, frame->window_size.x, frame->window_size.y); return window_rect; } 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; box->final_rect = box->full_rect; r2f32_t rect = box->final_rect; v4f32_t rect_color = primary_color_global; v4f32_t text_color = black_color_global; if (ui_is_hot_box(box)) { rect_color = secondary_color_global; text_color = accent2_color_global; } if (ui_is_active_box(box)) { rect_color = accent1_color_global; text_color = accent1_color_global; } if (box->flags.draw_rect) { rn_draw_rect(rect, rect_color); } if (box->flags.draw_border) { rn_draw_rect_border(rect, accent2_color_global); } if (box->flags.draw_text) { v2f32_t string_size = rn_measure_string(&rn_state.main_font, box->string); v2f32_t rect_size = r2f32_get_size(rect); v2f32_t rect_string_diff = v2f32_sub(rect_size, string_size); v2f32_t center_pos = v2f32_divs(rect_string_diff, 2); v2f32_t pos_in_rect = v2f32(0, center_pos.y); if (box->flags.fully_center_text) { pos_in_rect = center_pos; } v2f32_t pos = v2f32_add(pos_in_rect, rect.min); rn_draw_string(&rn_state.main_font, pos, 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, window_rect_from_frame(frame)); ui->top->op = ui_op_idle; ui_box_t *top_box = ui_boxf((ui_box_flags_t){.draw_rect = true, .draw_border = true}, "top_box"); ui_set_rect(top_box, r2f32_cut_top(&ui->top->rect, ui_em(1))); defer_block(ui_push_top(top_box), ui_pop_top()) { top_box->op = ui_op_cut_left; ui_button("file"); ui_button("edit"); } ui_box_t *scroller_box = ui_boxf((ui_box_flags_t){.draw_rect = true}, "scroller"); ui_set_rect(scroller_box, r2f32_cut_right(&ui->top->rect, ui_em(1))); ui_box_t *item_box = ui_boxf((ui_box_flags_t){.draw_rect = true}, "item_box"); ui_set_rect(item_box, r2f32_cut_left(&ui->top->rect, ui_max)); // @todo: now actually fill this out with struct data using type info static f32 scroller_value; defer_block(ui_push_top(item_box), ui_pop_top()) { defer_if (ui_begin_expander("app_event_t").clicked, ui_end_expander()) { for (int i = 0; i < 10; i += 1) { ui_label("kind: app_event_kind_t##a%d", i); ui_label("ctrl: b8##ds%d", i); ui_label("shift: b8##f%d", i); defer_if (ui_begin_expander("pos: v3f32_t##gg%d", i).clicked, ui_end_expander()) { ui_label("x: f64 = value##hgt%d", i); ui_label("y: f64 = value##r%d", i); ui_label("z: f64 = value##yr%d", i); } defer_if (ui_begin_expander("mouse_pos: v2f32_t##i2%d", i).clicked, ui_end_expander()) { ui_label("x: f64 = value##i3%d", i); ui_label("y: f64 = value##i4%d", i); } } } } defer_block(ui_push_top(scroller_box), ui_pop_top()) { f32 item_count = (f32)item_box->node_count; f32 all_items_size = item_count * rn_state.main_font.size; f32 item_box_size = r2f32_get_size(ui->top->full_rect).y; f32 scroller_box_size = r2f32_get_size(scroller_box->full_rect).y; f32 scroller_size = CLAMP(item_box_size / all_items_size, 0, 1.0f); f32 scrollable_space = (1 - scroller_size); f32 scroller_norm = scroller_value / (all_items_size); f32 scroller_percent = scroller_norm * scrollable_space; f32 scroller_second = scrollable_space - scroller_percent; scroller_box->op = ui_op_idle; r2f32_cut_top(&ui->top->rect, scroller_percent * scroller_box_size); ui_box_t *box = ui_build_box_from_id(UILOC, (ui_box_flags_t){.draw_border = true, .draw_rect = true}, ui_id_from_string(s8_lit("slider"))); ui_set_rect(box, r2f32_cut_top(&ui->top->rect, scroller_size * scroller_box_size)); ui_signal_t signal = ui_signal_from_box(box); if (signal.dragging) { scroller_value += (ev->mouse_delta.y / item_box_size * 2) * (all_items_size); scroller_value = CLAMP(scroller_value, 0, all_items_size); } if (ev->kind == app_event_kind_mouse_wheel) { scroller_value -= ev->mouse_wheel_delta.y; scroller_value = CLAMP(scroller_value, 0, all_items_size); } for (ui_box_t *it = item_box->first; it; it = it->next) { it->full_rect.min.y -= scroller_value; it->full_rect.max.y -= scroller_value; it->rect.min.y -= scroller_value; it->rect.max.y -= scroller_value; } } ui_end_build(); } rn_begin(); rn_draw_stringf(&rn_state.main_font, v2f32(0, frame->window_size.y - rn_state.main_font.size), black_color_global, "boxes: %d", ui->allocated_boxes); 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; } } }