Files
Seele/src/Engine/UI/Element/Text.h
T

106 lines
3.9 KiB
C++
Raw Normal View History

2025-01-08 19:15:12 +01:00
#pragma once
2026-10-13 21:49:28 +02:00
#include "Asset/AssetRegistry.h"
#include "Asset/FontAsset.h"
2025-01-08 19:15:12 +01:00
#include "UI/Element.h"
#include "UI/Style/Class.h"
2026-10-13 21:49:28 +02:00
#include <algorithm>
#include <string_view>
2025-01-08 19:15:12 +01:00
namespace Seele {
namespace UI {
2026-10-13 21:49:28 +02:00
// text: the string is data (mutable at runtime); fonts and sizing come from style
// classes. shaping happens in applyStyle (only when dirty); render() just emits quads.
template<StyleClass... classes>
class Text : public Element {
2025-01-08 19:15:12 +01:00
public:
2026-10-13 21:49:28 +02:00
// the string is data; Attributes is optional (defaults to no handlers).
// e.g. new Text<Font_Arial, Text_8XL>("hello") or new Text<>(Attributes{...}, "hi")
Text(std::string text, Attributes attr = Attributes{}) : Element(std::move(attr)), _text(std::move(text)) {}
virtual ~Text() {}
// runtime mutation: change the string, invalidate style+layout of this subtree
void setText(std::string text) {
if (text == _text) {
return;
}
_text = std::move(text);
markDirty();
}
const std::string& getText() const { return _text; }
virtual void applyStyle(Style parentStyle, const Theme& theme) override {
2025-01-12 11:26:52 +01:00
style = parentStyle;
2026-10-13 21:49:28 +02:00
(classes::apply(style, theme), ...);
// resolve the font: a font class set style.fontName (a string literal can't
// be a template arg), so we look it up at style() time. invariant: the font
// must be imported before the first System::style() call (see System.h);
// fall back to the theme default if the name isn't found.
PFontAsset font = nullptr;
if (!style.fontName.empty()) {
try {
font = AssetRegistry::findFont("", std::string_view(style.fontName));
} catch (const std::exception&) {
font = nullptr; // unknown name -> fall back to the theme default
}
}
if (font == nullptr) {
font = theme.defaultFont;
}
if (font == nullptr) {
glyphs.clear();
return;
}
style.fontFamily = font;
UVector2 measured = font->shapeText(_text, (uint32)style.fontSize, glyphs);
// measure for auto sizing (shapeText already returns the bounds)
float w = (float)measured.x;
float h = (float)measured.y;
if (style.widthType == UI::DimensionType::Auto) {
style.width = w;
}
if (style.heightType == UI::DimensionType::Auto) {
style.height = h;
}
2025-01-12 11:26:52 +01:00
}
2026-10-13 21:49:28 +02:00
2025-01-12 11:26:52 +01:00
virtual Array<UIRender> render(Vector2 anchor, uint32 level) override {
2026-10-13 21:49:28 +02:00
Array<UIRender> result;
if (style.hidden || style.outerDisplay == OuterDisplayType::Hidden) {
return result;
}
Vector2 pos = position + anchor;
// background quad (if any) — same shape as the base element quad
result.add(UIRender{
.backgroundTexture = nullptr,
.backgroundColor = style.backgroundColor,
.position = pos,
.dimensions = dimensions,
.z = style.z,
.level = level,
.element = this,
});
2026-10-21 21:21:49 +02:00
// text: one quad per shaped glyph, carrying its rect in the font's per-size atlas
2026-10-13 21:49:28 +02:00
if (style.fontFamily != nullptr) {
for (auto& g : glyphs) {
result.add(UIRender{
.backgroundTexture = g.texture,
.backgroundColor = style.textColor,
.position = pos + Vector2(g.position.x, g.position.y),
.dimensions = Vector2(g.dimensions.x, g.dimensions.y),
.z = style.z,
.level = level,
.element = this,
2026-10-21 21:21:49 +02:00
.uvOffset = g.uvOffset,
.uvSize = g.uvSize,
2026-10-13 21:49:28 +02:00
});
}
}
return result;
2025-01-12 11:26:52 +01:00
}
2025-01-08 19:15:12 +01:00
private:
2026-10-13 21:49:28 +02:00
std::string _text;
Array<FontAsset::RenderGlyph> glyphs;
2025-01-08 19:15:12 +01:00
};
} // namespace UI
2026-10-13 21:49:28 +02:00
} // namespace Seele