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

+84 -19
View File
@@ -1,11 +1,16 @@
#pragma once
#include "Attributes.h"
#include "UI/Attributes.h"
#include "Containers/Array.h"
#include "MinimalEngine.h"
#include "Style/Style.h"
#include "UI/Style/Style.h"
#include "UI/Style/Theme.h"
#include <type_traits>
namespace Seele {
namespace UI {
DECLARE_REF(Element)
// a render quad. background quads use backgroundTexture/backgroundColor;
// text quads additionally carry a glyph (one UIRender per glyph, see Text::render)
struct UIRender {
Gfx::PTexture2D backgroundTexture;
Vector backgroundColor;
@@ -13,23 +18,75 @@ struct UIRender {
Vector2 dimensions;
uint32 z;
uint32 level;
PElement element; // back-ref for input hit-testing
// glyph-only (text): the glyph texture is backgroundTexture, this is the
// per-glyph rect in *atlas* space (0..1) so the pass can emit a glyph quad
bool isGlyph = false;
Vector2 uvOffset = Vector2(0);
Vector2 uvSize = Vector2(0);
};
DECLARE_REF(Element)
class Element {
public:
Element(Attributes attr, Array<Element*> children);
virtual ~Element();
void setParent(PElement p) { parent = p; }
// calculates the final style object taking into account inherited properties
virtual void applyStyle(Style parentStyle) = 0;
void calcStyle(Style parentStyle) {
applyStyle(parentStyle);
for (auto& child : children) {
child->calcStyle(style);
// the System walks the retained tree (style/layout/input/render) and needs to read
// the protected layout/children state; it is the only external consumer.
friend class System;
// children are raw, non-owning pointers (matches the pre-existing call sites, e.g.
// the InspectorView demo: new Div(Attr{}, { new A, new B })). the caller's `new`
// yields the pointer; ownership/lifetime for the demo is the caller's concern (the
// engine does not free them here).
explicit Element(Attributes attr) : attr(attr) {}
Element(Attributes attr, std::initializer_list<Element*> children_) : attr(attr) {
for (auto c : children_) {
if (c != nullptr) {
c->setParent(this);
children.add(c);
}
}
}
virtual ~Element();
void setParent(Element* p) { parent = p; }
// resolves the inherited style + this element's static template classes against the theme.
// only re-run for this element when style.dirty or the theme changed.
virtual void applyStyle(Style parentStyle, const Theme& theme) = 0;
void calcStyle(Style parentStyle, const Theme& theme) {
if (style.dirty || themeChanged) {
style.dirty = false;
themeChanged = false;
applyStyle(parentStyle, theme);
}
for (auto& child : children) {
child->calcStyle(style, theme);
}
}
// explicit mutation entry point: applies runtime overrides, then re-styles the dirty subtree
void markDirty() {
style.dirty = true;
if (parent) {
parent->markDirty();
}
}
// applies runtime style overrides (the data-driven mutation layer)
virtual void applyOverrides() {
// default: no-op; interactive elements override this to reflect hover/focus/etc.
}
// input; concrete signatures differ per element (see Button/Text),
// so the base takes a typed pointer
virtual bool onMouseEvent(MouseEvent* event) { (void)event; return false; }
virtual bool onKeyEvent(KeyEvent* event) { (void)event; return false; }
// interaction state hooks: the System calls these without knowing the concrete
// (templated) Button type. default no-ops; Button overrides. this keeps the
// input pipeline data-driven (on the Element base) and avoids dynamic_cast to a
// template type.
virtual void setHovered(bool hovered) { (void)hovered; }
virtual void setFocused(bool focused) { (void)focused; }
virtual bool isFocusable() const { return attr.focusable; }
// calculates the relative positions of the child elements for the applied layout style
virtual void layout(UVector2 parentSize) {
if (style.hidden) {
return;
}
if (style.maxWidthType == DimensionType::Auto) {
maxDimensions.x = (float)parentSize.x;
}
@@ -63,6 +120,9 @@ class Element {
Vector2 cursor = Vector2(style.paddingLeft, style.paddingTop);
uint32 lineHeight = 0;
for (auto& child : children) {
if (child->style.hidden) {
continue;
}
// inline elements flow left to right
if (child->style.outerDisplay == OuterDisplayType::Inline) {
// only static and relative elements actually reserve
@@ -110,15 +170,17 @@ class Element {
}
}
virtual Array<UIRender> render(Vector2 anchor, uint32 level) {
Array<UIRender> result = {
UIRender{
Array<UIRender> result;
if (!style.hidden && style.outerDisplay != OuterDisplayType::Hidden) {
result.add(UIRender{
.backgroundColor = style.backgroundColor,
.position = position + anchor,
.dimensions = dimensions,
.z = style.z,
.level = level,
},
};
.element = this,
});
}
for (auto& child : children) {
for (const auto& render : child->render(position + anchor, level+1)) {
result.add(render);
@@ -135,9 +197,12 @@ class Element {
protected:
Attributes attr;
PElement parent = nullptr;
Array<OElement> children;
Element* parent = nullptr;
// raw, non-owning (see the ctor comment); matches the pre-existing call sites
Array<Element*> children;
// set by the System when the theme changed; forces a one-time full re-style
bool themeChanged = false;
};
DEFINE_REF(Element)
} // namespace UI
} // namespace Seele
} // namespace Seele