raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Text.hpp
1#ifndef RAYLIB_CPP_INCLUDE_TEXT_HPP_
2#define RAYLIB_CPP_INCLUDE_TEXT_HPP_
3
4#include <string>
5#include <string_view>
6
7#include "./RaylibException.hpp"
8#include "./raylib-cpp-utils.hpp"
9#include "./RadiansDegrees.hpp"
10#include "./Vector2.hpp"
11
12namespace raylib {
16class Text {
17public:
21 std::string text;
22
26 float fontSize;
27
31 ::Color color;
32
36 ::Font font;
37
41 float spacing;
42
53 const std::string_view text = "",
54 float fontSize = 10,
55 const ::Color& color = WHITE,
56 const ::Font& font = ::GetFontDefault(),
57 float spacing = 0) :
58 text(text),
60 color(color),
61 font(font),
63 // Nothing.
64 }
65
76 const ::Font& font,
77 const std::string_view text = "",
78 float fontSize = 10,
79 float spacing = 0,
80 const ::Color& color = WHITE) :
81 text(text),
83 color(color),
84 font(font),
86 // Nothing.
87 }
88
89 GETTERSETTER(const std::string&, Text, text)
90 GETTERSETTER(float, FontSize, fontSize)
91 GETTERSETTER(::Font, Font, font)
92 GETTERSETTER(::Color, Color, color)
93 GETTERSETTER(float, Spacing, spacing)
94
98 void Draw(const ::Vector2& position) const { ::DrawTextEx(font, text.c_str(), position, fontSize, spacing, color); }
99
103 void Draw(int posX, int posY) const {
105 font,
106 text.c_str(),
107 {static_cast<float>(posX), static_cast<float>(posY)},
108 fontSize,
109 spacing,
110 color);
111 }
112
118 void Draw(const ::Vector2& position, Degree rotation, const ::Vector2& origin = {0, 0}) const {
119 ::DrawTextPro(font, text.c_str(), position, origin, rotation, fontSize, spacing, color);
120 }
121
125 [[nodiscard]] int Measure() const { return ::MeasureText(text.c_str(), static_cast<int>(fontSize)); }
126
130 [[nodiscard]] Vector2 MeasureEx() const { return ::MeasureTextEx(font, text.c_str(), fontSize, spacing); }
131
132 Text& operator=(const Text& other) {
133 if (this == &other) {
134 return *this;
135 }
136
137 text = other.text;
138 fontSize = other.fontSize;
139 color = other.color;
140 font = other.font;
141 spacing = other.spacing;
142
143 return *this;
144 }
145
151 static void Draw(
152 const std::string_view text,
153 const int posX,
154 const int posY,
155 const int fontSize,
156 const ::Color& color) {
157 ::DrawText(text.data(), posX, posY, fontSize, color);
158 }
159
165 static void Draw(
166 const std::string_view text,
167 const ::Vector2& pos,
168 const int fontSize,
169 const ::Color& color) {
170 ::DrawText(text.data(), static_cast<int>(pos.x), static_cast<int>(pos.y), fontSize, color);
171 }
172
178 static void Draw(
179 const ::Font& font,
180 const std::string_view text,
181 const ::Vector2& position,
182 const float fontSize,
183 const float spacing,
184 const ::Color& color) {
185 ::DrawTextEx(font, text.data(), position, fontSize, spacing, color);
186 }
187
193 static void Draw(
194 const ::Font& font,
195 const std::string_view text,
196 const ::Vector2& position,
197 const ::Vector2& origin,
198 const Degree rotation,
199 const float fontSize,
200 const float spacing,
201 const ::Color& color) {
202 ::DrawTextPro(font, text.data(), position, origin, rotation, fontSize, spacing, color);
203 }
204};
205} // namespace raylib
206
207using RText = raylib::Text;
208
209#endif // RAYLIB_CPP_INCLUDE_TEXT_HPP_
Color type, RGBA (32bit)
Definition: Color.hpp:16
Degree type (allows automatic worry free conversion between radians and degrees)
Font type, includes texture and charSet array data.
Definition: Font.hpp:18
Text Functions.
Definition: Text.hpp:16
float fontSize
The size of the text.
Definition: Text.hpp:26
static void Draw(const std::string_view text, const int posX, const int posY, const int fontSize, const ::Color &color)
Draw text using font and color.
Definition: Text.hpp:151
Vector2 MeasureEx() const
Measure string size for Font.
Definition: Text.hpp:130
static void Draw(const std::string_view text, const ::Vector2 &pos, const int fontSize, const ::Color &color)
Draw text using font and color, with position defined as Vector2.
Definition: Text.hpp:165
int Measure() const
Measure string width for default font.
Definition: Text.hpp:125
static void Draw(const ::Font &font, const std::string_view text, const ::Vector2 &position, const ::Vector2 &origin, const Degree rotation, const float fontSize, const float spacing, const ::Color &color)
Draw text using font, color, position, origin, font size and spacing.
Definition: Text.hpp:193
float spacing
The character spacing for the text.
Definition: Text.hpp:41
void Draw(const ::Vector2 &position, Degree rotation, const ::Vector2 &origin={0, 0}) const
Draw text using Font and pro parameters (rotation).
Definition: Text.hpp:118
static void Draw(const ::Font &font, const std::string_view text, const ::Vector2 &position, const float fontSize, const float spacing, const ::Color &color)
Draw text using font, color, position, font size and spacing.
Definition: Text.hpp:178
::Font font
The internal raylib font to use for the text.
Definition: Text.hpp:36
Text(const std::string_view text="", float fontSize=10, const ::Color &color=WHITE, const ::Font &font=::GetFontDefault(), float spacing=0)
Initializes a new Text object.
Definition: Text.hpp:52
Text(const ::Font &font, const std::string_view text="", float fontSize=10, float spacing=0, const ::Color &color=WHITE)
Initializes a new Text object with a custom font.
Definition: Text.hpp:75
::Color color
The color of the text.
Definition: Text.hpp:31
std::string text
The internal text.
Definition: Text.hpp:21
void Draw(int posX, int posY) const
Draw text with values in class.
Definition: Text.hpp:103
void Draw(const ::Vector2 &position) const
Draw text with values in class.
Definition: Text.hpp:98
Vector2 type.
Definition: Vector2.hpp:20
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
static void DrawText(const char *text, int posX, int posY, int fontSize, ::Color color)
Draw text (using default font)
Definition: Functions.hpp:253
static void DrawTextPro(const Font &font, const char *text, Vector2 position, Vector2 origin, Degree rotation, float fontSize, float spacing, ::Color tint)
Draw text using Font and pro parameters (rotation)
Definition: Functions.hpp:283
static void DrawTextEx(const Font &font, char *text, Vector2 position, float fontSize, float spacing, ::Color tint)
Draw text using font and additional parameters.
Definition: Functions.hpp:268