color table, string_pos_offset
This commit is contained in:
154
src/ui/ui.c
154
src/ui/ui.c
@@ -201,6 +201,7 @@ fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, ui_box_flags_t flags, s
|
||||
r2f32_t rect = ui_next_rect(ui_top_lop(), &ui->top->rect, string_size);
|
||||
ui_set_rect(box, rect, ui_top_lop());
|
||||
}
|
||||
box->string_pos_offset = ui_top_string_pos_offset();
|
||||
return box;
|
||||
}
|
||||
|
||||
@@ -268,41 +269,70 @@ fn ui_signal_t ui_signal_from_box(ui_box_t *box) {
|
||||
return result;
|
||||
}
|
||||
|
||||
fn void ui_text_input_draw(ui_box_t *box) {
|
||||
r2f32_t rect = box->full_rect;
|
||||
f32 appear_t = f32_ease_out_n(f32_clamp01(box->appear_t*8), 10);
|
||||
fn v2f32_t ui_aligned_text_pos(f32 offset, 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);
|
||||
pos.x += offset;
|
||||
return pos;
|
||||
}
|
||||
|
||||
typedef struct ui_draw_compute_t ui_draw_compute_t;
|
||||
struct ui_draw_compute_t {
|
||||
r2f32_t rect;
|
||||
v4f32_t background_color;
|
||||
v4f32_t border_color;
|
||||
v4f32_t text_color;
|
||||
};
|
||||
|
||||
fn ui_draw_compute_t ui_draw_compute(ui_box_t *box) {
|
||||
ui_draw_compute_t co = {0};
|
||||
|
||||
co.rect = box->full_rect;
|
||||
f32 appear_t = f32_ease_out_n(f32_clamp01(box->appear_t*8), 10);
|
||||
if (!ui_id_is_null(box->id)) {
|
||||
v2f32_t size = v2f32_divs(r2f32_get_size(rect), 2);
|
||||
r2f32_t smaller_rect = r2f32_shrink(rect, size);
|
||||
rect = r2f32_lerp(smaller_rect, rect, appear_t);
|
||||
v2f32_t size = v2f32_muls(r2f32_get_size(co.rect), 0.15f);
|
||||
r2f32_t smaller_rect = r2f32_shrink(co.rect, size);
|
||||
co.rect = r2f32_lerp(smaller_rect, co.rect, appear_t);
|
||||
}
|
||||
|
||||
v4f32_t background_color = box->background_color;
|
||||
v4f32_t text_color = box->text_color;
|
||||
v4f32_t border_color = box->border_color;
|
||||
co.background_color = box->background_color;
|
||||
co.text_color = box->text_color;
|
||||
co.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);
|
||||
co.background_color = v4f32_lerp(co.background_color, box->bg_active_color, 1.0);
|
||||
co.text_color = v4f32_lerp(co.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);
|
||||
co.background_color = v4f32_lerp(co.background_color, box->bg_hot_color, hot_t);
|
||||
co.text_color = v4f32_lerp(co.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});
|
||||
co.background_color = ui_color_table[ui_color_focused_rect_color];
|
||||
box->flags.draw_rect = true;
|
||||
// co.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);
|
||||
return co;
|
||||
}
|
||||
|
||||
fn void ui_text_input_draw(ui_box_t *box) {
|
||||
ui_draw_compute_t co = ui_draw_compute(box);
|
||||
|
||||
rn_draw_rect(co.rect, co.background_color);
|
||||
|
||||
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);
|
||||
v2f32_t pos = ui_aligned_text_pos(box->string_pos_offset, box->text_align, co.rect, string);
|
||||
rn_draw_string(rn_state.main_font, pos, co.text_color, string);
|
||||
|
||||
ti->caret = ui_caret_clamp(ti->caret, 0, (i32)ti->len);
|
||||
{
|
||||
@@ -311,13 +341,29 @@ fn void ui_text_input_draw(ui_box_t *box) {
|
||||
|
||||
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));
|
||||
r2f32_t selection_rect = r2f32(co.rect.min.x + size_min.x, co.rect.min.y, co.rect.min.x + size_max.x, co.rect.min.y + ui_em(1));
|
||||
rn_draw_rect(selection_rect, v4f32(1,1,1,0.5f));
|
||||
|
||||
|
||||
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);
|
||||
r2f32_t caret_rect = r2f32(co.rect.min.x + size_front.x, co.rect.min.y, co.rect.min.x + size_front.x + 1, co.rect.min.y + ui_em(1));
|
||||
rn_draw_rect(caret_rect, co.text_color);
|
||||
}
|
||||
rn_draw_rect_border(co.rect, co.border_color, box->border_thickness);
|
||||
}
|
||||
|
||||
fn void ui_default_draw_box(ui_box_t *box) {
|
||||
ui_draw_compute_t co = ui_draw_compute(box);
|
||||
|
||||
if (box->flags.draw_rect) {
|
||||
rn_draw_rect(co.rect, co.background_color);
|
||||
}
|
||||
if (box->flags.draw_border) {
|
||||
rn_draw_rect_border(co.rect, co.border_color, box->border_thickness);
|
||||
}
|
||||
if (box->flags.draw_text) {
|
||||
v2f32_t text_pos = ui_aligned_text_pos(box->string_pos_offset, box->text_align, co.rect, box->string);
|
||||
rn_draw_string(rn_state.main_font, text_pos, co.text_color, box->string);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -466,14 +512,14 @@ fn ui_signal_t ui__begin_expander(ui_code_loc_t loc, char *str, ...) {
|
||||
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_string_pos_offset(ui_top_string_pos_offset() + 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_string_pos_offset();
|
||||
ui_pop_id();
|
||||
}
|
||||
|
||||
@@ -588,60 +634,6 @@ fn void ui_end_build(void) {
|
||||
}
|
||||
}
|
||||
|
||||
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->full_rect;
|
||||
f32 appear_t = f32_ease_out_n(f32_clamp01(box->appear_t*8), 10);
|
||||
|
||||
if (!ui_id_is_null(box->id)) {
|
||||
v2f32_t size = v2f32_divs(r2f32_get_size(rect), 2);
|
||||
r2f32_t smaller_rect = r2f32_shrink(rect, size);
|
||||
rect = r2f32_lerp(smaller_rect, rect, appear_t);
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -899,6 +891,7 @@ fn void ui_serial_type(void *p, type_t *type) {
|
||||
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));
|
||||
ui_init_colors();
|
||||
}
|
||||
|
||||
gb i32 ui_g_panel = 1;
|
||||
@@ -924,7 +917,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
|
||||
|
||||
if (ui_g_panel == 1) {
|
||||
ui_box_t *scroller_box = ui_boxf((ui_box_flags_t){.draw_rect = true, .clip_rect = true}, "scrollbar");
|
||||
ui_set_rect(scroller_box, r2f32_cut_right(&ui->top->rect, ui_em(0.5f)), ui_lop_cut_right);
|
||||
ui_set_rect(scroller_box, r2f32_cut_right(&ui->top->rect, 10 * frame->dpr), ui_lop_cut_right);
|
||||
|
||||
ui_box_t *item_box = ui_boxf((ui_box_flags_t){.draw_rect = true, .clip_rect = true}, "item_box");
|
||||
ui_set_rect(item_box, r2f32_cut_left(&ui->top->rect, ui_max), ui_lop_cut_left);
|
||||
@@ -980,7 +973,8 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
|
||||
// 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_box_t *box = ui_build_box_from_id(UILOC, (ui_box_flags_t){.draw_rect = true}, ui_idf("slider"));
|
||||
box->background_color = v4f32(1,0,1,1);
|
||||
ui_set_rect(box, r2f32_cut_top(&ui->top->rect, scroller_size * scroller_box_size), ui_lop_cut_top);
|
||||
ui_signal_t signal = ui_signal_from_box(box);
|
||||
if (signal.dragging) {
|
||||
|
||||
Reference in New Issue
Block a user