YAPOG  0.0.1
Yet Another Pokemon Online Game
GridMenu.cpp
Go to the documentation of this file.
9 
10 namespace yap
11 {
12 
14  const Vector2& size,
15  Padding ext,
16  Padding in,
17  bool extend)
18  : itemz_ (size.x, size.y, nullptr)
19  , currentSelec_ ()
20  , layoutV_ (new VerticalLayout (ext, in, extend))
21  , layoutHs_ (size.y, nullptr)
22  , selecBckgrd_ (nullptr)
23  , selecBrdr_ (nullptr)
24  , selecBrdSize_ (16)
25  , size_ (size)
26  , isFixed_ (!extend)
27  , currentLine_ (0)
28  , itemCount_ (size.y, 0)
29  {
30  for (int i = 0; i < size.y; ++i)
31  {
32  layoutHs_[i] = new HorizontalLayout (ext, in, extend);
34  }
35 
37  }
38 
39  bool GridMenu::IsFocusable () const
40  {
41  return true;
42  }
43 
45  {
46  }
47 
49  {
50  selecBckgrd_ = &background;
51  }
52 
54  {
55  selecBrdr_ = &border;
56  }
57 
59  {
60  if (isFixed_)
61  return GetUserSize ();
62  return layoutV_->GetSize () + ((border_ != nullptr) ? border_->GetSize () : Vector2 ());
63  }
64 
66  {
67  MenuItem* curItem = itemz_(currentSelec_.x, currentSelec_.y);
68 
69  if (selecBckgrd_ != nullptr)
70  curItem->SetBackground (*selecBckgrd_);
71  if (selecBrdr_ != nullptr)
72  curItem->SetBorder (*selecBrdr_);
73  }
74 
76  {
77  MenuItem* curItem = itemz_(currentSelec_.x, currentSelec_.y);
78 
79  curItem->UnsetBackground ();
80  curItem->UnsetBorder ();
81  }
82 
84  {
85  if (selecBrdr_ != nullptr)
86  child.SetBorder (*selecBrdr_);
87  if (currentLine_ >= size_.y)
88  return;
89  if (itemCount_[currentLine_] >= size_.x)
90  currentLine_++;
91  if (currentLine_ >= size_.y)
92  return;
93 
94  layoutHs_[currentLine_]->AddChild (child, align);
95  itemz_(itemCount_[currentLine_], currentLine_) = &child;
97 
98  child.UnsetBorder ();
99 
100  SetFormItem ();
101 
102  }
103 
105  {
106  return currentSelec_;
107  }
108 
110  {
111  //layout_->Draw (context);
112  }
113 
114  bool GridMenu::HandleOnEvent (const GuiEvent& guiEvent)
115  {
116  if (itemCount_.Count () > 0 && itemCount_[0] == 0)
117  return false;
118 
119  if (guiEvent.type == sf::Event::KeyPressed)
120  {
121  if (guiEvent.key.code == sf::Keyboard::Up)
122  {
123 
124  if (currentSelec_.y <= 0)
125  {
126  uint line = size_.y - 1;
127  while (line != 0)
128  {
129  if (itemCount_[line] != 0)
130  break;
131  line--;
132  }
133  SetUnformItem ();
134  currentSelec_.y = line;
135  }
136  else
137  {
138  SetUnformItem ();
139  currentSelec_.y--;
140  }
141  SetFormItem ();
143  ->OnSelected (*itemz_(currentSelec_.x, currentSelec_.y),
144  EmptyEventArgs ());
145  return true;
146  }
147  if (guiEvent.key.code == sf::Keyboard::Down)
148  {
149  //replace itemcount
150  if (currentSelec_.y >= itemCount_.Count () - 1)
151  {
152  SetUnformItem ();
153  currentSelec_.y = 0;
154  }
155  else
156  {
157  SetUnformItem ();
158  currentSelec_.y++;
159  }
160  SetFormItem ();
162  ->OnSelected (*itemz_(currentSelec_.x, currentSelec_.y),
163  EmptyEventArgs ());
164  return true;
165  }
166  if (guiEvent.key.code == sf::Keyboard::Left)
167  {
168  if (currentSelec_.x == 0 && currentSelec_.y == 0)
169  {
170  uint line = size_.y - 1;
171  while (line != 0)
172  {
173  if (itemCount_[line] != 0)
174  break;
175  line--;
176  }
177  SetUnformItem ();
178  currentSelec_.y = line;
179  currentSelec_.x = itemCount_[line] - 1;
180  }
181  else if (currentSelec_.x == 0)
182  {
183  SetUnformItem ();
184  currentSelec_.y--;
186  }
187  else
188  {
189  SetUnformItem ();
190  currentSelec_.x--;
191  }
192  SetFormItem ();
194  ->OnSelected (*itemz_(currentSelec_.x, currentSelec_.y),
195  EmptyEventArgs ());
196  return true;
197  }
198  if (guiEvent.key.code == sf::Keyboard::Right)
199  {
200  if (currentSelec_.x == itemCount_[currentSelec_.y] - 1
201  && currentSelec_.y == itemCount_.Count () - 1)
202  {
203  SetUnformItem ();
204  currentSelec_.x = 0;
205  currentSelec_.y = 0;
206  }
207  else if (currentSelec_.x == itemCount_[currentSelec_.y] - 1)
208  {
209  SetUnformItem ();
210  currentSelec_.y++;
211  currentSelec_.x = 0;
212  }
213  else
214  {
215  SetUnformItem ();
216  currentSelec_.x++;
217  }
218  SetFormItem ();
220  ->OnSelected (*itemz_(currentSelec_.x, currentSelec_.y),
221  EmptyEventArgs ());
222  return true;
223  }
224  if (guiEvent.key.code == sf::Keyboard::Return)
225  {
227  ->OnActivated (*itemz_(currentSelec_.x, currentSelec_.y),
228  EmptyEventArgs ());
229  return true;
230  }
231 
232  if (guiEvent.key.code == sf::Keyboard::Escape)
233  {
234  OnDesactivated (*this, EmptyEventArgs ());
235  return true;
236  }
237  }
238 
239  return false;
240  }
241 
242  void GridMenu::HandleShow (bool isVisible)
243  {
244  }
245 
246  void GridMenu::HandleMove (const Vector2& offset)
247  {
248  }
249 
250  void GridMenu::HandleScale (const Vector2& factor)
251  {
252  }
253 
254  void GridMenu::HandleUpdate (const Time& dt)
255  {
256  }
257 
258  void GridMenu::HandleChangeColor (const sf::Color& color)
259  {
260  }
261 } //namespace yap