Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
ui-context.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4
5#include "flint/core/api.h"
6#include "flint/graphics/gpu/gpu-texture.h"
7#include "flint/ui/ui-event.h"
8#include "flint/ui/ui-node.h"
9
10#include <memory>
11#include <string>
12#include <vector>
13
14namespace Flint {
15
16 class UICanvas;
17
28 class UIContext {
29 public:
31 FLINT_API explicit UIContext(std::string name);
32 FLINT_API ~UIContext();
33
34 UIContext(const UIContext &) = delete;
35 UIContext &operator=(const UIContext &) = delete;
36 UIContext(UIContext &&) = delete;
37 UIContext &operator=(UIContext &&) = delete;
38
41 [[nodiscard]] FLINT_API const std::string &getName() const;
42
52 FLINT_API void setRoot(std::unique_ptr<UINode> root);
53
55 [[nodiscard]] FLINT_API UINode *getRoot() const;
56
59 FLINT_API void update(float deltaTime);
60
70 FLINT_API void handleEvent(const UIEvent &event);
71
81 FLINT_API void render(UICanvas &canvas) const;
82
92 [[nodiscard]] FLINT_API UINode *findById(const std::string &id) const;
93
96 [[nodiscard]] FLINT_API UINode *getHoveredNode() const;
97
100 [[nodiscard]] FLINT_API UINode *getFocusedNode() const;
101
104 [[nodiscard]] FLINT_API UIInputMode getInputMode() const;
105
113 FLINT_API void setFocusedNode(UINode *node);
114
117 FLINT_API void clearFocus();
118
121 void setDisplayScale(float scale) {
122 if (m_displayScale != scale) {
123 m_displayScale = scale;
124 m_layoutDirty = true;
125 }
126 }
127
129 FLINT_API void setInputMode(UIInputMode mode);
130
133 [[nodiscard]] float getDisplayScale() const {
134 return m_displayScale;
135 }
136
137 void addTexture(std::shared_ptr<GPUTexture> tex) {
138 m_textures.push_back(std::move(tex));
139 }
140
141 std::unique_ptr<UINode> releaseRoot() {
142 m_hoveredNode = nullptr;
143 m_focusedNode = nullptr;
144 m_textures.clear();
145 return std::move(m_root);
146 }
147
148 private:
151 [[nodiscard]] UINode *findByIdRecursive(UINode *node, const std::string &id) const;
152
155 UINode *getNavigationStartNode(const std::vector<UINode *> &focusable) const;
156
163 UINode *findHitNode(UINode *node, Vec2f point);
164
165 std::string m_name;
166 std::unique_ptr<UINode> m_root;
167
168 mutable bool m_layoutDirty = true;
169 mutable Vec2f m_lastLayoutSize;
170
171 std::vector<std::shared_ptr<GPUTexture>> m_textures;
172
173 UINode *m_hoveredNode = nullptr;
174 UINode *m_focusedNode = nullptr;
175 float m_displayScale = 1.f;
176 UIInputMode m_inputMode = UIInputMode::Mouse;
177 };
178}
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
FLINT_API UIInputMode getInputMode() const
Extracts the persistent context tracking mode used to choose visual layouts and prompt glyph represen...
float getDisplayScale() const
Extracts the local scale coefficient factor used to translate base pixel assets for resolution alignm...
Definition ui-context.h:133
FLINT_API void update(float deltaTime)
Advances internal animation chronometers or custom ticking scripts linked across the canvas layer.
void setDisplayScale(float scale)
Sets a local resolution scale coefficient factor to properly offset layout metrics for DPI matching t...
Definition ui-context.h:121
FLINT_API UIContext(std::string name)
Instantiates a named user interface workspace canvas.
FLINT_API void setInputMode(UIInputMode mode)
Modifies the active user interaction mode tracker state context.
FLINT_API const std::string & getName() const
Returns the identifier lookup key string assigned to this workspace context layer.
FLINT_API void setRoot(std::unique_ptr< UINode > root)
Sets the root node of the UI hierarchy.
FLINT_API UINode * getHoveredNode() const
Returns a non-owning pointer to the interactive widget currently sitting directly beneath the mouse c...
FLINT_API void render(UICanvas &canvas) const
Intercepts semantic UI framework event packets to calculate canvas spatial routing updates.
FLINT_API UINode * getRoot() const
Extracts a non-owning pointer to the base root element layout tracking map.
FLINT_API void setFocusedNode(UINode *node)
Explicitly shifts directional focus priorities straight to a target element node.
FLINT_API UINode * findById(const std::string &id) const
Performs a linear tree-search traversal to locate a specific element matching the provided identifier...
FLINT_API void handleEvent(const UIEvent &event)
Intercepts semantic UI framework event packets to calculate canvas spatial routing updates.
FLINT_API UINode * getFocusedNode() const
Returns a non-owning pointer to the interactive widget currently holding active keyboard or gamepad d...
FLINT_API void clearFocus()
Strips focus flags cleanly away from all active tree elements, returning the canvas interaction state...
Abstract base class for all UI widgets, managing hierarchy tracking, layout passes,...
Definition ui-node.h:31
A normalized data packet containing translated input data routed down the UI node tree.
Definition ui-event.h:51