]> git.lizzy.rs Git - minetest-m13.git/blob - src/guiMainMenu.cpp
Update to 4.6 base
[minetest-m13.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         // Version
190         {
191                 core::rect<s32> rect(0, 0, 300, 30);
192                 rect += topleft_client + v2s32(-36, 0);
193                 Environment->addStaticText(narrow_to_wide(VERSION_STRING).c_str(), 
194                         rect, false, true, this, -1);
195         }
196         // CLIENT
197         {
198                 core::rect<s32> rect(0, 0, 20, 125);
199                 rect += topleft_client + v2s32(-15, 80);
200                 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
201                 //gui::IGUIStaticText *t =
202                 Environment->addStaticText(text, rect, false, true, this, -1);
203                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
204         }
205         // Nickname + password
206         {
207                 core::rect<s32> rect(0, 0, 110, 20);
208                 rect += topleft_client + v2s32(35, 50+6);
209                 Environment->addStaticText(wgettext("Name/Password"), 
210                         rect, false, true, this, -1);
211         }
212         changeCtype("C");
213         {
214                 core::rect<s32> rect(0, 0, 230, 30);
215                 rect += topleft_client + v2s32(160, 50);
216                 gui::IGUIElement *e = 
217                 Environment->addEditBox(text_name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
218                 if(text_name == L"")
219                         Environment->setFocus(e);
220         }
221         {
222                 core::rect<s32> rect(0, 0, 120, 30);
223                 rect += topleft_client + v2s32(size_client.X-60-100, 50);
224                 gui::IGUIEditBox *e =
225                 Environment->addEditBox(L"", rect, true, this, 264);
226                 e->setPasswordBox(true);
227                 if(text_name != L"" && text_address != L"")
228                         Environment->setFocus(e);
229
230         }
231         changeCtype("");
232         // Address + port
233         {
234                 core::rect<s32> rect(0, 0, 110, 20);
235                 rect += topleft_client + v2s32(35, 100+6);
236                 Environment->addStaticText(wgettext("Address/Port"),
237                         rect, false, true, this, -1);
238         }
239         changeCtype("C");
240         {
241                 core::rect<s32> rect(0, 0, 230, 30);
242                 rect += topleft_client + v2s32(160, 100);
243                 gui::IGUIElement *e = 
244                 Environment->addEditBox(text_address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
245                 if(text_name != L"" && text_address == L"")
246                         Environment->setFocus(e);
247         }
248         {
249                 core::rect<s32> rect(0, 0, 120, 30);
250                 //rect += topleft_client + v2s32(160+250+20, 125);
251                 rect += topleft_client + v2s32(size_client.X-60-100, 100);
252                 Environment->addEditBox(text_port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
253         }
254         changeCtype("");
255         {
256                 core::rect<s32> rect(0, 0, 400, 20);
257                 rect += topleft_client + v2s32(160, 100+35);
258                 Environment->addStaticText(wgettext("Leave address blank to start a local server."),
259                         rect, false, true, this, -1);
260         }
261         {
262                 core::rect<s32> rect(0, 0, 250, 30);
263                 rect += topleft_client + v2s32(35, 150);
264                 Environment->addCheckBox(fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
265                         wgettext("Fancy trees")); 
266         }
267         {
268                 core::rect<s32> rect(0, 0, 250, 30);
269                 rect += topleft_client + v2s32(35, 150+20);
270                 Environment->addCheckBox(smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
271                                 wgettext("Smooth Lighting"));
272         }
273         {
274                 core::rect<s32> rect(0, 0, 250, 30);
275                 rect += topleft_client + v2s32(35, 150+40);
276                 Environment->addCheckBox(clouds_3d, rect, this, GUI_ID_3D_CLOUDS_CB,
277                                 wgettext("3D Clouds"));
278         }
279         {
280                 core::rect<s32> rect(0, 0, 250, 30);
281                 rect += topleft_client + v2s32(35, 150+60);
282                 Environment->addCheckBox(opaque_water, rect, this, GUI_ID_OPAQUE_WATER_CB,
283                                 wgettext("Opaque water"));
284         }
285         // Start game button
286         {
287                 core::rect<s32> rect(0, 0, 180, 30);
288                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
289                 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
290                 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
291                         wgettext("Start Game / Connect"));
292         }
293
294         // Key change button
295         {
296                 core::rect<s32> rect(0, 0, 100, 30);
297                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
298                 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
299                 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
300                         wgettext("Change keys"));
301         }
302         /*
303                 Server section
304         */
305
306         v2s32 topleft_server(40, 290);
307         v2s32 size_server = size - v2s32(40, 0);
308         
309         // SERVER
310         {
311                 core::rect<s32> rect(0, 0, 20, 125);
312                 rect += topleft_server + v2s32(-15, 15);
313                 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
314                 //gui::IGUIStaticText *t =
315                 Environment->addStaticText(text, rect, false, true, this, -1);
316                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
317         }
318         // Server parameters
319         {
320                 core::rect<s32> rect(0, 0, 250, 30);
321                 rect += topleft_server + v2s32(35, 20);
322                 Environment->addCheckBox(creative_mode, rect, this, GUI_ID_CREATIVE_CB,
323                         wgettext("Creative Mode"));
324         }
325         {
326                 core::rect<s32> rect(0, 0, 250, 30);
327                 rect += topleft_server + v2s32(35, 40);
328                 Environment->addCheckBox(enable_damage, rect, this, GUI_ID_DAMAGE_CB,
329                         wgettext("Enable Damage"));
330         }
331         // Map delete button
332         {
333                 core::rect<s32> rect(0, 0, 130, 30);
334                 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
335                 rect += topleft_server + v2s32(40, 90);
336                 Environment->addButton(rect, this, GUI_ID_DELETE_MAP_BUTTON,
337                           wgettext("Delete map"));
338         }
339         changeCtype("C");
340 }
341
342 void GUIMainMenu::drawMenu()
343 {
344         gui::IGUISkin* skin = Environment->getSkin();
345         if (!skin)
346                 return;
347         video::IVideoDriver* driver = Environment->getVideoDriver();
348         
349         /*video::SColor bgcolor(140,0,0,0);
350         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
351
352         video::SColor bgcolor(140,0,0,0);
353
354         {
355                 core::rect<s32> rect(0, 0, 620, 270);
356                 rect += AbsoluteRect.UpperLeftCorner;
357                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
358         }
359
360         {
361                 core::rect<s32> rect(0, 290, 620, 430);
362                 rect += AbsoluteRect.UpperLeftCorner;
363                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
364         }
365
366         gui::IGUIElement::draw();
367 }
368
369 void GUIMainMenu::acceptInput()
370 {
371         {
372                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
373                 if(e != NULL)
374                         m_data->name = e->getText();
375         }
376         {
377                 gui::IGUIElement *e = getElementFromId(264);
378                 if(e != NULL)
379                         m_data->password = e->getText();
380         }
381         {
382                 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
383                 if(e != NULL)
384                         m_data->address = e->getText();
385         }
386         {
387                 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
388                 if(e != NULL)
389                         m_data->port = e->getText();
390         }
391         {
392                 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
393                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
394                         m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
395         }
396         {
397                 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
398                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
399                         m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
400         }
401         {
402                 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
403                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
404                         m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
405         }
406         {
407                 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
408                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
409                         m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
410         }
411         {
412                 gui::IGUIElement *e = getElementFromId(GUI_ID_3D_CLOUDS_CB);
413                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
414                         m_data->clouds_3d = ((gui::IGUICheckBox*)e)->isChecked();
415         }
416         {
417                 gui::IGUIElement *e = getElementFromId(GUI_ID_OPAQUE_WATER_CB);
418                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
419                         m_data->opaque_water = ((gui::IGUICheckBox*)e)->isChecked();
420         }
421         
422         m_accepted = true;
423 }
424
425 bool GUIMainMenu::OnEvent(const SEvent& event)
426 {
427         if(event.EventType==EET_KEY_INPUT_EVENT)
428         {
429                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
430                 {
431                         m_gamecallback->exitToOS();
432                         quitMenu();
433                         return true;
434                 }
435                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
436                 {
437                         acceptInput();
438                         quitMenu();
439                         return true;
440                 }
441         }
442         if(event.EventType==EET_GUI_EVENT)
443         {
444                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
445                                 && isVisible())
446                 {
447                         if(!canTakeFocus(event.GUIEvent.Element))
448                         {
449                                 dstream<<"GUIMainMenu: Not allowing focus change."
450                                                 <<std::endl;
451                                 // Returning true disables focus change
452                                 return true;
453                         }
454                 }
455                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
456                 {
457                         switch(event.GUIEvent.Caller->getID())
458                         {
459                         case GUI_ID_JOIN_GAME_BUTTON: // Start game
460                                 acceptInput();
461                                 quitMenu();
462                                 return true;
463                         case GUI_ID_CHANGE_KEYS_BUTTON: {
464                                 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
465                                 kmenu->drop();
466                                 return true;
467                         }
468                         case GUI_ID_DELETE_MAP_BUTTON: // Delete map
469                                 // Don't accept input data, just set deletion request
470                                 m_data->delete_map = true;
471                                 m_accepted = true;
472                                 quitMenu();
473                                 return true;
474                         }
475                 }
476                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
477                 {
478                         switch(event.GUIEvent.Caller->getID())
479                         {
480                                 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
481                                 acceptInput();
482                                 quitMenu();
483                                 return true;
484                         }
485                 }
486         }
487
488         return Parent ? Parent->OnEvent(event) : false;
489 }
490