-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiText.cpp
More file actions
332 lines (227 loc) · 7.13 KB
/
MultiText.cpp
File metadata and controls
332 lines (227 loc) · 7.13 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
// Created by Kristal Hong on 11/1/23.
//
#include "MultiText.h"
MultiText::MultiText()
{
// Initialize the cursor
cursor.setFont(Fonts::getFont(OPEN_SANS));
cursor.setString("|");
cursor.setFillColor(sf::Color::Black);
setCursorPosition({60, 535});
initialPosition = {60, 535};
cursorBlinkTimer.restart();
cursorVisible = false;
cursorBlinkInterval = 0.5f;
//
}
MultiText::MultiText(Fonts &fonts, const sf::Vector2f &position) {
//
cursorBlinkTimer.restart();
cursorVisible = false;
cursorBlinkInterval = 0.5f;
}
float MultiText::getTextWidth(const sf::Font& font, unsigned int characterSize, const std::string& text) {
float totalWidth = 0.0f;
for (char character : text) {
sf::Glyph glyph = font.getGlyph(character, characterSize, 0); // Use style 0 for plain style
totalWidth += static_cast<float>(glyph.advance);
}
return totalWidth;
}
bool MultiText::isNumber(char c) {
return (c >= '0' && c <= '9');
}
void MultiText::push(char letter) {
sf::Color charColor;
Letter newLetter(letter);
if(text.empty()){
currLetterUnicode = letter;
newLetter.setPosition(60, 535);
}
else {
prevLetterUnicode = currLetterUnicode;
currLetterUnicode = letter;
lastLetter = text.back();
prevLetterGlyph = text.back().getFont()->getGlyph(prevLetterUnicode, 24, "bold") ;
if (currLetterUnicode == '\n') {
numNewLine++;
lineHeight = numNewLine * 30.f;
lastLetter.setPosition(initialPosition.x - (2 * prevLetterGlyph.advance), initialPosition.y + lineHeight);
addLineBreak();
}
sf::Vector2f prevPosition = lastLetter.getPosition();
newLetter.setPosition(prevPosition.x+prevLetterGlyph.advance, prevPosition.y);
}
text.push_back(newLetter);
if (!text.empty()) {
sf::Vector2f cursorPosition = back().getPosition();
cursor.setPosition(cursorPosition.x + back().getGlobalBounds().width, cursorPosition.y);
}
// Restart the cursor blink timer when a character is added
cursorBlinkTimer.restart();
cursorVisible = true;
}
Letter &MultiText::back() {
return text.back();
}
void MultiText::push(Letter letter) {
if(text.empty()){
// currLetterUnicode = letter;
letter.setPosition(60, 535);
}
else {
prevLetterUnicode = currLetterUnicode;
// currLetterUnicode = letter;
lastLetter = text.back();
prevLetterGlyph = Fonts::getFont(OPEN_SANS).getGlyph(prevLetterUnicode, 24, "bold");
sf::Vector2f prevPosition = lastLetter.getPosition();
letter.setPosition(prevPosition.x+prevLetterGlyph.advance, 535);
}
text.push_back(letter);
if (!text.empty()) {
sf::Vector2f cursorPosition = text.back().getPosition();
cursor.setPosition(cursorPosition.x + text.back().getGlobalBounds().width, cursorPosition.y);
}
// Restart the cursor blink timer when a character is added
cursorBlinkTimer.restart();
cursorVisible = true;
}
MultiText::iterator MultiText::begin() {
return text.begin();
}
MultiText::iterator MultiText::end() {
return text.end();
}
void MultiText::draw(sf::RenderTarget& window, sf::RenderStates states) const {
for (const Letter& letter : text) {
window.draw(letter, states);
}
// Draw the cursor only if it's currently visible
if (cursorVisible) {
window.draw(cursor, states);
}
}
bool MultiText::empty() {
return text.empty();
}
void MultiText::pop() {
if (!text.empty()) {
//
lastLetter = text.back();
if (!text.empty()) {
sf::Vector2f cursorPosition = lastLetter.getPosition();
text.pop_back();
cursor.setPosition(cursorPosition.x - (0.5 * text.back().getGlobalBounds().width), cursorPosition.y);
if(lastLetter.getChar() == '\n') {
numNewLine--;
}
} else {
cursor.setPosition(60, 535); // Adjust the position as needed
}
}
}
void MultiText::setPosition(const sf::Vector2f& position) {
float xOffset = position.x;
for (Letter& letter : text) {
letter.setPosition({xOffset, position.y});
xOffset += letter.getGlobalBounds().width;
}
}
sf::Vector2f MultiText::getPosition() const {
if (!text.empty()) {
return text.front().getPosition();
}
return sf::Vector2f(0.f, 0.f);
}
void MultiText::addLineBreak() {
int height = 30.0f;
sf::Vector2f cursorPosition = cursor.getPosition();
cursorPosition.x = initialPosition.x;
cursorPosition.y += height;
cursor.setPosition(cursorPosition);
}
void MultiText::minusLineBreak() {
int height = 30.0f;
sf::Vector2f cursorPosition = cursor.getPosition();
cursorPosition.x = lastLetter.getPosition().x;
cursorPosition.y -= height;
cursor.setPosition(cursorPosition);
}
std::string MultiText::getString() {
std::string newText;
for (Letter& letter : text) {
newText += letter.getString();
}
return newText;
}
void MultiText::setString(const std::string& newText) {
// Clear the existing text
text.clear();
for (char c: newText) {
push(c);
}
}
void MultiText::updateCursorBlink() {
if (cursorBlinkTimer.getElapsedTime().asSeconds() >= cursorBlinkInterval) {
cursorVisible = !cursorVisible;
cursorBlinkTimer.restart();
}
}
void MultiText::setCursorPosition(sf::Vector2f position) {
cursor.setPosition(position);
}
void MultiText::setLastCharacterColor(sf::Color color) {
if (!text.empty()) {
text.back().setColor(color);
}
}
void MultiText::enableState(statesEnum state) {
cursorState.enableState(state);
}
void MultiText::disableState(statesEnum state) {
cursorState.disableState(state);
}
bool MultiText::checkState(statesEnum state) const {
return cursorState.checkState(state);
}
void MultiText::setCursorStatus() {
if (cursorState.checkState(HIDDEN)) {
cursorVisible = !cursorVisible;
setCursorPosition({-100, -100});
} else {
sf::Vector2f rePosition = text.back().getPosition();
setCursorPosition({rePosition.x + text.back().getGlobalBounds().width, rePosition.y});
}
}
std::string MultiText::getWord(MultiText::iterator begin, MultiText::iterator end) {
std::string word;
// std::cout << "Accessing function";
for (auto it = begin; it != end; ++it) {
word += it->getChar();
// std::cout << "Character:" << word << std::endl;
}
return word;
}
bool MultiText::isLineBreak(char character) {
return character == '\n';
}
std::string MultiText::getLastNCharacters(std::size_t n) const {
std::string result;
auto it = text.end();
for (std::size_t i = 0; i < n && it != text.begin(); ++i) {
--it;
}
for (; it != text.end(); ++it) {
result += it->getChar();
}
return result;
}
void MultiText::updateFont(fontEnum font) {
for(auto x = text.begin(); x != text.end(); ++x){
x->setFont(Fonts::getFont(font));
}
}
Letter& MultiText::getLastCharacter() {
return lastLetter;
}