Fix button background drawing

This commit is contained in:
David Gonzalez Martin 2024-12-10 21:31:03 -06:00 committed by David
parent a9e0c8bfcb
commit 0a74af7ef6
8 changed files with 59 additions and 12 deletions

View File

@ -39,6 +39,28 @@ typedef double f64;
typedef u32 Hash32;
typedef u64 Hash64;
#define STRUCT_FORWARD_DECL(S) typedef struct S S
#define STRUCT(S) STRUCT_FORWARD_DECL(S); struct S
#define UNION_FORWARD_DECL(U) typedef union U U
#define UNION(U) UNION_FORWARD_DECL(U); union U
STRUCT(UVec2)
{
struct
{
u32 x;
u32 y;
};
u32 v[2];
};
STRUCT(Vec4)
{
f32 v[4];
}__attribute__((aligned(16)));
typedef Vec4 Color;
typedef enum Axis2
{
AXIS2_X,
@ -58,10 +80,6 @@ typedef enum Axis2
#define NO_EXCEPT
#endif
#define STRUCT_FORWARD_DECL(S) typedef struct S S
#define STRUCT(S) STRUCT_FORWARD_DECL(S); struct S
#define UNION_FORWARD_DECL(U) typedef union U U
#define UNION(U) UNION_FORWARD_DECL(U); union U
#define Slice(T) Slice_ ## T
#define SliceP(T) SliceP_ ## T

View File

@ -37,3 +37,4 @@ STRUCT(TextureAtlasCreate)
#include <std/render.h>
EXPORT TextureAtlas font_texture_atlas_create(Arena* arena, Renderer* renderer, TextureAtlasCreate create);
EXPORT UVec2 texture_atlas_compute_string_rect(String string, const TextureAtlas* atlas);

View File

@ -11,12 +11,6 @@ typedef struct Renderer Renderer;
typedef struct RenderWindow RenderWindow;
typedef struct Pipeline Pipeline;
STRUCT(Vec4)
{
f32 v[4];
}__attribute__((aligned(16)));
typedef Vec4 Color;
STRUCT(RenderRect)
{
u32 x0;
@ -188,6 +182,7 @@ EXPORT PipelineLayoutIndex renderer_pipeline_get_layout(PipelineIndex pipeline);
EXPORT void renderer_window_frame_begin(Renderer* renderer, RenderWindow* window);
EXPORT void renderer_window_frame_end(Renderer* renderer, RenderWindow* window);
EXPORT TextureIndex renderer_texture_create(Renderer* renderer, TextureMemory texture_memory);
EXPORT UVec2 renderer_font_compute_string_rect(Renderer* renderer, RenderFontType type, String string);
EXPORT void window_command_begin(RenderWindow* window);
EXPORT void window_command_end(RenderWindow* window);
EXPORT void window_render_begin(RenderWindow* window);

View File

@ -48,7 +48,6 @@ STRUCT(UI_State)
Arena* arena;
Renderer* renderer;
RenderWindow* render;
UI_Widget* root;
UI_MousePosition mouse_position;
OSEventMouseButtonEvent mouse_button_events[OS_EVENT_MOUSE_BUTTON_COUNT];
@ -81,5 +80,6 @@ EXPORT u8 ui_build_begin(OSWindow window, f64 frame_time, OSEventQueue* event_qu
EXPORT void ui_build_end();
EXPORT void ui_draw();
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, UI_Rect rect);

View File

@ -107,3 +107,20 @@ TextureAtlas font_texture_atlas_create(Arena* arena, Renderer* renderer, Texture
return result;
}
UVec2 texture_atlas_compute_string_rect(String string, const TextureAtlas* atlas)
{
auto height = atlas->ascent - atlas->descent;
u32 x_offset = 0;
u32 y_offset = height;
for (u64 i = 0; i < string.length; i += 1)
{
auto ch = string.pointer[i];
auto* character = &atlas->characters[ch];
auto kerning = (atlas->kerning_tables + ch * 256)[string.pointer[i + 1]];
x_offset += character->advance + kerning;
}
return (UVec2) { .x = x_offset, .y = y_offset };
}

View File

@ -2270,7 +2270,7 @@ void window_render_text(Renderer* renderer, RenderWindow* window, String string,
auto ch = string.pointer[i];
auto* character = &texture_atlas->characters[ch];
auto pos_x = x_offset;
auto pos_y = y_offset + character->y_offset + height; // Offset of the height to render the character from the bottom (y + height) up (y)
auto pos_y = y_offset + character->y_offset + height + texture_atlas->descent; // Offset of the height to render the character from the bottom (y + height) up (y)
auto uv_x = character->x;
auto uv_y = character->y;
auto uv_width = character->width;
@ -2328,3 +2328,10 @@ void window_render_text(Renderer* renderer, RenderWindow* window, String string,
x_offset += character->advance + kerning;
}
}
UVec2 renderer_font_compute_string_rect(Renderer* renderer, RenderFontType type, String string)
{
auto* texture_atlas = &renderer->fonts[type];
auto result = texture_atlas_compute_string_rect(string, texture_atlas);
return result;
}

View File

@ -1,7 +1,11 @@
#include <std/ui_builder.h>
#include <std/render.h>
UI_Signal ui_button(String string, UI_Rect rect)
{
auto rect_offset = renderer_font_compute_string_rect(ui_state_get()->renderer, RENDER_FONT_TYPE_PROPORTIONAL, string);
rect.x1 = rect.x0 + rect_offset.x;
rect.y1 = rect.y0 + rect_offset.y;
auto* widget = ui_widget_make((UI_WidgetFlags) {
.draw_text = 1,
.draw_background = 1,

View File

@ -8,6 +8,11 @@ void ui_state_select(UI_State* state)
ui_state = state;
}
UI_State* ui_state_get()
{
return ui_state;
}
u8 ui_build_begin(OSWindow window, f64 frame_time, OSEventQueue* event_queue)
{
u8 open = 1;