R-Type  2
Doom but in better
Loading...
Searching...
No Matches
Font.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** Font.cpp
6*/
7
15
17
18GUI::ECS::Systems::Font::Font(const std::uint32_t entityId) : EntityNode(entityId) {};
19
21 : EntityNode(other.getEntityNodeId())
22{
23 update(other);
24 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
25};
26
27GUI::ECS::Systems::Font::Font(const std::string &name, const std::string &path)
28 : EntityNode(0)
29{
30 setFontPath(path);
31 setFontName(name);
32 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
33};
34
35GUI::ECS::Systems::Font::Font(const std::string &name, const std::string &path, const unsigned int size)
36 : EntityNode(0)
37{
38 setFontPath(path);
39 setFontName(name);
41 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
42};
43
44GUI::ECS::Systems::Font::Font(const std::string &name, const std::string &path, const unsigned int size, const std::string &application)
45 : EntityNode(0)
46{
47 setFontPath(path);
48 setFontName(name);
50 setFontApplication(application);
51 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
52};
53
54GUI::ECS::Systems::Font::Font(const std::string &name, const std::string &path, const unsigned int size, const std::string &application, const bool bold)
55 : EntityNode(0)
56{
57 setBold(bold);
58 setFontPath(path);
59 setFontName(name);
61 setFontApplication(application);
62 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
63};
64
65GUI::ECS::Systems::Font::Font(const std::string &name, const std::string &path, const unsigned int size, const std::string &application, const bool bold, const bool italic)
66 : EntityNode(0)
67{
68 setBold(bold);
69 setItalic(italic);
70 setFontPath(path);
71 setFontName(name);
73 setFontApplication(application);
74 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
75};
76
77GUI::ECS::Systems::Font::Font(const std::uint32_t entityId, const std::string &name, const std::string &path)
78 : EntityNode(entityId)
79{
80 setFontPath(path);
81 setFontName(name);
82 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
83};
84
85GUI::ECS::Systems::Font::Font(const std::uint32_t entityId, const std::string &name, const std::string &path, const unsigned int size)
86 : EntityNode(entityId)
87{
88 setFontPath(path);
89 setFontName(name);
91 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
92};
93
94GUI::ECS::Systems::Font::Font(const std::uint32_t entityId, const std::string &name, const std::string &path, const unsigned int size, const std::string &application)
95 : EntityNode(entityId)
96{
97 setFontPath(path);
98 setFontName(name);
100 setFontApplication(application);
101 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
102};
103
104GUI::ECS::Systems::Font::Font(const std::uint32_t entityId, const std::string &name, const std::string &path, const unsigned int size, const std::string &application, const bool bold)
105 : EntityNode(entityId)
106{
107 setBold(bold);
108 setFontPath(path);
109 setFontName(name);
110 setFontDefaultSize(size);
111 setFontApplication(application);
112 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
113};
114
115GUI::ECS::Systems::Font::Font(const std::uint32_t entityId, const std::string &name, const std::string &path, const unsigned int size, const std::string &application, const bool bold, const bool italic)
116 : EntityNode(entityId)
117{
118 setBold(bold);
119 setItalic(italic);
120 setFontPath(path);
121 setFontName(name);
122 setFontDefaultSize(size);
123 setFontApplication(application);
124 PRETTY_INFO << "The font " << _fontName << " is loaded and ready to use." << std::endl;
125};
126
128
130{
131 _bold = bold;
132}
133
135{
136 _italic = italic;
137}
138
139void GUI::ECS::Systems::Font::setFontName(const std::string &name)
140{
141 _fontName = name;
142}
143
144void GUI::ECS::Systems::Font::setFontPath(const std::string &path)
145{
146 _fontInstanceSet = false;
147 PRETTY_DEBUG << "Creating a font instance" << std::endl;
148 std::shared_ptr<sf::Font> node = std::make_shared<sf::Font>();
149 PRETTY_DEBUG << "Font instance created" << std::endl;
150 const bool response = node->openFromFile(path);
151 PRETTY_DEBUG << "Font loading" << std::endl;
152 if (!response) {
153 PRETTY_CRITICAL << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' "
154 << "Error: Failed to load font from " << _fontPath << std::endl;
156 };
157 PRETTY_DEBUG << "Font loaded" << std::endl;
158 _fontPath = path;
159 PRECISE_DEBUG << "Font path updated" << std::endl;
160 _fontInstance = node;
161 PRECISE_DEBUG << "Font instance updated" << std::endl;
162 _fontInstanceSet = true;
163}
164
166{
167 _fontDefaultSize = size;
168}
169
170void GUI::ECS::Systems::Font::setFontApplication(const std::string application)
171{
172 _fontApplication = application;
173}
174
176{
177 return _bold;
178}
179
181{
182 return _italic;
183}
184
186{
187 return _fontName;
188}
189
191{
192 return _fontPath;
193}
194
196{
197 return _fontApplication;
198}
199
201{
202 return _fontDefaultSize;
203}
204
206{
207 if (!_fontInstanceSet) {
208 PRETTY_CRITICAL << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' " << "Error: Font instance not set." << std::endl;
209 throw CustomExceptions::NoFont(_fontName);
210 }
211 std::any node = std::make_any<std::shared_ptr<sf::Font>>(_fontInstance);
212 if (!node.has_value()) {
213 PRETTY_CRITICAL << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' " << "There is no font in the node" << std::endl;
214 } else {
215 PRETTY_SUCCESS << "There is a font in the node" << std::endl;
216 }
217 return node;
218}
219
220const std::string GUI::ECS::Systems::Font::getInfo(const unsigned int indent) const
221{
222
223 std::string indentation = "";
224 for (unsigned int i = 0; i < indent; ++i) {
225 indentation += "\t";
226 }
227 std::string result = indentation + "Font:\n";
228 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
229 result += indentation + "- Bold: " + Recoded::myToString(_bold) + "\n";
230 result += indentation + "- Italic: " + Recoded::myToString(_italic) + "\n";
231 result += indentation + "- Font Name: '" + _fontName + "'\n";
232 result += indentation + "- Font Path: '" + _fontPath + "'\n";
233 result += indentation + "- Font instance set: " + Recoded::myToString(_fontInstanceSet) + "\n";
234 result += indentation + "- Font Application: '" + _fontApplication + "'\n";
235 result += indentation + "- Default Size: " + Recoded::myToString(_fontDefaultSize) + "\n";
236 return result;
237}
238
240{
241 std::any systemFont = copy.getFontInstance();
242 if (!systemFont.has_value()) {
243 PRETTY_CRITICAL << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' "
244 << "No font found." << std::endl;
245 throw CustomExceptions::NoFont("<There is no sf::Font instance to manipulate>");
246 }
247 const std::string errMsg = "<There is no sf::Font instance to manipulate>, system error: ";
248 std::optional<std::shared_ptr<sf::Font>> fontCapsule = Utilities::unCast<std::shared_ptr<sf::Font>, CustomExceptions::NoFont>(systemFont, true, errMsg);
249 if (!fontCapsule.has_value()) {
250 throw CustomExceptions::NoFont(errMsg);
251 }
252 const std::shared_ptr<sf::Font> font = fontCapsule.value();
253 _fontInstance = font;
254 _fontInstanceSet = true;
255 _fontPath = copy.getFontPath();
256 _fontName = copy.getFontName();
257 _fontApplication = copy.getApplication();
258 _bold = copy.isBold();
259 _italic = copy.isItalic();
260 _fontDefaultSize = copy.getDefaultSize();
261}
262
264{
265 return _fontInstanceSet;
266}
267
269{
270 update(copy);
271 return *this;
272}
273
274std::ostream &GUI::ECS::Systems::operator<<(std::ostream &os, const GUI::ECS::Systems::Font &item)
275{
276 os << item.getInfo();
277 return os;
278}
Header file for the Font class, responsible for managing fonts within the program.
#define PRECISE_DEBUG
Debug log with precise details.
#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_SUCCESS
Success log with details and colour.
This is the class in charge of informing the user that the height they entered is invalid.
Definition Invalid.hpp:458
This is the class in charge of informing the user that they tried to access a non-existant font insta...
Definition No.hpp:358
Manages font entities in the GUI ECS.
Definition Font.hpp:45
const std::any getFontInstance() const
Retrieves the font instance.
Definition Font.cpp:205
void setFontPath(const std::string &path)
Sets the file path of the font and loads it.
Definition Font.cpp:144
const bool isItalic() const
Checks if the font is italic.
Definition Font.cpp:180
Font()
Default constructor.
Definition Font.cpp:16
void setFontApplication(const std::string application)
Sets the application or context for the font.
Definition Font.cpp:170
void update(const GUI::ECS::Systems::Font &copy)
Updates the font with properties from another font.
Definition Font.cpp:239
const bool isLoaded() const
Checks if the font instance is loaded.
Definition Font.cpp:263
const std::string getApplication() const
Retrieves the application context of the font.
Definition Font.cpp:195
void setFontName(const std::string &name)
Sets the font name.
Definition Font.cpp:139
const std::string getFontPath() const
Retrieves the file path of the font.
Definition Font.cpp:190
const std::string getFontName() const
Retrieves the font name.
Definition Font.cpp:185
~Font()
Destructor.
Definition Font.cpp:127
GUI::ECS::Systems::Font & operator=(const GUI::ECS::Systems::Font &copy)
Assignment operator.
Definition Font.cpp:268
void setItalic(const bool italic)
Sets the italic property of the font.
Definition Font.cpp:134
const unsigned int getDefaultSize() const
Retrieves the default size of the font.
Definition Font.cpp:200
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 ...
Definition Font.cpp:220
const bool isBold() const
Checks if the font is bold.
Definition Font.cpp:175
void setBold(const bool bold)
Sets the bold property of the font.
Definition Font.cpp:129
void setFontDefaultSize(const unsigned int size)
Sets the default size of the font.
Definition Font.cpp:165
std::ostream & operator<<(std::ostream &os, const Clock &item)
Outputs the clock's info to a stream.
Definition Clock.cpp:73
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