YAPOG  0.0.1
Yet Another Pokemon Online Game
ChatWidget.cpp
Go to the documentation of this file.
6 
7 #include "Gui/ChatWidget.hpp"
8 #include "Game.hpp"
9 
10 namespace ycl
11 {
12 
14  : userLogin_ (userLogin)
15  , chat_ (new yap::Chat ())
16  , line_ ("")
17  , lineCatcher_ (new yap::TextBoxWidget ("", 12))
18  , tabTitle_ ()
19  , tabLayout_ (new yap::HorizontalLayout (yap::Padding (5, 5, 0, 0),
20  yap::Padding (0, 5, 0, 0), false))
21  , dialog_ (new yap::MultiLabelWidget (yap::Padding (5, 5, 5, 5),
22  yap::Padding (0, 0, 0, 0), false))
23  , dialLayout_ (new yap::HorizontalLayout (yap::Padding (5, 5, 0, 0),
24  yap::Padding (0, 0, 0, 0), false))
25  , entryLayout_ (new yap::HorizontalLayout (yap::Padding (5, 5, 0, 0),
26  yap::Padding (0, 10, 0, 0), false))
27  , bigLayout_ (new yap::VerticalLayout (yap::Padding (5, 5, 5, 5),
28  yap::Padding (5, 5, 5, 5), false))
29  {
30  yap::Texture* cursor = new yap::Texture ();
31  cursor->LoadFromFile ("WindowSkins/BasicSkin/Global/TextCursor.png");
32  bigLayout_->SetSize (yap::Vector2 (300, 150));
33  lineCatcher_->SetCursor (*cursor);
34  lineCatcher_->SetSize (yap::Vector2 (242, 12));
35  }
36 
38  {
39  delete (chat_);
40  chat_ = nullptr;
41  delete (lineCatcher_);
42  lineCatcher_ = nullptr;
43  delete (tabLayout_);
44  tabLayout_ = nullptr;
45  delete (dialog_);
46  dialog_ = nullptr;
47  delete (dialLayout_);
48  dialLayout_ = nullptr;
49  delete (entryLayout_);
50  entryLayout_ = nullptr;
51  delete (bigLayout_);
52  bigLayout_ = nullptr;
53 
54  for (yap::Label* l : tabTitle_)
55  {
56  delete (l);
57  l = nullptr;
58  }
59  }
60 
62  {
63  tabLayout_->SetSize (yap::Vector2 (242, 12));
64  for (yap::UInt32 i = 0; i < chat_->GetTabCount (); i++)
65  {
66  yap::String name = chat_->GetTabName (i);
67  yap::Label* title = new yap::Label (name);
68  title->ChangeColor (sf::Color::Black);
69  title->SetTextSize (12);
70  title->SetBorder (*new yap::WidgetBorder ("Test/black.png"));
71  tabTitle_.Add (title);
72  tabLayout_->AddChild (*title);
73  }
74  tabLayout_->SetBorder (*new yap::WidgetBorder ("Test/red.png"));
76  }
77 
79  {
80  dialLayout_->SetSize (yap::Vector2 (242, 96));
81  dialog_->SetSize (yap::Vector2 (242, 96));
82 
83  dialLayout_->SetBorder (*new yap::WidgetBorder ("Test/red.png"));
86  }
87 
89  {
90  entryLayout_->SetSize (yap::Vector2 (242, 20));
91 
92  entryLayout_->SetBorder (*new yap::WidgetBorder ("Test/red.png"));
93  lineCatcher_->ChangeColor (sf::Color::Black);
96  }
97 
99  {
100  yap::WidgetBackground* chatBground =
102  "WindowSkins/BasicSkin/Global/WindowBackgroundTexture.png",
103  true);
104  yap::Texture* t = new yap::Texture ();
105  t->LoadFromFile ("WindowSkins/BasicSkin/Global/TopBorder.png");
106  yap::Texture* tr = new yap::Texture ();
107  tr->LoadFromFile ("WindowSkins/BasicSkin/Global/TopRightCorner.png");
108  yap::Texture* r = new yap::Texture ();
109  r->LoadFromFile ("WindowSkins/BasicSkin/Global/RightBorder.png");
110  yap::Texture* br = new yap::Texture ();
111  br->LoadFromFile ("WindowSkins/BasicSkin/Global/BottomRightCorner.png");
112  yap::Texture* b = new yap::Texture ();
113  b->LoadFromFile ("WindowSkins/BasicSkin/Global/BottomBorder.png");
114  yap::Texture* bl = new yap::Texture ();
115  bl->LoadFromFile ("WindowSkins/BasicSkin/Global/BottomLeftCorner.png");
116  yap::Texture* l = new yap::Texture ();
117  l->LoadFromFile ("WindowSkins/BasicSkin/Global/LeftBorder.png");
118  yap::Texture* tl = new yap::Texture ();
119  tl->LoadFromFile ("WindowSkins/BasicSkin/Global/TopLeftCorner.png");
120 
121  yap::WidgetBorder* chatBorder =
122  new yap::WidgetBorder (*t, *tr, *r, *br, *b, *bl, *l, *tl, true);
123 
124  InitTab ();
125  InitDial ();
126  InitEntry ();
127 
128  AddChild (*bigLayout_);
129  bigLayout_->SetBorder (*chatBorder);
131  yap::Vector2 (
132  20,
133  Game::SCREEN_SIZE.y - GetSize ().y - 20));
134  bigLayout_->SetBackground (*chatBground);
135 
136  TabAndChanHandler (true, true, 1);
137  }
138 
140  {
141  return true;
142  }
143 
145  {
146  return bigLayout_->GetSize ();
147  }
148 
150  {
151  yap::String fullMessage =
152  message.GetSenderName () + ": " + message.GetContent ();
153 
154  dialog_->AddText (fullMessage, 12, sf::Color (127, 127, 127));
155  }
156 
158  {
159  bool toclear = res.Clean;
160  yap::collection::Array<yap::UInt32> responseChan = res.Channb;
161  yap::collection::Array<yap::String> responseString = res.Message;
162 
163  if (toclear)
164  dialog_->Clear ();
165 
166  for (size_t i = 0; i < responseString.Count (); i++)
167  {
168  sf::Color color = sf::Color::Black;
169  switch (responseChan[i])
170  {
171  case 1:
172  color = sf::Color::Black;
173  break;
174  case 2:
175  color = sf::Color::Green;
176  break;
177  case 3:
178  color = sf::Color::Blue;
179  break;
180  case 4:
181  color = sf::Color (128, 128, 0);
182  break;
183  case 5:
184  color = sf::Color::Magenta;
185  break;
186  default:
187  color = sf::Color::Red;
188  break;
189  }
190 
191  yap::String message = responseString[i];
192 
193  if (res.Command)
194  {
195  dialog_->AddText (
196  message,
197  12,
198  color);
199  continue;
200  }
201 
202  yap::GameMessage gameMessage;
203  gameMessage.SetSenderName (userLogin_);
204  gameMessage.SetContent (message);
205  OnMessageSent (*this, gameMessage);
206  }
207  }
208 
209  bool ChatWidget::TabAndChanHandler (bool chan, bool add, int i)
210  {
211  yap::ResponseType response;
212  if (chan)
213  chat_->SetBuf ((add ? "/addchan " : "/switchchan ") +
215  else
216  chat_->SetBuf ("/switchtab " + yap::StringHelper::ToString (i));
217  chat_->Parse ();
218  response = chat_->Exec ();
219 
220  if (!chan && response.Message.IsEmpty ())
221  response.Clean = true;
222  response.Command = true;
223  DisplayResponse (response);
224 
225  return true;
226  }
227 
228  bool ChatWidget::TestAddChan (const yap::GuiEvent& guiEvent)
229  {
230  if (guiEvent.key.alt
231  && guiEvent.key.code == sf::Keyboard::Num0)
232  return TabAndChanHandler (true, true, 0);
233  if (guiEvent.key.alt
234  && guiEvent.key.code == sf::Keyboard::Num1)
235  return TabAndChanHandler (true, true, 1);
236  if (guiEvent.key.alt
237  && guiEvent.key.code == sf::Keyboard::Num2)
238  return TabAndChanHandler (true, true, 2);
239  if (guiEvent.key.alt
240  && guiEvent.key.code == sf::Keyboard::Num3)
241  return TabAndChanHandler (true, true, 3);
242  if (guiEvent.key.alt
243  && guiEvent.key.code == sf::Keyboard::Num4)
244  return TabAndChanHandler (true, true, 4);
245  if (guiEvent.key.alt
246  && guiEvent.key.code == sf::Keyboard::Num5)
247  return TabAndChanHandler (true, true, 5);
248  if (guiEvent.key.alt
249  && guiEvent.key.code == sf::Keyboard::Num6)
250  return TabAndChanHandler (true, true, 6);
251  if (guiEvent.key.alt
252  && guiEvent.key.code == sf::Keyboard::Num7)
253  return TabAndChanHandler (true, true, 7);
254  if (guiEvent.key.alt
255  && guiEvent.key.code == sf::Keyboard::Num8)
256  return TabAndChanHandler (true, true, 8);
257  if (guiEvent.key.alt
258  && guiEvent.key.code == sf::Keyboard::Num9)
259  return TabAndChanHandler (true, true, 9);
260  return false;
261  }
262 
264  {
265  if (guiEvent.key.control
266  && guiEvent.key.code == sf::Keyboard::Num0)
267  return TabAndChanHandler (false, false, 0);
268  if (guiEvent.key.control
269  && guiEvent.key.code == sf::Keyboard::Num1)
270  return TabAndChanHandler (false, false, 1);
271  if (guiEvent.key.control
272  && guiEvent.key.code == sf::Keyboard::Num2)
273  return TabAndChanHandler (false, false, 2);
274  if (guiEvent.key.control
275  && guiEvent.key.code == sf::Keyboard::Num3)
276  return TabAndChanHandler (false, false, 3);
277  if (guiEvent.key.control
278  && guiEvent.key.code == sf::Keyboard::Num4)
279  return TabAndChanHandler (false, false, 4);
280  if (guiEvent.key.control
281  && guiEvent.key.code == sf::Keyboard::Num5)
282  return TabAndChanHandler (false, false, 5);
283  if (guiEvent.key.control
284  && guiEvent.key.code == sf::Keyboard::Num6)
285  return TabAndChanHandler (false, false, 6);
286  if (guiEvent.key.control
287  && guiEvent.key.code == sf::Keyboard::Num7)
288  return TabAndChanHandler (false, false, 7);
289  if (guiEvent.key.control
290  && guiEvent.key.code == sf::Keyboard::Num8)
291  return TabAndChanHandler (false, false, 8);
292  if (guiEvent.key.control
293  && guiEvent.key.code == sf::Keyboard::Num9)
294  return TabAndChanHandler (false, false, 9);
295  return false;
296  }
297 
299  {
300  if (guiEvent.type == sf::Event::KeyPressed)
301  {
302  if (guiEvent.key.code == sf::Keyboard::Escape)
303  {
304  this->Close ();
305  return true;
306  }
307 
308  if (TestAddChan (guiEvent) || TestSwitchTab (guiEvent))
309  return true;
310  }
311 
312  return false;
313  }
314 
316  {
317  if (guiEvent.type == sf::Event::KeyPressed)
318  {
319  if (guiEvent.key.code == sf::Keyboard::Return)
320  {
321  yap::ResponseType response;
322  yap::String todisplay = lineCatcher_->GetContent ();
323 
324  chat_->SetBuf (todisplay);
325  chat_->Parse ();
326  response = chat_->Exec ();
327  response.Command = chat_->GetIsCommand ();
328  if (!response.Message.IsEmpty ())
329  DisplayResponse (response);
330  lineCatcher_->Clear ();
331  }
332 
333  if (guiEvent.key.code == sf::Keyboard::Up)
334  {
335  chat_->SetBuf ("/up");
336  yap::String todisplay = chat_->Parse ();
337  lineCatcher_->Clear ();
338  if (!todisplay.empty ())
339  lineCatcher_->SetText (todisplay);
340  }
341 
342  if (guiEvent.key.code == sf::Keyboard::Down)
343  {
344  chat_->SetBuf ("/down");
345  yap::String todisplay = chat_->Parse ();
346  lineCatcher_->Clear ();
347  if (!todisplay.empty ())
348  lineCatcher_->SetText (todisplay);
349  }
350 
351  return true;
352  }
353  return false;
354  }
355 
357  {
358 
359  }
361  {
362 
363  }
365  {
366 
367  }
368  void ChatWidget::HandleShow (bool isVisible)
369  {
370 
371  }
372  void ChatWidget::HandleChangeColor (const sf::Color& color)
373  {
374 
375  }
377  {
378 
379  }
380 } // namespace ycl