YAPOG  0.0.1
Yet Another Pokemon Online Game
TextBoxWidget.cpp
Go to the documentation of this file.
7 
8 namespace yap
9 {
11  : label_ (nullptr)
12  , curser_ (nullptr)
13  , curserPos_ (0)
14  , curserRelPos_ (0)
15  {
16  label_ = new Label ();
18  GetPosition ().y + label_->GetCharHeight () / 2));
19  }
20 
22  : label_ (nullptr)
23  , curser_ (nullptr)
24  , curserPos_ (0)
25  , curserRelPos_ (0)
26  {
27  label_ = new Label ();
28  if (size != 30)
29  SetTextSize (size);
31  Vector2 (
32  GetPosition ().x,
33  GetPosition ().y + label_->GetCharHeight () / 2));
34  SetText (content);
35  }
36 
38  {
39  }
40 
42  {
43  return true;
44  }
45 
47  {
48  label_->SetText ("");
49  curserRelPos_ = 0;
50  curserPos_ = 0;
51  content_ = "";
52  }
54  {
55  cursor.SetSize (Vector2 (cursor.GetSize ().x, label_->GetCharHeight ()));
56  cursor.SetPosition (GetPosition ());
57  curser_ = &cursor;
58  }
59 
61  {
63  GetPosition ().y));
64 
65  uint labelMaxWidth = GetUserSize ().x - padding_.left - padding_.right;
66  while (label_->GetSize ().x > labelMaxWidth)
67  {
68  label_->SetText (label_->GetText ().substr (1));
69  }
70 
72  }
73 
75  {
76  return GetUserSize ()
77  + ((border_ != nullptr) ? border_->GetSize () : Vector2 ());
78  }
79 
81  {
82  label_->Draw (context);
83 
84  if (curser_ != nullptr)
85  {
87  label_->Length () - curserRelPos_) - Vector2 (2, -2));
88  curser_->Draw (context);
89  }
90  }
91 
92  void TextBoxWidget::HandleShow (bool isVisible)
93  {
94  }
95 
96  void TextBoxWidget::HandleMove (const Vector2& offset)
97  {
98  label_->Move (offset);
99  if (curser_ != nullptr)
100  curser_->Move (offset);
101  //drawableText_.setPosition (GetPosition() + offset);
102  }
103 
104  void TextBoxWidget::HandleScale (const Vector2& factor)
105  {
106  label_->Scale (factor);
107  if (curser_ != nullptr)
108  curser_->Scale (factor);
109  }
110 
112  {
113  }
114 
115  void TextBoxWidget::HandleChangeColor (const sf::Color& color)
116  {
117  label_->ChangeColor (color);
118  }
119 
121  {
122  return content_;
123  }
124 
126  {
127  label_->SetTextSize (size);
128  if (curser_ != nullptr)
129  curser_->SetSize (Vector2 (curser_->GetSize ().x, label_->GetSize ().y));
130  Refresh ();
131  }
132  bool TextBoxWidget::HandleOnEvent (const GuiEvent& guiEvent)
133  {
134  if ((guiEvent.type == sf::Event::KeyPressed)
135  && !guiEvent.key.alt
136  && !guiEvent.key.control
137  && !(guiEvent.key.code == sf::Keyboard::Escape)
138  && !(guiEvent.key.code >= sf::Keyboard::F1
139  && guiEvent.key.code <= sf::Keyboard::F12))
140  {
141  if (guiEvent.key.code == sf::Keyboard::Back)
142  {
143  if (label_->Length () - curserRelPos_ == 0)
144  return true;
145  uint contentLength = content_.length ();
146  if (content_.length () > 0)
147  {
148  if (curserPos_ == 0)
149  content_ = content_.substr (0, content_.length () - 1);
150  else
151  {
152  String firstPart =
153  content_.substr (0, content_.length () - curserPos_ - 1);
154  String lastPart =
155  content_.substr (content_.length () - curserPos_);
156 
157  content_ = firstPart + lastPart;
158  }
159  }
160 
161  if (label_->Length () < contentLength)
162  {
163  if (curserRelPos_ == 0)
164  {
165  String temp;
166  if (content_.length () == label_->Length ())
167  temp = content_.substr (0, 1);
168  else
169  temp = content_.substr
170  (content_.length () - label_->Length () - 1, 1);
171  String temp2 = label_->GetText ();
172  label_->SetText (temp + temp2.substr (0, temp2.length () - 1));
173 
174  uint labelMaxWidth =
176 
177  Vector2 labelWidth (
179  - label_->GetPosition ());
180 
181  while (labelWidth.x > labelMaxWidth)
182  {
183  label_->SetText (label_->GetText ().substr (1));
184  labelWidth =
186  - label_->GetPosition ();
187  }
188  }
189  else
190  {
191  String temp = label_->GetText ();
192  String firstPart =
193  temp.substr (0, temp.length () - curserRelPos_ - 1);
194  String lastPart = temp.substr (temp.length () - curserRelPos_);
195 
196  int charleft = label_->Length () - curserRelPos_;
197  char first =
198  content_.at (content_.length () - curserPos_ - charleft);
199 
200  label_->SetText (first + firstPart + lastPart);
201  }
202  }
203  else
205 
206  return true;
207  }
208  if (guiEvent.key.code == sf::Keyboard::Left)
209  {
210  if (curserPos_ >= content_.length ())
211  return true;
212 
213  curserPos_++;
214  if (curserRelPos_ < label_->Length ())
215  curserRelPos_++;
216  else
217  {
218  int charleft = label_->Length () - curserRelPos_;
219  char first =
220  content_.at (content_.length () - curserPos_ - charleft);
221 
222  String temp = label_->GetText ().substr (0, label_->Length () - 1);
223 
224  label_->SetText (first + temp);
225  }
226 
227  return true;
228  }
229 
230  if (guiEvent.key.code == sf::Keyboard::Right)
231  {
232  if (curserPos_ == 0)
233  return true;
234 
235  if (curserRelPos_ > 0)
236  curserRelPos_--;
237  else
238  {
239  char end = content_.at (content_.length () - curserPos_);
240  label_->SetText (label_->GetText ().substr (1) + end);
241  }
242 
243  curserPos_--;
244 
245  return true;
246  }
247  return false;
248  }
249 
250  if (guiEvent.type == sf::Event::TextEntered)
251  {
252  char txt = static_cast<char> (guiEvent.text.unicode);
253  if (txt == '\b')
254  return true;
255  if (txt == '\t')
256  return false;
257  if (txt == '\r')
258  return false;
259  if (curserPos_ > 0)
260  {
261  String firstPart =
262  content_.substr (0, content_.length () - curserPos_);
263  String lastPart = content_.substr (content_.length () - curserPos_);
264 
265  content_ = firstPart + txt + lastPart;
266  }
267  else
268  content_ += txt;
269 
270  if (curserRelPos_ > 0)
271  {
272  String temp = label_->GetText ();
273 
274  if (temp.length () < curserRelPos_)
275  return true;
276 
277  String firstPart = temp.substr (0, temp.length () - curserRelPos_);
278  String lastPart = temp.substr (temp.length () - curserRelPos_);
279 
280  label_->SetText (firstPart + txt + lastPart);
281  }
282  else
283  label_->SetText (label_->GetText () + txt);
284 
285  uint labelMaxWidth = GetUserSize ().x - padding_.left - padding_.right;
286  Vector2 labelWidth (label_->CharPos (label_->Length ())
287  - label_->GetPosition ());
288  while (labelWidth.x > labelMaxWidth - 10)
289  {
290  label_->SetText (label_->GetText ().substr (1));
291  labelWidth = label_->CharPos (label_->Length ())
292  - label_->GetPosition ();
293  }
294  while (label_->GetSize ().x > labelMaxWidth - 10)
295  {
296  label_->SetText (label_->GetText ().substr (1));
297  }
298  return true;
299  }
300  return false;
301  }
302 
303  void TextBoxWidget::SetText (const String& contentArg)
304  {
305  if (contentArg.empty())
306  return;
307  else if (contentArg == content_)
308  return;
309 
310  content_ = contentArg;
312 
313  String contentTemp = content_;
314  uint labelMaxWidth = GetUserSize ().x - padding_.left - padding_.right;
315 
316  if (label_->GetSize ().x > labelMaxWidth)
317  {
318  uint endStrWidth = label_->GetSize ().x - labelMaxWidth;
319  uint posChar =
320  (label_->GetSize ().x - endStrWidth) * label_->GetCharWidth ();
321 
322  contentTemp = contentTemp.substr (posChar);
323  label_->SetText (contentTemp);
324 
325  uint size = content_.length () - 1;
326  while (label_->GetSize ().x < labelMaxWidth)
327  {
328  label_->SetText (content_.substr (0, size--) + contentTemp);
329  }
330 
331  while (label_->GetSize ().x > labelMaxWidth && labelMaxWidth != 0)
332  {
333  contentTemp = contentTemp.substr (1);
334  label_->SetText (contentTemp);
335  }
336  }
337  }
338 } // namespace yap