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 u64 ui_hash_from_stack(void) { u64 result = 34294923; for (ui_id_node_t *it = ui->id_stack; it; it = it->next) { result = hash_mix(result, it->value.value); } return result; } fn ui_id_t ui_id(s8_t string) { string = ui_get_hash_string(string); u64 string_hash = hash_data(string); u64 stack_hash = ui_hash_from_stack(); u64 result = hash_mix(string_hash, stack_hash); return (ui_id_t){result}; } fn ui_id_t ui_idf(char *str, ...) { S8_FMT(tcx.temp, str, string); return ui_id(string); } fn void ui_push_id_string(s8_t string) { ui_push_id(ui_id(string)); } fn void ui_push_top(ui_box_t *new_top) { assert(new_top->parent == ui->top); ui->top = new_top; ui_push_id(new_top->id); } fn void ui_pop_top(void) { ui->top = ui->top->parent; ui_pop_id(); } #define ui_set_top(x) defer_block(ui_push_top(x), ui_pop_top()) 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 ui_axis2_t ui_axis_from_lop(ui_lop_t op) { switch (op) { case ui_lop_cut_top: return ui_axis2_y; break; case ui_lop_cut_bottom: return ui_axis2_y; break; case ui_lop_cut_left: return ui_axis2_x; break; case ui_lop_cut_right: return ui_axis2_x; break; case ui_lop_idle: return ui_axis2_invalid; break; default_is_invalid; } return ui_axis2_invalid; } fn r2f32_t ui_next_rect(ui_lop_t op, r2f32_t *rect, v2f32_t required_size) { switch (op) { case ui_lop_cut_top: return r2f32_cut_top_no_squash(rect, required_size.y); break; case ui_lop_cut_bottom: return r2f32_cut_bottom_no_squash(rect, required_size.y); break; case ui_lop_cut_left: return r2f32_cut_left_no_squash(rect, required_size.x); break; case ui_lop_cut_right: return r2f32_cut_right_no_squash(rect, required_size.x); break; case ui_lop_idle: break; default_is_invalid; } return (r2f32_t){0}; } fn void ui_offset_box(ui_box_t *box, v2f32_t offset) { box->full_rect = r2f32_sub_v2f32(box->full_rect, offset); box->rect = r2f32_sub_v2f32(box->rect, offset); } 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; box->text_align = ui_top_text_align(); box->border_thickness = ui_top_border_thickness(); ui_box_fill_with_colors(box); 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(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); ui_axis2_t axis = ui_axis_from_lop(ui_top_lop()); if (ui->required_size_stack && (axis == ui_axis2_x || axis == ui_axis2_y)) { string_size.e[axis] = ui_top_required_size(); } if (ui->padding_stack && (axis == ui_axis2_x || axis == ui_axis2_y)) { string_size.e[axis] += ui_top_padding(); } r2f32_t rect = ui_next_rect(ui->lop_stack->value, &ui->top->rect, string_size); ui_set_rect(box, rect); return box; } #define ui_boxf(...) ui__boxf(UILOC, __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; } #define ui_box0(...) ui_build_box_from_id(UILOC, __VA_ARGS__, ui_null_id) #define ui_box0f(...) ui__box0f(UILOC, __VA_ARGS__) fn ui_box_t *ui__box0f(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_id(loc, flags, ui_null_id); box->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 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; ui->focus = 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_text_input_draw(ui_box_t *box) { r2f32_t rect = box->final_rect; v4f32_t background_color = box->background_color; v4f32_t text_color = box->text_color; v4f32_t border_color = box->border_color; if (ui_is_active_box(box)) { f32 active_t = f32_ease_out_n(box->active_t, 50.f); active_t = f32_clamp01(active_t); background_color = v4f32_lerp(background_color, box->bg_active_color, 1.0); text_color = v4f32_lerp(text_color, box->text_active_color, active_t); } else if (ui_is_hot_box(box)) { f32 hot_t = f32_ease_out_n(box->hot_t, 3.f); hot_t = f32_clamp01(hot_t); background_color = v4f32_lerp(background_color, box->bg_hot_color, hot_t); text_color = v4f32_lerp(background_color, box->text_hot_color, hot_t); } else if (ui_is_focused_box(box)) { background_color = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.95f, 1.0f}); text_color = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.7f, 1.0f}); } rn_draw_rect(rect, background_color); rn_draw_rect_border(rect, border_color, box->border_thickness); ui_text_input_t *ti = &box->text_input; s8_t string = s8(ti->str, ti->len); v2f32_t pos = ui_aligned_text_pos(box->text_align, rect, string); rn_draw_string(rn_state.main_font, pos, text_color, string); ti->caret = ui_caret_clamp(ti->caret, 0, (i32)ti->len); { s8_t string_min = s8(ti->str, ti->caret.range.min); s8_t string_max = s8(ti->str, ti->caret.range.max); v2f32_t size_min = rn_measure_string(rn_state.main_font, string_min); v2f32_t size_max = rn_measure_string(rn_state.main_font, string_max); r2f32_t selection_rect = r2f32(rect.min.x + size_min.x, rect.min.y, rect.min.x + size_max.x, rect.min.y + ui_em(1)); rn_draw_rect(selection_rect, v4f32(1,1,1,0.2f)); v2f32_t size_front = ti->caret.ifront == 0 ? size_min : size_max; r2f32_t caret_rect = r2f32(rect.min.x + size_front.x, rect.min.y, rect.min.x + size_front.x + 1, rect.min.y + ui_em(1)); rn_draw_rect(caret_rect, text_color); } } fn i32 ui_text_replace(ui_text_input_t *ti, r1i32_t range, s8_t string) { range.min = CLAMP(range.min, 0, (i32)ti->len); range.max = CLAMP(range.max, range.min, (i32)ti->len); assert(range.min >= 0 && range.max <= ti->len); i32 size_to_remove = r1i32_size(range); i32 size_to_add = (i32)string.len; i32 change_size = size_to_add - size_to_remove; if (ti->len + change_size > ti->cap) { i32 rem = ((i32)ti->len + change_size) - ti->cap; size_to_add -= rem; change_size -= rem; assert(change_size >= 0); } u8 *begin_remove = ti->str + range.min; u8 *end_remove = begin_remove + size_to_remove; i32 remain_len = (i32)ti->len - (range.min + size_to_remove); u8 *begin_add = begin_remove; u8 *end_add = begin_add + size_to_add; memory_move(end_add, end_remove, remain_len); memory_copy(begin_add, string.str, size_to_add); ti->len = ti->len + change_size; return change_size; } fn_test void ui_test_text_replace(void) { ui_text_input_t ti = {0}; char buff[4] = {0}; ti.str = buff; ti.cap = lengthof(buff); ui_text_replace(&ti, r1i32(0,0), s8_lit("astfpo")); assert(s8_are_equal(ti.string, s8_lit("astf"))); ui_text_replace(&ti, r1i32(0,4), s8_lit("qwer")); assert(s8_are_equal(ti.string, s8_lit("qwer"))); ui_text_replace(&ti, r1i32(1,2), s8_lit("a")); assert(s8_are_equal(ti.string, s8_lit("qaer"))); } fn ui_signal_t ui_text_input(ui_code_loc_t loc, char *buffer, i32 buffer_size) { ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){ .draw_border = true, .draw_rect = true, .draw_text = true }, s8_lit("text_input")); box->custom_draw = ui_text_input_draw; ui_text_input_t *ti = &box->text_input; ti->str = buffer; ti->cap = buffer_size; ui_signal_t signal = ui_signal_from_box(box); if (ui_is_focused_box(box)) { app_event_t *ev = ui->event; i32 sel_size = r1i32_size(ti->caret.range); if (ev->kind == app_event_kind_text) { ui_text_replace(ti, ti->caret.range, ev->text); if (sel_size) { ti->caret = ui_carets(ti->caret.range.min); } else { ti->caret = ui_carets(ti->caret.range.min + 1); } } else if (ev->kind == app_event_kind_key_down) { if (ev->key == app_key_backspace) { if (sel_size) { ui_text_replace(ti, ti->caret.range, s8_lit("")); } else { ui_text_replace(ti, r1i32(ti->caret.e[0] - 1, ti->caret.e[0]), s8_null); ti->caret = ui_carets(ti->caret.range.min - 1); } } else if (ev->key == app_key_delete) { if (sel_size) { ui_text_replace(ti, ti->caret.range, s8_lit("")); } else { ui_text_replace(ti, r1i32(ti->caret.e[0], ti->caret.e[0] + 1), s8_null); } } else if (ev->key == app_key_left) { if (ev->shift) { ti->caret = ui_caret_set_front(ti->caret, ui_caret_front(ti->caret) - 1); } else { ti->caret = ui_carets(ui_caret_front(ti->caret) - 1); } } else if (ev->key == app_key_right) { if (ev->shift) { ti->caret = ui_caret_set_front(ti->caret, ui_caret_front(ti->caret) + 1); } else { ti->caret = ui_carets(ui_caret_front(ti->caret) + 1); } } } else if (signal.dragging) { v2f32_t size = rn_measure_string(rn_state.main_font, s8_lit("_")); v2f32_t pos = v2f32_sub(ev->mouse_pos, box->final_rect.min); i32 p = (i32)f32_round(pos.x / size.x); if (ev->kind == app_event_kind_mouse_down && ev->mouse_button == app_mouse_button_left) { ti->caret = ui_carets(p); } ti->caret = ui_caret_set_front(ti->caret, p); } ti->caret = ui_caret_clamp(ti->caret, 0, (i32)ti->len); } return signal; } #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_border = true, .draw_rect = true, .draw_text = true }, string); ui_signal_t signal = ui_signal_from_box(box); return signal; } #define ui_radio_button(...) ui__radio_button(UILOC, __VA_ARGS__) fn ui_signal_t ui__radio_button(ui_code_loc_t loc, i32 *value, i32 value_clicked, char *str, ...) { S8_FMT(tcx.temp, str, string); ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){ .draw_border = true, .draw_rect = true, .draw_text = true }, string); ui_signal_t signal = ui_signal_from_box(box); if (signal.clicked) *value = value_clicked; // @todo? if (*value == value_clicked) box->background_color = ui_top_bg_active_color(); return signal; } #define ui_label_button(...) ui__label_button(UILOC, __VA_ARGS__) fn ui_signal_t ui__label_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_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_expander(...) defer_if (ui_begin_expander(__VA_ARGS__).clicked, ui_end_expander()) #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)); ui_push_id(box->id); } return signal; } fn void ui_end_expander(void) { r2f32_add_left(&ui->top->rect, ui_em(0.5f)); ui_pop_id(); } 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); } } ui_push_init_values(); 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); ui_pop_init_values(); ui_assert_stacks_are_null(); 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 v2f32_t ui_aligned_text_pos(ui_text_align_t text_align, r2f32_t rect, s8_t string) { v2f32_t string_size = rn_measure_string(rn_state.main_font, 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 (text_align == ui_text_align_center) { pos_in_rect = center_pos; } v2f32_t pos = v2f32_add(pos_in_rect, rect.min); return pos; } fn void ui_default_draw_box(ui_box_t *box) { r2f32_t rect = box->final_rect; v4f32_t background_color = box->background_color; v4f32_t text_color = box->text_color; v4f32_t border_color = box->border_color; if (ui_is_active_box(box)) { f32 active_t = f32_ease_out_n(box->active_t, 50.f); active_t = f32_clamp01(active_t); background_color = v4f32_lerp(background_color, box->bg_active_color, 1.0); text_color = v4f32_lerp(text_color, box->text_active_color, active_t); } else if (ui_is_hot_box(box)) { f32 hot_t = f32_ease_out_n(box->hot_t, 3.f); hot_t = f32_clamp01(hot_t); background_color = v4f32_lerp(background_color, box->bg_hot_color, hot_t); text_color = v4f32_lerp(background_color, box->text_hot_color, hot_t); } else if (ui_is_focused_box(box)) { background_color = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.95f, 1.0f}); text_color = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.7f, 1.0f}); } if (box->flags.draw_rect) { rn_draw_rect(rect, background_color); } if (box->flags.draw_border) { rn_draw_rect_border(rect, border_color, box->border_thickness); } if (box->flags.draw_text) { v2f32_t pos = ui_aligned_text_pos(box->text_align, rect, box->string); rn_draw_string(rn_state.main_font, pos, text_color, box->string); } } fn void ui__draw_box(app_frame_t *frame, ui_box_t *box) { box->final_rect = r2f32_intersect(box->full_rect, ui->clip_rect); if (box->custom_draw) { box->custom_draw(box); } else { ui_default_draw_box(box); } r2f32_t prev_clip_rect = ui->clip_rect; if (box->flags.clip_rect) { ui->clip_rect = box->full_rect; rn_set_clip(ui->clip_rect); } for (ui_box_t *it = box->first; it; it = it->next) { ui__draw_box(frame, it); } if (box->flags.clip_rect) { ui->clip_rect = prev_clip_rect; rn_set_clip(ui->clip_rect); } } fn void ui_draw(void) { ui->clip_rect = window_rect_from_frame(ui->frame); rn_set_clip(ui->clip_rect); ui__draw_box(ui->frame, &ui->root); } fn void ui_begin_frame(app_frame_t *frame) { ui->frame = frame; } fn void ui_end_frame(void) { for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) { next = box->hash_next; if (ui_is_hot_box(box)) { box->hot_t += (f32)ui->frame->delta; } else { box->hot_t = 0; } if (ui_is_active_box(box)) { box->active_t += (f32)ui->frame->delta; } else { box->active_t = 0; } } 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_serial_subtype(void *p, type_t *type, s8_t name) { assert(type->kind != type_kind_invalid); switch(type->kind) { case type_kind_i8: { i8 *n = (i8 *)p; if (ui_label_button("%S: %d##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (i8)delta; } return; } break; case type_kind_i16: { i16 *n = (i16 *)p; if (ui_label_button("%S: %d##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (i16)delta; } return; } break; case type_kind_i32: { i32 *n = (i32 *)p; if (ui_label_button("%S: %d##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (i32)delta; } return; } break; case type_kind_i64: { i64 *n = (i64 *)p; if (ui_label_button("%S: %lld##slider%S", name, (long long)*n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (i64)delta; } return; } break; case type_kind_u8: { u8 *n = (u8 *)p; if (ui_label_button("%S: %u##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (u8)delta; } return; } break; case type_kind_u16: { u16 *n = (u16 *)p; if (ui_label_button("%S: %u##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (u16)delta; } return; } break; case type_kind_u32: { u32 *n = (u32 *)p; if (ui_label_button("%S: %u##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (u32)delta; } return; } break; case type_kind_u64: { u64 *n = (u64 *)p; if (ui_label_button("%S: %llu##slider%S", name, (unsigned long long)*n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (u64)delta; } return; } break; case type_kind_b8: { b8 *n = (b8 *)p; if (ui_label_button("%S: %s", name, *n ? "true" : "false").clicked) { *n = !*n; } return; } break; case type_kind_b16: { b16 *n = (b16 *)p; if (ui_label_button("%S: %s", name, *n ? "true" : "false").clicked) { *n = !*n; } return; } break; case type_kind_b32: { b32 *n = (b32 *)p; if (ui_label_button("%S: %s", name, *n ? "true" : "false").clicked) { *n = !*n; } return; } break; case type_kind_b64: { b64 *n = (b64 *)p; if (ui_label_button("%S: %s", name, *n ? "true" : "false").clicked) { *n = !*n; } return; } break; case type_kind_f32: { f32 *n = (f32 *)p; if (ui_label_button("%S: %f##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += delta; } return; } break; case type_kind_f64: { f64 *n = (f64 *)p; if (ui_label_button("%S: %f##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += delta; } return; } break; case type_kind_isize: { isize *n = (isize *)p; if (ui_label_button("%S: %lld##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (isize)delta; } return;; } break; case type_kind_usize: { usize *n = (usize *)p; if (ui_label_button("%S: %llu##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (usize)delta; } return;; } break; case type_kind_int: { int *n = (int *)p; if (ui_label_button("%S: %d##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (int)delta; } return;; } break; case type_kind_char: { char *n = (char *)p; if (ui_label_button("%S: %c##slider%S", name, *n, name).dragging) { f32 delta = ui->event->mouse_delta.x; n[0] += (char)delta; } return;; } break; } if (type->kind == type_kind_pointer) { ui_label("%S: %S = %llx", name, type->name, *(usize *)p); return; } if (type->kind == type_kind_enum) { i64 value = ti_enum_read_value(p, type); s8_t string_value = ti_enum_value_to_name(value, type); if (ui_label_button("%S: %S = %S", name, type->name, string_value).clicked) { i64 new_value = ti_enum_next_value(p, value, type); ti_enum_write_value(p, new_value, type); } return; } if (type->kind == type_kind_array) { ui_expander("%S:", name) { for (i32 i = 0; i < type->count; i += 1) { ma_temp_t scratch = ma_begin_scratch(); s8_t index_name = s8_printf(scratch.arena, "[%d]", i); void *tmp = ti_extract_array_idx(p, type, i); ui_serial_subtype(tmp, type->base, index_name); ma_end_scratch(scratch); } } return; } if (type->kind == type_kind_struct) { ui_expander("%S:", name) { for (i32 i = 0; i < type->count; i += 1) { type_member_t *tm = type->members + i; void *tmp = ti_extract_member(p, tm); ui_serial_subtype(tmp, tm->type, tm->name); } } return; } else { ui_label("%S: %S", name, type->name); } } fn void ui_serial_type(void *p, type_t *type) { ui_serial_subtype(p, type, type->name); } 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)); } gb i32 ui_g_panel = 1; gb app_event_t ui_test_event; fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_count) { ui_begin_frame(frame); for (app_event_t *ev = frame->first_event; ev; ev = ev->next) { ui_begin_build(UILOC, ev, window_rect_from_frame(frame)); ui_box_t *top_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true}); ui_set_rect(top_box, r2f32_cut_top(&ui->top->rect, ui_em(1.5f))); ui_set_padding(ui_em(3)) ui_set_text_align(ui_text_align_center) ui_set_lop(ui_lop_cut_left) ui_set_top(top_box) { ui_radio_button(&ui_g_panel, 1, "1"); ui_radio_button(&ui_g_panel, 2, "2"); } ui->top->rect = r2f32_shrinks(ui->top->rect, ui_em(1)); if (ui_g_panel == 1) { ui_box_t *scroller_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true}); ui_set_rect(scroller_box, r2f32_cut_right(&ui->top->rect, ui_em(0.5f))); ui_box_t *item_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true}); ui_set_rect(item_box, r2f32_cut_left(&ui->top->rect, ui_max)); item_box->rect = r2f32_shrinks(item_box->rect, ui_em(1)); ui_set_text_align(ui_text_align_left) ui_set_top(item_box) { static char buff[128]; ui_text_input(UILOC, buff, sizeof(buff)); for (i32 i = 0; i < tweak_count; i += 1) { mt_tweak_t *tweak = tweak_table + i; if (s8_starts_with(tweak->name, s8_lit("_"), false)) { continue; } if (tweak->type->kind == type_kind_b32) { b32 *n = (b32 *)tweak->ptr; if (ui_label_button("%S: %s##slider%S", tweak->name, *n ? "true" : "false", tweak->name).clicked) { *n = !*n; } } else if (tweak->type->kind == type_kind_f32) { f32 *n = (f32 *)tweak->ptr; ui_signal_t signal = ui_label_button("%S: %f##slider%S", tweak->name, *n, tweak->name); if (signal.dragging) { f32 size = tweak->max - tweak->min; v2f32_t string_size = rn_measure_string(rn_state.main_font, signal.box->string); f32 delta = (ui->event->mouse_delta.x / string_size.x) * size; *n = CLAMP(*n + delta, tweak->min, tweak->max); } } else_is_invalid; } ui_label("allocated boxes: %d", ui->allocated_boxes); ui_serial_type(&ui_test_event, type(app_event_t)); } locl f32 scroller_value; 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 + frame->window_size.y), 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; 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_idf("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) { ui_offset_box(it, v2f32(0, scroller_value)); } } } else if (ui_g_panel == 2) { } ui_end_build(); } rn_begin(frame, white_color); ui_draw(); rn_end(); ui_end_frame(); }