wasm f32_pow, f64_pow, lots of changes to math, ui animations start

This commit is contained in:
Krzosa Karol
2025-01-22 17:59:30 +01:00
parent b20a507834
commit de35c4a705
19 changed files with 775 additions and 544 deletions

View File

@@ -127,7 +127,9 @@ fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flags_t flags, ui_id
box->flags = flags;
box->text_align = ui->text_align_stack->value;
box->border_thickness = ui->border_thickness_stack->value;
if (ui->background_color_stack) box->background_color = ui->background_color_stack->value;
box->background_color = ui->background_color_stack->value;
box->text_color = ui->text_color_stack->value;
box->border_color = ui->border_color_stack->value;
ui_push_box(ui->top, box);
return box;
}
@@ -262,6 +264,9 @@ fn void ui_begin_build(ui_code_loc_t loc, app_event_t *ev, r2f32_t window_rect)
ui_push_id(ui_idf("root"));
ui_push_border_thickness(1.0f);
ui_push_text_align(ui_text_align_left);
ui_push_background_color(primary_color_global);
ui_push_text_color(black_color_global);
ui_push_border_color(accent2_color_global);
zero_struct(&ui->root);
ui->root.full_rect = ui->root.rect = window_rect;
@@ -274,9 +279,15 @@ fn void ui_begin_build(ui_code_loc_t loc, app_event_t *ev, r2f32_t window_rect)
fn void ui_end_build(void) {
assert(ui->top == &ui->root);
SLLS_POP(ui->lop_stack);
SLLS_POP(ui->id_stack);
SLLS_POP(ui->border_thickness_stack);
SLLS_POP(ui->text_align_stack);
SLLS_POP(ui->background_color_stack);
SLLS_POP(ui->text_color_stack);
SLLS_POP(ui->border_color_stack);
ui_assert_stacks_are_null();
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
next = box->hash_next;
@@ -298,23 +309,36 @@ fn void ui__draw_box(app_frame_t *frame, ui_box_t *box) {
r2f32_t rect = box->full_rect;
box->final_rect = r2f32_intersect(box->full_rect, ui->clip_rect);
v4f32_t rect_color = primary_color_global;
if (!v4f32_is_null(box->background_color)) rect_color = box->background_color;
v4f32_t text_color = black_color_global;
v4f32_t background_color = box->background_color;
v4f32_t text_color = box->text_color;
v4f32_t border_color = box->border_color;
if (ui_is_hot_box(box)) {
rect_color = secondary_color_global;
text_color = accent2_color_global;
v4f32_t hsla_rect = v4f32_rgba_to_hsla(background_color);
hsla_rect.s = f32_lerp(hsla_rect.s, 1.0f, f32_ease_out_elastic(f32_clamp01(box->hot_t)));
background_color = v4f32_hsla_to_rgba(hsla_rect);
// v4f32_t hsla_text = v4f32_rgba_to_hsla(text_color);
// hsla_text.l = f32_lerp(hsla_text.l, 1.0f, box->hot_t);
// text_color = v4f32_hsla_to_rgba(hsla_text);
}
if (ui_is_active_box(box)) {
rect_color = accent1_color_global;
text_color = white_color_global;
// v4f32_t hsla_rect = v4f32_rgba_to_hsla(background_color);
// hsla_rect.s = f32_lerp(hsla_rect.s, 1.0f, box->active_t);
// background_color = v4f32_hsla_to_rgba(hsla_rect);
// v4f32_t hsla_text = v4f32_rgba_to_hsla(text_color);
// hsla_text.l = f32_lerp(hsla_text.l, 1.0f, box->active_t);
// text_color = v4f32_hsla_to_rgba(hsla_text);
}
if (box->flags.draw_rect) {
rn_draw_rect(rect, rect_color);
rn_draw_rect(rect, background_color);
}
if (box->flags.draw_border) {
rn_draw_rect_border(rect, accent2_color_global, box->border_thickness);
rn_draw_rect_border(rect, border_color, box->border_thickness);
}
if (box->flags.draw_text) {
v2f32_t string_size = rn_measure_string(rn_state.main_font, box->string);
@@ -341,6 +365,7 @@ fn void ui__draw_box(app_frame_t *frame, ui_box_t *box) {
ui->clip_rect = prev_clip_rect;
rn_set_clip(ui->clip_rect);
}
}
fn void ui_draw(void) {
@@ -354,6 +379,20 @@ fn void ui_begin_frame(app_frame_t *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;
@@ -561,15 +600,15 @@ fn void ui_demo_init(ma_arena_t *arena) {
gb i32 ui_g_panel = 1;
gb app_event_t ui_test_event;
fn void ui_demo_update(app_frame_t *frame) {
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)));
ui_set_rect(top_box, r2f32_cut_top(&ui->top->rect, ui_em(1.5f)));
ui_set_padding(v2f32(50, 0))
ui_set_padding(v2f32(ui_em(3), 0))
ui_set_text_align(ui_text_align_center)
ui_set_lop(ui_lop_cut_left)
ui_set_top(top_box) {
@@ -577,7 +616,7 @@ fn void ui_demo_update(app_frame_t *frame) {
ui_radio_button(&ui_g_panel, 2, "2");
}
ui->top->rect = r2f32_shrinks(ui->top->rect, 100);
ui->top->rect = r2f32_shrinks(ui->top->rect, ui_em(1));
if (ui_g_panel == 1) {
@@ -586,10 +625,12 @@ fn void ui_demo_update(app_frame_t *frame) {
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) {
for (i32 i = 0; i < lengthof(tweak_table); i += 1) {
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;

View File

@@ -23,6 +23,12 @@ fn void ui_pop_padding(void) { SLLS_POP(ui->padding_stack); }
fn void ui_push_background_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->background_color_stack, n); }
fn void ui_pop_background_color(void) { SLLS_POP(ui->background_color_stack); }
#define ui_set_background_color(x) defer_block(ui_push_background_color(x), ui_pop_background_color())
fn void ui_push_border_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->border_color_stack, n); }
fn void ui_pop_border_color(void) { SLLS_POP(ui->border_color_stack); }
#define ui_set_border_color(x) defer_block(ui_push_border_color(x), ui_pop_border_color())
fn void ui_push_text_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->text_color_stack, n); }
fn void ui_pop_text_color(void) { SLLS_POP(ui->text_color_stack); }
#define ui_set_text_color(x) defer_block(ui_push_text_color(x), ui_pop_text_color())
fn void ui_assert_stacks_are_null(void) {
assert(ui->id_stack == NULL);
@@ -33,4 +39,6 @@ assert(ui->required_width_stack == NULL);
assert(ui->required_height_stack == NULL);
assert(ui->padding_stack == NULL);
assert(ui->background_color_stack == NULL);
assert(ui->border_color_stack == NULL);
assert(ui->text_color_stack == NULL);
}

View File

@@ -13,6 +13,8 @@ f32 required_width;\
f32 required_height;\
v2f32_t padding;\
v4f32_t background_color;\
v4f32_t border_color;\
v4f32_t text_color;\
#define UI_DECL_STACKS \
ui_id_node_t *id_stack;\
@@ -23,3 +25,5 @@ ui_f32_node_t *required_width_stack;\
ui_f32_node_t *required_height_stack;\
ui_v2f32_node_t *padding_stack;\
ui_v4f32_node_t *background_color_stack;\
ui_v4f32_node_t *border_color_stack;\
ui_v4f32_node_t *text_color_stack;\

View File

@@ -58,6 +58,9 @@ struct ui_box_t {
ui_box_t *hash_prev;
u64 last_touched_event_id;
f32 hot_t;
f32 active_t;
r2f32_t final_rect;
b32 expanded;
};

View File

@@ -9,6 +9,8 @@ void mt_ui(ma_arena_t *arena) {
{ f32 required_height 0 }
{ v2f32_t padding 0 }
{ v4f32_t background_color 0 }
{ v4f32_t border_color 0 }
{ v4f32_t text_color 0 }
));
///////////////////////////////

3
src/ui/ui_inc.c Normal file
View File

@@ -0,0 +1,3 @@
#include "ui_iter.c"
#include "ui.gen.c"
#include "ui.c"

1
src/ui/ui_inc.h Normal file
View File

@@ -0,0 +1 @@
#include "ui.h"