-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBox.cpp
More file actions
225 lines (164 loc) · 6.55 KB
/
TextBox.cpp
File metadata and controls
225 lines (164 loc) · 6.55 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
#include "TextBox.hpp"
#include "Resources.hpp"
TextBox::TextBox(){
lecturePointer = 0;
clicked = false;
is_clicked = false;
textFinished = false;
setPosition(0,0);
text.setString("");
}
std::string TextBox::getFractionText(std::string text, int ini, int end){
std::string ret = "";
for(int i = ini; i < end; ++i){
if(i >= int(text.size())) {
textFinished = true;
ret += " ";
}
else ret += text[i];
}
return ret;
}
bool TextBox::getTextFinished(){return textFinished;}
TextBox::TextBox(std::string myText, std::string texturePath, std::string fontPath, float sizeX, float sizeY): boxTexts(4, ""){
totalText = myText;
text.setString("OnCreationText");
clicked = false;
is_clicked = false;
textFinished = false;
if(!texture.loadFromFile(texturePath)) std::cerr << "Failed on load texture from " << texturePath << std::endl;
else sprite.setTexture(texture);
setSize(sizeX, sizeY);
if(!font.loadFromFile(fontPath)){ std::cerr << "Can't find the font file" << std::endl; }
else setFont(font);
setTextColor(sf::Color::Black);
}
void TextBox::setText(float charSize){
sf::FloatRect totalSpace;
totalSpace = sprite.getGlobalBounds();
sf::Vector2f spaces( totalSpace.width/10, totalSpace.height/8);
sf::IntRect box(totalSpace.left+spaces.x, totalSpace.top+spaces.y,
totalSpace.width-spaces.x, totalSpace.height-spaces.y);
sf::Vector2f writted(box.left,box.top);
int row = 0;
sf::Text aux;
aux.setCharacterSize(charSize);
aux.setFont(Resources::pauseMenuFont);
boxTexts = std::vector<std::string>();
while(! textFinished && writted.y+charSize < box.top+box.height){
float maxYsize = 1;
writted.x = box.left;
boxTexts.push_back("");
while(! textFinished && writted.x+charSize < box.left+box.width){
boxTexts[row] += (totalText[lecturePointer]);
aux.setString(totalText[lecturePointer]);
++lecturePointer;
if(lecturePointer > totalText.size()) textFinished = true;
writted.x += aux.getGlobalBounds().width;
if(aux.getGlobalBounds().height > maxYsize) maxYsize = aux.getGlobalBounds().height;
}
//remove and move the pointer to the last ' '
int last = boxTexts[row].size()-1;
while(! textFinished && boxTexts[row][last] != ' '){
--last;
--lecturePointer;
boxTexts[row].pop_back();
//if we are deletting the char which setted the size on max, it will not be recalculated, get reckt
}
++row;
writted.y += maxYsize;
}
//if(boxTexts.size() > 0) std::cout <<"first text "<< boxTexts[0] << std::endl;
}
void TextBox::setTextBestFit(std::string s = "Click", float charSize = 100){
totalText = s;
lecturePointer = 0;
textFinished = false;
text.setCharacterSize(charSize);
setText(charSize);
}
std::string TextBox::getText(){ return text.getString();}
void TextBox::draw(sf::RenderTarget& w){
w.draw(sprite);
sf::FloatRect totalSpace;
totalSpace = sprite.getGlobalBounds();
sf::Vector2f spaces( totalSpace.width/10, totalSpace.height/8);
float yPos = totalSpace.top;
for(int i = 0; i < int(boxTexts.size()); ++i){
text.setString(boxTexts[i]);
text.setPosition(sprite.getPosition().x+spaces.x, yPos+spaces.y/2 );
sf::Vector2f textSize(text.getLocalBounds().width, text.getLocalBounds().height);
float oldSize = text.getCharacterSize();
text.setCharacterSize(50);
text.setScale(textSize.x/text.getLocalBounds().width, textSize.y/text.getLocalBounds().height);
w.draw(text);
text.setCharacterSize(oldSize);
yPos += textSize.y;
}
}
void TextBox::setOrigin(sf::Vector2f origin){
sprite.setOrigin(origin);
}
void TextBox::setTexture(std::string name){
float sizeX = getSize().x;
float sizeY = getSize().y;
if(!texture.loadFromFile(name)) std::cerr << " texture not loaded on setTexture" << std::endl;
else sprite.setTexture(texture, true);
setSize(sizeX, sizeY);
}
void TextBox::setTexture(sf::Texture tex){
texture = tex;
sprite.setTexture(texture, true);
}
//TODO change it so with a key it can be skiped and not with mouse and so.
void TextBox::handleEvent(sf::Event e){
float delayx, delayy;
delayx = sprite.getOrigin().x*sprite.getScale().x;
delayy = sprite.getOrigin().y*sprite.getScale().y;
sprite.move(-delayx, -delayy);
if(e.type == sf::Event::MouseButtonPressed){
}
if(e.type == sf::Event::MouseButtonReleased){
if (e.mouseButton.button == sf::Mouse::Left) {
is_clicked = false;
}
}
if(e.type == sf::Event::KeyPressed) {
if(e.key.code == sf::Keyboard::Space) {
clicked = true;
is_clicked = true;
}
}
if(e.type == sf::Event::KeyReleased) {
if(e.key.code == sf::Keyboard::Space && !textFinished) {
is_clicked = false;
setText(getCharacterSize());
}
}
sprite.move(delayx, delayy);
}
void TextBox::setTextColor(sf::Color c){text.setColor(c); }
sf::Color TextBox::getTextColor(){ return text.getColor();}
void TextBox::setCharacterSize(int u){ text.setCharacterSize(u); }
int TextBox::getCharacterSize(){ return text.getCharacterSize(); }
void TextBox::setFont(sf::Font f){ font = f; text.setFont(font); }
sf::Vector2f TextBox::getPosition(){ return sprite.getPosition(); }
void TextBox::setPosition(float x, float y){ setPosition(sf::Vector2f(x, y)); }
void TextBox::setPosition(sf::Vector2f position){
sprite.setPosition(position);
text.setPosition(position.x - (sprite.getGlobalBounds().width * (sprite.getOrigin().x/sprite.getLocalBounds().width) ) +
sprite.getGlobalBounds().width/2 -
text.getGlobalBounds().width/2,
position.y - (sprite.getGlobalBounds().height * (sprite.getOrigin().y/sprite.getLocalBounds().height) ) + sprite.getGlobalBounds().height/2 - (text.getGlobalBounds().height/2));
}
sf::Vector2f TextBox::getSize(){ return sf::Vector2f(sprite.getGlobalBounds().width,sprite.getGlobalBounds().height); }
bool TextBox::hasBeenClicked(){
bool r = clicked;
if(clicked) clicked = false;
return r;
}
void TextBox::setSize(float x, float y){ setSize(sf::Vector2f(x,y)); }
void TextBox::setSize(sf::Vector2f size){
//std::cout << "setSize" << std::endl;
sprite.setScale(size.x/sprite.getLocalBounds().width, size.y/sprite.getLocalBounds().height);
}