YAPOG  0.0.1
Yet Another Pokemon Online Game
BaseWidget.cpp
Go to the documentation of this file.
6 
7 namespace yap
8 {
9  const bool BaseWidget::DEFAULT_VISIBLE_STATE = true;
10  const bool BaseWidget::DEFAULT_ENABLED_STATE = true;
11 
12  const sf::Color BaseWidget::DEFAULT_COLOR = sf::Color::Black;
13 
15  : spatialInfo_ ()
16  , isVisible_ (DEFAULT_VISIBLE_STATE)
17  , isEnabled_ (DEFAULT_ENABLED_STATE)
18  , color_ (DEFAULT_COLOR)
19  , drawables_ ()
20  , eventHandlers_ ()
21  , childen_ ()
22  , root_ (nullptr)
23  , parent_ (nullptr)
24  , padding_ ()
25  , background_ (nullptr)
26  , border_ (nullptr)
27  , userSize_ (0, 0)
28  , isExtensible_ (false)
29  , isFocused_ (true)
30  , userColor_ (DEFAULT_COLOR)
31  , isChangeColorCall_ (false)
32  {
33  }
34 
36  {
37  }
38 
40  {
41  return border_;
42  }
44  {
45  return spatialInfo_.GetPosition ();
46  }
47 
48  const Vector2& BaseWidget::GetSize () const
49  {
51 
52  return spatialInfo_.GetSize ();
53  }
54 
56  {
57  return spatialInfo_.GetTopLeft ();
58  }
59 
61  {
62  return spatialInfo_.GetBottomRight ();
63  }
64 
66  {
67  return spatialInfo_.GetCenter ();
68  }
69 
71  {
72  return spatialInfo_.GetRectangle ();
73  }
74 
75  void BaseWidget::Move (const Vector2& offset)
76  {
77  for (IWidget* child : childen_)
78  {
79  child->Move (offset);
80  }
81 
82  if (border_ != nullptr)
83  border_->Move (offset);
84 
85  if (background_ != nullptr)
86  background_->Move (offset);
87 
88  spatialInfo_.SetPosition (GetPosition () + offset);
89  OnMoved (*this, EventArgs (offset));
90  HandleMove (offset);
91  }
92 
93  void BaseWidget::Scale (const Vector2& factor)
94  {
95  for (IWidget* child : childen_)
96  {
97  child->Scale (factor);
98  }
99  if (border_ != nullptr)
100  border_->Scale (factor);
101  if (background_ != nullptr)
102  background_->Scale (factor);
103 
105  Vector2 (
106  GetSize ().x * factor.x,
107  GetSize ().y * factor.y));
108 
109  OnScaled (*this, EventArgs (factor));
110  HandleScale (factor);
111 
112  Refresh ();
113  }
114 
115  void BaseWidget::SetPosition (const Vector2& position)
116  {
117  Move (position - GetPosition ());
118  }
119 
121  {
122  background_ = nullptr;
123  }
125  {
126  border_ = nullptr;
127  }
128 
129  void BaseWidget::SetSize (const Vector2& size)
130  {
131  userSize_ = size;
132  Vector2 scaleFactor (size.x / GetSize ().x,
133  size.y / GetSize ().y);
134 
135  for (IWidget* child : childen_)
136  {
137  child->SetSize (size);
138  }
139  if (border_ != nullptr)
140  border_->SetSize (size);
141  if (background_ != nullptr)
142  background_->SetSize (size);
143 
145  Vector2 (
146  size.x,
147  size.y));
148 
149  OnSizeSet (*this, EventArgs (size));
150  HandleScale (scaleFactor);
151 
152  Refresh ();
153  }
154 
156  {
157  if (!isVisible_)
158  return;
159  if (background_ != nullptr)
160  background_->Draw (context);
161  if (border_ != nullptr)
162  border_->Draw (context);
163 
164  OnDraw (*this, EventArgsDraw (context));
165  HandleDraw (context);
166 
167  for (IDrawable* child : drawables_)
168  {
169  child->Draw (context);
170  }
171  }
172 
174  {
175  return userSize_;
176  }
177 
179  {
180  if (parent_ == nullptr)
181  return;
182 
183  parent_->RemoveChild (*this);
184  parent_->FrontAddChild (*this);
185 
186  parent_->SetToTop ();
187  }
188 
189  bool BaseWidget::IsVisible () const
190  {
191  return isVisible_;
192  }
193 
194  void BaseWidget::SetFocused (bool state)
195  {
196  isFocused_ = state;
197  }
198 
199 
200  void BaseWidget::Show (bool isVisible)
201  {
202  isVisible_ = isVisible;
203 
204  HandleShow (isVisible);
205  }
206 
207  void BaseWidget::ChangeColor (const sf::Color& color)
208  {
209  isChangeColorCall_ = true;
210  for (IWidget* child : childen_)
211  {
212  child->ChangeColor (color);
213  }
214  if (border_ != nullptr)
215  border_->ChangeColor (color);
216  if (background_ != nullptr)
217  background_->ChangeColor (color);
218 
219  color_ = color;
220  OnColorChanged (*this, EventArgsColor (color));
221  HandleChangeColor (color);
222  }
223 
224  bool BaseWidget::OnEvent (const GuiEvent& guiEvent)
225  {
226  if (!isEnabled_)
227  return false;
228 
229  for (IEventHandler* child : eventHandlers_)
230  {
231  if (child->OnEvent (guiEvent))
232  return true;
233  }
234  if (border_ != nullptr)
235  if (border_->OnEvent (guiEvent))
236  return true;
237  if (background_ != nullptr)
238  if (background_->OnEvent (guiEvent))
239  return true;
240 
241  return HandleOnEvent (guiEvent);
242  }
243 
244  bool BaseWidget::OnPriorityEvent (const GuiEvent& guiEvent)
245  {
246  if (!isEnabled_)
247  return false;
248 
249  if (HandleOnPriorityEvent (guiEvent))
250  return true;
251 
252  for (IEventHandler* child : eventHandlers_)
253  {
254  if (child->OnPriorityEvent (guiEvent))
255  return true;
256  }
257  if (border_ != nullptr)
258  if (border_->OnPriorityEvent (guiEvent))
259  return true;
260  if (background_ != nullptr)
261  if (background_->OnPriorityEvent (guiEvent))
262  return true;
263 
264  return false;
265  }
266 
267  void BaseWidget::SetDefaultColor (const sf::Color& color)
268  {
269  userColor_ = color;
270  }
271 
272  void BaseWidget::Update (const Time& dt)
273  {
274  for (IWidget* child : childen_)
275  {
276  child->Update (dt);
277  }
278  if (border_ != nullptr)
279  border_->Update (dt);
280  if (background_ != nullptr)
281  background_->Update (dt);
282 
283  HandleUpdate (dt);
284  }
285 
287  {
288  return spatialInfo_.GetSize ();
289  }
290 
292  {
293  drawables_.Add (&drawable);
294  }
295 
297  {
298  child.SetDefaultColor (userColor_);
299  childen_.Add (&child);
300  AddDrawable (child);
301  updatables_.Add (&child);
302  eventHandlers_.Add (&child);
303 
304  //child.SetPosition (GetPosition ());
305  child.SetParent (*this);
306  OnChildAdded (*this, EventArgsIWidget (child));
307  }
308 
310  {
311  child.SetParent (*this);
312 
313  child.SetDefaultColor (userColor_);
314 
315  childen_.AddFront (&child);
316  drawables_.Add (&child);
317  updatables_.AddFront (&child);
318  eventHandlers_.AddFront (&child);
319 
320  OnChildAdded (*this, EventArgsIWidget (child));
321  }
322 
324  {
325  childen_.Remove (&child);
326  drawables_.Remove (&child);
327  updatables_.Remove (&child);
328  eventHandlers_.Remove (&child);
329  }
330 
332  {
333  return *root_;
334  }
335 
337  {
338  parent_ = &parent;
339  root_ = &parent.GetRoot ();
340  }
341 
342  void BaseWidget::SetPadding (const Padding& padding)
343  {
344  padding_ = padding;
345  Refresh ();
346  }
347 
349  {
350  if (parent_ != nullptr)
351  parent_->Refresh ();
352  }
353 
355  {
356  background_= &background;
358  if (background_->GetFixed ())
361  }
362 
363 
364  void BaseWidget::SetPosAfterBorder (uint width, uint height, bool refreshing)
365  {
366  if (spatialInfo_.GetPosition ().x > width)
367  spatialInfo_.SetPosition (GetPosition () - Vector2 (width, 0));
368  else
369  {
370  Move (Vector2 (width - GetPosition ().x, 0));
371  spatialInfo_.SetPosition (GetPosition () - Vector2 (width/2, 0));
372  }
373  if (spatialInfo_.GetPosition ().y > height)
374  spatialInfo_.SetPosition (GetPosition () - Vector2 (0, height));
375  else
376  {
377  Move (Vector2 (0, height - GetPosition ().y));
378  spatialInfo_.SetPosition (GetPosition () - Vector2 (0, height/2));
379  }
380  if (refreshing)
381  Refresh ();
382  }
383 
385  {
386  border_ = &border;
388  if (isExtensible_ || GetUserSize () == Vector2 (0, 0))
389  border_->SetBorder (GetSize (), width);
390  else
391  border_->SetBorder (GetUserSize (), width);
392 
393  uint paddingBorder = border.GetTexture ().GetSize ().y > 0
394  ? border.GetTexture ().GetSize ().y : border.GetTexture ().GetSize ().x;
395  if (border.GetTexture ().GetSize ().y == 0)
396  SetPosAfterBorder (paddingBorder, 0, true);
397  else
398  SetPosAfterBorder (paddingBorder, paddingBorder, true);
399  }
400 
402  {
403  if (border_ == nullptr)
404  return;
405 
407  if (isExtensible_ || GetUserSize () == Vector2 (0, 0))
408  border_->SetBorder (GetSize ());
409  else
411  uint paddingBorder = border_->GetSize ().y > 0
412  ? border_->GetSize ().y : border_->GetSize ().x;
413  /* if (border_->GetSize ().y == 0)
414  SetPosAfterBorder (paddingBorder, 0, false);
415  else
416  SetPosAfterBorder (paddingBorder, paddingBorder, false);*/
417  }
419  {
420 
421  border.SetPosition (GetPosition ());
422  if (isExtensible_ || GetUserSize () == Vector2 (0, 0))
423  border.SetBorder (GetSize ());
424  else
425  border.SetBorder (GetUserSize ());
426  border_ = &border;
427  uint paddingBorder = border.GetSize ().y > 0
428  ? border.GetSize ().y : border.GetSize ().x;
429  if (border.GetSize ().y == 0)
430  SetPosAfterBorder (paddingBorder, 0, true);
431  else
432  SetPosAfterBorder (paddingBorder, paddingBorder, true);
433  }
434 
435  bool BaseWidget::HandleOnEvent (const GuiEvent& guiEvent)
436  {
437  return false;
438  }
439 
441  {
442  return false;
443  }
444 
445  void BaseWidget::SetEnable (bool enable)
446  {
447  isEnabled_ = enable;
448  }
449 
451  {
452  SetEnable (true);
453  Show (true);
454  }
455 
457  {
458  SetEnable (false);
459  Show (false);
460  }
461 
462  void BaseWidget::HandleMove (const Vector2& offset)
463  {
464  }
465 
466  void BaseWidget::HandleScale (const Vector2& factor)
467  {
468  }
469 
471  {
472  }
473 
474  void BaseWidget::HandleShow (bool isVisible)
475  {
476  }
477 
478  void BaseWidget::HandleChangeColor (const sf::Color& color)
479  {
480  }
481 
483  {
484  }
485 } // namespace yap