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/core/api.h"
5#include "flint/graphics/types/color.h"
6#include "flint/math/types.h"
7#include "flint/ui/ui-event.h"
8#include "flint/ui/ui-types.h"
9
10#include <memory>
11#include <string>
12#include <vector>
13
14namespace Flint {
15 class UIContext;
16 class UICanvas;
17
31 class UINode {
32 public:
33 virtual ~UINode() = default;
34
41 FLINT_API void setContext(UIContext *context);
42
48 FLINT_API void addChild(std::unique_ptr<UINode> child);
49
51 FLINT_API void setPosition(Vec2f position);
52
55 return m_localPosition;
56 }
57
59 FLINT_API void setSize(Vec2f size);
60
62 Vec2f getSize() const {
63 return m_baseSize;
64 }
65
70 FLINT_API void setVisible(bool visible);
71
75 FLINT_API void setEnabled(bool enabled);
76
84 FLINT_API void setID(std::string id);
86 [[nodiscard]] FLINT_API const std::string &getId() const;
87
89 [[nodiscard]] FLINT_API const Rect<float> &getLayoutRect() const;
90
91 [[nodiscard]] bool isVisible() const {
92 return m_visible;
93 }
94
95 [[nodiscard]] bool isEnabled() const {
96 return m_enabled;
97 }
98
100 [[nodiscard]] Vec2f getDesiredSize() const {
101 return m_desiredSize;
102 }
103
105 [[nodiscard]] const std::vector<std::unique_ptr<UINode>> &getChildren() const {
106 return m_children;
107 }
108
110 virtual void setFocused(bool focused) {
111 m_focused = focused;
112 }
113
115 FLINT_API void setText(std::string text);
117 [[nodiscard]] FLINT_API const std::string &getText() const;
118
120 FLINT_API void setFontSize(uint16_t fontSize);
121 [[nodiscard]] FLINT_API uint16_t getFontSize() const;
122
124 FLINT_API void setColor(Color color);
125 [[nodiscard]] FLINT_API Color getColor() const;
126
130 virtual void setNormalColor(Color color) {
131 m_normalColor = color;
132 }
133
136 virtual void setActiveColor(Color color) {
137 m_activeColor = color;
138 }
139
146 virtual FLINT_API void measure(UIConstraints constraints);
147
154 virtual FLINT_API void arrange(const Rect<float> &rect);
155
158 virtual FLINT_API void handleEvent(const UIEvent &event);
159
162 virtual FLINT_API void render(UICanvas &canvas) const;
163
166 virtual bool isFocusable() const {
167 return false;
168 }
169
172 virtual FLINT_API bool hitTest(Vec2f point) const;
173
176 void setFont(std::string name) {
177 m_fontName = std::move(name);
178 }
179
182 void setFontWeight(uint16_t weight) {
183 m_fontWeight = weight;
184 }
185
186 void setTextOpticalBias(float bias) {
187 m_textOpticalBias = bias;
188 }
189
190 [[nodiscard]] float getTextOpticalBias() const {
191 return m_textOpticalBias;
192 }
193
199 virtual bool consumesDirectionalInput() const {
200 return false;
201 }
202
203 [[nodiscard]] bool isFocused() const {
204 return m_focused;
205 }
206
207 FLINT_API void setHorizontalAlign(UIAlign align);
208 FLINT_API void setVerticalAlign(UIAlign align);
209
210 [[nodiscard]] FLINT_API UIAlign getHorizontalAlign() const;
211 [[nodiscard]] FLINT_API UIAlign getVerticalAlign() const;
212
215 FLINT_API void setLayoutGrow(float grow);
217 [[nodiscard]] FLINT_API float getLayoutGrow() const;
218
219 protected:
220 std::string m_id;
221 UINode *m_parent = nullptr;
222 std::vector<std::unique_ptr<UINode>> m_children;
223
224 Vec2f
231 bool m_visible = true;
232 bool m_enabled = true;
233 float m_displayScale = 1.0f;
234
235 std::string m_fontName;
236 uint16_t m_fontWeight = 400;
237 uint16_t m_fontSize = 16;
238 float m_textOpticalBias = 0.f;
239
240 std::string m_text;
241
242 Color m_normalColor = Colors::white;
243 Color m_activeColor = {0, 255, 0, 255};
244
247 virtual const Color &currentColor() const {
248 return (m_hovered || m_focused) ? m_activeColor : m_normalColor;
249 }
250
251 bool m_hovered = false;
252 bool m_focused = false;
253
254 UIAlign m_horizontalAlign = UIAlign::Stretch;
255 UIAlign m_verticalAlign = UIAlign::Start;
256 float m_layoutGrow = 0.f;
257
258 private:
259 UIContext *m_context = nullptr;
260
264 bool idExists(const std::string &id) const;
265 };
266}
Dynamic 2D batching engine processing shape vector geometry and hardware-accelerated text strings....
Definition ui-canvas.h:107
Manages an isolated user interface canvas workspace sandbox, hosting its own independent node tree an...
Definition ui-context.h:28
Abstract base class for all UI widgets, managing hierarchy tracking, layout passes,...
Definition ui-node.h:31
void setFontWeight(uint16_t weight)
Assigns thickness parameters across structural type vectors (e.g., 400 for regular,...
Definition ui-node.h:182
Vec2f getDesiredSize() const
Get the node's requested size from the measure pass.
Definition ui-node.h:100
const std::vector< std::unique_ptr< UINode > > & getChildren() const
Get the list of this node's child nodes.
Definition ui-node.h:105
FLINT_API const std::string & getText() const
Gets the current text content of the widget.
FLINT_API void setLayoutGrow(float grow)
Assigns dynamic expansion multipliers used to balance alignment distributions inside sequential layou...
virtual FLINT_API bool hitTest(Vec2f point) const
Computes true if a vector coordinate sits inside the spatial arrangement bounding tracking layout box...
FLINT_API void setFontSize(uint16_t fontSize)
Set the rendering font size in points.
Vec2f m_localPosition
Definition ui-node.h:225
virtual void setNormalColor(Color color)
Sets the base color of the widget.
Definition ui-node.h:130
virtual FLINT_API void measure(UIConstraints constraints)
Pass 1 of the layout engine: Crawls branches recursively to compute base display boundaries requested...
virtual void setActiveColor(Color color)
Sets visual colors used when inputs hover across or lock selections on the widget container.
Definition ui-node.h:136
virtual const Color & currentColor() const
Dynamically picks active color metrics using real-time interaction states.
Definition ui-node.h:247
Vec2f m_baseSize
Definition ui-node.h:227
virtual void setFocused(bool focused)
Sets whether this node is currently focused.
Definition ui-node.h:110
FLINT_API void setText(std::string text)
Sets the text content for widgets that display text.
FLINT_API void setContext(UIContext *context)
Recursively propagates a pointer back to the parent-managing workspace layer.
Rect< float > m_layoutRect
Definition ui-node.h:229
Vec2f getPosition() const
Get the node's relative local position.
Definition ui-node.h:54
Vec2f getSize() const
Get the node's base size.
Definition ui-node.h:62
FLINT_API void setID(std::string id)
Sets a unique string identifier for this node.
Vec2f m_desiredSize
Definition ui-node.h:226
virtual FLINT_API void render(UICanvas &canvas) const
Pushes current styling rules and geometries straight down to the active pipeline vertex drawing queue...
FLINT_API void setSize(Vec2f size)
Assigns a manual static override layout budget footprint dimensions limit.
virtual FLINT_API void arrange(const Rect< float > &rect)
Pass 2 of the layout engine: Evaluates layout adjustments to pin final screen coordinate values to th...
FLINT_API const std::string & getId() const
Gets the unique lookup ID string of this node.
void setFont(std::string name)
Assigns a custom typography description token mapping back to cached font elements.
Definition ui-node.h:176
FLINT_API void setVisible(bool visible)
Sets widget visibility.
virtual bool isFocusable() const
Dictates if keyboard selection trackers or gamepad vectors can snap control focuses onto this item.
Definition ui-node.h:166
FLINT_API float getLayoutGrow() const
Gets the dynamic expansion weight used inside stack layout grids.
FLINT_API void addChild(std::unique_ptr< UINode > child)
Inserts a child widget to live inside this layout element container boundary.
virtual FLINT_API void handleEvent(const UIEvent &event)
Traverses active child collections to pass translated physical hardware inputs down to focused widget...
FLINT_API void setEnabled(bool enabled)
Enables or disables user interaction for this widget.
virtual bool consumesDirectionalInput() const
Dictates whether this widget captures directional inputs instead of passing them to focus navigation.
Definition ui-node.h:199
FLINT_API void setPosition(Vec2f position)
Updates the relative local rendering position coordinates of this element block.
FLINT_API const Rect< float > & getLayoutRect() const
Gets the final screen-space bounding box calculated during the arrange pass.
FLINT_API void setColor(Color color)
Sets the base color 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