27
27
#include " Text.hpp"
28
28
29
29
Text::Text () : m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>(" texture-font" )) {
30
+ m_background.setFillColor (gk::Color::Transparent);
31
+
30
32
updateCharWidth ();
31
33
}
32
34
@@ -47,6 +49,10 @@ void Text::setColor(const gk::Color &color) {
47
49
void Text::draw (gk::RenderTarget &target, gk::RenderStates states) const {
48
50
states.transform *= getTransform ();
49
51
52
+ target.draw (m_background, states);
53
+
54
+ states.transform .translate (m_padding.x , m_padding.y );
55
+
50
56
for (const gk::Sprite &sprite : m_textSprites) {
51
57
target.draw (sprite, states);
52
58
}
@@ -56,12 +62,12 @@ void Text::draw(gk::RenderTarget &target, gk::RenderStates states) const {
56
62
void Text::updateTextSprites () {
57
63
m_textSprites.clear ();
58
64
59
- int x = 0 ;
60
- int y = 0 ;
61
- int maxX = 0 ;
65
+ unsigned int x = 0 ;
66
+ unsigned int y = 0 ;
67
+ unsigned int maxX = 0 ;
62
68
gk::Color color = gk::Color{70 , 70 , 70 , 255 };
63
69
for (char c : m_text) {
64
- if (c == ' \n ' ) {
70
+ if (c == ' \n ' || (m_maxLineLength && x + m_charWidth[(u8)c] >= m_maxLineLength) ) {
65
71
y += 9 ;
66
72
x = 0 ;
67
73
continue ;
@@ -78,7 +84,7 @@ void Text::updateTextSprites() {
78
84
y = 0 ;
79
85
color = m_color;
80
86
for (char c : m_text) {
81
- if (c == ' \n ' ) {
87
+ if (c == ' \n ' || (m_maxLineLength && x + m_charWidth[(u8)c] >= m_maxLineLength) ) {
82
88
maxX = std::max (x, maxX);
83
89
y += 9 ;
84
90
x = 0 ;
@@ -96,7 +102,12 @@ void Text::updateTextSprites() {
96
102
}
97
103
98
104
m_size.x = std::max (x, maxX);
99
- m_size.y = 8 + y * 9 ;
105
+ m_size.y = y + 9 ;
106
+
107
+ unsigned int backgroundX = std::max<int >(m_background.getSize ().x , m_size.x + m_padding.x );
108
+ unsigned int backgroundY = std::max<int >(m_background.getSize ().y , m_size.y + m_padding.y );
109
+
110
+ m_background.setSize (backgroundX, backgroundY);
100
111
}
101
112
102
113
// FIXME: Since I use the font from Minecraft assets, I needed to use
0 commit comments