diff --git a/bootstrap/bloat-buster/bb_core.c b/bootstrap/bloat-buster/bb_core.c index 6b2e5ea..192831d 100644 --- a/bootstrap/bloat-buster/bb_core.c +++ b/bootstrap/bloat-buster/bb_core.c @@ -10,6 +10,10 @@ #include #include +#define default_font_height (64) +auto proportional_font_height = default_font_height; +auto monospace_font_height = default_font_height; + fn TextureIndex white_texture_create(Arena* arena, Renderer* renderer) { u32 white_texture_width = 1024; @@ -83,16 +87,18 @@ fn void app_update() if (likely(ui_build_begin(window->os, frame_ms, &state.event_queue))) { - { - if (unlikely(ui_button(strlit("Hello world\n")).clicked_left)) - { - print("Clicked on hello world\n"); - } + ui_font_size(default_font_height); + ui_pref_width(ui_em(10, 1)); + ui_pref_height(ui_em(2, 1)); - if (unlikely(ui_button(strlit("Bye world\n")).clicked_left)) - { - print("Clicked on bye world\n"); - } + if (unlikely(ui_button(strlit("Hello world\n")).clicked_left)) + { + print("Clicked on hello world\n"); + } + + if (unlikely(ui_button(strlit("Bye world\n")).clicked_left)) + { + print("Clicked on bye world\n"); } ui_build_end(); @@ -181,7 +187,7 @@ strlit("/Users/david/Library/Fonts/FiraSans-Regular.ttf"); auto white_texture = white_texture_create(state.arena, state.renderer); auto monospace_font = font_texture_atlas_create(state.arena, state.renderer, (TextureAtlasCreate) { .font_path = font_path, - .text_height = 12, + .text_height = monospace_font_height, }); auto proportional_font = monospace_font; diff --git a/bootstrap/include/std/ui_core.h b/bootstrap/include/std/ui_core.h index dd451fd..a9a7a62 100644 --- a/bootstrap/include/std/ui_core.h +++ b/bootstrap/include/std/ui_core.h @@ -98,6 +98,7 @@ STRUCT(UI_Widget) u64 last_build_touched; F32Vec2 view_offset; Color background_color; + Color text_color; }; decl_vbp(UI_Widget); @@ -197,3 +198,9 @@ EXPORT UI_Signal ui_signal_from_widget(UI_Widget* widget); EXPORT UI_State* ui_state_get(); EXPORT UI_Widget* ui_widget_make(UI_WidgetFlags flags, String string); +EXPORT UI_Size ui_pixels(u32 width, f32 strictness); +EXPORT UI_Size ui_percentage(f32 percentage, f32 strictness); +EXPORT UI_Size ui_em(f32 value, f32 strictness); +EXPORT void ui_pref_width(UI_Size size); +EXPORT void ui_pref_height(UI_Size size); +EXPORT void ui_font_size(f32 size); diff --git a/bootstrap/std/ui_core.c b/bootstrap/std/ui_core.c index c181d6d..b23a3a7 100644 --- a/bootstrap/std/ui_core.c +++ b/bootstrap/std/ui_core.c @@ -28,6 +28,16 @@ fn u8* ui_pop_generic(VirtualBuffer(u8)* stack, u32 element_size) #define ui_stack_pop(field_name) (typeof(ui_state->stacks.field_name.pointer)) ui_pop_generic(&ui_state->stacks.field_name, sizeof(*ui_state->stacks.field_name.pointer)) #define ui_stack_top(field_name) (ui_state->stacks.field_name.length ? ui_state->stacks.field_name.pointer[ui_state->stacks.field_name.length - 1] : ui_state->stack_nulls.field_name) +void ui_pref_width(UI_Size size) +{ + ui_stack_push(pref_width, size); +} + +void ui_pref_height(UI_Size size) +{ + ui_stack_push(pref_height, size); +} + fn void ui_autopop(UI_State* state) { auto* restrict stack_end = (u32*)((u8*)&state->stacks + sizeof(state->stacks)); @@ -256,6 +266,7 @@ UI_Widget* ui_widget_make_from_key(UI_WidgetFlags flags, UI_Key key) auto color = count % 3 == 0; widget->key = key; widget->background_color = Color4(color, color, color, 1); + widget->text_color = Color4(!color, !color, !color, 1); widget->flags = flags; widget->first = 0; widget->last = 0; @@ -327,8 +338,7 @@ fn void ui_stack_reset(UI_State* state) } } - -fn UI_Size ui_pixels(u32 width, f32 strictness) +UI_Size ui_pixels(u32 width, f32 strictness) { return (UI_Size) { .kind = UI_SIZE_PIXEL_COUNT, @@ -337,7 +347,7 @@ fn UI_Size ui_pixels(u32 width, f32 strictness) }; } -fn UI_Size ui_percentage(f32 percentage, f32 strictness) +UI_Size ui_percentage(f32 percentage, f32 strictness) { return (UI_Size) { .kind = UI_SIZE_PERCENTAGE, @@ -346,15 +356,22 @@ fn UI_Size ui_percentage(f32 percentage, f32 strictness) }; } -fn UI_Size ui_em(f32 value, f32 strictness) +UI_Size ui_em(f32 value, f32 strictness) { + auto font_size = ui_stack_top(font_size); + assert(font_size); return (UI_Size) { - .kind = UI_SIZE_PERCENTAGE, + .kind = UI_SIZE_PIXEL_COUNT, .strictness = strictness, - .value = value * ui_stack_top(font_size), + .value = value * font_size, }; } +void ui_font_size(f32 size) +{ + ui_stack_push(font_size, size); +} + u8 ui_build_begin(OSWindow os_window, f64 frame_time, OSEventQueue* event_queue) { ui_state->build_count += 1; @@ -713,8 +730,7 @@ void ui_draw() if (widget->flags.draw_text) { - // todo(); - // window_render_text(renderer, window, widget->text_string, Color4(1, 1, 1, 1), RENDER_FONT_TYPE_PROPORTIONAL, widget->rect.x0, widget->rect.y0); + window_render_text(renderer, window, widget->text, widget->text_color, RENDER_FONT_TYPE_PROPORTIONAL, widget->rect.x0, widget->rect.y0); } if (widget->first)