forked from skyrpex/RichText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRichText.cpp
241 lines (204 loc) · 6.46 KB
/
RichText.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
////////////////////////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////////////////////////
#include "RichText.hpp"
#include <Sfml/Graphics/RenderTarget.hpp>
#include <iostream>
namespace sfe
{
RichText::RichText()
: myCurrentColor(sf::Color::White),
myCurrentStyle(sf::Text::Regular),
mySizeUpdated(false),
myPositionUpdated(false)
{
}
////////////////////////////////////////////////////////////////////////////////
// Operator << sf::Color
////////////////////////////////////////////////////////////////////////////////
RichText & RichText::operator << (const sf::Color &color)
{
myCurrentColor = color;
return *this;
}
////////////////////////////////////////////////////////////////////////////////
// Operator << sf::Text::Style
////////////////////////////////////////////////////////////////////////////////
RichText & RichText::operator << (sf::Text::Style style)
{
myCurrentStyle = style;
return *this;
}
////////////////////////////////////////////////////////////////////////////////
// Operator << sf::String
////////////////////////////////////////////////////////////////////////////////
/*
** Must parse the strings to look for '\n' characters. If found, we break
** the string into two pieces.
*/
RichText & RichText::operator << (const sf::String &string)
{
// It is not updated
mySizeUpdated = false;
myPositionUpdated = false;
// Find \n characters (assert)
//assert(string.Find('\n') == std::string::npos);
if(string.find('\n') != std::string::npos)
std::cerr << "sfe::RichtText: Oops, character \n found."
"You will get visual errors." << std::endl;
// Add string
myTexts.resize(myTexts.size()+1);
// Setup string
sf::Text &text = *(--myTexts.end());
text.setColor(myCurrentColor);
text.setStyle(myCurrentStyle);
text.setString(string);
// Return
return *this;
}
////////////////////////////////////////////////////////////////////////////////
// Set size
////////////////////////////////////////////////////////////////////////////////
void RichText::setCharacterSize(unsigned int size)
{
// Set character size
for(std::list<sf::Text>::iterator it = myTexts.begin();
it != myTexts.end(); ++it)
{
it->setCharacterSize(size);
}
// It is not updated
mySizeUpdated = false;
myPositionUpdated = false;
}
////////////////////////////////////////////////////////////////////////////////
// Set font
////////////////////////////////////////////////////////////////////////////////
void RichText::setFont(const sf::Font &font)
{
// Set character size
for(std::list<sf::Text>::iterator it = myTexts.begin();
it != myTexts.end(); ++it)
{
it->setFont(font);
}
// It is not updated
mySizeUpdated = false;
myPositionUpdated = false;
}
////////////////////////////////////////////////////////////////////////////////
// Clear
////////////////////////////////////////////////////////////////////////////////
void RichText::clear()
{
// Clear text list
myTexts.clear();
// Reset size
mySize = sf::Vector2f(0.f, 0.f);
// It is updated
mySizeUpdated = true;
myPositionUpdated = true;
}
////////////////////////////////////////////////////////////////////////////////
// Get text list
////////////////////////////////////////////////////////////////////////////////
const std::list<sf::Text> & RichText::getTextList() const
{
return myTexts;
}
////////////////////////////////////////////////////////////////////////////////
// Get character size
////////////////////////////////////////////////////////////////////////////////
unsigned int RichText::getCharacterSize() const
{
if(myTexts.size()) return myTexts.begin()->getCharacterSize();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Get font
////////////////////////////////////////////////////////////////////////////////
const sf::Font & RichText::getFont() const
{
if(myTexts.size()) return myTexts.begin()->getFont();
return sf::Font::getDefaultFont();
}
////////////////////////////////////////////////////////////////////////////////
// Get width
////////////////////////////////////////////////////////////////////////////////
float RichText::getWidth() const
{
updateSize();
return mySize.x;
}
////////////////////////////////////////////////////////////////////////////////
// Get height
////////////////////////////////////////////////////////////////////////////////
float RichText::getHeight() const
{
updateSize();
return mySize.y;
}
////////////////////////////////////////////////////////////////////////////////
// Render
////////////////////////////////////////////////////////////////////////////////
void RichText::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Update position
updatePosition();
states.transform *= getTransform();
// Draw
for(std::list<sf::Text>::const_iterator it = myTexts.begin();
it != myTexts.end(); ++it)
{
// Add transformation
//it->setT
// Draw text
target.draw(*it, states);
}
}
////////////////////////////////////////////////////////////////////////////////
// Update size
////////////////////////////////////////////////////////////////////////////////
void RichText::updateSize() const
{
// Return if updated
if(mySizeUpdated) return;
// Return if empty
if(myTexts.begin() == myTexts.end()) return;
// It is updated
mySizeUpdated = true;
// Sum all sizes (height not implemented)
mySize.x = 0.f;
mySize.y = myTexts.begin()->getGlobalBounds().height;
for(std::list<sf::Text>::const_iterator it = myTexts.begin();
it != myTexts.end(); ++it)
{
// Update width
mySize.x += it->getGlobalBounds().width;
}
}
////////////////////////////////////////////////////////////////////////////////
// Update position
////////////////////////////////////////////////////////////////////////////////
void RichText::updatePosition() const
{
// Return if updated
if(myPositionUpdated) return;
// Return if empty
if(myTexts.begin() == myTexts.end()) return;
// It is updated
myPositionUpdated = true;
// Get starting position
sf::Vector2f offset;
// Draw
for(std::list<sf::Text>::iterator it = myTexts.begin();
it != myTexts.end(); ++it)
{
// Set all the origins to the first one
it->setOrigin(it->getPosition() - myTexts.begin()->getPosition() - offset);
// Set offset
const sf::FloatRect rect = it->getGlobalBounds();
offset.x += rect.width;
}
}
}