YAPOG  0.0.1
Yet Another Pokemon Online Game
LayoutBox.cpp
Go to the documentation of this file.
5 
6 namespace yap
7 {
9  : items_ ()
10  , focusables_ ()
11  , innerPad_ (0, 0, 0, 0)
12  , externPad_ (0, 0, 0, 0)
13  , realSize_ ()
14  , focusedChild_ (0)
15  {
16  }
17 
18  LayoutBox::LayoutBox (Padding ext, Padding in, bool isExt)
19  : items_ ()
20  , focusables_ ()
21  , innerPad_ (in)
22  , externPad_ (ext)
23  , realSize_ ()
24  , focusedChild_ (0)
25  {
26  isExtensible_ = isExt;
27  }
28 
30  {
31  return items_.Count ();
32  }
34  {
36 
37  RefreshBorder ();
39  }
40 
42  {
43  items_.Clear ();
44  focusables_.Clear ();
45 
46  drawables_.Clear ();
47  childen_.Clear();
50  }
51 
52  bool LayoutBox::IsFocusable () const
53  {
54  return true;
55  }
56 
57  void LayoutBox::SetExtensible (bool isExt)
58  {
59  isExtensible_ = isExt;
60  }
61 
63  {
64  return isExtensible_;
65  }
66 
67  void LayoutBox::SetAlign (Align global)
68  {
69  globalAlign_ = global;
70  }
71 
73  {
74  return globalAlign_;
75  }
76 
78  {
79  }
80 
82  {
83  if (isExtensible_)
84  return realSize_ + ((border_ != nullptr) ?
85  border_->GetSize () : Vector2 ());
86 
87  return spatialInfo_.GetSize ();
88  }
89 
91  {
92  return innerPad_;
93  }
94 
95  void LayoutBox::AddChild (IWidget& child, Align align)
96  {
97  BaseWidget::AddChild (child);
98  items_.Add (&child, align);
99 
100  if (child.IsFocusable ())
101  focusables_.Add (&child);
102 
103  GeneratePosition ();
104  }
105 
107  {
108  BaseWidget::RemoveChild (child);
109  items_.Remove (&child);
110 
111  if (child.IsFocusable ())
112  focusables_.Remove (&child);
113 
114  GeneratePosition ();
115  }
116 
118  {
119  if (guiEvent.type == sf::Event::KeyPressed)
120  {
121  if (guiEvent.key.code == sf::Keyboard::Tab)
122  {
123  if (focusables_.Count () == 0 || !isFocused_)
124  return false;
125 
126  IWidget* child;
127  uint cycle = focusedChild_;
128 
129  // if current focussable is a layout, give it control for tab
130  if (focusables_[cycle]->OnPriorityEvent (guiEvent))
131  return true;
133 
134  //Performed a cycle, give control to the parent
135  if (focusedChild_ == 0)
136  {
137  child = focusables_[focusedChild_];
138  child->SetFocused (true);
139  eventHandlers_.Remove (child);
140  eventHandlers_.AddFront (child);
141  isFocused_ = true; //to make cycle
142  return false;
143  }
144 
145  child = focusables_[focusedChild_];
146  child->SetFocused (true);
147  eventHandlers_.Remove (child);
148  eventHandlers_.AddFront (child);
149 
150  return true;
151  }
152  return false;
153  }
154  return false;
155  }
156 
158  {
159  }
160 
161  void LayoutBox::HandleShow (bool isVisible)
162  {
163  }
164 
165  void LayoutBox::HandleMove (const Vector2& offset)
166  {
167  }
168 
169  void LayoutBox::HandleScale (const Vector2& factor)
170  {
171  }
172 
173  void LayoutBox::HandleUpdate (const Time& dt)
174  {
175  }
176 
177  void LayoutBox::HandleChangeColor (const sf::Color& color)
178  {
179  }
180 
181  float LayoutBox::MaxSize (char coord)
182  {
183  float maxSizeItem = 0;
184 
185  if (coord == 'x')
186  {
187  for (IWidget* item : childen_)
188  {
189  float currentSize = item->GetSize ().x;
190 
191  maxSizeItem = MathHelper::Max (maxSizeItem, currentSize);
192  }
193 
194  return MathHelper::Max (
195  maxSizeItem + externPad_.left + externPad_.right,
196  GetUserSize ().x);
197  }
198  else if (coord == 'y')
199  {
200  for (IWidget* item : childen_)
201  {
202  float currentSize = item->GetSize ().y;
203 
204  maxSizeItem = MathHelper::Max (maxSizeItem, currentSize);
205  }
206 
207  return MathHelper::Max (
208  maxSizeItem + externPad_.top + externPad_.bottom,
209  GetUserSize ().y);
210  }
211 
212  // Error
213  return -42;
214  }
215 } //namespace yap