Initial UI implementation by AI

This commit is contained in:
Dynamitos committed 2026-10-13 21:49:28 +02:00
1 parent f2a3f59ab1
commit 908b52441f
29 files changed
+884 -186

No files matched your search

+92 -22
View File
@@ -1,37 +1,107 @@
#pragma once
#include "Asset/AssetRegistry.h"
#include "Asset/FontAsset.h"
#include "UI/Element.h"
#include "UI/Style/Class.h"
#include <ranges>
#include <algorithm>
#include <string_view>
namespace Seele {
namespace UI {
template <StyleClass... classes> class Text : public Element {
// 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:
Text(std::string text) : Element({}, {}), text(text) {}
virtual void applyStyle(Style parentStyle) {
// 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;
style.outerDisplay = OuterDisplayType::Inline;
(classes::apply(style), ...);
}
virtual void layout(UVector2) override {
UVector2 cursor = UVector2(0);
dimensions = style.fontFamily->shapeText(text, style.fontSize, glyphRenders);
(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 {
return Array<UIRender>(std::from_range, glyphRenders | std::views::transform([=](const FontAsset::RenderGlyph& glyph) {
return UIRender{
.backgroundTexture = glyph.texture,
.position = Vector2(glyph.position) + anchor,
.dimensions = Vector2(glyph.dimensions),
.level = level,
};
}));
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 render quad per shaped glyph, carrying its atlas rect
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,
.isGlyph = true,
.uvOffset = Vector2(0),
.uvSize = Vector2(1),
});
}
}
return result;
}
// height = fontsize * 1.12
private:
std::string text;
Array<FontAsset::RenderGlyph> glyphRenders;
std::string _text;
Array<FontAsset::RenderGlyph> glyphs;
};
} // namespace UI
} // namespace Seele
} // namespace Seele