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

107 lines
3.9 KiB
C++

#pragma once
#include "Asset/AssetRegistry.h"
#include "Asset/FontAsset.h"
#include "UI/Element.h"
#include "UI/Style/Class.h"
#include <algorithm>
#include <string_view>
namespace Seele {
namespace UI {
// 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 {
public:
// 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 {
style = parentStyle;
(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;
}
}
virtual Array<UIRender> render(Vector2 anchor, uint32 level) override {
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,
});
// text: one quad per shaped glyph, carrying its rect in the font's per-size atlas
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,
.uvOffset = g.uvOffset,
.uvSize = g.uvSize,
});
}
}
return result;
}
private:
std::string _text;
Array<FontAsset::RenderGlyph> glyphs;
};
} // namespace UI
} // namespace Seele