text alignment

This commit is contained in:
jacob 2025-11-04 00:54:33 -06:00
parent b1f285f3f1
commit b3779981bd
4 changed files with 53 additions and 12 deletions

View File

@ -2011,6 +2011,7 @@ void PP_UpdateUser(void)
// UI_SetNext(Padding, UI_PADEX(.left = UI_PIX(100, 0), .right = UI_PIX(50, 0), .bottom = UI_PIX(10, 0)));
// UI_SetNext(Padding, UI_PADEX(.left = UI_PIX(1, 0)));
UI_SetNext(Padding, UI_PADALL(UI_FILL(1, 0)));
// UI_SetNext(Padding, UI_PAD(.left = UI_FILL(1, 0), .right = UI_FILL(1, 0)));
UI_Box *title_box = UI_BuildBox(Zstr);
}
UI_PopCP();

View File

@ -55,7 +55,6 @@ UI_Box *PP_BuildDebugConsole(b32 minimized)
{
UI_SetNext(BackgroundColor, Rgba32F(1, 1, 1, 0.02));
UI_SetNext(Width, UI_FILL(1, 0));
// UI_SetNext(Height, UI_FILL(0.33, 1));
UI_SetNext(Height, UI_FIT(1));
}
console_box = UI_BuildBox(Lit("Console box"));
@ -124,10 +123,11 @@ UI_Box *PP_BuildDebugConsole(b32 minimized)
UI_Box *log_box = UI_BuildBox(Zstr);
UI_PushCP(log_box);
{
UI_SetNext(Padding, UI_PADALL(UI_FNT(0.25, 0)));
UI_SetNext(Padding, UI_PADALL(UI_FNT(0.25, 1)));
UI_SetNext(BackgroundColor, 0);
UI_SetNext(Border, 0);
UI_SetNext(Text, text);
UI_SetNext(TextAlignment, UI_Alignment_Left);
UI_SetNext(Width, UI_FILL(1, 0));
UI_SetNext(Height, UI_FIT(1));
UI_SetNext(Flags, UI_BoxFlag_DrawText);

View File

@ -334,10 +334,8 @@ UI_Box *UI_BuildPadAlongAxis(UI_Box *parent, UI_Size start, UI_Size end, Axis fi
{
UI_PushStack();
{
UI_PushCP(parent);
UI_Push(Parent, parent);
{
UI_ForcePush(Padding, UI_PIX(0, 0));
UI_Push(Tint, 0);
/* Start */
if (has_start_padding)
{
@ -376,7 +374,6 @@ UI_Box *UI_BuildPadAlongAxis(UI_Box *parent, UI_Size start, UI_Size end, Axis fi
UI_BuildBox(Zstr);
}
}
UI_PopCP();
}
UI_PopStack();
}
@ -484,6 +481,7 @@ UI_Box *UI_BuildBox(String seed)
box->rounding = UI_UseTop(Rounding);
box->text = UI_UseTop(Text);
box->floating_pos = UI_UseTop(FloatingPos);
box->text_alignment = UI_UseTop(TextAlignment);
/* Prefetch font */
if (box->text.len > 0)
@ -1104,12 +1102,6 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
Texture2DRid tex_rid = GPU_Texture2DRidFromResource(box->font->texture);
Vec2 inv_font_image_size = VEC2(1.0f / (f32)box->font->image_width, 1.0f / (f32)box->font->image_height);
f32 ascent = box->font->ascent;
f32 descent = box->font->descent;
Vec2 baseline = box->p0;
baseline.y += ascent;
F_Run run = box->glyph_run;
f32 max_baseline = box->p1.x;
b32 should_truncate = run.count > 0 && (run.rects[run.count - 1].pos + run.rects[run.count - 1].advance) > max_baseline;
@ -1152,6 +1144,42 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
run.rects = new_rects;
}
UI_Alignment alignment = box->text_alignment;
if (should_truncate)
{
alignment = UI_Alignment_Left;
}
/* Calculate baseline */
f32 ascent = box->font->ascent;
f32 descent = box->font->descent;
f32 baseline_width = run.count > 0 ? (run.rects[run.count - 1].pos + run.rects[run.count - 1].advance) : 0;
f32 baseline_height = ascent + descent;
f32 box_width = box->p1.x - box->p0.x;
f32 box_height = box->p1.y - box->p0.y;
Vec2 baseline = ZI;
switch (alignment)
{
case UI_Alignment_Left:
{
baseline.x = box->p0.x;
} break;
case UI_Alignment_Right:
{
baseline.x = box->p1.x;
baseline.x -= baseline_width;
} break;
case UI_Alignment_Center:
{
baseline.x = box->p0.x;
baseline.x += (box_width - baseline_width) / 2;
} break;
}
baseline.y = box->p0.y;
baseline.y += box_height / 2;
baseline.y += baseline_height / 4;
/* Push text rects */
for (u64 i = 0; i < run.count; ++i)
{

View File

@ -50,6 +50,16 @@ Struct(UI_Round)
f32 v;
};
////////////////////////////////////////////////////////////
//~ Alignment types
Enum(UI_Alignment)
{
UI_Alignment_Center,
UI_Alignment_Left,
UI_Alignment_Right,
};
////////////////////////////////////////////////////////////
//~ Padding types
@ -102,6 +112,7 @@ Enum(UI_BoxFlag)
x(Font, ResourceKey) \
x(FontSize, u32) \
x(Text, String) \
x(TextAlignment, UI_Alignment) \
x(LeftPadding, UI_Size) \
x(RightPadding, UI_Size) \
x(TopPadding, UI_Size) \
@ -210,6 +221,7 @@ Struct(UI_Box)
f32 border;
Vec2 floating_pos;
String text;
UI_Alignment text_alignment;
ResourceKey font_resource;
f32 font_size;
Axis layout_axis;