R-Type  2
Doom but in better
Loading...
Searching...
No Matches
ImageComponent.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** ImageComponent.cpp
6*/
7
15
17 : EntityNode(0), _collision(0), _base(0),
18 _normalColor(0, GUI::ECS::Systems::Colour::White), _hoverColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
19{
20};
21
23 : EntityNode(entityId), _collision(entityId), _base(entityId),
24 _normalColor(entityId, GUI::ECS::Systems::Colour::White), _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
25{
26};
27
28GUI::ECS::Components::ImageComponent::ImageComponent(const std::uint32_t entityId, const GUI::ECS::Components::TextureComponent &imageTexture, const std::string &name, const std::string &application)
29 : EntityNode(entityId), _collision(entityId), _base(entityId),
30 _normalColor(entityId, GUI::ECS::Systems::Colour::White), _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
31{
32 _inConstructor = true;
33 setApplication(application);
34 setImage(imageTexture, name);
35 _inConstructor = false;
36 _processImageComponent();
37};
38
39GUI::ECS::Components::ImageComponent::ImageComponent(const std::uint32_t entityId, const GUI::ECS::Components::TextureComponent &imageTexture, std::pair<unsigned int, unsigned int> dimensions, const std::string &name, const std::string &application)
40 : EntityNode(entityId), _collision(entityId), _base(entityId),
41 _normalColor(entityId, GUI::ECS::Systems::Colour::White), _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
42{
43 _inConstructor = true;
44 setApplication(application);
45 setImage(imageTexture, name);
46 setDimension(dimensions);
47 _inConstructor = false;
48 _processImageComponent();
49};
50
51GUI::ECS::Components::ImageComponent::ImageComponent(const std::uint32_t entityId, const GUI::ECS::Components::TextureComponent &imageTexture, std::pair<unsigned int, unsigned int> dimensions, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor, const std::string &name, const std::string &application)
52 : EntityNode(entityId), _collision(entityId), _base(entityId),
53 _normalColor(entityId, GUI::ECS::Systems::Colour::White), _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
54{
55 _inConstructor = true;
56 setApplication(application);
57 setImage(imageTexture, name);
58 setDimension(dimensions);
59 setClickedColor(clickedColor);
60 setHoverColor(hoverColor);
61 setNormalColor(normalColor);
62 _inConstructor = false;
63 _processImageComponent();
64};
65
66GUI::ECS::Components::ImageComponent::ImageComponent(const std::uint32_t entityId, const GUI::ECS::Components::TextureComponent &imageTexture, std::pair<unsigned int, unsigned int> dimensions, const std::pair<float, float> &position, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor, const std::string &name, const std::string &application)
67 : EntityNode(entityId), _collision(entityId), _base(entityId),
68 _normalColor(entityId, GUI::ECS::Systems::Colour::White), _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
69{
70 _inConstructor = true;
71 setApplication(application);
72 setImage(imageTexture, name);
73 setDimension(dimensions);
74 setPosition(position);
75 setNormalColor(normalColor);
76 setHoverColor(hoverColor);
77 setClickedColor(clickedColor);
78 _inConstructor = false;
79 _processImageComponent();
80};
81
82GUI::ECS::Components::ImageComponent::ImageComponent(const std::uint32_t entityId, const std::string &imagePath, const std::string &name, const std::string &application)
83 : EntityNode(entityId), _base(entityId), _collision(entityId),
84 _normalColor(entityId, GUI::ECS::Systems::Colour::White), _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
85{
86 _inConstructor = true;
87 setApplication(application);
88 setImage(imagePath, name);
89 _inConstructor = false;
90 _processImageComponent();
91};
92
93GUI::ECS::Components::ImageComponent::ImageComponent(const std::uint32_t entityId, const std::string &imagePath, std::pair<unsigned int, unsigned int> dimensions, const std::string &name, const std::string &application)
94 : EntityNode(entityId), _base(entityId), _collision(entityId),
95 _normalColor(entityId, GUI::ECS::Systems::Colour::White), _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
96{
97 _inConstructor = true;
98 setApplication(application);
99 setImage(imagePath, name);
100 setDimension(dimensions);
101 _inConstructor = false;
102 _processImageComponent();
103};
104
105GUI::ECS::Components::ImageComponent::ImageComponent(const std::uint32_t entityId, const std::string &imagePath, std::pair<unsigned int, unsigned int> dimensions, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor, const std::string &name, const std::string &application)
106 : EntityNode(entityId), _base(entityId), _collision(entityId),
107 _normalColor(entityId, GUI::ECS::Systems::Colour::White), _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
108{
109 _inConstructor = true;
110 setApplication(application);
111 setImage(imagePath, name);
112 setDimension(dimensions);
113 setNormalColor(normalColor);
114 setHoverColor(hoverColor);
115 setClickedColor(clickedColor);
116 _inConstructor = false;
117 _processImageComponent();
118};
119
120GUI::ECS::Components::ImageComponent::ImageComponent(const std::uint32_t entityId, const std::string &imagePath, std::pair<unsigned int, unsigned int> dimensions, const std::pair<float, float> &position, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor, const std::string &name, const std::string &application)
121 : EntityNode(entityId), _base(entityId), _collision(entityId),
122 _normalColor(entityId, GUI::ECS::Systems::Colour::White), _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
123{
124 _inConstructor = true;
125 setApplication(application);
126 setImage(imagePath, name);
127 setDimension(dimensions);
128 setPosition(position);
129 setNormalColor(normalColor);
130 setHoverColor(hoverColor);
131 setClickedColor(clickedColor);
132 _inConstructor = false;
133 _processImageComponent();
134};
135
136
138
140{
141 _name = name;
142}
143
145{
146 _application = application;
147}
148
150{
151 _normalColor = color;
152}
153
155{
156 _hoverColor = color;
157};
158
160{
161 _clickedColor = color;
162};
163
165{
166 _base = image;
167 if (!name.empty()) {
168 _name = name;
169 }
170 _textureAltered = true;
171 if (!_inConstructor) {
172 _processImageComponent();
173 }
174};
175
176void GUI::ECS::Components::ImageComponent::setImage(const std::string &path, const std::string &name)
177{
178 _base.setFilePath(path);
179 if (!name.empty()) {
180 _name = name;
181 }
182 if (!_inConstructor) {
183 _processImageComponent();
184 }
185};
186
187void GUI::ECS::Components::ImageComponent::setDimension(const std::pair<float, float> &dimension)
188{
189 _base.setSize(dimension);
190 _collision.setDimension(_base.getCollisionInfo().getDimension());
191 _sizeAltered = true;
192 if (!_inConstructor) {
193 _processImageComponent();
194 }
195};
196
197void GUI::ECS::Components::ImageComponent::setPosition(const std::pair<int, int> &position)
198{
199 _collision.setPosition(position);
200 _base.setPosition(position);
201 _positionAltered = true;
202 if (!_inConstructor) {
203 _processImageComponent();
204 }
205};
206
208{
209 _visible = visible;
210};
211
213{
214 _levelBackground = levelBackground;
215}
216
218{
219 if (_visible) {
220 _visible = false;
221 } else {
222 _visible = true;
223 }
224};
225
227{
228 return _visible;
229}
230
232{
233 return _levelBackground;
234}
235
237{
238 return _name;
239}
240
242{
243 return _application;
244}
245
250
255
260
265
266const std::pair<float, float> GUI::ECS::Components::ImageComponent::getDimension() const
267{
268 return _collision.getDimension();
269};
270
272{
273 return _collision.getPosition();
274};
275
277{
278 return _visible;
279};
280
285
286const std::string GUI::ECS::Components::ImageComponent::getInfo(const unsigned int indent) const
287{
288 std::string indentation = "";
289 for (unsigned int i = 0; i < indent; ++i) {
290 indentation += "\t";
291 }
292 std::string result = indentation + "Image:\n";
293 result += indentation + "- EntityId: " + Recoded::myToString(getEntityNodeId()) + "\n";
294 result += indentation + "- Visible: " + Recoded::myToString(_visible) + "\n";
295 result += indentation + "- Size altered: " + Recoded::myToString(_sizeAltered) + "\n";
296 result += indentation + "- In Constructor: " + Recoded::myToString(_inConstructor) + "\n";
297 result += indentation + "- Texture altered: " + Recoded::myToString(_textureAltered) + "\n";
298 result += indentation + "- Position altered: " + Recoded::myToString(_positionAltered) + "\n";
299 result += indentation + "- Name: " + _name + "\n";
300 result += indentation + "- Application: " + _application + "\n";
301 result += indentation + "- Image Component present?: " + Recoded::myToString(_sfImage.has_value()) + "\n";
302 result += indentation + "- Position: {\n" + _collision.getInfo(indent + 1) + indentation + "}\n";
303 result += indentation + "- Texture Base: {\n" + _base.getInfo(indent + 1) + indentation + "}\n";
304 result += indentation + "- Hover Color: {\n" + _hoverColor.getInfo(indent + 1) + indentation + "}\n";
305 result += indentation + "- Normal Color: {\n" + _normalColor.getInfo(indent + 1) + indentation + "}\n";
306 result += indentation + "- Clicked Color: {\n" + _clickedColor.getInfo(indent + 1) + indentation + "}\n";
307 return result;
308};
309
311{
312 return isLevelBackgroundCompatible();
313}
314
316{
317 if (!_visible || !_sfImage.has_value()) {
318 PRETTY_INFO << "Instance is hidden or no sfImage instance found, not rendering" << std::endl;
319 return std::nullopt;
320 }
321 return std::make_any<sf::Sprite>(_sfImage.value());
322};
323
325{
326 _collision.update(mouse);
327 _processImageComponent();
328};
329
331{
332 _inConstructor = true;
333
334 setName(copy.getName());
335 setApplication(copy.getApplication());
336
337 setImage(copy.getImage());
338 setVisible(copy.getVisible());
339
340 _collision = copy.getCollision();
341 setPosition(copy.getPosition());
342 setDimension(copy.getDimension());
343
344 setHoverColor(copy.getHoverColor());
345 setNormalColor(copy.getNormalColor());
346 setClickedColor(copy.getClickedColor());
347
348 setLevelBackgroundCompatible(copy.getLevelBackgroundCompatibility());
349
350 _inConstructor = false;
351 _processImageComponent();
352};
353
359
360void GUI::ECS::Components::ImageComponent::_initialiseImage()
361{
362 if (!_sfImage.has_value()) {
363 const std::any textureCapsule = _base.getTexture();
364 if (textureCapsule.has_value()) {
365 const std::string errMsg = "<No texture was found when a texture of type sf::Texture was expected>, system error: ";
366 const std::optional<std::shared_ptr<sf::Texture>> texture = Utilities::unCast<std::shared_ptr<sf::Texture>, CustomExceptions::NoTexture>(textureCapsule, true, errMsg);
367 if (!texture.has_value()) {
368 PRETTY_ERROR << errMsg << std::endl;
369 throw CustomExceptions::NoTexture(errMsg);
370 }
371 _sfImage.emplace(*(texture.value()));
372 GUI::ECS::Systems::Collision node(_collision);
373 const sf::FloatRect bounds = _sfImage->getGlobalBounds();
374 const Recoded::FloatRect sysBounds = { {bounds.position.x, bounds.position.y}, {bounds.size.x, bounds.size.y} };
375 PRETTY_INFO << "Image dimensions = " << sysBounds << std::endl;
376 node.setGeometry(sysBounds);
377 PRETTY_INFO << "Collision node = " << node << std::endl;
378 _collision.update(node);
379 PRETTY_SUCCESS << "Collision updated" << std::endl;
380 }
381 }
382}
383
384void GUI::ECS::Components::ImageComponent::_processColour()
385{
386 if (!_sfImage.has_value()) {
387 PRETTY_WARNING << "ImageComponent has no image" << std::endl;
388 return;
389 }
390 std::any systemColour;
391 if (_collision.isClicked()) {
392 PRETTY_DEBUG << "The colour being applied is : {\n"
393 << _clickedColor << "\n} and corresponds to the _clickedColour"
394 << std::endl;
395 systemColour = _clickedColor.getRenderColour();
396 } else if (_collision.isHovered()) {
397 PRETTY_DEBUG << "The colour being applied is : {\n"
398 << _hoverColor << "\n} and corresponds to the _hoverColour"
399 << std::endl;
400 systemColour = _hoverColor.getRenderColour();
401 } else {
402 PRETTY_DEBUG << "The colour being applied is : {\n"
403 << _normalColor << "\n} and corresponds to the _normalColour"
404 << std::endl;
405 systemColour = _normalColor.getRenderColour();
406 }
407 const std::string errMsg = "<The content returned by the getRenderColour function is not of type sf::Color>, system error: ";
408 const std::optional<sf::Color> color = Utilities::unCast<sf::Color, CustomExceptions::NoColour>(systemColour, true, errMsg);
409 if (!color.has_value()) {
410 PRETTY_CRITICAL << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' "
411 << "No Color found, " << errMsg << std::endl;
412 throw CustomExceptions::NoColour(errMsg);
413 }
414 _sfImage->setColor(color.value());
415 PRETTY_SUCCESS << "Image colour has been set" << std::endl;
416}
417
418void GUI::ECS::Components::ImageComponent::_processImageComponent()
419{
420 if (!_sfImage.has_value()) {
421 _initialiseImage();
422 }
423 _processColour();
424 if (_sfImage.has_value()) {
425 if (_sizeAltered) {
426 PRETTY_DEBUG << "The size has been altered, updating size." << std::endl;
427 std::pair<float, float> dimension = _collision.getDimension();
428 _sfImage->setScale({ dimension.first, dimension.second });
429 _sizeAltered = false;
430 }
431 if (_positionAltered) {
432 PRETTY_DEBUG << "The position has been altered, updating position." << std::endl;
433 std::pair<float, float> position = _collision.getPosition();
434 _sfImage->setPosition({ position.first, position.second });
435 _positionAltered = false;
436 }
437 if (_textureAltered) {
438 PRETTY_DEBUG << "The texture has been altered, updating image." << std::endl;
439 const std::any textureCapsule = _base.getTexture();
440 const std::string errMsg = "<There is no texture instance in this cast, expected a cast of type sf::Texture>, system error: ";
441 const std::optional<std::shared_ptr<sf::Texture>> texture = Utilities::unCast<std::shared_ptr<sf::Texture>, CustomExceptions::NoTexture>(textureCapsule, true, errMsg);
442 if (texture.has_value()) {
443 _sfImage->setTexture(*(texture.value()));
444 _textureAltered = false;
445 }
446
447 }
448 }
449}
450
452{
453 os << item.getInfo();
454 return os;
455}
This is the file that contains a class in charge of managing a test component.
#define PRETTY_ERROR
Error log with details and colour.
#define PRETTY_DEBUG
Debug log with details and colour.
#define PRETTY_INFO
Info log with details and colour.
#define PRETTY_CRITICAL
Critical log with details and colour.
#define PRETTY_WARNING
Warning log with details and colour.
#define PRETTY_SUCCESS
Success log with details and colour.
This is the class in charge of informing the user that they tried to access a non-existant colour ins...
Definition No.hpp:388
This is the class in charge of informing the user that they tried to access a non-existant texture in...
Definition No.hpp:446
Represents an image component in the GUI ECS system.
void setHoverColor(const GUI::ECS::Systems::Colour &color)
Sets the hover color of the image.
~ImageComponent()
Destructor for ImageComponent.
void setClickedColor(const GUI::ECS::Systems::Colour &color)
Sets the clicked color of the image.
std::any render() const
Renders the image to the given window.
void setName(const std::string &name)
Set the Name of the image.
const std::string getName() const
Get the Name of the image.
const GUI::ECS::Systems::Colour getHoverColor() const
Retrieves the hover color of the image.
void setPosition(const std::pair< int, int > &position)
Sets the position of the image.
void setApplication(const std::string &application)
Set the Application (the context) of the image.
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 ...
const GUI::ECS::Systems::Collision getCollision() const
Get the Collision object.
GUI::ECS::Components::ImageComponent & operator=(const GUI::ECS::Components::ImageComponent &copy)
Assigns values from another ImageComponent.
const std::pair< int, int > getPosition() const
Get the Position of the image.
const GUI::ECS::Systems::Colour getNormalColor() const
Retrieves the normal color of the image.
const bool isVisible() const
Get the visibility state of the Image.
void update(const GUI::ECS::Systems::MouseInfo &mouse)
Updates the image based on mouse interactions.
void setLevelBackgroundCompatible(const bool levelBackground)
Set the Level Background Compatible variable.
const bool isLevelBackgroundCompatible() const
Know if the background can be used as a level bacground.
void setImage(const GUI::ECS::Components::TextureComponent &image, const std::string &name="")
Assigns a texture to the image component.
const GUI::ECS::Systems::Colour getClickedColor() const
Retrieves the clicked color of the image.
const bool getLevelBackgroundCompatibility() const
Get the Level Background Compatibility object.
const bool getVisible() const
Get the Visibility of the text.
void setDimension(const std::pair< float, float > &dimension)
Sets the dimensions of the image.
const std::pair< float, float > getDimension() const
Get the Size of the text.
void toggleVisibility()
Toggles the visibility of the image.
const GUI::ECS::Components::TextureComponent getImage() const
Retrieves the texture associated with the image.
void setNormalColor(const GUI::ECS::Systems::Colour &color)
Sets the normal color of the image.
void setVisible(const bool visible)
Set the visibility of the object using a boolean.
const std::string getApplication() const
Get the Application of the image.
ImageComponent()
Default constructor. Initializes the ImageComponent with default values.
Represents a texture component used in an entity component system.
void setFilePath(const std::string &filePath)
Sets the file path for the texture.
Represents a rectangular component that can detect collisions and mouse interactions,...
Definition Collision.hpp:39
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 ...
A class for representing and manipulating colors using RGBA components. Inherits from EntityNode to a...
Definition Colour.hpp:37
A generic 2D rectangle class that holds position and size as pairs.
Definition Rect.hpp:38
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
std::optional< T > unCast(const std::any &classNode, const bool raiseOnError=true, const std::string customErrorMessage="")
Casts the content of a std::any back to its original type.
Definition UnCast.hpp:65