Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
UiFactory.cpp
Go to the documentation of this file.
1
8#include "UI/UiFactory.hpp"
9
10namespace rtp::client {
11 namespace graphics {
12
14 // Public API
16
18 ecs::Registry& registry,
19 const position& position,
20 const size& size,
21 const std::string& label,
22 std::function<void()> onClick)
23 {
24 auto entityRes = registry.spawn();
25 if (!entityRes) {
26 log::error("Failed to spawn button entity: {}", entityRes.error().message());
27 throw std::runtime_error(std::string("Failed to spawn button entity: ") + std::string(entityRes.error().message()));
28 }
29
30 ecs::Entity entity = entityRes.value();
31
33 button.position = { position.x, position.y };
34 button.size = { size.width, size.height };
35 button.text = label;
36 button.onClick = onClick;
37
38 registry.add<ecs::components::ui::Button>(entity, button);
39
40 return entity;
41 }
42
44 ecs::Registry& registry,
45 const position& position,
46 const std::string& content,
47 const std::string& fontPath,
48 unsigned int fontSize,
49 const std::uint8_t zIndex,
50 const color& textColor)
51 {
52 auto entityRes = registry.spawn();
53 if (!entityRes) {
54 log::error("Failed to spawn text entity: {}", entityRes.error().message());
55 throw std::runtime_error(std::string("Failed to spawn text entity: ") + std::string(entityRes.error().message()));
56 }
57
58 ecs::Entity entity = entityRes.value();
59
61 text.position = { position.x, position.y };
62 text.content = content;
63 text.fontPath = fontPath;
64 text.fontSize = fontSize;
65 text.zIndex = zIndex;
66 text.red = textColor.r;
67 text.green = textColor.g;
68 text.blue = textColor.b;
69
70 registry.add<ecs::components::ui::Text>(entity, text);
71
72 return entity;
73 }
74
76 ecs::Registry& registry,
77 const position& position,
78 const size& size,
79 float minValue,
80 float maxValue,
81 float initialValue,
82 std::function<void(float)> onChange)
83 {
84 auto entityRes = registry.spawn();
85 if (!entityRes) {
86 log::error("Failed to spawn slider entity: {}", entityRes.error().message());
87 throw std::runtime_error(std::string("Failed to spawn slider entity: ") + std::string(entityRes.error().message()));
88 }
89
90 ecs::Entity entity = entityRes.value();
91
93
94 slider.position = { position.x, position.y };
95 slider.size = { size.width, size.height };
96 slider.minValue = minValue;
97 slider.maxValue = maxValue;
98 slider.currentValue = initialValue;
99 slider.onChange = onChange;
100
101 registry.add<ecs::components::ui::Slider>(entity, slider);
102
103 return entity;
104 }
105
107 ecs::Registry& registry,
108 const position& position,
109 const size& size,
110 const std::vector<std::string>& options,
111 const int selectedIndex,
112 std::function<void(int index)> onSelect)
113 {
114 auto entityRes = registry.spawn();
115 if (!entityRes) {
116 log::error("Failed to spawn dropdown entity: {}", entityRes.error().message());
117 throw std::runtime_error(std::string("Failed to spawn dropdown entity: ") + std::string(entityRes.error().message()));
118 }
119
120 ecs::Entity entity = entityRes.value();
121
123
124 dropdown.position = { position.x, position.y - 5.0f };
125 dropdown.size = { size.width, size.height };
126 dropdown.options = options;
127 dropdown.selectedIndex = selectedIndex;
128 dropdown.onSelect = [options, onSelect](int index) {
129 if (index < 0 || index >= static_cast<int>(options.size())) {
130 log::warning("Dropdown selection index {} is out of bounds", index);
131 return;
132 }
133 if (onSelect) {
134 onSelect(index);
135 }
136 };
137
138 registry.add<ecs::components::ui::Dropdown>(entity, dropdown);
139 return entity;
140 }
141
143 ecs::Registry& registry,
144 const position& position,
145 const size& size,
146 const std::string& fontPath,
147 unsigned int fontSize,
148 const int maxLength,
149 const std::string& placeholder,
150 std::function<void(const std::string&)> onSubmit,
151 std::function<void(const std::string&)> onChange)
152 {
153 auto entityRes = registry.spawn();
154 if (!entityRes) {
155 log::error("Failed to spawn text input entity: {}", entityRes.error().message());
156 throw std::runtime_error(std::string("Failed to spawn text input entity: ") + std::string(entityRes.error().message()));
157 }
158
159 ecs::Entity entity = entityRes.value();
160
162
163 textInput.position = { position.x, position.y };
164 textInput.size = { size.width, size.height };
165 textInput.fontPath = fontPath;
166 textInput.fontSize = fontSize;
167 textInput.maxLength = maxLength;
168 textInput.placeholder = placeholder;
169 textInput.onSubmit = onSubmit;
170 textInput.onChange = onChange;
171
172 registry.add<ecs::components::ui::TextInput>(entity, textInput);
173
174 return entity;
175 }
176
178 ecs::Registry& registry,
179 const position& position,
180 const std::string& texturePath,
181 int rectLeft,
182 int rectTop,
183 int rectWidth,
184 int rectHeight,
185 float scale,
186 int zIndex)
187 {
188 auto entityRes = registry.spawn();
189 if (!entityRes) {
190 log::error("Failed to spawn sprite preview entity: {}", entityRes.error().message());
191 throw std::runtime_error(std::string("Failed to spawn sprite preview entity: ") + std::string(entityRes.error().message()));
192 }
193
194 ecs::Entity entity = entityRes.value();
195
197 spritePreview.texturePath = texturePath;
198 spritePreview.x = position.x;
199 spritePreview.y = position.y;
200 spritePreview.scale = scale;
201 spritePreview.rectLeft = rectLeft;
202 spritePreview.rectTop = rectTop;
203 spritePreview.rectWidth = rectWidth;
204 spritePreview.rectHeight = rectHeight;
205 spritePreview.zIndex = zIndex;
206
207 registry.add<ecs::components::ui::SpritePreview>(entity, spritePreview);
208
209 return entity;
210 }
211 } // namespace graphics
212} // namespace rtp::client
static ecs::Entity createTextInput(ecs::Registry &registry, const position &position, const size &size, const std::string &fontPath, unsigned int fontSize, const int maxLength=64, const std::string &placeholder="", std::function< void(const std::string &)> onSubmit=nullptr, std::function< void(const std::string &)> onChange=nullptr)
static ecs::Entity createSpritePreview(ecs::Registry &registry, const position &position, const std::string &texturePath, int rectLeft, int rectTop, int rectWidth, int rectHeight, float scale=2.0f, int zIndex=0)
Create a sprite preview UI component.
static ecs::Entity createText(ecs::Registry &registry, const position &position, const std::string &content, const std::string &fontPath, unsigned int fontSize, const std::uint8_t zIndex=0, const color &textColor={255, 255, 255})
Definition UiFactory.cpp:43
static ecs::Entity createButton(ecs::Registry &registry, const position &position, const size &size, const std::string &label, std::function< void()> onClick=nullptr)
Create a button UI component.
Definition UiFactory.cpp:17
static ecs::Entity createDropdown(ecs::Registry &registry, const position &position, const size &size, const std::vector< std::string > &options, const int selectedIndex, std::function< void(int index)> onSelect=nullptr)
static ecs::Entity createSlider(ecs::Registry &registry, const position &position, const size &size, float minValue, float maxValue, float initialValue, std::function< void(float)> onChange=nullptr)
Definition UiFactory.cpp:75
Represents an entity in the ECS (Entity-Component-System) architecture.
Definition Entity.hpp:63
auto spawn(void) -> std::expected< Entity, rtp::Error >
Definition Registry.cpp:51
auto add(Entity entity, Args &&...args) -> std::expected< std::reference_wrapper< T >, rtp::Error >
R-Type client namespace.
void error(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an error message.
void warning(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a warning message.
Represents an RGB color.
Definition UiFactory.hpp:34
Represents the position of a UI component.
Definition UiFactory.hpp:45
Represents the size of a UI component.
Definition UiFactory.hpp:55
Component representing a clickable button.
Definition Button.hpp:30
std::string text
Text displayed on the button.
Definition Button.hpp:31
std::function< void()> onClick
Callback when clicked.
Definition Button.hpp:35
Vec2f position
Position of the button.
Definition Button.hpp:32
Vec2f size
Size of the button.
Definition Button.hpp:33
Component for dropdown menu selection.
Definition Dropdown.hpp:21
int selectedIndex
Currently selected option.
Definition Dropdown.hpp:25
Vec2f position
Position of the dropdown.
Definition Dropdown.hpp:22
std::function< void(int)> onSelect
Callback when option selected.
Definition Dropdown.hpp:26
std::vector< std::string > options
Available options.
Definition Dropdown.hpp:24
Vec2f size
Size of the dropdown button.
Definition Dropdown.hpp:23
Component for a draggable slider control.
Definition Slider.hpp:19
Vec2f position
Position of the slider.
Definition Slider.hpp:20
float currentValue
Current value.
Definition Slider.hpp:24
Vec2f size
Size of the slider.
Definition Slider.hpp:21
float minValue
Minimum value.
Definition Slider.hpp:22
float maxValue
Maximum value.
Definition Slider.hpp:23
std::function< void(float)> onChange
Callback when value changes.
Definition Slider.hpp:25
Component for displaying a sprite preview in the UI.
float x
X position for rendering.
int rectHeight
Height of sprite in texture.
float scale
Scale factor for display.
int rectTop
Top coordinate in texture.
int rectWidth
Width of sprite in texture.
std::string texturePath
Path to the texture file.
float y
Y position for rendering.
int rectLeft
Left coordinate in texture.
std::function< void(const std::string &)> onChange
Definition TextInput.hpp:35
std::function< void(const std::string &)> onSubmit
Definition TextInput.hpp:34
Component representing text to render.
Definition Text.hpp:19
uint8_t green
Green component.
Definition Text.hpp:25
int zIndex
Rendering order.
Definition Text.hpp:28
uint8_t blue
Blue component.
Definition Text.hpp:26
Vec2f position
Position of the text.
Definition Text.hpp:21
uint8_t red
Red component.
Definition Text.hpp:24
unsigned int fontSize
Font size.
Definition Text.hpp:23
std::string fontPath
Path to the font file.
Definition Text.hpp:22
std::string content
Text content.
Definition Text.hpp:20