Skip to main content

UiTextComponent.ixx File

Component for displaying formatted text in the UI. More...

Included Headers

#include <cassert> #include <format> #include <memory> #include <string> #include <helios.engine.modules.rendering.renderable.components.RenderableComponent> #include <helios.engine.ecs.GameObject> #include <helios.core.units.Unit> #include <helios.math.types> #include <helios.rendering.text.TextRenderable>

Namespaces Index

namespacehelios
namespaceengine

Main engine module aggregating core infrastructure and game systems. More...

namespacemodules

Domain-specific components and systems. More...

namespaceui

User interface components and systems for game entities. More...

namespacewidgets

UI widget components and systems. More...

namespacecomponents

UI widget state components. More...

Classes Index

classUiTextComponent

Component for displaying formatted text in the UI. More...

Description

Component for displaying formatted text in the UI.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file UiTextComponent.ixx
3 * @brief Component for displaying formatted text in the UI.
4 */
5module;
6
7#include <cassert>
8#include <format>
9#include <memory>
10#include <string>
11
12
13export module helios.engine.modules.ui.widgets.components.UiTextComponent;
14
15import helios.rendering.text.TextRenderable;
16import helios.math.types;
17import helios.core.units.Unit;
18
19
20import helios.engine.ecs.GameObject;
21
22import helios.engine.modules.rendering.renderable.components.RenderableComponent;
23
24
26
27
28/**
29 * @brief Component for displaying formatted text in the UI.
30 *
31 * Wraps a TextRenderable and provides template-based value formatting.
32 * Supports dirty tracking to minimize rendering updates.
33 */
35
36 protected:
37
38 /**
39 * @brief Pointer to the underlying text renderable. Not owned.
40 */
42
43 /**
44 * @brief The current text content.
45 */
46 std::string text_;
47
48 /**
49 * @brief True if the text content needs to be updated.
50 */
51 bool isDirty_ = true;
52
53 /**
54 * @brief True if the AABB bounds need to be recalculated.
55 */
56 bool needsResize_ = true;
57
58 /**
59 * @brief Whether this component is enabled.
60 */
61 bool isEnabled_ = true;
62
63
64 public:
65
66 /**
67 * @brief Checks whether this component is enabled.
68 *
69 * @return True if enabled, false otherwise.
70 */
71 [[nodiscard]] bool isEnabled() const noexcept {
72 return isEnabled_;
73 }
74
75 /**
76 * @brief Enables this component.
77 */
78 void enable() noexcept {
79 isEnabled_ = true;
80 }
81
82 /**
83 * @brief Disables this component.
84 */
85 void disable() noexcept {
86 isEnabled_ = false;
87 }
88
89 /**
90 * @brief Constructs a UiTextComponent with the given renderable.
91 *
92 * @param renderable Pointer to the TextRenderable. Must not be nullptr.
93 */
96
97 assert(renderable_ != nullptr && "TextRenderable must not be nullptr");
98 }
99
100 /**
101 * @brief Copy constructor.
102 *
103 * @param other The component to copy from.
104 */
106 assert(renderable_ != nullptr && "TextRenderable must not be nullptr");
107 }
108
110 UiTextComponent(UiTextComponent&&) noexcept = default;
111 UiTextComponent& operator=(UiTextComponent&&) noexcept = default;
112
113 /**
114 * @brief Returns the underlying text renderable as const pointer.
115 *
116 * @return Const pointer to the TextRenderable. Never nullptr.
117 */
118 [[nodiscard]] const helios::rendering::text::TextRenderable* renderable() const noexcept {
119 return renderable_;
120 }
121
122 /**
123 * @brief Returns the underlying text renderable as a pointer.
124 *
125 * @return Pointer to the TextRenderable. Never nullptr.
126 */
128 return renderable_;
129 }
130
131 /**
132 * @brief Resets the component to its initial state.
133 */
134 void reset() noexcept {
135 isDirty_ = true;
136 }
137
138 /**
139 * @brief Called when the component is acquired from an object pool.
140 *
141 * Resets the component to its initial dirty state.
142 */
143 void onAcquire() noexcept {
144 reset();
145 }
146
147 /**
148 * @brief Called when the component is released back to an object pool.
149 *
150 * Resets the component to its initial dirty state.
151 */
152 void onRelease() noexcept {
153 reset();
154 }
155
156 /**
157 * @brief Updates the text content if dirty.
158 *
159 * Applies the template to the current value and updates the renderable.
160 */
161 void update() {
162
163 if (!isDirty_) {
164 return;
165 }
166
167 renderable_->setText(text_);
168
169 needsResize_ = true;
170 isDirty_ = false;
171 }
172
173 /**
174 * @brief Sets the text content and immediately updates the renderable.
175 *
176 * No-op if the new text is identical to the current content.
177 * Otherwise marks the component as dirty and calls update().
178 *
179 * @param text The new text content.
180 */
181 void setText(const std::string& text) {
182 if (text == text_) {
183 return;
184 }
185 text_ = text;
186 isDirty_ = true;
187 update();
188 }
189
190 /**
191 * @brief Sets the text content by move and immediately updates the renderable.
192 *
193 * @copydoc setText(const std::string&)
194 */
195 void setText(std::string&& text) {
196 if (text == text_) {
197 return;
198 }
199 text_ = std::move(text);
200 isDirty_ = true;
201 update();
202 }
203
204 /**
205 * @brief Checks if the AABB bounds need recalculation.
206 *
207 * @return True if a resize is needed, false otherwise.
208 */
209 [[nodiscard]] bool needsResize() const noexcept {
210 return needsResize_;
211 }
212
213 /**
214 * @brief Marks the resize as complete.
215 */
216 void resizeComplete() noexcept {
217 needsResize_ = false;
218 }
219
220 };
221
222}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.