ui misc
This commit is contained in:
184
src/ui/ui.c
184
src/ui/ui.c
@@ -622,7 +622,7 @@ fn ui_signal_t ui__label_button(ui_code_loc_t loc, char *str, ...) {
|
|||||||
#define ui_label(...) ui__label(UILOC, __VA_ARGS__)
|
#define ui_label(...) ui__label(UILOC, __VA_ARGS__)
|
||||||
fn ui_box_t *ui__label(ui_code_loc_t loc, char *str, ...) {
|
fn ui_box_t *ui__label(ui_code_loc_t loc, char *str, ...) {
|
||||||
S8_FMT(tcx->temp, str, string);
|
S8_FMT(tcx->temp, str, string);
|
||||||
ui_box_t *box = ui_box(.loc = loc, .string = string, .flags = {.draw_text = true, .animate_appear = true});
|
ui_box_t *box = ui_box(.loc = loc, .string = string, .flags = {.draw_text = true});
|
||||||
return box;
|
return box;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1047,7 +1047,7 @@ fn ui_scroller_t ui_begin_scroller(ui_code_loc_t loc, ui_scroller_params_t p) {
|
|||||||
f32 render_start_count = f32_floor(p.verti.value[0] / p.verti.item_pixels);
|
f32 render_start_count = f32_floor(p.verti.value[0] / p.verti.item_pixels);
|
||||||
|
|
||||||
i32 istart = (i32)render_start_count;
|
i32 istart = (i32)render_start_count;
|
||||||
i32 iend = (i32)render_start_count + (i32)visible_item_count;
|
i32 iend = (i32)render_start_count + (i32)visible_item_count + 1;
|
||||||
s.verti.istart = CLAMP(istart, 0, (i32)p.verti.item_count);
|
s.verti.istart = CLAMP(istart, 0, (i32)p.verti.item_count);
|
||||||
s.verti.iend = CLAMP(iend, 0, (i32)p.verti.item_count);
|
s.verti.iend = CLAMP(iend, 0, (i32)p.verti.item_count);
|
||||||
}
|
}
|
||||||
@@ -1065,6 +1065,11 @@ fn void ui_cut_top_scroller_offset(ui_scroller_t s) {
|
|||||||
r2f32_cut_top_no_squash(ui_top_rectp(), (f32)s.verti.istart * s.p.verti.item_pixels);
|
r2f32_cut_top_no_squash(ui_top_rectp(), (f32)s.verti.istart * s.p.verti.item_pixels);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn v2f32_t ui_scroller_get_scroll_value(ui_scroller_t *s) {
|
||||||
|
v2f32_t result = v2f32(s->p.hori.value ? s->p.hori.value[0] : 0, s->p.verti.value ? s->p.verti.value[0] : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
fn void ui_end_scroller(ui_scroller_t s) {
|
fn void ui_end_scroller(ui_scroller_t s) {
|
||||||
if (s.p.verti.enabled && s.p.verti.item_count == 0) {
|
if (s.p.verti.enabled && s.p.verti.item_count == 0) {
|
||||||
ui_scroller_calc_vertical(s);
|
ui_scroller_calc_vertical(s);
|
||||||
@@ -1105,7 +1110,7 @@ fn void ui_end_scroller(ui_scroller_t s) {
|
|||||||
s.p.hori.value[0] = CLAMP(s.p.hori.value[0], 0, s.p.hori.max_size);
|
s.p.hori.value[0] = CLAMP(s.p.hori.value[0], 0, s.p.hori.max_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ui_offset_children(s.p.parent, v2f32(s.p.hori.value ? s.p.hori.value[0] : 0, s.p.verti.value ? s.p.verti.value[0] : 0));
|
ui_offset_children(s.p.parent, ui_scroller_get_scroll_value(&s));
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
@@ -1235,8 +1240,91 @@ fn void ui_init(ma_arena_t *arena) {
|
|||||||
ui_reload();
|
ui_reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
gb app_event_t ui_test_event;
|
fn r2f32_t ui_get_centered_rect(v2f32_t parent_rect_size, v2f32_t size) {
|
||||||
|
v2f32_t max_size = v2f32_sub(parent_rect_size, v2f32(ui_em(2), ui_em(2)));
|
||||||
|
v2f32_t lister_size = v2f32_clamp(size, v2f32_null, max_size);
|
||||||
|
v2f32_t pos = v2f32_divs(v2f32_sub(parent_rect_size, lister_size), 2.0);
|
||||||
|
pos.y *= 0.05f;
|
||||||
|
r2f32_t rect = r2f32_min_dim(pos, lister_size);
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
gb i32 ui_g_panel = 1;
|
gb i32 ui_g_panel = 1;
|
||||||
|
fn void ui_demo_everything_lister(void) {
|
||||||
|
app_event_t *ev = ui->event;
|
||||||
|
app_frame_t *frame = ui->frame;
|
||||||
|
ui_id_t lister_id = ui_id_from_loc(UILOC);
|
||||||
|
locl b32 lister_open;
|
||||||
|
b32 lister_just_opened = false;
|
||||||
|
if (ev->kind == app_event_kind_key_down && ev->key == app_key_p && ev->ctrl) {
|
||||||
|
lister_open = !lister_open;
|
||||||
|
if (lister_open) lister_just_opened = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
s8_t cmds[] = {
|
||||||
|
s8_lit("show data tab"),
|
||||||
|
s8_lit("show log tab"),
|
||||||
|
s8_lit("show menus tab"),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (lister_open) {
|
||||||
|
r2f32_t rect = ui_get_centered_rect(frame->window_size, v2f32(ui_dm(50), ui_em(25)));
|
||||||
|
ui_box_t *lister = ui_box(.id = lister_id, .rect = rect, .flags = {.animate_appear = true, .draw_rect = true, .draw_border = true, .clip_rect = true});
|
||||||
|
|
||||||
|
locl f32 verti_scroller_value;
|
||||||
|
ui_scroller_t scroller = ui_begin_scroller(UILOC, (ui_scroller_params_t){
|
||||||
|
.parent = lister,
|
||||||
|
.verti = {
|
||||||
|
.enabled = true,
|
||||||
|
.value = &verti_scroller_value,
|
||||||
|
.item_pixels = ui_em(1),
|
||||||
|
.item_count = (i32)lengthof(cmds),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
ui_set_top(lister)
|
||||||
|
ui_set_lop(ui_lop_cut_top) {
|
||||||
|
locl char buff[128];
|
||||||
|
locl ui_text_input_t text_input;
|
||||||
|
text_input.str = buff;
|
||||||
|
text_input.cap = lengthof(buff);
|
||||||
|
ui_signal_t ti_sig = ui_text_input(&text_input, true);
|
||||||
|
if (lister_just_opened) text_input.len = 0;
|
||||||
|
|
||||||
|
ma_temp_t scratch = ma_begin_scratch();
|
||||||
|
s8_t needle = s8(text_input.str, text_input.len);
|
||||||
|
fuzzy_pair_t *pairs = fuzzy_rate_array(scratch.arena, needle, cmds, lengthof(cmds));
|
||||||
|
|
||||||
|
b32 set_focus = lister_just_opened || ti_sig.text_changed;
|
||||||
|
|
||||||
|
ui_set_string_pos_offset(ui_dm(1)) {
|
||||||
|
ui_cut_top_scroller_offset(scroller);
|
||||||
|
for (i32 i = scroller.verti.istart; i < scroller.verti.iend; i += 1) {
|
||||||
|
ui_signal_t sig = ui_button(cmds[pairs[i].index].str);
|
||||||
|
|
||||||
|
if (set_focus) {
|
||||||
|
ui->focus = sig.box->id;
|
||||||
|
set_focus = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (ti_sig.text_commit) {
|
||||||
|
fuzzy_pair_t *pair = &pairs[0];
|
||||||
|
ui_g_panel = (i32)pair->index + 1;
|
||||||
|
ui_text_clear(&text_input);
|
||||||
|
lister_open = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ma_end_scratch(scratch);
|
||||||
|
}
|
||||||
|
|
||||||
|
ui_end_scroller(scroller);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gb app_event_t ui_test_event;
|
||||||
fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_count) {
|
fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_count) {
|
||||||
ui_begin_frame(frame);
|
ui_begin_frame(frame);
|
||||||
rn_begin_frame(frame);
|
rn_begin_frame(frame);
|
||||||
@@ -1266,7 +1354,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
|
|||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
// Data panel
|
// Data panel
|
||||||
if (ui_g_panel == 1) {
|
if (ui_g_panel == 1) {
|
||||||
ui_box_t *item_box = ui_box(r2f32_cut_left(ui_top_rectp(), ui_max), {.draw_rect = true, .clip_rect = true});
|
ui_box_t *item_box = ui_box(r2f32_cut_left(ui_top_rectp(), ui_max), {.draw_rect = true, .clip_rect = true, .animate_appear = true});
|
||||||
|
|
||||||
static f32 verti_scroller_value;
|
static f32 verti_scroller_value;
|
||||||
ui_scroller_t scroller = ui_begin_scroller(UILOC, (ui_scroller_params_t){
|
ui_scroller_t scroller = ui_begin_scroller(UILOC, (ui_scroller_params_t){
|
||||||
@@ -1453,7 +1541,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_box_t *item_box = ui_box(.rect = r2f32_cut_left(ui_top_rectp(), ui_max), .flags = {.draw_rect = true, .clip_rect = true});
|
ui_box_t *item_box = ui_box(.rect = r2f32_cut_left(ui_top_rectp(), ui_max), .flags = {.draw_rect = true, .clip_rect = true, .animate_appear = true});
|
||||||
|
|
||||||
locl f32 right_scroller_value;
|
locl f32 right_scroller_value;
|
||||||
locl f32 bottom_scroller_value;
|
locl f32 bottom_scroller_value;
|
||||||
@@ -1483,7 +1571,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
|
|||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
// Menus panel
|
// Menus panel
|
||||||
if (ui_g_panel == 3) {
|
if (ui_g_panel == 3) {
|
||||||
ui_box_t *item_box = ui_box(.rect = r2f32_cut_left(ui_top_rectp(), ui_max), .flags = {.draw_rect = true, .clip_rect = true});
|
ui_box_t *item_box = ui_box(.rect = r2f32_cut_left(ui_top_rectp(), ui_max), .flags = {.draw_rect = true, .clip_rect = true, .animate_appear = true});
|
||||||
ui_set_top(item_box) {
|
ui_set_top(item_box) {
|
||||||
ui_label("right click to bring up the context menu!");
|
ui_label("right click to bring up the context menu!");
|
||||||
}
|
}
|
||||||
@@ -1500,7 +1588,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (context_menu_open) {
|
if (context_menu_open) {
|
||||||
ui_box_t *menu = ui_box(.rect = r2f32_min_dim(menu_pos, v2f32(ui_em(10), ui_children_sum)), .flags = {.draw_border = true, .draw_rect = true, .children_sum_y = true});
|
ui_box_t *menu = ui_box(.rect = r2f32_min_dim(menu_pos, v2f32(ui_em(10), ui_children_sum)), .flags = {.draw_border = true, .draw_rect = true, .children_sum_y = true, .animate_appear = true});
|
||||||
ui_set_top(menu)
|
ui_set_top(menu)
|
||||||
ui_set_text_align(ui_text_align_left)
|
ui_set_text_align(ui_text_align_left)
|
||||||
ui_set_string_pos_offset(ui_em(1))
|
ui_set_string_pos_offset(ui_em(1))
|
||||||
@@ -1508,7 +1596,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
|
|||||||
ui_label_button("file");
|
ui_label_button("file");
|
||||||
ui_label_button("memes");
|
ui_label_button("memes");
|
||||||
r2f32_cut_left(ui_top_rectp(), ui_em(1));
|
r2f32_cut_left(ui_top_rectp(), ui_em(1));
|
||||||
ui_box_t *a = ui_box(.flags = {.draw_border = true, .draw_rect = true, .children_sum_y = true});
|
ui_box_t *a = ui_box(.flags = {.draw_border = true, .draw_rect = true, .children_sum_y = true, .animate_appear = true});
|
||||||
ui_set_top(a) {
|
ui_set_top(a) {
|
||||||
ui_label_button("a");
|
ui_label_button("a");
|
||||||
ui_label_button("memes");
|
ui_label_button("memes");
|
||||||
@@ -1527,83 +1615,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////
|
ui_demo_everything_lister();
|
||||||
// Everything lister
|
|
||||||
ui_id_t lister_id = ui_id_from_loc(UILOC);
|
|
||||||
locl b32 lister_open;
|
|
||||||
b32 lister_just_opened = false;
|
|
||||||
if (ev->kind == app_event_kind_key_down && ev->key == app_key_p && ev->ctrl) {
|
|
||||||
lister_open = !lister_open;
|
|
||||||
if (lister_open) lister_just_opened = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
s8_t cmds[] = {
|
|
||||||
s8_lit("show data tab"),
|
|
||||||
s8_lit("show log tab"),
|
|
||||||
s8_lit("show menus tab"),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (lister_open) {
|
|
||||||
v2f32_t max_size = v2f32_sub(frame->window_size, v2f32(ui_em(2), ui_em(2)));
|
|
||||||
v2f32_t _lister_size = v2f32(ui_dm(50), ui_em(25));
|
|
||||||
v2f32_t lister_size = v2f32_clamp(_lister_size, v2f32_null, max_size);
|
|
||||||
v2f32_t pos = v2f32_divs(v2f32_sub(frame->window_size, lister_size), 2.0);
|
|
||||||
pos.y *= 0.05f;
|
|
||||||
r2f32_t rect = r2f32_min_dim(pos, lister_size);
|
|
||||||
ui_box_t *lister = ui_box(.id = lister_id, .rect = rect, .flags = {.animate_appear = true, .draw_rect = true, .draw_border = true, .clip_rect = true});
|
|
||||||
|
|
||||||
locl f32 verti_scroller_value;
|
|
||||||
ui_scroller_t scroller = ui_begin_scroller(UILOC, (ui_scroller_params_t){
|
|
||||||
.parent = lister,
|
|
||||||
.verti = {
|
|
||||||
.enabled = true,
|
|
||||||
.value = &verti_scroller_value,
|
|
||||||
.item_pixels = ui_em(1),
|
|
||||||
.item_count = (i32)lengthof(cmds),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
ui_set_top(lister)
|
|
||||||
ui_set_lop(ui_lop_cut_top) {
|
|
||||||
locl char buff[128];
|
|
||||||
locl ui_text_input_t text_input;
|
|
||||||
text_input.str = buff;
|
|
||||||
text_input.cap = lengthof(buff);
|
|
||||||
ui_signal_t ti_sig = ui_text_input(&text_input, true);
|
|
||||||
if (lister_just_opened) text_input.len = 0;
|
|
||||||
|
|
||||||
ma_temp_t scratch = ma_begin_scratch();
|
|
||||||
s8_t needle = s8(text_input.str, text_input.len);
|
|
||||||
fuzzy_pair_t *pairs = fuzzy_rate_array(scratch.arena, needle, cmds, lengthof(cmds));
|
|
||||||
|
|
||||||
b32 set_focus = lister_just_opened || ti_sig.text_changed;
|
|
||||||
|
|
||||||
ui_set_string_pos_offset(ui_dm(1)) {
|
|
||||||
ui_cut_top_scroller_offset(scroller);
|
|
||||||
for (i32 i = scroller.verti.istart; i < scroller.verti.iend; i += 1) {
|
|
||||||
ui_signal_t sig = ui_button(cmds[pairs[i].index].str);
|
|
||||||
|
|
||||||
if (set_focus) {
|
|
||||||
ui->focus = sig.box->id;
|
|
||||||
set_focus = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (ti_sig.text_commit) {
|
|
||||||
fuzzy_pair_t *pair = &pairs[0];
|
|
||||||
ui_g_panel = (i32)pair->index + 1;
|
|
||||||
ui_text_clear(&text_input);
|
|
||||||
lister_open = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ma_end_scratch(scratch);
|
|
||||||
}
|
|
||||||
|
|
||||||
ui_end_scroller(scroller);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
ui_end_build();
|
ui_end_build();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user