16GUI::ECS::Systems::Window::Window(
const std::uint32_t entityId,
const std::uint32_t windowWidth,
const std::uint32_t windowHeight,
const std::string &windowName,
unsigned int frameRateLimit)
18 _sfWindow(sf::VideoMode({ windowWidth, windowHeight }), windowName),
19 _windowWidth(windowWidth),
20 _windowHeight(windowHeight),
21 _windowName(windowName)
23 _sfWindow.setFramerateLimit(frameRateLimit);
31 _sfWindow.setPosition({ position.first, position.second });
39 const std::any textureCast = iconTexture.
getTexture();
41 if (!textureCapsule.has_value()) {
42 PRETTY_CRITICAL <<
"There is no texture to work with (gathered from ImageComponent)." << std::endl;
45 const sf::Texture texture = textureCapsule.value();
46 const sf::Image image = texture.copyToImage();
47 if (image.getSize().x == 0 || image.getSize().y == 0) {
48 PRETTY_CRITICAL <<
"Image extracted from texture is invalid." << std::endl;
49 throw CustomExceptions::NoIcon(
"Invalid image size (extracted from sf::Texture for setting the icon of the window).");
51 const std::uint8_t *pixelData = image.getPixelsPtr();
52 const sf::Vector2u size = image.getSize();
53 _sfWindow.setIcon(size, pixelData);
59 PRETTY_DEBUG <<
"Setting the window title to: '" << title <<
"'." << std::endl;
60 _sfWindow.setTitle(title);
68 if (!systemColour.has_value()) {
69 throw CustomExceptions::NoColour(
"<There was no content returned by getRenderColour when std::any (containing sf::Font was expected)>");
72 sf::Color result = std::any_cast<sf::Color>(systemColour);
73 _sfWindow.clear(result);
75 catch (std::bad_any_cast &e) {
76 throw CustomExceptions::NoColour(
"<The content returned by the getRenderColour function is not of type sf::Color>, system error: " + std::string(e.what()));
87 return _sfWindow.isOpen();
98 std::optional<sf::Event> node = _sfWindow.pollEvent();
99 if (node.has_value()) {
100 sf::Event evt = node.value();
101 std::any tmp(
static_cast<sf::Event
>(evt));
109 _sfWindow.setFramerateLimit(framerateLimit);
114 _fullScreen = fullScreen;
116 if (_sfWindow.isOpen()) {
121 _sfWindow.create(_desktopMode, _windowName, sf::Style::None);
123 _sfWindow.create(sf::VideoMode({ _windowWidth, _windowHeight }), _windowName, sf::Style::Close);
139 sf::Vector2u dim = _sfWindow.getSize();
140 return std::make_pair<int>(dim.x, dim.y);
145 sf::Vector2i pos = _sfWindow.getPosition();
146 return std::pair<int, int>(pos.x, pos.y);
152 std::string indentation =
"";
153 for (
unsigned int i = 0; i < indent; ++i) {
156 std::string result = indentation +
"Window:\n";
158 result += indentation +
"- Window Name: '" + _windowName +
"'\n";
172 const std::any textCapsule = text.
render();
174 if (!txt.has_value()) {
178 _sfWindow.draw(txt.value());
192 const std::optional<std::pair<GUI::ECS::Systems::ActiveShape, std::any>> shapeCapsule = shape.
render();
193 if (!shapeCapsule.has_value()) {
199 const std::pair<GUI::ECS::Systems::ActiveShape, std::any> pairNode = shapeCapsule.value();
201 const std::any shapeData = pairNode.second;
203 PRETTY_WARNING <<
"There is no shape to render, skipping" << std::endl;
208 if (!renderableShape.has_value()) {
212 _sfWindow.draw(renderableShape.value());
218 if (!renderableShape.has_value()) {
222 _sfWindow.draw(renderableShape.value());
228 if (!renderableShape.has_value()) {
232 _sfWindow.draw(renderableShape.value());
243 <<
"' is set to invisible, it will thus not be rendered."
247 const std::any imageCapsule = image.
render();
249 if (!spriteCapsule.has_value()) {
252 <<
"' could not be rendered.\n" << std::endl;
255 _sfWindow.draw(spriteCapsule.value());
258 <<
"' is drawn" << std::endl;
266 <<
"' is set to invisible, it will thus not be rendered."
270 const std::any spriteNode = sprite.
render();
272 if (!spriteCapsule.has_value()) {
275 <<
"' could not be rendered.\n" << std::endl;
278 _sfWindow.draw(spriteCapsule.value());
281 <<
"' is drawn" << std::endl;
289 <<
"' is set to invisible, it will thus not be rendered."
293 std::unordered_map<std::type_index, std::any> buttonCapsule = button.
render();
296 if (shapeCapsule.has_value()) {
298 if (!shape.has_value()) {
299 PRETTY_WARNING <<
"There is no shape to render for the button, skipping shape rendering." << std::endl;
306 if (textCapsule.has_value()) {
308 if (!text.has_value()) {
309 PRETTY_WARNING <<
"There is no text to render for the button, skipping text rendering." << std::endl;
#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.
Header file for the Window class, which handles the graphical window and rendering logic.
This is the class in charge of informing the user that they tried to access a non-existant colour ins...
This is the class in charge of informing the user that they tried to access a non-existant Icon.
This is the class in charge of informing the user that they tried to render a non-existing Image comp...
This is the class in charge of informing the user that they tried to render a non-existing Shape comp...
This is the class in charge of informing the user that they tried to render a non-existing text compo...
This is the class in charge of informing the user that they tried to access a non-existant texture in...
Represents an image component in the GUI ECS system.
std::any render() const
Renders the image to the given window.
const std::string getName() const
Get the Name of the image.
const bool isVisible() const
Get the visibility state of the Image.
const GUI::ECS::Components::TextureComponent getImage() const
Retrieves the texture associated with the image.
const std::string getApplication() const
Get the Application of the image.
Manages shapes and their associated properties in the ECS framework.
const bool isVisible() const
Get the visibility status of the 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 ...
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::string getShapeTypeString() const
Get the Shape Type shape, none is returned if none are initialised.
Represents a drawable and interactive sprite in the ECS system.
const std::string getApplication() const
Gets the application name associated with the sprite.
const std::string getName() const
Gets the name of the sprite.
std::any render() const
Renders the sprite to a window.
const bool isVisible() const
Check if the sprite is set to be rendred or not.
A class that represents a text component in the GUI system. It manages font, size,...
const bool isVisible() const
Check if the text is set to be rendered or not.
std::any render() const
Function in charge of rendering the text if it is set to visible, otherwise, does nothing.
Represents a texture component used in an entity component system.
const std::any getTexture() const
Retrieves the texture as a std::any object.
virtual size_t getEntityNodeId() const
A class for representing and manipulating colors using RGBA components. Inherits from EntityNode to a...
const std::any getRenderColour() const
Retrieves the color as an SFML sf::Color if the sfml library is the underlying library,...
Manages an SFML-based graphical window and handles rendering of ECS components.
void setPosition(const std::pair< int, int > &position)
Set the Position of the window.
void close()
Closes the window.
Window(const std::uint32_t entityId=0, const std::uint32_t windowWidth=800, const std::uint32_t windowHeight=600, const std::string &windowName="R-Type", unsigned int frameRateLimit=60)
Constructs a new Window object.
~Window()
Destroys the Window object.
void setTitle(const std::string &title="R-Type")
Set the Title of the window.
void draw(const GUI::ECS::Components::TextComponent &text)
Renders a text component to the window.
void display()
Displays the contents of the window on the screen.
const bool getFullScreen() const
Retrieves the current fullscreen mode of the window.
void setFullScreen(const bool fullScreen)
Enables or disables fullscreen mode for the window.
void setIcon(const GUI::ECS::Components::ImageComponent &icon)
Set the Icon of the window.
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 clear(const GUI::ECS::Systems::Colour &color=GUI::ECS::Systems::Colour::Black)
Clears the window with the specified color.
void setFramerateLimit(const unsigned int framerateLimit)
Sets the frame rate limit of the window.
bool isOpen() const
Checks if the window is currently open.
const std::pair< int, int > getPosition() const
Get the Position of the window.
const std::string getTitle() const
Get the Title of the window.
std::any pollEvent()
Polls for the next event in the window's event queue.
const std::pair< int, int > getDimensions() const
Get the Dimensions of the window.
ActiveShape
Enum representing different types of shapes managed by the ShapeComponent.
@ RECTANGLE
Rectangle shape.
@ NONE
No shape initialized.
std::ostream & operator<<(std::ostream &os, const Clock &item)
Outputs the clock's info to a stream.
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.