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 "./raylib.hpp"
8#include "./RaylibException.hpp"
9#include "./raylib-cpp-utils.hpp"
10#include "./RadiansDegrees.hpp"
11#include "./Vector2.hpp"
12
13namespace raylib {
17class Text {
18 public:
22 std::string text;
23
27 float fontSize;
28
32 ::Color color;
33
37 ::Font font;
38
42 float spacing;
43
54 const std::string_view text = "",
55 float fontSize = 10,
56 const ::Color& color = WHITE,
57 const ::Font& font = ::GetFontDefault(),
58 float spacing = 0) :
59 text(text),
61 color(color),
62 font(font),
64 // Nothing.
65 }
66
77 const ::Font& font,
78 const std::string_view text = "",
79 float fontSize = 10,
80 float spacing = 0,
81 const ::Color& color = WHITE) :
82 text(text),
84 color(color),
85 font(font),
87 // Nothing.
88 }
89
90 GETTERSETTER(std::string, Text, text)
91 GETTERSETTER(float, FontSize, fontSize)
92 GETTERSETTER(::Font, Font, font)
93 GETTERSETTER(::Color, Color, color)
94 GETTERSETTER(float, Spacing, spacing)
95
99 void Draw(const ::Vector2& position) const {
100 ::DrawTextEx(font, text.c_str(), position, fontSize, spacing, color);
101 }
102
106 void Draw(int posX, int posY) const {
107 ::DrawTextEx(font,
108 text.c_str(),
109 { static_cast<float>(posX), static_cast<float>(posY) },
110 fontSize,
111 spacing,
112 color);
113 }
114
120 void Draw(const ::Vector2& position, Degree rotation, const ::Vector2& origin = {0, 0}) const {
121 ::DrawTextPro(font, text.c_str(), position, origin, rotation, fontSize, spacing, color);
122 }
123
127 int Measure() const {
128 return ::MeasureText(text.c_str(), static_cast<int>(fontSize));
129 }
130
135 return ::MeasureTextEx(font, text.c_str(), fontSize, spacing);
136 }
137
138 Text& operator=(const Text& other) {
139 if (this == &other) {
140 return *this;
141 }
142
143 text = other.text;
144 fontSize = other.fontSize;
145 color = other.color;
146 font = other.font;
147 spacing = other.spacing;
148
149 return *this;
150 }
151
157 static void Draw(
158 const std::string_view text,
159 const int posX,
160 const int posY,
161 const int fontSize,
162 const ::Color& color) {
163 ::DrawText(text.data(), posX, posY, fontSize, color);
164 }
165
171 static void Draw(
172 const std::string_view text,
173 const ::Vector2& pos,
174 const int fontSize,
175 const ::Color& color) {
176 ::DrawText(text.data(), static_cast<int>(pos.x), static_cast<int>(pos.y), fontSize, color);
177 }
178
184 static void Draw(
185 const ::Font& font,
186 const std::string_view text,
187 const ::Vector2& position,
188 const float fontSize,
189 const float spacing,
190 const ::Color& color) {
191 ::DrawTextEx(font, text.data(), position, fontSize, spacing, color);
192 }
193
199 static void Draw(
200 const ::Font& font,
201 const std::string_view text,
202 const ::Vector2& position,
203 const ::Vector2& origin,
204 const Degree rotation,
205 const float fontSize,
206 const float spacing,
207 const ::Color& color) {
208 ::DrawTextPro(font, text.data(), position, origin, rotation, fontSize, spacing, color);
209 }
210};
211} // namespace raylib
212
213using RText = raylib::Text;
214
215#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:17
float fontSize
The size of the text.
Definition: Text.hpp:27
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:157
Vector2 MeasureEx() const
Measure string size for Font.
Definition: Text.hpp:134
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:171
int Measure() const
Measure string width for default font.
Definition: Text.hpp:127
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:199
float spacing
The character spacing for the text.
Definition: Text.hpp:42
void Draw(const ::Vector2 &position, Degree rotation, const ::Vector2 &origin={0, 0}) const
Draw text using Font and pro parameters (rotation).
Definition: Text.hpp:120
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:184
::Font font
The internal raylib font to use for the text.
Definition: Text.hpp:37
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:53
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:76
::Color color
The color of the text.
Definition: Text.hpp:32
std::string text
The internal text.
Definition: Text.hpp:22
void Draw(int posX, int posY) const
Draw text with values in class.
Definition: Text.hpp:106
void Draw(const ::Vector2 &position) const
Draw text with values in class.
Definition: Text.hpp:99
Vector2 type.
Definition: Vector2.hpp:19