ugui.c3l/src/ugui_text.c3

84 lines
1.8 KiB
Plaintext
Raw Normal View History

2024-12-11 01:14:14 +01:00
module ugui;
import std::io;
fn void! Ctx.text_unbounded(&ctx, String label, String text)
{
Id id = label.hash();
Elem *parent = ctx.get_parent()!;
Elem *c_elem = ctx.get_elem(id)!;
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
// 1. Fill the element fields
// this resets the flags
c_elem.type = ETYPE_TEXT;
2024-12-11 20:39:06 +01:00
short line_height = (short)ctx.font.ascender - (short)ctx.font.descender;
short baseline = (short)ctx.font.ascender;
2024-12-11 01:14:14 +01:00
bool update_atlas;
// if the element is new or the parent was updated then redo layout
if (c_elem.flags.is_new || parent.flags.updated) {
Rect text_size;
Glyph* gp;
// FIXME: newlines are not counted
foreach (c: text) {
Codepoint cp = (Codepoint)c;
bool n;
gp = ctx.font.get_glyph(cp, &n)!;
2024-12-11 20:39:06 +01:00
text_size.w += gp.adv;
text_size.h += line_height;
2024-12-11 01:14:14 +01:00
if (n) { update_atlas = true; }
}
// 2. Layout
c_elem.bounds = ctx.position_element(parent, text_size, true);
// 3. Fill the button specific fields
c_elem.text.str = text;
}
if (update_atlas) {
// FIXME: atlas here is hardcoded, look at the todo in ugui_data
Cmd up = {
.type = CMD_UPDATE_ATLAS,
.update_atlas = {
.raw_buffer = ctx.font.atlas[0].buffer,
.width = ctx.font.atlas[0].width,
.height = ctx.font.atlas[0].height,
},
};
ctx.cmd_queue.enqueue(&up)!;
}
Point orig = {
.x = c_elem.bounds.x,
.y = c_elem.bounds.y,
};
foreach (c: text) {
Glyph* gp;
Codepoint cp = (Codepoint)c;
gp = ctx.font.get_glyph(cp)!;
Cmd cmd = {
.type = CMD_SPRITE,
.sprite.rect = {
.x = orig.x + gp.ox,
2024-12-11 20:39:06 +01:00
.y = orig.y + gp.oy + baseline,
2024-12-11 01:14:14 +01:00
.w = gp.w,
.h = gp.h,
},
.sprite.texture_rect = {
.x = gp.u,
.y = gp.v,
.w = gp.w,
.h = gp.h,
},
};
2024-12-11 20:39:06 +01:00
orig.x += gp.adv;
2024-12-11 01:14:14 +01:00
ctx.cmd_queue.enqueue(&cmd)!;
}
}