ui colors

This commit is contained in:
Krzosa Karol
2025-01-22 22:18:18 +01:00
parent de35c4a705
commit 057d6b6f50
13 changed files with 169 additions and 105 deletions

View File

@@ -4,4 +4,9 @@
#include "app_win32.c"
#elif PLATFORM_WASM
#include "app_wasm.c"
#endif
#endif
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;
}

View File

@@ -46,7 +46,7 @@ https://gitlab.com/nakst/essence/-/blob/master/shared/math.cpp
*/
fn f64 f64_exp2(f64 x) {
f64 a = f64_floor(x * 8);
f64 a = f64_floor(x * 8);
int64_t ai = (i64)a;
if (ai < -1024) {
@@ -56,13 +56,13 @@ fn f64 f64_exp2(f64 x) {
f64 b = x - a / 8;
f64 y = U64_TO_F64(0x3FF0000000000000) +
b * (U64_TO_F64(0x3FE62E42FEFA3A00) +
b * (U64_TO_F64(0x3FCEBFBDFF829140) +
b * (U64_TO_F64(0x3FAC6B08D73C4A40) +
b * (U64_TO_F64(0x3F83B2AB53873280) +
b * (U64_TO_F64(0x3F55D88F363C6C00) +
b * (U64_TO_F64(0x3F242C003E4A2000) +
b * U64_TO_F64(0x3EF0B291F6C00000)))))));
b * (U64_TO_F64(0x3FE62E42FEFA3A00) +
b * (U64_TO_F64(0x3FCEBFBDFF829140) +
b * (U64_TO_F64(0x3FAC6B08D73C4A40) +
b * (U64_TO_F64(0x3F83B2AB53873280) +
b * (U64_TO_F64(0x3F55D88F363C6C00) +
b * (U64_TO_F64(0x3F242C003E4A2000) +
b * U64_TO_F64(0x3EF0B291F6C00000)))))));
const f64 m[8] = {
U64_TO_F64(0x3FF0000000000000),
@@ -84,7 +84,7 @@ fn f64 f64_exp2(f64 x) {
}
fn f32 f32_exp2(f32 x) {
f32 a = f32_floor(x);
f32 a = f32_floor(x);
i32 ai = (i32)a;
if (ai < -128) {
@@ -93,8 +93,7 @@ fn f32 f32_exp2(f32 x) {
f32 b = x - a;
f32 y = U32_TO_F32(0x3F7FFFFE) + b * (U32_TO_F32(0x3F31729A) + b * (U32_TO_F32(0x3E75E700)
+ b * (U32_TO_F32(0x3D64D520) + b * (U32_TO_F32(0x3C128280) + b * U32_TO_F32(0x3AF89400)))));
f32 y = U32_TO_F32(0x3F7FFFFE) + b * (U32_TO_F32(0x3F31729A) + b * (U32_TO_F32(0x3E75E700) + b * (U32_TO_F32(0x3D64D520) + b * (U32_TO_F32(0x3C128280) + b * U32_TO_F32(0x3AF89400)))));
convert_f32_u32_t c;
c.f = y;
@@ -104,10 +103,10 @@ fn f32 f32_exp2(f32 x) {
fn f64 f64_log2(f64 x) {
convert_f64_u64_t c;
c.f = x;
c.f = x;
int64_t e = ((c.i >> 52) & 2047) - 0x3FF;
c.i = (c.i & ~((uint64_t) 0x7FF << 52)) + ((uint64_t) 0x3FF << 52);
x = c.f;
c.i = (c.i & ~((uint64_t)0x7FF << 52)) + ((uint64_t)0x3FF << 52);
x = c.f;
f64 a;
@@ -136,10 +135,7 @@ fn f64 f64_log2(f64 x) {
a = U64_TO_F64(0xBFEA8FF971810A5E);
}
f64 y = U64_TO_F64(0xC00FF8445026AD97) + x * (U64_TO_F64(0x40287A7A02D9353F) + x * (U64_TO_F64(0xC03711C58D55CEE2)
+ x * (U64_TO_F64(0x4040E8263C321A26) + x * (U64_TO_F64(0xC041EB22EA691BB3) + x * (U64_TO_F64(0x403B00FB376D1F10)
+ x * (U64_TO_F64(0xC02C416ABE857241) + x * (U64_TO_F64(0x40138BA7FAA3523A) + x * (U64_TO_F64(0xBFF019731AF80316)
+ x * U64_TO_F64(0x3FB7F1CD3852C200)))))))));
f64 y = U64_TO_F64(0xC00FF8445026AD97) + x * (U64_TO_F64(0x40287A7A02D9353F) + x * (U64_TO_F64(0xC03711C58D55CEE2) + x * (U64_TO_F64(0x4040E8263C321A26) + x * (U64_TO_F64(0xC041EB22EA691BB3) + x * (U64_TO_F64(0x403B00FB376D1F10) + x * (U64_TO_F64(0xC02C416ABE857241) + x * (U64_TO_F64(0x40138BA7FAA3523A) + x * (U64_TO_F64(0xBFF019731AF80316) + x * U64_TO_F64(0x3FB7F1CD3852C200)))))))));
return y - a + e;
}
@@ -148,14 +144,12 @@ fn f32 f32_log2(f32 x) {
// TODO f32_log2(0.9999971379999999f) gives 0.00000143051147460938 (should be negative!!).
convert_f32_u32_t c;
c.f = x;
c.f = x;
i32 e = ((c.i >> 23) & 255) - 0x7F;
c.i = (c.i & ~(0xFF << 23)) + (0x7F << 23);
x = c.f;
c.i = (c.i & ~(0xFF << 23)) + (0x7F << 23);
x = c.f;
f32 y = U32_TO_F32(0xC05B5154) + x * (U32_TO_F32(0x410297C6) + x * (U32_TO_F32(0xC1205CEB)
+ x * (U32_TO_F32(0x4114DF63) + x * (U32_TO_F32(0xC0C0DBBB) + x * (U32_TO_F32(0x402942C6)
+ x * (U32_TO_F32(0xBF3FF98A) + x * (U32_TO_F32(0x3DFE1050) + x * U32_TO_F32(0xBC151480))))))));
f32 y = U32_TO_F32(0xC05B5154) + x * (U32_TO_F32(0x410297C6) + x * (U32_TO_F32(0xC1205CEB) + x * (U32_TO_F32(0x4114DF63) + x * (U32_TO_F32(0xC0C0DBBB) + x * (U32_TO_F32(0x402942C6) + x * (U32_TO_F32(0xBF3FF98A) + x * (U32_TO_F32(0x3DFE1050) + x * U32_TO_F32(0xBC151480))))))));
return y + e;
}

View File

@@ -96,11 +96,11 @@ fn f32 f32_ease_in_cubic(f32 x) { return x * x * x; }
fn f32 f32_ping_pong(f32 x) { return 1.0f - f32_abs(1.0f - f32_mod(x,2)); }
fn v4f32_t v4f32_lerp(v4f32_t a, v4f32_t b, f32 t) {
return (v4f32_t){f32_lerp(a.x, a.y, t), f32_lerp(a.y, b.y, t), f32_lerp(a.z, b.z, t), f32_lerp(a.w, b.w, t)};
return (v4f32_t){f32_lerp(a.x, b.x, t), f32_lerp(a.y, b.y, t), f32_lerp(a.z, b.z, t), f32_lerp(a.w, b.w, t)};
}
fn v4f32_t v4f32_lerp_noa(v4f32_t a, v4f32_t b, f32 t) {
return (v4f32_t){f32_lerp(a.x, a.y, t), f32_lerp(a.y, b.y, t), f32_lerp(a.z, b.z, t), a.w};
return (v4f32_t){f32_lerp(a.x, b.x, t), f32_lerp(a.y, b.y, t), f32_lerp(a.z, b.z, t), a.w};
}
fn f64 _Sine(f64 x) {
@@ -538,3 +538,10 @@ fn f32 f32_ease_out_elastic(f32 x) {
: f32_pow(2.f, -10.f * x) * f32_sin((x * 10.f - 0.75f) * c4) + 1.f;
}
fn f32 f32_ease_out_cubic(f32 x) {
return 1.f - f32_pow(1.f - x, 3.f);
}
fn f32 f32_ease_out_n(f32 x, f32 n) {
return 1.f - f32_pow(1.f - x, n);
}

View File

@@ -203,11 +203,11 @@ union r3i64_t {
#define F64_RAD2DEG (180.0 / F64_PI);
#define F32_PI ((f32)F64_PI)
#define rgba_macro_const(r, g, b, a) { r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }
#define v4f32_const_rgba255(r, g, b, a) { (f32)(r) / 255.0f, (f32)(g) / 255.0f, (f32)(b) / 255.0f, (f32)(a) / 255.0f }
#define v4f32_rgba255(r, g, b, a) (v4f32_t){ (f32)(r) / 255.0f, (f32)(g) / 255.0f, (f32)(b) / 255.0f, (f32)(a) / 255.0f }
gb v4f32_t primary_color_global = rgba_macro_const(245, 238, 230, 255);
gb v4f32_t secondary_color_global = rgba_macro_const(255, 248, 227, 255);
gb v4f32_t accent1_color_global = rgba_macro_const(243, 215, 202, 255);
gb v4f32_t accent2_color_global = rgba_macro_const(230, 164, 180, 255);
gb v4f32_t white_color_global = rgba_macro_const(255, 255, 255, 255);
gb v4f32_t black_color_global = rgba_macro_const(0, 0, 0, 255);
gb_read_only v4f32_t white_color = v4f32_const_rgba255(255, 255, 255, 255);
gb_read_only v4f32_t black_color = v4f32_const_rgba255(0, 0, 0, 255);
gb_read_only v4f32_t magenta_color = v4f32_const_rgba255(255, 0, 255, 255);
gb_read_only v4f32_t red_color = v4f32_const_rgba255(255, 0, 0, 255);
gb_read_only v4f32_t blue_color = v4f32_const_rgba255(0, 0, 255, 255);

View File

@@ -165,7 +165,6 @@ int main(int argc, char **argv) {
test_s8();
test_hash_table();
test_intern_table();
debugf("%f %f", f64_pow(2, 10.4f), powf(2, 10.4f));
printf("all done!\n");
}

View File

@@ -72,6 +72,7 @@ struct rn_state_t {
rn_font_t *main_font;
rn_shader_t shader2d;
app_frame_t *frame;
v4f32_t clear_color;
rn_cmd_t *first_cmd;
rn_cmd_t *last_cmd;

View File

@@ -126,8 +126,9 @@ fn void rn_init(ma_arena_t *perm, f32 font_size, f32 dpr) {
rn_state.shader2d = rn_create_shader(glsl_vshader, glsl_fshader);
}
void rn_begin(app_frame_t *frame) {
void rn_begin(app_frame_t *frame, v4f32_t clear_color) {
rn_state.frame = frame;
rn_state.clear_color = clear_color;
}
void rn_end(void) {
@@ -141,7 +142,7 @@ void rn_end(void) {
glViewport(0, 0, (GLsizei)wx, (GLsizei)wy);
glScissor(0, 0, (GLsizei)wx, (GLsizei)wy);
glClearColor(1,1,1,1);
glClearColor(rn_state.clear_color.r, rn_state.clear_color.g, rn_state.clear_color.b, rn_state.clear_color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Default draw using the font texture

View File

@@ -2,7 +2,6 @@ fn_wasm_import void wasm_clear(void);
fn_wasm_import void wasm_draw_text(isize str, i32 len, f32 x, f32 y, isize font_str, i32 font_len, i32 font_size, f32 r, f32 g, f32 b, f32 a);
fn_wasm_import void wasm_draw_rect(f32 x, f32 y, f32 w, f32 h, f32 r, f32 g, f32 b, f32 a);
fn_wasm_import f32 wasm_measure_text(isize str, i32 len, isize font_str, i32 font_len, i32 font_size);
// fn_wasm_import f32 wasm_get_font_height(isize font_str, i32 font_len, i32 font_size);
fn_wasm_import void wasm_set_clip(f32 x, f32 y, f32 w, f32 h);
gb_read_only s8_t font_face = s8_const_lit("main_font");
@@ -63,11 +62,11 @@ fn void rn_set_clip(r2f32_t rect) {
wasm_set_clip(rect.min.x, rect.min.y, (rect.max.x - rect.min.x), (rect.max.y - rect.min.y));
}
fn void rn_begin(app_frame_t *frame) {
rn_state.frame = frame;
fn void rn_begin(app_frame_t *frame, v4f32_t clear_color) {
rn_state.frame = frame;
r2f32_t window_rect = r2f32(0, 0, rn_state.frame->window_size.x, rn_state.frame->window_size.y);
wasm_clear();
rn_draw_rect(window_rect, white_color_global);
rn_draw_rect(window_rect, clear_color);
}
fn void rn_end() {

View File

@@ -125,11 +125,11 @@ fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flags_t flags, ui_id
box->last_touched_event_id = ui->event->id;
box->id = id;
box->flags = flags;
box->text_align = ui->text_align_stack->value;
box->border_thickness = ui->border_thickness_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;
box->text_align = ui_top_text_align();
box->border_thickness = ui_top_border_thickness();
box->background_color = ui_top_background_color();
box->text_color = ui_top_text_color();
box->border_color = ui_top_border_color();
ui_push_box(ui->top, box);
return box;
}
@@ -139,6 +139,7 @@ fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, ui_box_flags_t flags, s
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);
// @todo: this is un0intuitive
if (ui->required_width_stack) string_size.x = ui->required_width_stack->value;
if (ui->required_height_stack) string_size.y = ui->required_height_stack->value;
if (ui->padding_stack) string_size = v2f32_add(string_size, ui->padding_stack->value);
@@ -166,8 +167,6 @@ fn ui_box_t *ui__box0f(ui_code_loc_t loc, ui_box_flags_t flags, char *str, ...)
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)) {
@@ -206,7 +205,7 @@ fn ui_signal_t ui__radio_button(ui_code_loc_t loc, i32 *value, i32 value_clicked
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;
if (*value == value_clicked) box->background_color = accent1_color_global;
if (*value == value_clicked) box->background_color = ui_top_radio_color();
return signal;
}
@@ -260,33 +259,18 @@ fn void ui_begin_build(ui_code_loc_t loc, app_event_t *ev, r2f32_t window_rect)
}
}
ui_push_lop(ui_lop_cut_top);
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);
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);
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_pop_init_values();
ui_assert_stacks_are_null();
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
@@ -300,11 +284,6 @@ fn void ui_end_build(void) {
}
}
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_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);
@@ -314,25 +293,21 @@ fn void ui__draw_box(app_frame_t *frame, ui_box_t *box) {
v4f32_t border_color = box->border_color;
if (ui_is_hot_box(box)) {
if (ui_is_active_box(box)) {
f32 active_t = f32_clamp01(f32_ease_out_n(box->active_t, 50.f));
// background_color = v4f32_lerp(background_color, v4f32(1,1,1,1), 1.0);
text_color = v4f32_lerp(text_color, v4f32(1,1,1,1), active_t);
} else if (ui_is_hot_box(box)) {
f32 hot_t = f32_ease_out_n(box->hot_t, 25.f);
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)));
hsla_rect.s = f32_lerp(hsla_rect.s, 1.0f, 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);
v4f32_t hsla_text = v4f32_rgba_to_hsla(text_color);
hsla_text = v4f32_lerp(hsla_text, v4f32(0.2f, 0.3f, 0.3f, 1.0f), hot_t);
text_color = v4f32_hsla_to_rgba(hsla_text);
}
if (ui_is_active_box(box)) {
// 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, background_color);
@@ -684,14 +659,16 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
ui_offset_box(it, v2f32(0, scroller_value));
}
}
} else if (ui_g_panel == 2) {
}
ui_end_build();
}
rn_begin(frame);
rn_begin(frame, white_color);
ui_draw();
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);
rn_draw_stringf(rn_state.main_font, v2f32(0, frame->window_size.y - rn_state.main_font->size), v4f32(0,0,0,1), "boxes: %d", ui->allocated_boxes);
rn_end();
ui_end_frame();

View File

@@ -1,34 +1,48 @@
// automatically generated using: C:\dev\wasm\src/ui/ui.meta.c
fn void ui_push_id(ui_id_t v) { ui_id_node_t *n = ma_push_type(tcx.temp, ui_id_node_t); n->value = v; SLLS_PUSH(ui->id_stack, n); }
fn void ui_pop_id(void) { SLLS_POP(ui->id_stack); }
fn ui_id_t ui_top_id(void) { return ui->id_stack->value; }
#define ui_set_id(x) defer_block(ui_push_id(x), ui_pop_id())
fn void ui_push_lop(ui_lop_t v) { ui_lop_node_t *n = ma_push_type(tcx.temp, ui_lop_node_t); n->value = v; SLLS_PUSH(ui->lop_stack, n); }
fn void ui_pop_lop(void) { SLLS_POP(ui->lop_stack); }
fn ui_lop_t ui_top_lop(void) { return ui->lop_stack->value; }
#define ui_set_lop(x) defer_block(ui_push_lop(x), ui_pop_lop())
fn void ui_push_border_thickness(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->border_thickness_stack, n); }
fn void ui_pop_border_thickness(void) { SLLS_POP(ui->border_thickness_stack); }
fn f32 ui_top_border_thickness(void) { return ui->border_thickness_stack->value; }
#define ui_set_border_thickness(x) defer_block(ui_push_border_thickness(x), ui_pop_border_thickness())
fn void ui_push_text_align(ui_text_align_t v) { ui_text_align_node_t *n = ma_push_type(tcx.temp, ui_text_align_node_t); n->value = v; SLLS_PUSH(ui->text_align_stack, n); }
fn void ui_pop_text_align(void) { SLLS_POP(ui->text_align_stack); }
fn ui_text_align_t ui_top_text_align(void) { return ui->text_align_stack->value; }
#define ui_set_text_align(x) defer_block(ui_push_text_align(x), ui_pop_text_align())
fn void ui_push_required_width(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->required_width_stack, n); }
fn void ui_pop_required_width(void) { SLLS_POP(ui->required_width_stack); }
fn f32 ui_top_required_width(void) { return ui->required_width_stack->value; }
#define ui_set_required_width(x) defer_block(ui_push_required_width(x), ui_pop_required_width())
fn void ui_push_required_height(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->required_height_stack, n); }
fn void ui_pop_required_height(void) { SLLS_POP(ui->required_height_stack); }
fn f32 ui_top_required_height(void) { return ui->required_height_stack->value; }
#define ui_set_required_height(x) defer_block(ui_push_required_height(x), ui_pop_required_height())
fn void ui_push_padding(v2f32_t v) { ui_v2f32_node_t *n = ma_push_type(tcx.temp, ui_v2f32_node_t); n->value = v; SLLS_PUSH(ui->padding_stack, n); }
fn void ui_pop_padding(void) { SLLS_POP(ui->padding_stack); }
fn v2f32_t ui_top_padding(void) { return ui->padding_stack->value; }
#define ui_set_padding(x) defer_block(ui_push_padding(x), ui_pop_padding())
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); }
fn v4f32_t ui_top_background_color(void) { return ui->background_color_stack->value; }
#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); }
fn v4f32_t ui_top_border_color(void) { return ui->border_color_stack->value; }
#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); }
fn v4f32_t ui_top_text_color(void) { return ui->text_color_stack->value; }
#define ui_set_text_color(x) defer_block(ui_push_text_color(x), ui_pop_text_color())
fn void ui_push_radio_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->radio_color_stack, n); }
fn void ui_pop_radio_color(void) { SLLS_POP(ui->radio_color_stack); }
fn v4f32_t ui_top_radio_color(void) { return ui->radio_color_stack->value; }
#define ui_set_radio_color(x) defer_block(ui_push_radio_color(x), ui_pop_radio_color())
fn void ui_assert_stacks_are_null(void) {
assert(ui->id_stack == NULL);
@@ -41,4 +55,25 @@ assert(ui->padding_stack == NULL);
assert(ui->background_color_stack == NULL);
assert(ui->border_color_stack == NULL);
assert(ui->text_color_stack == NULL);
assert(ui->radio_color_stack == NULL);
}
fn void ui_push_init_values(void) {
ui_push_id(ui_idf("root"));
ui_push_lop(ui_lop_cut_top);
ui_push_border_thickness(1.0f);
ui_push_text_align(ui_text_align_left);
ui_push_background_color(v4f32_rgba255(245, 238, 230, 255));
ui_push_border_color(v4f32_rgba255(230, 164, 180, 255));
ui_push_text_color(v4f32_rgba255(0, 0, 0, 255));
ui_push_radio_color(v4f32_rgba255(243, 215, 202, 255));
}
fn void ui_pop_init_values(void) {
ui_pop_id();
ui_pop_lop();
ui_pop_border_thickness();
ui_pop_text_align();
ui_pop_background_color();
ui_pop_border_color();
ui_pop_text_color();
ui_pop_radio_color();
}

View File

@@ -15,6 +15,7 @@ v2f32_t padding;\
v4f32_t background_color;\
v4f32_t border_color;\
v4f32_t text_color;\
v4f32_t radio_color;\
#define UI_DECL_STACKS \
ui_id_node_t *id_stack;\
@@ -27,3 +28,4 @@ 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;\
ui_v4f32_node_t *radio_color_stack;\

View File

@@ -119,3 +119,6 @@ fn void ui_set_rect(ui_box_t *box, r2f32_t rect) { box->rect = box->full_rect =
#define ui_em(x) ((x) * rn_state.main_font->size)
#define ui_max 200000000.f
#define ui_box_flags(...) (ui_box_flag_t){__VA_ARGS__}
fn ui_id_t ui_id(s8_t string);
fn ui_id_t ui_idf(char *str, ...);

View File

@@ -1,16 +1,31 @@
void mt_ui(ma_arena_t *arena) {
// gb_read_only v4f32_t primary_color_global = v4f32_rgba(245, 238, 230, 255);
// gb_read_only v4f32_t secondary_color_global = v4f32_rgba(255, 248, 227, 255);
// gb_read_only v4f32_t accent1_color_global = v4f32_rgba(243, 215, 202, 255);
// gb_read_only v4f32_t accent2_color_global = v4f32_rgba(230, 164, 180, 255);
/*
white color 0xE9F7F7 0xE9, 0xF7, 0xF7
blueish color 0xBCF7F3 0xBC, 0xF7, 0xF3
redish color 0xFB8D82 0xFB, 0x8D, 0x82
background 0x0A, 0x11, 0x17
*/
fn void mt_ui_stacks(ma_arena_t *arena, sb8_t *c, sb8_t *h) {
ast_t *table = mtt_parse(arena, __FILE__, S8_CODE(
{ type name skip_box_member }
{ ui_id_t id 1 }
{ ui_lop_t lop 1 }
{ f32 border_thickness 0 }
{ ui_text_align_t text_align 0 }
{ f32 required_width 0 }
{ f32 required_height 0 }
{ v2f32_t padding 0 }
{ v4f32_t background_color 0 }
{ v4f32_t border_color 0 }
{ v4f32_t text_color 0 }
// skip = don't include in ui_box_t as field
{ type name skip init }
{ ui_id_t id 1 `ui_idf("root")` }
{ ui_lop_t lop 1 ui_lop_cut_top }
{ f32 border_thickness 0 1.0f }
{ ui_text_align_t text_align 0 ui_text_align_left }
{ f32 required_width 0 x }
{ f32 required_height 0 x }
{ v2f32_t padding 0 x }
{ v4f32_t background_color 0 `v4f32_rgba255(245, 238, 230, 255)` }
{ v4f32_t border_color 0 `v4f32_rgba255(230, 164, 180, 255)` }
{ v4f32_t text_color 0 `v4f32_rgba255(0, 0, 0, 255)` }
{ v4f32_t radio_color 0 `v4f32_rgba255(243, 215, 202, 255)` }
));
///////////////////////////////
@@ -33,11 +48,6 @@ void mt_ui(ma_arena_t *arena) {
mt_ast_append(it, mt_kv(arena, s8_lit("stack"), stack));
}
sb8_t *h = sb8_serial_begin(arena);
sb8_t *c = sb8_serial_begin(arena);
sb8_printf(h, "// automatically generated using: " __FILE__ "\n");
sb8_printf(c, "// automatically generated using: " __FILE__ "\n");
///////////////////////////////
// generate types
@@ -55,7 +65,7 @@ void mt_ui(ma_arena_t *arena) {
// generate field embeds
sb8_stmtf(h, "#define UI_DECL_BOX_MEMBERS \\");
for (ast_t *it = table->first; it; it = it->next) {
if (mtt(it, "skip_box_member")->integer) continue;
if (mtt(it, "skip")->integer) continue;
mt_stmtf(h, it, "@type @name;\\");
}
sb8_printf(h, "\n");
@@ -71,15 +81,46 @@ void mt_ui(ma_arena_t *arena) {
for (ast_t *it = table->first; it; it = it->next) {
mt_sbprintf(c, it, "fn void ui_push_@name(@type v) { @node *n = ma_push_type(tcx.temp, @node); n->value = v; SLLS_PUSH(ui->@stack, n); }\n"
"fn void ui_pop_@name(void) { SLLS_POP(ui->@stack); }\n"
"fn @type ui_top_@name(void) { return ui->@stack->value; }\n"
"#define ui_set_@name(x) defer_block(ui_push_@name(x), ui_pop_@name())\n");
}
///////////////////////////////
// generate init stack functions
sb8_stmtf(c, "fn void ui_assert_stacks_are_null(void) {");
for (ast_t *it = table->first; it; it = it->next) {
mt_stmtf(c, it, "assert(ui->@stack == NULL);");
}
sb8_stmtf(c, "}");
sb8_stmtf(c, "fn void ui_push_init_values(void) {");
for (ast_t *it = table->first; it; it = it->next) {
if (s8_are_equal(mtts(it, "init"), s8_lit("x"))) {
continue;
}
mt_stmtf(c, it, "ui_push_@name(@init);");
}
sb8_stmtf(c, "}");
sb8_stmtf(c, "fn void ui_pop_init_values(void) {");
for (ast_t *it = table->first; it; it = it->next) {
if (s8_are_equal(mtts(it, "init"), s8_lit("x"))) {
continue;
}
mt_stmtf(c, it, "ui_pop_@name();");
}
sb8_stmtf(c, "}");
}
fn void mt_ui(ma_arena_t *arena) {
sb8_t *h = sb8_serial_begin(arena);
sb8_t *c = sb8_serial_begin(arena);
sb8_printf(h, "// automatically generated using: " __FILE__ "\n");
sb8_printf(c, "// automatically generated using: " __FILE__ "\n");
mt_ui_stacks(arena, c, h);
os_write_file(mt_cpath(arena), sb8_serial_end(arena, c));
os_write_file(mt_hpath(arena), sb8_serial_end(arena, h));