18 _hoverColor(entityID,
GUI::ECS::Systems::Colour::White),
19 _normalColor(entityID,
GUI::ECS::Systems::Colour::White),
20 _clickedColor(entityID,
GUI::ECS::Systems::Colour::White),
21 _shape(
GUI::ECS::Systems::ActiveShape::
NONE),
25 _inConstructor =
false;
29 :
EntityNode(0), _hoverColor(0), _normalColor(0), _clickedColor(0), _collision(0)
31 _inConstructor =
true;
38 _inConstructor =
false;
45 :
EntityNode(0), _hoverColor(0), _normalColor(0), _clickedColor(0), _collision(0)
47 _inConstructor =
true;
54 _inConstructor =
false;
60 :
EntityNode(0), _hoverColor(0), _normalColor(0), _clickedColor(0), _collision(0)
62 _inConstructor =
true;
69 _inConstructor =
false;
75 :
EntityNode(0), _hoverColor(0), _normalColor(0), _clickedColor(0), _collision(0)
77 _inConstructor =
true;
83 _inConstructor =
false;
89 :
EntityNode(entityId), _hoverColor(entityId), _normalColor(entityId), _clickedColor(entityId), _collision(entityId)
91 _inConstructor =
true;
98 _inConstructor =
false;
100 _processCollisions();
104 :
EntityNode(entityId), _hoverColor(entityId), _normalColor(entityId), _clickedColor(entityId), _collision(entityId)
106 _inConstructor =
true;
113 _inConstructor =
false;
115 _processCollisions();
119 :
EntityNode(entityId), _hoverColor(entityId), _normalColor(entityId), _clickedColor(entityId), _collision(entityId)
121 _inConstructor =
true;
128 _inConstructor =
false;
130 _processCollisions();
134 :
EntityNode(entityId), _hoverColor(entityId), _normalColor(entityId), _clickedColor(entityId), _collision(entityId)
136 _inConstructor =
true;
142 _inConstructor =
false;
144 _processCollisions();
152 _hoverColor = hoverColor;
157 _normalColor = normalColor;
162 _clickedColor = clickedColor;
168 _sfShapeConvex.emplace(pointCount);
170 if (!_inConstructor) {
171 _processCollisions();
179 _sfShapeCircle.emplace(radius, pointCount);
181 if (!_inConstructor) {
182 _processCollisions();
193 _sfShapeRectangle.emplace(dim);
194 _collision.setDimension(size);
196 if (!_inConstructor) {
197 _processCollisions();
205 sf::Vector2f dim = { rect.
size.first, rect.
size.second };
207 _sfShapeRectangle.emplace(dim);
208 _sfShapeRectangle->setPosition(pos);
209 _collision.setDimension(rect.
size);
210 _collision.setPosition(rect.
position);
212 if (!_inConstructor) {
213 _processCollisions();
226 PRETTY_INFO <<
"Clearing any instances of any previous shapes" << std::endl;
233 PRETTY_INFO <<
"Setting the shape to a rectangle." << std::endl;
234 _assignShape(shape, _sfShapeRectangle);
236 PRETTY_INFO <<
"Setting the shape to a circle." << std::endl;
237 _assignShape(shape, _sfShapeCircle);
239 PRETTY_INFO <<
"Setting the shape to a convex." << std::endl;
240 _assignShape(shape, _sfShapeConvex);
242 PRETTY_INFO <<
"Setting the shape to none." << std::endl;
249 setShape(shape.first, shape.second);
259 _collision.setPosition(position);
260 if (!_inConstructor) {
261 _processCollisions();
267 _collision.setDimension(dimension);
272 _collision.update(collision);
274 _processCollisions();
286 _application = application;
308 if (_sfShapeRectangle.has_value() || _sfShapeCircle.has_value() || _sfShapeConvex.has_value()) {
327 return std::pair<GUI::ECS::Systems::ActiveShape, std::any>(_shape, std::make_any<std::string>(
""));
330 if (!_sfShapeRectangle.has_value()) {
331 sf::RectangleShape tt({ 10,10 });
332 return std::pair<GUI::ECS::Systems::ActiveShape, std::any>(_shape, std::make_any<sf::RectangleShape>(tt));
334 return std::pair<GUI::ECS::Systems::ActiveShape, std::any>(
336 std::make_any<sf::RectangleShape>(_sfShapeRectangle.value())
340 if (!_sfShapeCircle.has_value()) {
341 sf::CircleShape tt(10, 30);
342 return std::pair<GUI::ECS::Systems::ActiveShape, std::any>(_shape, std::make_any<sf::CircleShape>(tt));
344 return std::pair<GUI::ECS::Systems::ActiveShape, std::any>(
346 std::make_any<sf::CircleShape>(_sfShapeCircle.value())
350 if (!_sfShapeConvex.has_value()) {
351 sf::ConvexShape tt(10);
352 return std::pair<GUI::ECS::Systems::ActiveShape, std::any>(_shape, std::make_any<sf::ConvexShape>(tt));
354 return std::pair<GUI::ECS::Systems::ActiveShape, std::any>(
356 std::make_any<sf::ConvexShape>(_sfShapeConvex.value())
359 return std::pair<GUI::ECS::Systems::ActiveShape, std::any>(_shape, std::make_any<std::string>(
""));
374 return _clickedColor;
379 return _collision.getPosition();
384 return _collision.getDimension();
389 return getActiveShape();
411 std::string indentation =
"";
412 for (
unsigned int i = 0; i < indent; ++i) {
415 std::string result = indentation +
"Shape:\n";
418 result += indentation +
"- shape type: " + getShapeTypeString() +
"\n";
419 result += indentation +
"- Shape rectangle set?: " +
Recoded::myToString(_sfShapeRectangle.has_value()) +
"\n";
420 result += indentation +
"- Shape circle set?: " +
Recoded::myToString(_sfShapeCircle.has_value()) +
"\n";
421 result += indentation +
"- Shape convex set?: " +
Recoded::myToString(_sfShapeConvex.has_value()) +
"\n";
422 result += indentation +
"- Hover Color: {\n" + _hoverColor.
getInfo(indent + 1) + indentation +
"}\n";
423 result += indentation +
"- Normal Color: {\n" + _normalColor.getInfo(indent + 1) + indentation +
"}\n";
424 result += indentation +
"- Clicked Color: {\n" + _clickedColor.getInfo(indent + 1) + indentation +
"}\n";
425 result += indentation +
"- Collision: {\n" + _collision.getInfo(indent + 1) + indentation +
"}\n";
436 _collision.update(mouse);
437 if (!_inConstructor) {
458 PRETTY_DEBUG <<
"Clearing all shape instances" << std::endl;
461 _sfShapeCircle.reset();
462 _sfShapeConvex.reset();
463 _sfShapeRectangle.reset();
470 PRETTY_WARNING <<
"The shape is either hidden or it's value is None" << std::endl;
472 PRETTY_DEBUG <<
"Shape status: '" << getShapeTypeString() <<
"'." << std::endl;
476 PRETTY_DEBUG <<
"Returning value for the Rectangle shape." << std::endl;
477 return std::optional(
480 std::make_any<sf::RectangleShape>(_sfShapeRectangle.value())
485 PRETTY_DEBUG <<
"Returning value for the Circle shape." << std::endl;
486 return std::optional(
489 std::make_any<sf::CircleShape>(_sfShapeCircle.value())
494 PRETTY_DEBUG <<
"Returning value for the Convex shape." << std::endl;
495 return std::optional(
498 std::make_any<sf::ConvexShape>(_sfShapeConvex.value())
502 PRETTY_WARNING <<
"No shape is set for this component" << std::endl;
514void GUI::ECS::Components::ShapeComponent::_processColor()
516 std::any systemColour;
517 if (_collision.isClicked()) {
519 << _clickedColor <<
"\n} and corresponds to the _clickedColour"
521 systemColour = _clickedColor.getRenderColour();
522 }
else if (_collision.isHovered()) {
524 << _hoverColor <<
"\n} and corresponds to the _hoverColour"
526 systemColour = _hoverColor.getRenderColour();
529 << _normalColor <<
"\n} and corresponds to the _normalColour"
531 systemColour = _normalColor.getRenderColour();
533 const std::string errMsg =
"<The content returned by the getRenderColour function is not of type sf::Color>, system error: ";
535 if (!colourCapsule.has_value()) {
537 << errMsg << std::endl;
540 if (_sfShapeCircle.has_value()) {
541 _sfShapeCircle->setFillColor(colourCapsule.value());
543 if (_sfShapeConvex.has_value()) {
544 _sfShapeConvex->setFillColor(colourCapsule.value());
546 if (_sfShapeRectangle.has_value()) {
547 _sfShapeRectangle->setFillColor(colourCapsule.value());
551void GUI::ECS::Components::ShapeComponent::_processCollisions()
554 PRETTY_WARNING <<
"There are no shapes with which to calculate collisions." << std::endl;
558 bool hitboxUpdated =
false;
559 const sf::Vector2f pos = { _collision.getPositionX(), _collision.getPositionY() };
560 const sf::Vector2f size = { _collision.getWidth(), _collision.getHeight() };
562 sf::FloatRect collisionHitBox = { pos, size };
564 PRETTY_DEBUG <<
"Setting positions for : Circle shape" << std::endl;
565 _sfShapeCircle->setPosition(pos);
566 collisionHitBox = _sfShapeCircle->getGlobalBounds();
567 hitboxUpdated =
true;
570 PRETTY_DEBUG <<
"Setting positions for : Convex shape" << std::endl;
571 _sfShapeConvex->setPosition(pos);
572 collisionHitBox = _sfShapeConvex->getGlobalBounds();
573 hitboxUpdated =
true;
576 PRETTY_DEBUG <<
"Setting positions for : Rectangle shape" << std::endl;
577 _sfShapeRectangle->setPosition(pos);
578 collisionHitBox = _sfShapeRectangle->getGlobalBounds();
579 hitboxUpdated =
true;
582 _collision.setDimension({ collisionHitBox.size.x, collisionHitBox.size.y });
588void GUI::ECS::Components::ShapeComponent::_assignShape(
const std::any &shape, std::optional<T> &shapeStorage)
590 const std::string errMsg =
"<Unknown shape type>, any_cast error: ";
592 if (!instanceCapsule.has_value()) {
594 <<
"The shape that was attempted to be uncast failed." << std::endl;
597 T instance = instanceCapsule.value();
598 shapeStorage.emplace(instance);
#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 file that contains the class in charge of managing shapes.
This is the class in charge of informing the user that they tried to display a non-existent or invali...
This is the class in charge of informing the user that they tried to access a non-existant colour ins...
Manages shapes and their associated properties in the ECS framework.
const bool isVisible() const
Get the visibility status of the component.
void setCollision(const GUI::ECS::Systems::Collision &collision)
Set the internal collision class using another Collision component.
void setName(const std::string &name)
Set the Name of the component.
void setClickedColor(const GUI::ECS::Systems::Colour &clickedColor)
Sets the clicked color for the shape.
const bool isShapeInitialised() const
Know if there is a shape initialised or not.
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 std::string getName() const
Get the Name of the component.
const GUI::ECS::Systems::Colour getHoverColor() const
Get the Hover Color of the shape.
std::optional< std::pair< GUI::ECS::Systems::ActiveShape, std::any > > render() const
Get an optional std::pair of the object so that it can be displayed on screen.
const std::pair< GUI::ECS::Systems::ActiveShape, std::any > getActiveShape() const
Get the Active Shape and it's type all contained in an std::pair.
void setShape(const std::size_t pointCount)
Create a convex shape.
const std::pair< float, float > getDimension() const
Get the Dimension of the shape.
const bool getVisible() const
Get the info about if the shape is visible or not.
const GUI::ECS::Systems::Colour getNormalColor() const
Get the Normal Color of the shape.
ShapeComponent & operator=(const ShapeComponent ©)
'=' operator in charge of allowing the user to seamlessly assing the content of one ShapeComponent to...
void update(const GUI::ECS::Systems::MouseInfo &mouse)
Update the mouse information of the component (this is used for mouse collision calculations)
void setDimension(const std::pair< float, float > dimension)
Set the Dimension object.
const std::string getApplication() const
Get the Application context of the component.
const GUI::ECS::Systems::ActiveShape getShapeType() const
Get the Shape Type shape, none is returned if none are initialised.
const GUI::ECS::Systems::Collision getCollision() const
Get the Collision Component of the object.
const std::string getShapeTypeString() const
Get the Shape Type shape, none is returned if none are initialised.
void setHoverColor(const GUI::ECS::Systems::Colour &hoverColor)
Sets the hover color for the shape.
void clearShapes()
Remove all and any initialised shapes in the class, this will reset the type to NONE.
ShapeComponent(const std::uint32_t entityId=0)
Default constructor.
void setVisible(const bool visible)
Set the visible toggle informing the program if it should (or not) render the component.
void toggleVisibility()
Toggles the visibility of the shape.
~ShapeComponent()
Destructor.
const std::pair< GUI::ECS::Systems::ActiveShape, std::any > getShape() const
Get the Shape and it's type all packaged in an std::pair.
const GUI::ECS::Systems::Colour getClickedColor() const
Get the Clicked Color shape.
void setNormalColor(const GUI::ECS::Systems::Colour &normalColor)
Sets the normal color for the shape.
void setPosition(const std::pair< float, float > position)
Set the position of the component.
void setApplication(const std::string &application)
Set the Application context of the component.
const std::pair< float, float > getPosition() const
Get the Position of the shape.
Represents a rectangular component that can detect collisions and mouse interactions,...
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...
A generic 2D rectangle class that holds position and size as pairs.
std::pair< T, T > size
The size of the rectangle (width, height)
std::pair< T, T > position
the position (top and left corner)
std::ostream & operator<<(std::ostream &os, const AnimationComponent &item)
Outputs the animation's info to a stream.
ActiveShape
Enum representing different types of shapes managed by the ShapeComponent.
@ RECTANGLE
Rectangle shape.
@ NONE
No shape initialized.
const std::string myToString(const Rect< RectType > &rectangle)
Converts a Rect<T> object to its string representation.
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.