#pragma once #include "UI/Attributes.h" #include "Containers/Array.h" #include "MinimalEngine.h" #include "UI/Style/Style.h" #include "UI/Style/Theme.h" #include 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; Vector2 position; 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); }; class Element { public: // 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 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; } if (style.maxHeightType == DimensionType::Auto) { maxDimensions.y = (float)parentSize.y; } if (style.widthType == DimensionType::Pixel) { dimensions.x = style.width; } else if (style.widthType == DimensionType::Percent) { dimensions.x = parentSize.x * style.width / 100.0f; } if (style.heightType == DimensionType::Pixel) { dimensions.y = style.height; } else if (style.heightType == DimensionType::Percent) { dimensions.y = parentSize.y * style.height / 100.0f; } for (auto& child : children) { child->layout(maxDimensions - Vector2(style.marginLeft + style.marginRight, style.marginTop + style.marginBottom)); } switch (style.innerDisplay) { case InnerDisplayType::Flow: flowLayout(); break; default:break; } } // normal flow of the elements, inline elements go left to right, blocks get their own new lines void flowLayout() { // use a cursor going from left to right, top to bottom, starting at padding 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 if (child->style.position == PositionType::Static || child->style.position == PositionType::Relative) { // create a new line in case the element doesnt fit on the current one anymore, // but keep in current line in case we are still at the start, as a new line wouldnt help here if (cursor.x + child->dimensions.x > maxDimensions.x - child->style.left && cursor.x != 0) { cursor.x = (float)style.paddingLeft; cursor.y += lineHeight; lineHeight = 0; } // todo: border child->position.x = cursor.x + child->style.marginLeft; child->position.y = cursor.y + child->style.marginTop; cursor.x = child->position.x + child->dimensions.x; // apply offsets after updating the cursor for relative elements if (child->style.position == PositionType::Relative) { child->position.x += child->style.left; child->position.y += child->style.top; } lineHeight = std::max(lineHeight, child->style.lineHeight); } // block elements always get their own lines, like paragraphs } else if (child->style.outerDisplay == OuterDisplayType::Block) { // static and relative require space to be created for the elements if (child->style.position == PositionType::Static || child->style.position == PositionType::Relative) { // create a new line in case we are not already at one if (cursor.x != 0) { cursor.x = (float)style.paddingLeft; cursor.y += lineHeight; } child->position.x = cursor.x + child->style.marginLeft; child->position.y = cursor.y + child->style.marginTop; // new line after block cursor.x = 0; cursor.y = child->position.y + child->dimensions.y; } } } if (style.widthType == DimensionType::Auto) { dimensions.x = cursor.x + style.paddingRight; } if (style.heightType == DimensionType::Auto) { dimensions.y = lineHeight + cursor.y + style.paddingBottom; } } virtual Array render(Vector2 anchor, uint32 level) { Array 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); } } return result; } Style style; Vector2 maxDimensions = Vector2(0); // calculated position relative to parent Vector2 position = Vector2(0); // calculated dimensions in pixels after layouting Vector2 dimensions = Vector2(0); protected: Attributes attr; Element* parent = nullptr; // raw, non-owning (see the ctor comment); matches the pre-existing call sites Array 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