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

+119
View File
@@ -0,0 +1,119 @@
#pragma once
#include "UI/Element/Div.h"
#include "UI/Style/Class.h"
namespace Seele {
namespace UI {
// an interactive block: hover/focus visuals are data (runtime state) toggled by
// the input pipeline, never template args. the user's classes set the *defaults*;
// the System layers the interaction state on top during style() (see System.h).
template<StyleClass... classes>
class Button : public Div<classes...> {
public:
// forward every Div construction form (no children / variadic / initializer list)
using Div<classes...>::Div;
// Button is a template with a DEPENDENT base (Div<classes...>), so unqualified
// access to inherited members is ill-formed; these using-declarations bring the
// specific members into Button's scope so the body below can use them.
using Div<classes...>::attr;
using Div<classes...>::style;
using Div<classes...>::dimensions;
using Div<classes...>::markDirty;
using Div<classes...>::setParent;
virtual ~Button() {}
virtual void applyStyle(Style parentStyle, const Theme& theme) override {
Div<classes...>::applyStyle(parentStyle, theme);
// hover/focus/pressed overrides are applied by the System right after,
// keyed off this state (data, not template args)
}
virtual bool onMouseEvent(MouseEvent* event) override {
switch (event->kind) {
case MouseEvent::Kind::Move:
if (!hovered) {
hovered = true;
if (attr.onHover) {
attr.onHover(*event);
}
}
break;
case MouseEvent::Kind::Down:
pressed = true;
break;
case MouseEvent::Kind::Up:
if (pressed && hovered) {
pressed = false;
if (attr.onClick) {
attr.onClick(*event);
}
} else {
pressed = false;
}
break;
case MouseEvent::Kind::Scroll:
if (attr.onScroll) {
attr.onScroll(*event);
}
break;
}
markDirty();
return true;
}
virtual bool onKeyEvent(KeyEvent* event) override {
if (event->action == InputAction::PRESS) {
switch (event->code) {
case KeyCode::KEY_SPACE:
case KeyCode::KEY_ENTER:
if (focused && attr.onClick) {
MouseEvent click;
click.kind = MouseEvent::Kind::Down;
click.position = Vector2(dimensions.x / 2, dimensions.y / 2);
attr.onClick(click);
}
return true;
default:
break;
}
}
if (attr.onKey) {
return attr.onKey(*event);
}
return false;
}
// overrides of the Element base virtuals: this is how the System drives hover/focus
// data-drivenly without dynamic_cast to a concrete template type
void setHovered(bool hovered) override {
if (this->hovered == hovered) {
return;
}
this->hovered = hovered;
if (!hovered && attr.onHoverLeave) {
MouseEvent leave;
leave.kind = MouseEvent::Kind::Move;
attr.onHoverLeave(leave);
}
markDirty();
}
void setFocused(bool focused) override {
if (this->focused == focused) {
return;
}
this->focused = focused;
if (focused && attr.onFocus) {
attr.onFocus();
} else if (!focused && attr.onBlur) {
attr.onBlur();
}
markDirty();
}
bool isFocusable() const override { return attr.focusable; }
bool isHovered() const { return hovered; }
bool isFocused() const { return focused; }
bool isPressed() const { return pressed; }
private:
bool hovered = false;
bool focused = false;
bool pressed = false;
};
} // namespace UI
} // namespace Seele
+15 -3
View File
@@ -1,4 +1,16 @@
target_sources(Engine
PRIVATE
Div.h
Text.h)
PRIVATE
Div.h
Text.h
Button.h
Panel.h
)
target_sources(Engine
PUBLIC FILE_SET HEADERS
FILES
Div.h
Text.h
Button.h
Panel.h
)
+6 -4
View File
@@ -7,14 +7,16 @@ namespace UI {
template<StyleClass... classes>
class Div : public Element {
public:
Div(Attributes attr, Array<Element*> children) : Element(attr, std::move(children)) { }
// forward every Element construction form (no children / variadic / initializer list)
using Element::Element;
Div() = delete; // force an explicit style/attrs choice, no accidental empty divs
virtual ~Div() {}
virtual void applyStyle(Style parentStyle) override {
virtual void applyStyle(Style parentStyle, const Theme& theme) override {
style = parentStyle;
style.outerDisplay = OuterDisplayType::Block;
(classes::apply(style), ...);
(classes::apply(style, theme), ...);
}
private:
};
} // namespace UI
} // namespace Seele
} // namespace Seele
+31
View File
@@ -0,0 +1,31 @@
#pragma once
#include "UI/Element/Div.h"
#include "UI/Style/Class.h"
namespace Seele {
namespace UI {
// a styled block container: defaults to the standard editor chrome
// (dark background, padding, text color); the user's template args override any of it
template<StyleClass... classes>
class Panel : public Div<classes...> {
public:
// forward every Div construction form (no children / variadic / initializer list)
using Div<classes...>::Div;
virtual ~Panel() {}
virtual void applyStyle(Style parentStyle, const Theme& theme) override {
// start from the panel chrome, then let the user's classes win
style = parentStyle;
style.outerDisplay = OuterDisplayType::Block;
style.backgroundColor = theme.color(ColorToken::Gray900);
style.textColor = theme.color(ColorToken::White);
uint32 pad = theme.spacing(8);
style.paddingTop = pad;
style.paddingBottom = pad;
style.paddingLeft = pad;
style.paddingRight = pad;
(classes::apply(style, theme), ...);
}
private:
};
} // namespace UI
} // namespace Seele
+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