Catch SendFailedException when replying back in Connection::Receive()
[oweals/minetest.git] / src / guiMainMenu.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "guiMainMenu.h"
21 #include "guiKeyChangeMenu.h"
22 #include "debug.h"
23 #include "serialization.h"
24 #include <string>
25 #include <IGUICheckBox.h>
26 #include <IGUIEditBox.h>
27 #include <IGUIButton.h>
28 #include <IGUIStaticText.h>
29 #include <IGUIFont.h>
30
31
32 #include "gettext.h"
33
34 GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
35                 gui::IGUIElement* parent, s32 id,
36                 IMenuManager *menumgr,
37                 MainMenuData *data,
38                 IGameCallback *gamecallback
39 ):
40         GUIModalMenu(env, parent, id, menumgr),
41         m_data(data),
42         m_accepted(false),
43         m_gamecallback(gamecallback)
44 {
45         assert(m_data);
46         this->env = env;
47         this->parent = parent;
48         this->id = id;
49         this->menumgr = menumgr;
50 }
51
52 GUIMainMenu::~GUIMainMenu()
53 {
54         removeChildren();
55 }
56
57 void GUIMainMenu::removeChildren()
58 {
59         const core::list<gui::IGUIElement*> &children = getChildren();
60         core::list<gui::IGUIElement*> children_copy;
61         for(core::list<gui::IGUIElement*>::ConstIterator
62                         i = children.begin(); i != children.end(); i++)
63         {
64                 children_copy.push_back(*i);
65         }
66         for(core::list<gui::IGUIElement*>::Iterator
67                         i = children_copy.begin();
68                         i != children_copy.end(); i++)
69         {
70                 (*i)->remove();
71         }
72 }
73
74 void GUIMainMenu::regenerateGui(v2u32 screensize)
75 {
76         std::wstring text_name;
77         std::wstring text_address;
78         std::wstring text_port;
79         bool creative_mode;
80         bool enable_damage;
81         bool fancy_trees;
82         bool smooth_lighting;
83         
84         // Client options
85         {
86                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
87                 if(e != NULL)
88                         text_name = e->getText();
89                 else
90                         text_name = m_data->name;
91         }
92         {
93                 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
94                 if(e != NULL)
95                         text_address = e->getText();
96                 else
97                         text_address = m_data->address;
98         }
99         {
100                 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
101                 if(e != NULL)
102                         text_port = e->getText();
103                 else
104                         text_port = m_data->port;
105         }
106         {
107                 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
108                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
109                         fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
110                 else
111                         fancy_trees = m_data->fancy_trees;
112         }
113         {
114                 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
115                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
116                         smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
117                 else
118                         smooth_lighting = m_data->smooth_lighting;
119         }
120         
121         // Server options
122         {
123                 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
124                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
125                         creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
126                 else
127                         creative_mode = m_data->creative_mode;
128         }
129         {
130                 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
131                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
132                         enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
133                 else
134                         enable_damage = m_data->enable_damage;
135         }
136
137         /*
138                 Remove stuff
139         */
140         removeChildren();
141         
142         /*
143                 Calculate new sizes and positions
144         */
145         
146         v2s32 size(620, 430);
147
148         core::rect<s32> rect(
149                         screensize.X/2 - size.X/2,
150                         screensize.Y/2 - size.Y/2,
151                         screensize.X/2 + size.X/2,
152                         screensize.Y/2 + size.Y/2
153         );
154
155         DesiredRect = rect;
156         recalculateAbsolutePosition(false);
157
158         //v2s32 size = rect.getSize();
159
160         /*
161                 Add stuff
162         */
163
164         /*
165                 Client section
166         */
167
168         v2s32 topleft_client(40, 0);
169         v2s32 size_client = size - v2s32(40, 0);
170         
171         changeCtype("");
172         {
173                 core::rect<s32> rect(0, 0, 20, 125);
174                 rect += topleft_client + v2s32(-15, 60);
175                 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
176                 //gui::IGUIStaticText *t =
177                 Environment->addStaticText(text, rect, false, true, this, -1);
178                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
179         }
180
181         // Nickname + password
182         {
183                 core::rect<s32> rect(0, 0, 110, 20);
184                 rect += topleft_client + v2s32(35, 50+6);
185                 Environment->addStaticText(wgettext("Name/Password"), 
186                         rect, false, true, this, -1);
187         }
188         changeCtype("C");
189         {
190                 core::rect<s32> rect(0, 0, 230, 30);
191                 rect += topleft_client + v2s32(160, 50);
192                 gui::IGUIElement *e = 
193                 Environment->addEditBox(text_name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
194                 if(text_name == L"")
195                         Environment->setFocus(e);
196         }
197         {
198                 core::rect<s32> rect(0, 0, 120, 30);
199                 rect += topleft_client + v2s32(size_client.X-60-100, 50);
200                 gui::IGUIEditBox *e =
201                 Environment->addEditBox(L"", rect, true, this, 264);
202                 e->setPasswordBox(true);
203                 if(text_name != L"" && text_address != L"")
204                         Environment->setFocus(e);
205
206         }
207         changeCtype("");
208         // Address + port
209         {
210                 core::rect<s32> rect(0, 0, 110, 20);
211                 rect += topleft_client + v2s32(35, 100+6);
212                 Environment->addStaticText(wgettext("Address/Port"),
213                         rect, false, true, this, -1);
214         }
215         changeCtype("C");
216         {
217                 core::rect<s32> rect(0, 0, 230, 30);
218                 rect += topleft_client + v2s32(160, 100);
219                 gui::IGUIElement *e = 
220                 Environment->addEditBox(text_address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
221                 if(text_name != L"" && text_address == L"")
222                         Environment->setFocus(e);
223         }
224         {
225                 core::rect<s32> rect(0, 0, 120, 30);
226                 //rect += topleft_client + v2s32(160+250+20, 125);
227                 rect += topleft_client + v2s32(size_client.X-60-100, 100);
228                 Environment->addEditBox(text_port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
229         }
230         changeCtype("");
231         {
232                 core::rect<s32> rect(0, 0, 400, 20);
233                 rect += topleft_client + v2s32(160, 100+35);
234                 Environment->addStaticText(wgettext("Leave address blank to start a local server."),
235                         rect, false, true, this, -1);
236         }
237         {
238                 core::rect<s32> rect(0, 0, 250, 30);
239                 rect += topleft_client + v2s32(35, 150);
240                 Environment->addCheckBox(fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
241                         wgettext("Fancy trees")); 
242         }
243         {
244                 core::rect<s32> rect(0, 0, 250, 30);
245                 rect += topleft_client + v2s32(35, 150+30);
246                 Environment->addCheckBox(smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
247                                 wgettext("Smooth Lighting"));
248         }
249         // Start game button
250         {
251                 core::rect<s32> rect(0, 0, 180, 30);
252                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
253                 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
254                 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
255                         wgettext("Start Game / Connect"));
256         }
257
258         // Key change button
259         {
260                 core::rect<s32> rect(0, 0, 100, 30);
261                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
262                 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
263                 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
264                         wgettext("Change keys"));
265         }
266         /*
267                 Server section
268         */
269
270         v2s32 topleft_server(40, 250);
271         v2s32 size_server = size - v2s32(40, 0);
272         
273         {
274                 core::rect<s32> rect(0, 0, 20, 125);
275                 rect += topleft_server + v2s32(-15, 40);
276                 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
277                 //gui::IGUIStaticText *t =
278                 Environment->addStaticText(text, rect, false, true, this, -1);
279                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
280         }
281
282         // Server parameters
283         {
284                 core::rect<s32> rect(0, 0, 250, 30);
285                 rect += topleft_server + v2s32(35, 30);
286                 Environment->addCheckBox(creative_mode, rect, this, GUI_ID_CREATIVE_CB,
287                         wgettext("Creative Mode"));
288         }
289         {
290                 core::rect<s32> rect(0, 0, 250, 30);
291                 rect += topleft_server + v2s32(35, 60);
292                 Environment->addCheckBox(enable_damage, rect, this, GUI_ID_DAMAGE_CB,
293                         wgettext("Enable Damage"));
294         }
295         // Map delete button
296         {
297                 core::rect<s32> rect(0, 0, 130, 30);
298                 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
299                 rect += topleft_server + v2s32(40, 100+25);
300                 Environment->addButton(rect, this, GUI_ID_DELETE_MAP_BUTTON,
301                           wgettext("Delete map"));
302         }
303         changeCtype("C");
304 }
305
306 void GUIMainMenu::drawMenu()
307 {
308         gui::IGUISkin* skin = Environment->getSkin();
309         if (!skin)
310                 return;
311         video::IVideoDriver* driver = Environment->getVideoDriver();
312         
313         /*video::SColor bgcolor(140,0,0,0);
314         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
315
316         video::SColor bgcolor(140,0,0,0);
317
318         {
319                 core::rect<s32> rect(0, 0, 620, 230);
320                 rect += AbsoluteRect.UpperLeftCorner;
321                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
322         }
323
324         {
325                 core::rect<s32> rect(0, 250, 620, 430);
326                 rect += AbsoluteRect.UpperLeftCorner;
327                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
328         }
329
330         gui::IGUIElement::draw();
331 }
332
333 void GUIMainMenu::acceptInput()
334 {
335         {
336                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
337                 if(e != NULL)
338                         m_data->name = e->getText();
339         }
340         {
341                 gui::IGUIElement *e = getElementFromId(264);
342                 if(e != NULL)
343                         m_data->password = e->getText();
344         }
345         {
346                 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
347                 if(e != NULL)
348                         m_data->address = e->getText();
349         }
350         {
351                 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
352                 if(e != NULL)
353                         m_data->port = e->getText();
354         }
355         {
356                 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
357                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
358                         m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
359         }
360         {
361                 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
362                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
363                         m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
364         }
365         {
366                 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
367                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
368                         m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
369         }
370         {
371                 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
372                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
373                         m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
374         }
375         
376         m_accepted = true;
377 }
378
379 bool GUIMainMenu::OnEvent(const SEvent& event)
380 {
381         if(event.EventType==EET_KEY_INPUT_EVENT)
382         {
383                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
384                 {
385                         m_gamecallback->exitToOS();
386                         quitMenu();
387                         return true;
388                 }
389                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
390                 {
391                         acceptInput();
392                         quitMenu();
393                         return true;
394                 }
395         }
396         if(event.EventType==EET_GUI_EVENT)
397         {
398                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
399                                 && isVisible())
400                 {
401                         if(!canTakeFocus(event.GUIEvent.Element))
402                         {
403                                 dstream<<"GUIMainMenu: Not allowing focus change."
404                                                 <<std::endl;
405                                 // Returning true disables focus change
406                                 return true;
407                         }
408                 }
409                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
410                 {
411                         switch(event.GUIEvent.Caller->getID())
412                         {
413                         case GUI_ID_JOIN_GAME_BUTTON: // Start game
414                                 acceptInput();
415                                 quitMenu();
416                                 return true;
417                         case GUI_ID_CHANGE_KEYS_BUTTON: {
418                                 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
419                                 kmenu->drop();
420                                 return true;
421                         }
422                         case GUI_ID_DELETE_MAP_BUTTON: // Delete map
423                                 // Don't accept input data, just set deletion request
424                                 m_data->delete_map = true;
425                                 m_accepted = true;
426                                 quitMenu();
427                                 return true;
428                         }
429                 }
430                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
431                 {
432                         switch(event.GUIEvent.Caller->getID())
433                         {
434                                 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
435                                 acceptInput();
436                                 quitMenu();
437                                 return true;
438                         }
439                 }
440         }
441
442         return Parent ? Parent->OnEvent(event) : false;
443 }
444