Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
ui-node.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4#include "flint/graphics/types/color.h"
5#include "flint/math/types.h"
6#include "flint/ui/ui-event.h"
7#include "flint/ui/ui-types.h"
8
9#include <memory>
10#include <string>
11#include <vector>
12
13namespace Flint {
14 class UIContext;
15 class UICanvas;
16
30 class UINode {
31 public:
32 virtual ~UINode() = default;
33
40 void setContext(UIContext *context);
41
47 void addChild(std::unique_ptr<UINode> child);
48
50 void setPosition(Vec2f position);
51
54 return m_localPosition;
55 }
56
58 void setSize(Vec2f size);
59
61 Vec2f getSize() const {
62 return m_baseSize;
63 }
64
69 void setVisible(bool visible);
70
74 void setEnabled(bool enabled);
75
83 void setID(std::string id);
85 [[nodiscard]] const std::string &getId() const;
86
88 [[nodiscard]] const Rect<float> &getLayoutRect() const;
89
90 [[nodiscard]] bool isVisible() const {
91 return m_visible;
92 }
93
94 [[nodiscard]] bool isEnabled() const {
95 return m_enabled;
96 }
97
99 [[nodiscard]] Vec2f getDesiredSize() const {
100 return m_desiredSize;
101 }
102
104 [[nodiscard]] const std::vector<std::unique_ptr<UINode>> &getChildren() const {
105 return m_children;
106 }
107
109 virtual void setFocused(bool focused) {
110 m_focused = focused;
111 }
112
114 void setText(std::string text);
116 [[nodiscard]] const std::string &getText() const;
117
119 void setFontSize(uint16_t fontSize);
120 [[nodiscard]] uint16_t getFontSize() const;
121
123 void setColor(Color color);
124 [[nodiscard]] Color getColor() const;
125
129 virtual void setNormalColor(Color color) {
130 m_normalColor = color;
131 }
132
135 virtual void setActiveColor(Color color) {
136 m_activeColor = color;
137 }
138
145 virtual void measure(UIConstraints constraints);
146
153 virtual void arrange(const Rect<float> &rect);
154
157 virtual void handleEvent(const UIEvent &event);
158
161 virtual void render(UICanvas &canvas) const;
162
165 virtual bool isFocusable() const {
166 return false;
167 }
168
171 virtual bool hitTest(Vec2f point) const;
172
175 void setFont(std::string name) {
176 m_fontName = std::move(name);
177 }
178
181 void setFontWeight(uint16_t weight) {
182 m_fontWeight = weight;
183 }
184
185 void setTextOpticalBias(float bias) {
186 m_textOpticalBias = bias;
187 }
188
189 [[nodiscard]] float getTextOpticalBias() const {
190 return m_textOpticalBias;
191 }
192
198 virtual bool consumesDirectionalInput() const {
199 return false;
200 }
201
202 [[nodiscard]] bool isFocused() const {
203 return m_focused;
204 }
205
206 void setHorizontalAlign(UIAlign align);
207 void setVerticalAlign(UIAlign align);
208
209 [[nodiscard]] UIAlign getHorizontalAlign() const;
210 [[nodiscard]] UIAlign getVerticalAlign() const;
211
214 void setLayoutGrow(float grow);
216 [[nodiscard]] float getLayoutGrow() const;
217
218 protected:
219 std::string m_id;
220 UINode *m_parent = nullptr;
221 std::vector<std::unique_ptr<UINode>> m_children;
222
223 Vec2f
230 bool m_visible = true;
231 bool m_enabled = true;
232 float m_displayScale = 1.0f;
233
234 std::string m_fontName;
235 uint16_t m_fontWeight = 400;
236 uint16_t m_fontSize = 16;
237 float m_textOpticalBias = 0.f;
238
239 std::string m_text;
240
241 Color m_normalColor = Colors::white;
242 Color m_activeColor = {0, 255, 0, 255};
243
246 virtual const Color &currentColor() const {
247 return (m_hovered || m_focused) ? m_activeColor : m_normalColor;
248 }
249
250 bool m_hovered = false;
251 bool m_focused = false;
252
253 UIAlign m_horizontalAlign = UIAlign::Stretch;
254 UIAlign m_verticalAlign = UIAlign::Start;
255 float m_layoutGrow = 0.f;
256
257 private:
258 UIContext *m_context = nullptr;
259
263 bool idExists(const std::string &id) const;
264 };
265}
Dynamic 2D batching engine processing shape vector geometry and hardware-accelerated text strings....
Definition ui-canvas.h:106
Manages an isolated user interface canvas workspace sandbox, hosting its own independent node tree an...
Definition ui-context.h:27
void setID(std::string id)
Sets a unique string identifier for this node.
void setContext(UIContext *context)
Recursively propagates a pointer back to the parent-managing workspace layer.
void setFontWeight(uint16_t weight)
Assigns thickness parameters across structural type vectors (e.g., 400 for regular,...
Definition ui-node.h:181
Vec2f getDesiredSize() const
Get the node's requested size from the measure pass.
Definition ui-node.h:99
const std::vector< std::unique_ptr< UINode > > & getChildren() const
Get the list of this node's child nodes.
Definition ui-node.h:104
virtual void render(UICanvas &canvas) const
Pushes current styling rules and geometries straight down to the active pipeline vertex drawing queue...
void setEnabled(bool enabled)
Enables or disables user interaction for this widget.
Vec2f m_localPosition
Definition ui-node.h:224
virtual void setNormalColor(Color color)
Sets the base color of the widget.
Definition ui-node.h:129
void setVisible(bool visible)
Sets widget visibility.
virtual void setActiveColor(Color color)
Sets visual colors used when inputs hover across or lock selections on the widget container.
Definition ui-node.h:135
virtual const Color & currentColor() const
Dynamically picks active color metrics using real-time interaction states.
Definition ui-node.h:246
Vec2f m_baseSize
Definition ui-node.h:226
virtual void setFocused(bool focused)
Sets whether this node is currently focused.
Definition ui-node.h:109
Rect< float > m_layoutRect
Definition ui-node.h:228
Vec2f getPosition() const
Get the node's relative local position.
Definition ui-node.h:53
Vec2f getSize() const
Get the node's base size.
Definition ui-node.h:61
Vec2f m_desiredSize
Definition ui-node.h:225
void setFont(std::string name)
Assigns a custom typography description token mapping back to cached font elements.
Definition ui-node.h:175
virtual bool isFocusable() const
Dictates if keyboard selection trackers or gamepad vectors can snap control focuses onto this item.
Definition ui-node.h:165
virtual void handleEvent(const UIEvent &event)
Traverses active child collections to pass translated physical hardware inputs down to focused widget...
virtual void arrange(const Rect< float > &rect)
Pass 2 of the layout engine: Evaluates layout adjustments to pin final screen coordinate values to th...
virtual bool consumesDirectionalInput() const
Dictates whether this widget captures directional inputs instead of passing them to focus navigation.
Definition ui-node.h:198
void addChild(std::unique_ptr< UINode > child)
Inserts a child widget to live inside this layout element container boundary.
virtual bool hitTest(Vec2f point) const
Computes true if a vector coordinate sits inside the spatial arrangement bounding tracking layout box...
virtual void measure(UIConstraints constraints)
Pass 1 of the layout engine: Crawls branches recursively to compute base display boundaries requested...
Abstract base class for all UI widgets, managing hierarchy tracking, layout passes,...
Definition ui-node.h:30
void setSize(Vec2f size)
Assigns a manual static override layout budget footprint dimensions limit.
const std::string & getId() const
Gets the unique lookup ID string of this node.
void setFontSize(uint16_t fontSize)
Set the rendering font size in points.
void setColor(Color color)
Sets the base color of the widget.
float getLayoutGrow() const
Gets the dynamic expansion weight used inside stack layout grids.
void setText(std::string text)
Sets the text content for widgets that display text.
void setPosition(Vec2f position)
Updates the relative local rendering position coordinates of this element block.
void setLayoutGrow(float grow)
Assigns dynamic expansion multipliers used to balance alignment distributions inside sequential layou...
const Rect< float > & getLayoutRect() const
Gets the final screen-space bounding box calculated during the arrange pass.
const std::string & getText() const
Gets the current text content of the widget.
RGBA color with 8-bit unsigned components.
Definition color.h:13
Generic axis-aligned rectangle defined by a position and size.
Definition types.h:87
Represents the boundary budget passed down the node tree during layout allocation.
Definition ui-types.h:76
A normalized data packet containing translated input data routed down the UI node tree.
Definition ui-event.h:51