YAPOG  0.0.1
Yet Another Pokemon Online Game
Menu.cpp
Go to the documentation of this file.
9 
10 namespace yap
11 {
12 
13  Menu::Menu (Type type, Padding ext, Padding in, bool fixed)
14  : itemz_ ()
15  , currentSelec_ (0)
16  , layout_ (nullptr)
17  , selecBckgrd_ (nullptr)
18  , selecBrdr_ (nullptr)
19  , selecBrdSize_ (16)
20  , isFixed_ (fixed)
21  , type_ (type)
22  , layoutManager_ (nullptr)
23  {
24  if (type == Type::HORIZONTAL)
25  layout_ = new HorizontalLayout (ext, in, !fixed);
26  else if (type == Type::VERTICAL)
27  layout_ = new VerticalLayout (ext, in, !fixed);
28 
30  layoutManager_->SetEnable (fixed);
32  }
33 
34  void Menu::SetFixed (bool state)
35  {
36  isFixed_ = state;
37  }
38 
39  bool Menu::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 layout_->GetSize () + ((border_ != nullptr) ? border_->GetSize () : Vector2 ());
63  }
64 
66  {
67  MenuItem* curItem = itemz_[currentSelec_];
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_];
78  curItem->UnsetBackground ();
79  curItem->UnsetBorder ();
80  }
81 
83  {
84  if (selecBrdr_ != nullptr)
85  child.SetBorder (*selecBrdr_);
86  if (itemz_.Count () == 0)
87  {
88  itemz_.Add (&child);
89  child.OnSelected (child, EmptyEventArgs ());
90  }
91  else
92  {
93  itemz_.Add (&child);
94  }
95 
96  layout_->AddChild (child, align);
97  layoutManager_->AddItem (&child);
98 
99  /*
100  if (isFixed_ && type_ == Type::VERTICAL)
101  {
102  if (GetUserSize () != Vector2 (0, 0) && layout_->GetSize ().y > GetUserSize ().y)
103  layout_->RemoveChild (child);
104  }
105  else if (isFixed_ && type_ == Type::HORIZONTAL)
106  if (GetUserSize () != Vector2 (0, 0) && layout_->GetSize ().x > GetUserSize ().x)
107  layout_->RemoveChild (child);
108  */
109 
110  child.UnsetBorder ();
111 
112  SetFormItem ();
113 
114  if (type_ == Menu::Type::HORIZONTAL)
116  if (type_== Menu::Type::VERTICAL)
118 
119  }
120 
121  void Menu::Clear ()
122  {
123  itemz_.Clear ();
124  currentSelec_ = 0;
125  layout_->Clear ();
126  layoutManager_->Clear ();
127  }
129  {
130  return currentSelec_;
131  }
132 
134  {
135  //layout_->Draw (context);
136  }
137 
138  bool Menu::HandleOnEvent (const GuiEvent& guiEvent)
139  {
140  if (guiEvent.type == sf::Event::KeyPressed)
141  {
142  if (guiEvent.key.code == sf::Keyboard::Up)
143  {
144 
145  if (currentSelec_ <= 0)
146  return true;
147 
148  SetUnformItem ();
149  currentSelec_--;
151  SetFormItem ();
154  return true;
155  }
156  if (guiEvent.key.code == sf::Keyboard::Down)
157  {
158  if (itemz_.Count () == 0 || currentSelec_ >= itemz_.Count () - 1)
159  return true;
160 
161  SetUnformItem ();
162  currentSelec_++;
164  SetFormItem ();
167  return true;
168  }
169 
170  if (guiEvent.key.code == sf::Keyboard::Return)
171  {
172  itemz_[currentSelec_]->Do ();
175  return true;
176  }
177 
178  /*
179  if (guiEvent.key.code == sf::Keyboard::Escape)
180  {
181  OnDesactivated (*this, EmptyEventArgs ());
182  return true;
183  }
184  */
185 
186  // Menu captures every key pressed
187  //return true;
188  }
189 
190  // Menu captures every key released
191  if (guiEvent.type == sf::Event::KeyReleased)
192  return true;
193 
194  return false;
195  }
196 
197  void Menu::HandleShow (bool isVisible)
198  {
199  }
200 
201  void Menu::HandleMove (const Vector2& offset)
202  {
203  }
204 
205  void Menu::HandleScale (const Vector2& factor)
206  {
207  }
208 
209  void Menu::HandleUpdate (const Time& dt)
210  {
211  }
212 
213  void Menu::HandleChangeColor (const sf::Color& color)
214  {
215  }
216 
218  {
219  }
220 
222  {
223  }
224 
225 } //namespace yap