utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[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         bool clouds_3d;
84         bool opaque_water;
85         
86         // Client options
87         {
88                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
89                 if(e != NULL)
90                         text_name = e->getText();
91                 else
92                         text_name = m_data->name;
93         }
94         {
95                 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
96                 if(e != NULL)
97                         text_address = e->getText();
98                 else
99                         text_address = m_data->address;
100         }
101         {
102                 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
103                 if(e != NULL)
104                         text_port = e->getText();
105                 else
106                         text_port = m_data->port;
107         }
108         {
109                 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
110                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
111                         fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
112                 else
113                         fancy_trees = m_data->fancy_trees;
114         }
115         {
116                 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
117                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
118                         smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
119                 else
120                         smooth_lighting = m_data->smooth_lighting;
121         }
122         {
123                 gui::IGUIElement *e = getElementFromId(GUI_ID_3D_CLOUDS_CB);
124                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
125                         clouds_3d = ((gui::IGUICheckBox*)e)->isChecked();
126                 else
127                         clouds_3d = m_data->clouds_3d;
128         }
129         {
130                 gui::IGUIElement *e = getElementFromId(GUI_ID_OPAQUE_WATER_CB);
131                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
132                         opaque_water = ((gui::IGUICheckBox*)e)->isChecked();
133                 else
134                         opaque_water = m_data->opaque_water;
135         }
136         
137         // Server options
138         {
139                 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
140                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
141                         creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
142                 else
143                         creative_mode = m_data->creative_mode;
144         }
145         {
146                 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
147                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
148                         enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
149                 else
150                         enable_damage = m_data->enable_damage;
151         }
152
153         /*
154                 Remove stuff
155         */
156         removeChildren();
157         
158         /*
159                 Calculate new sizes and positions
160         */
161         
162         v2s32 size(620, 430);
163
164         core::rect<s32> rect(
165                         screensize.X/2 - size.X/2,
166                         screensize.Y/2 - size.Y/2,
167                         screensize.X/2 + size.X/2,
168                         screensize.Y/2 + size.Y/2
169         );
170
171         DesiredRect = rect;
172         recalculateAbsolutePosition(false);
173
174         //v2s32 size = rect.getSize();
175
176         /*
177                 Add stuff
178         */
179
180         /*
181                 Client section
182         */
183
184         v2s32 topleft_client(40, 0);
185         v2s32 size_client = size - v2s32(40, 0);
186         
187         changeCtype("");
188         {
189                 core::rect<s32> rect(0, 0, 20, 125);
190                 rect += topleft_client + v2s32(-15, 60);
191                 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
192                 //gui::IGUIStaticText *t =
193                 Environment->addStaticText(text, rect, false, true, this, -1);
194                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
195         }
196
197         // Nickname + password
198         {
199                 core::rect<s32> rect(0, 0, 110, 20);
200                 rect += topleft_client + v2s32(35, 50+6);
201                 Environment->addStaticText(wgettext("Name/Password"), 
202                         rect, false, true, this, -1);
203         }
204         changeCtype("C");
205         {
206                 core::rect<s32> rect(0, 0, 230, 30);
207                 rect += topleft_client + v2s32(160, 50);
208                 gui::IGUIElement *e = 
209                 Environment->addEditBox(text_name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
210                 if(text_name == L"")
211                         Environment->setFocus(e);
212         }
213         {
214                 core::rect<s32> rect(0, 0, 120, 30);
215                 rect += topleft_client + v2s32(size_client.X-60-100, 50);
216                 gui::IGUIEditBox *e =
217                 Environment->addEditBox(L"", rect, true, this, 264);
218                 e->setPasswordBox(true);
219                 if(text_name != L"" && text_address != L"")
220                         Environment->setFocus(e);
221
222         }
223         changeCtype("");
224         // Address + port
225         {
226                 core::rect<s32> rect(0, 0, 110, 20);
227                 rect += topleft_client + v2s32(35, 100+6);
228                 Environment->addStaticText(wgettext("Address/Port"),
229                         rect, false, true, this, -1);
230         }
231         changeCtype("C");
232         {
233                 core::rect<s32> rect(0, 0, 230, 30);
234                 rect += topleft_client + v2s32(160, 100);
235                 gui::IGUIElement *e = 
236                 Environment->addEditBox(text_address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
237                 if(text_name != L"" && text_address == L"")
238                         Environment->setFocus(e);
239         }
240         {
241                 core::rect<s32> rect(0, 0, 120, 30);
242                 //rect += topleft_client + v2s32(160+250+20, 125);
243                 rect += topleft_client + v2s32(size_client.X-60-100, 100);
244                 Environment->addEditBox(text_port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
245         }
246         changeCtype("");
247         {
248                 core::rect<s32> rect(0, 0, 400, 20);
249                 rect += topleft_client + v2s32(160, 100+35);
250                 Environment->addStaticText(wgettext("Leave address blank to start a local server."),
251                         rect, false, true, this, -1);
252         }
253         {
254                 core::rect<s32> rect(0, 0, 250, 30);
255                 rect += topleft_client + v2s32(35, 150);
256                 Environment->addCheckBox(fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
257                         wgettext("Fancy trees")); 
258         }
259         {
260                 core::rect<s32> rect(0, 0, 250, 30);
261                 rect += topleft_client + v2s32(35, 150+20);
262                 Environment->addCheckBox(smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
263                                 wgettext("Smooth Lighting"));
264         }
265         {
266                 core::rect<s32> rect(0, 0, 250, 30);
267                 rect += topleft_client + v2s32(35, 150+40);
268                 Environment->addCheckBox(clouds_3d, rect, this, GUI_ID_3D_CLOUDS_CB,
269                                 wgettext("3D Clouds"));
270         }
271         {
272                 core::rect<s32> rect(0, 0, 250, 30);
273                 rect += topleft_client + v2s32(35, 150+60);
274                 Environment->addCheckBox(opaque_water, rect, this, GUI_ID_OPAQUE_WATER_CB,
275                                 wgettext("Opaque water"));
276         }
277         // Start game button
278         {
279                 core::rect<s32> rect(0, 0, 180, 30);
280                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
281                 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
282                 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
283                         wgettext("Start Game / Connect"));
284         }
285
286         // Key change button
287         {
288                 core::rect<s32> rect(0, 0, 100, 30);
289                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
290                 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
291                 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
292                         wgettext("Change keys"));
293         }
294         /*
295                 Server section
296         */
297
298         v2s32 topleft_server(40, 250);
299         v2s32 size_server = size - v2s32(40, 0);
300         
301         {
302                 core::rect<s32> rect(0, 0, 20, 125);
303                 rect += topleft_server + v2s32(-15, 40);
304                 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
305                 //gui::IGUIStaticText *t =
306                 Environment->addStaticText(text, rect, false, true, this, -1);
307                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
308         }
309
310         // Server parameters
311         {
312                 core::rect<s32> rect(0, 0, 250, 30);
313                 rect += topleft_server + v2s32(35, 30);
314                 Environment->addCheckBox(creative_mode, rect, this, GUI_ID_CREATIVE_CB,
315                         wgettext("Creative Mode"));
316         }
317         {
318                 core::rect<s32> rect(0, 0, 250, 30);
319                 rect += topleft_server + v2s32(35, 60);
320                 Environment->addCheckBox(enable_damage, rect, this, GUI_ID_DAMAGE_CB,
321                         wgettext("Enable Damage"));
322         }
323         // Map delete button
324         {
325                 core::rect<s32> rect(0, 0, 130, 30);
326                 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
327                 rect += topleft_server + v2s32(40, 100+25);
328                 Environment->addButton(rect, this, GUI_ID_DELETE_MAP_BUTTON,
329                           wgettext("Delete map"));
330         }
331         changeCtype("C");
332 }
333
334 void GUIMainMenu::drawMenu()
335 {
336         gui::IGUISkin* skin = Environment->getSkin();
337         if (!skin)
338                 return;
339         video::IVideoDriver* driver = Environment->getVideoDriver();
340         
341         /*video::SColor bgcolor(140,0,0,0);
342         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
343
344         video::SColor bgcolor(140,0,0,0);
345
346         {
347                 core::rect<s32> rect(0, 0, 620, 230);
348                 rect += AbsoluteRect.UpperLeftCorner;
349                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
350         }
351
352         {
353                 core::rect<s32> rect(0, 250, 620, 430);
354                 rect += AbsoluteRect.UpperLeftCorner;
355                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
356         }
357
358         gui::IGUIElement::draw();
359 }
360
361 void GUIMainMenu::acceptInput()
362 {
363         {
364                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
365                 if(e != NULL)
366                         m_data->name = e->getText();
367         }
368         {
369                 gui::IGUIElement *e = getElementFromId(264);
370                 if(e != NULL)
371                         m_data->password = e->getText();
372         }
373         {
374                 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
375                 if(e != NULL)
376                         m_data->address = e->getText();
377         }
378         {
379                 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
380                 if(e != NULL)
381                         m_data->port = e->getText();
382         }
383         {
384                 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
385                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
386                         m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
387         }
388         {
389                 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
390                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
391                         m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
392         }
393         {
394                 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
395                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
396                         m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
397         }
398         {
399                 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
400                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
401                         m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
402         }
403         {
404                 gui::IGUIElement *e = getElementFromId(GUI_ID_3D_CLOUDS_CB);
405                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
406                         m_data->clouds_3d = ((gui::IGUICheckBox*)e)->isChecked();
407         }
408         {
409                 gui::IGUIElement *e = getElementFromId(GUI_ID_OPAQUE_WATER_CB);
410                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
411                         m_data->opaque_water = ((gui::IGUICheckBox*)e)->isChecked();
412         }
413         
414         m_accepted = true;
415 }
416
417 bool GUIMainMenu::OnEvent(const SEvent& event)
418 {
419         if(event.EventType==EET_KEY_INPUT_EVENT)
420         {
421                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
422                 {
423                         m_gamecallback->exitToOS();
424                         quitMenu();
425                         return true;
426                 }
427                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
428                 {
429                         acceptInput();
430                         quitMenu();
431                         return true;
432                 }
433         }
434         if(event.EventType==EET_GUI_EVENT)
435         {
436                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
437                                 && isVisible())
438                 {
439                         if(!canTakeFocus(event.GUIEvent.Element))
440                         {
441                                 dstream<<"GUIMainMenu: Not allowing focus change."
442                                                 <<std::endl;
443                                 // Returning true disables focus change
444                                 return true;
445                         }
446                 }
447                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
448                 {
449                         switch(event.GUIEvent.Caller->getID())
450                         {
451                         case GUI_ID_JOIN_GAME_BUTTON: // Start game
452                                 acceptInput();
453                                 quitMenu();
454                                 return true;
455                         case GUI_ID_CHANGE_KEYS_BUTTON: {
456                                 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
457                                 kmenu->drop();
458                                 return true;
459                         }
460                         case GUI_ID_DELETE_MAP_BUTTON: // Delete map
461                                 // Don't accept input data, just set deletion request
462                                 m_data->delete_map = true;
463                                 m_accepted = true;
464                                 quitMenu();
465                                 return true;
466                         }
467                 }
468                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
469                 {
470                         switch(event.GUIEvent.Caller->getID())
471                         {
472                                 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
473                                 acceptInput();
474                                 quitMenu();
475                                 return true;
476                         }
477                 }
478         }
479
480         return Parent ? Parent->OnEvent(event) : false;
481 }
482