R-Type  2
Doom but in better
Loading...
Searching...
No Matches
ButtonComponent.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** ButtonComponent.cpp
6*/
7
15
17 : EntityNode(entityId), _callback(nullptr), _componentText(entityId), _componentShape(entityId)
18{
19 _collision = _componentShape.getCollision();
20 _textSize = _componentText.getSize();
21};
22
24 : EntityNode(0), _componentText(0), _componentShape(std::uint32_t(0))
25{
26 _componentText.update(textItem);
27 _componentShape.update(shapeItem);
28 _collision = _componentShape.getCollision();
29 _textSize = _componentText.getSize();
30};
31
33 : EntityNode(0), _componentText(0), _componentShape(std::uint32_t(0)), _callback(callback)
34{
35 _componentText.update(textItem);
36 _componentShape.update(shapeItem);
37 _collision = _componentShape.getCollision();
38 _textSize = _componentText.getSize();
39};
40
42 : EntityNode(0), _componentText(0), _componentShape(std::uint32_t(0))
43{
44 _componentText.update(textItem);
45 _componentShape.update(shapeItem);
46 setCollision(collisionItem, textSize);
47};
48
49GUI::ECS::Components::ButtonComponent::ButtonComponent(const GUI::ECS::Components::ShapeComponent &shapeItem, const GUI::ECS::Components::TextComponent &textItem, std::function<void()> callback, const GUI::ECS::Systems::Collision &collisionItem, const std::uint32_t textSize)
50 : EntityNode(0), _componentText(0), _componentShape(std::uint32_t(0)), _callback(callback)
51{
52 _componentText.update(textItem);
53 _componentShape.update(shapeItem);
54 setCollision(collisionItem, textSize);
55};
56
57
59 : EntityNode(entityId), _componentText(entityId), _componentShape(entityId)
60{
61 _componentText.update(textItem);
62 _componentShape.update(shapeItem);
63 _collision = _componentShape.getCollision();
64 _textSize = _componentText.getSize();
65};
66
67GUI::ECS::Components::ButtonComponent::ButtonComponent(const std::uint32_t entityId, const GUI::ECS::Components::ShapeComponent &shapeItem, const GUI::ECS::Components::TextComponent &textItem, std::function<void()> callback)
68 : EntityNode(entityId), _componentText(entityId), _componentShape(entityId), _callback(callback)
69{
70 _componentText.update(textItem);
71 _componentShape.update(shapeItem);
72 _collision = _componentShape.getCollision();
73 _textSize = _componentText.getSize();
74};
75
76GUI::ECS::Components::ButtonComponent::ButtonComponent(const std::uint32_t entityId, const GUI::ECS::Components::ShapeComponent &shapeItem, const GUI::ECS::Components::TextComponent &textItem, const GUI::ECS::Systems::Collision &collisionItem, const std::uint32_t textSize)
77 : EntityNode(entityId), _componentText(entityId), _componentShape(entityId)
78{
79 _componentText.update(textItem);
80 _componentShape.update(shapeItem);
81 setCollision(collisionItem, textSize);
82};
83
84GUI::ECS::Components::ButtonComponent::ButtonComponent(const std::uint32_t entityId, const GUI::ECS::Components::ShapeComponent &shapeItem, const GUI::ECS::Components::TextComponent &textItem, std::function<void()> callback, const GUI::ECS::Systems::Collision &collisionItem, const std::uint32_t textSize)
85 : EntityNode(entityId), _componentText(entityId), _componentShape(entityId), _callback(callback)
86{
87 _componentText.update(textItem);
88 _componentShape.update(shapeItem);
89 setCollision(collisionItem, textSize);
90};
91
93
95{
96 _componentShape.setHoverColor(hoverColor);
97}
98
100{
101 _componentShape.setNormalColor(normalColor);
102}
103
105{
106 _componentShape.setClickedColor(clickedColor);
107}
109{
110 _componentText.setHoverColor(hoverColor);
111}
112
114{
115 _componentText.setNormalColor(normalColor);
116}
117
119{
120 _componentText.setClickedColor(clickedColor);
121}
122
124{
125 _componentText.setText(text);
126}
127
128void GUI::ECS::Components::ButtonComponent::setPosition(const std::pair<float, float> &position)
129{
130 _componentText.setPosition(position);
131 _componentShape.setPosition(position);
132
133}
134
135void GUI::ECS::Components::ButtonComponent::setDimension(const std::pair<float, float> &dimension, const std::uint32_t textSize)
136{
137 _componentText.setSize(textSize);
138 _componentShape.setDimension(dimension);
139}
140
141void GUI::ECS::Components::ButtonComponent::setTextPosition(const std::pair<float, float> &position)
142{
143 _componentText.setPosition(position);
144
145}
146
148{
149 if (_textSize == textSize) {
150 return;
151 }
152 _componentText.setSize(textSize);
153 _textSize = textSize;
154}
155
156void GUI::ECS::Components::ButtonComponent::setShapePosition(const std::pair<float, float> &position)
157{
158 _componentShape.setPosition(position);
159}
160
161void GUI::ECS::Components::ButtonComponent::setShapeDimension(const std::pair<float, float> &dimension)
162{
163 _componentShape.setDimension(dimension);
164}
165
167{
168 setPosition(collision.getPosition());
169 setDimension(collision.getDimension(), textSize);
170 if (_collision.isClicked()) {
171 _callback();
172 }
173}
174
175void GUI::ECS::Components::ButtonComponent::setCallback(std::function<void()> callback, const std::string &callbackName)
176{
177 _callback = callback;
178 _callbackName = callbackName;
179}
180
182{
183 _visible = visible;
184 _componentText.setVisible(visible);
185 _componentShape.setVisible(visible);
186}
187
189{
190 _name = name;
191}
192
194{
195 _application = application;
196}
197
199{
200 return _callback;
201}
202
204{
205 return _visible;
206}
207
209{
210 if (_visible) {
211 _visible = false;
212 _componentText.setVisible(false);
213 _componentShape.setVisible(false);
214 } else {
215 _visible = true;
216 _componentText.setVisible(true);
217 _componentShape.setVisible(true);
218 }
219}
220
222{
223 return _componentShape.getClickedColor();
224}
225
227{
228 return _componentShape.getNormalColor();
229}
230
232{
233 return _componentShape.getHoverColor();
234}
236{
237 return _componentText.getClickedColor();
238}
239
241{
242 return _componentText.getNormalColor();
243}
244
246{
247 return _componentText.getHoverColor();
248}
249
251{
252 return _textSize;
253}
254
255const std::pair<float, float> GUI::ECS::Components::ButtonComponent::getPosition() const
256{
257 return _collision.getPosition();
258}
259
260const std::pair<float, float> GUI::ECS::Components::ButtonComponent::getDimension() const
261{
262 return _collision.getDimension();
263}
264
266{
267 return _componentText.getPosition();
268}
269
271{
272 return _componentShape.getPosition();
273}
274
276{
277 return _componentShape.getDimension();
278}
279
284
286{
287 return _callbackName;
288}
289
290const std::function<void()> GUI::ECS::Components::ButtonComponent::getCallback() const
291{
292 return _callback;
293}
294
296{
297 return _visible;
298}
299
301{
302 return _name;
303}
304
306{
307 return _application;
308}
309
314
319
320
321const std::string GUI::ECS::Components::ButtonComponent::getInfo(const unsigned int indent) const
322{
323
324 std::string indentation = "";
325 for (unsigned int i = 0; i < indent; ++i) {
326 indentation += "\t";
327 }
328 std::string result = indentation + "Button:\n";
329 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
330 result += indentation + "- Visible: " + Recoded::myToString(_visible) + "\n";
331 result += indentation + "- Name: " + _name + "\n";
332 result += indentation + "- Application: " + _application + "\n";
333 result += indentation + "- Callback: " + _callbackName + "\n";
334 result += indentation + "- Text: {\n" + _componentText.getInfo(indent + 1) + indentation + "}\n";
335 result += indentation + "- Shape: {\n" + _componentShape.getInfo(indent + 1) + indentation + "}\n";
336 result += indentation + "- Text Size: " + Recoded::myToString(_textSize) + "\n";
337 result += indentation + "- Collision: {\n" + _collision.getInfo(indent + 1) + indentation + "}\n";
338 return result;
339}
340
342{
343 _collision.update(mouse);
344 _componentText.update(mouse);
345 _componentShape.update(mouse);
346 if (_collision.isClicked() || _componentShape.getCollision().isClicked() || _componentText.getCollision().isClicked()) {
347 _callback();
348 }
349}
350
352{
353 _componentText = copy;
354}
355
357{
358 _componentShape = copy;
359}
360
362{
363 if (this != &copy) {
364 setCallback(copy.getCallback());
365 _componentText.update(copy.getTextComponent());
366 _componentShape.update(copy.getShapeComponent());
367 } else {
369 }
370}
371
372std::unordered_map<std::type_index, std::any> GUI::ECS::Components::ButtonComponent::render() const
373{
374 std::unordered_map<std::type_index, std::any> result;
375 result[typeid(GUI::ECS::Components::ShapeComponent)] = std::make_any<GUI::ECS::Components::ShapeComponent>(_componentShape);
376 result[typeid(GUI::ECS::Components::TextComponent)] = std::make_any<GUI::ECS::Components::TextComponent>(_componentText);
377 return result;
378}
379
381{
382 if (this != &copy) {
383 update(copy);
384 }
385 return *this;
386};
387
389{
390 os << item.getInfo();
391 return os;
392}
This file contains the definition of the ButtonComponent class, which handles button-related function...
This is the class in charge of informing the user that they tried to access a non-existant Button cla...
Definition No.hpp:328
Manages button functionalities, including appearance, position, and callbacks.
~ButtonComponent()
Destructor for ButtonComponent.
void setName(const std::string &name)
Set the Name of the button.
ButtonComponent(const std::uint32_t entityId=0)
Default constructor for ButtonComponent.
void setShapePosition(const std::pair< float, float > &position)
Set the Shape Position.
const std::string getName() const
Get the Name button component.
const std::function< void()> getCallback() const
Get the Callback object.
void setTextNormalColor(const GUI::ECS::Systems::Colour &normalColor)
Set the Text Normal Color.
const GUI::ECS::Systems::Colour getTextNormalColor() const
Get the Text Normal Color.
void setDimension(const std::pair< float, float > &dimension, const std::uint32_t textSize=40)
Set the Dimension of all objects contained in the button.
void update(const GUI::ECS::Systems::MouseInfo &mouse)
Update the mouse position information for all the entities in the button.
const GUI::ECS::Components::ShapeComponent & getShapeComponent() const
Get the Shape Component.
const GUI::ECS::Systems::Collision getCollision() const
Get the button collision object.
void setCollision(const GUI::ECS::Systems::Collision &collision, const std::uint32_t textSize=40)
Set the Collision for all objects of the Button class.
const std::pair< float, float > getShapeDimension() const
Get the Shape's Dimensions.
void setPosition(const std::pair< float, float > &position)
Set the Position of all objects contained in the button.
const std::string getApplication() const
Get the Application of the button component.
const GUI::ECS::Systems::Colour getTextClickedColor() const
Get the Text Clicked Color.
const bool isVisible() const
Get the information about if the element is visible.
std::function< void()> callback()
Get the callback function.
void setHoverColor(const GUI::ECS::Systems::Colour &hoverColor)
Set the Hover Color of the button for the child components.
void setNormalColor(const GUI::ECS::Systems::Colour &normalColor)
Set the Normal Color of the button for the child objects.
void setTextPosition(const std::pair< float, float > &position)
Set the Text Position.
const GUI::ECS::Components::TextComponent getTextComponent() const
Get the Text Component.
const GUI::ECS::Systems::Colour getTextHoverColor() const
Get the Text Hover Color.
const std::pair< float, float > getDimension() const
Get the Dimensions of the button.
const GUI::ECS::Systems::Colour getNormalColor() const
Get the Normal Color.
void setTextHoverColor(const GUI::ECS::Systems::Colour &hoverColor)
Set the Text Hover Color.
const GUI::ECS::Systems::Colour getClickedColor() const
Get the Clicked Color.
void setCallback(std::function< void()> callback, const std::string &callbackName="Callback Function")
Set the Callback function for the the button is clicked.
std::unordered_map< std::type_index, std::any > render() const
Get the components that need rendering.
const GUI::ECS::Systems::Colour getHoverColor() const
Get the Hover Color.
void toggleVisibility()
Toggle the visibility of the component.
void setTextClickedColor(const GUI::ECS::Systems::Colour &clickedColor)
Set the Text Clicked Color.
void setApplication(const std::string &application)
Set the Application context of the button.
const bool getVisible() const
Get the information about if the Button is visible or hidden.
const std::uint32_t getTextSize() const
Get the Text Size.
void setTextSize(const std::uint32_t textSize)
Set the Text Size.
const std::pair< float, float > getPosition() const
Get the Position of the button.
void setVisible(const bool visible)
Set the visibility of the element.
void setTextString(const std::string &text)
Set the text contained in the Text component.
const std::string getInfo(const unsigned int indent=0) const
This is a function meant for debugging purposes It will dump the current state of the variables upon ...
ButtonComponent & operator=(const GUI::ECS::Components::ButtonComponent &copy)
A operator to allow seemless updating/asining from one button class to another.
const std::string getCallbackName() const
Get the Callback Name of the callback function.
const std::pair< float, float > getShapePosition() const
Get the Shape Position.
void setShapeDimension(const std::pair< float, float > &dimension)
Set the Shape Dimensions.
void setClickedColor(const GUI::ECS::Systems::Colour &clickedColor)
Set the Clicked Color of the button for the child objects.
const std::pair< float, float > getTextPosition() const
Get the Text Position.
Manages shapes and their associated properties in the ECS framework.
const std::string getInfo(const unsigned int indent=0) const
This is a function meant for debugging purposes It will dump the current state of the variables upon ...
void update(const GUI::ECS::Systems::MouseInfo &mouse)
Update the mouse information of the component (this is used for mouse collision calculations)
const GUI::ECS::Systems::Collision getCollision() const
Get the Collision Component of the object.
A class that represents a text component in the GUI system. It manages font, size,...
const std::uint32_t getSize() const
Get the Size of the text.
void update(const GUI::ECS::Systems::MouseInfo &mouse)
The function in charge of updating the stored mouse information.
Represents a rectangular component that can detect collisions and mouse interactions,...
Definition Collision.hpp:39
const std::pair< float, float > getDimension() const
Get the dimension of the item in the form of an sf::Vector2i.
const std::pair< float, float > getPosition() const
Get the position of the item in the form of an sf::Vector2i.
A class for representing and manipulating colors using RGBA components. Inherits from EntityNode to a...
Definition Colour.hpp:37
std::ostream & operator<<(std::ostream &os, const AnimationComponent &item)
Outputs the animation's info to a stream.
const std::string myToString(const Rect< RectType > &rectangle)
Converts a Rect<T> object to its string representation.
Definition Rect.hpp:223