]> git.lizzy.rs Git - minetest.git/blob - src/guiMainMenu.cpp
Fix click areas of singleplayer checkboxes
[minetest.git] / src / guiMainMenu.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-12 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 "guiCreateWorld.h"
23 #include "guiMessageMenu.h"
24 #include "guiConfirmMenu.h"
25 #include "debug.h"
26 #include "serialization.h"
27 #include <string>
28 #include <IGUICheckBox.h>
29 #include <IGUIEditBox.h>
30 #include <IGUIButton.h>
31 #include <IGUIStaticText.h>
32 #include <IGUIFont.h>
33 #include <IGUIListBox.h>
34 #include <IGUITabControl.h>
35 #include <IGUIImage.h>
36 // For IGameCallback
37 #include "guiPauseMenu.h"
38 #include "gettext.h"
39 #include "utility.h"
40 #include "tile.h" // getTexturePath
41
42 struct CreateWorldDestMainMenu : public CreateWorldDest
43 {
44         CreateWorldDestMainMenu(GUIMainMenu *menu):
45                 m_menu(menu)
46         {}
47         void accepted(std::wstring name, std::string gameid)
48         {
49                 m_menu->createNewWorld(name, gameid);
50         }
51         GUIMainMenu *m_menu;
52 };
53
54 struct ConfirmDestDeleteWorld : public ConfirmDest
55 {
56         ConfirmDestDeleteWorld(WorldSpec spec, GUIMainMenu *menu):
57                 m_spec(spec),
58                 m_menu(menu)
59         {}
60         void answer(bool answer)
61         {
62                 if(answer == false)
63                         return;
64                 m_menu->deleteWorld(m_spec);
65         }
66         WorldSpec m_spec;
67         GUIMainMenu *m_menu;
68 };
69
70 enum
71 {
72         GUI_ID_QUIT_BUTTON = 101,
73         GUI_ID_NAME_INPUT,
74         GUI_ID_ADDRESS_INPUT,
75         GUI_ID_PORT_INPUT,
76         GUI_ID_FANCYTREE_CB,
77         GUI_ID_SMOOTH_LIGHTING_CB,
78         GUI_ID_3D_CLOUDS_CB,
79         GUI_ID_OPAQUE_WATER_CB,
80         GUI_ID_DAMAGE_CB,
81         GUI_ID_CREATIVE_CB,
82         GUI_ID_JOIN_GAME_BUTTON,
83         GUI_ID_CHANGE_KEYS_BUTTON,
84         GUI_ID_DELETE_WORLD_BUTTON,
85         GUI_ID_CREATE_WORLD_BUTTON,
86         GUI_ID_CONFIGURE_WORLD_BUTTON,
87         GUI_ID_WORLD_LISTBOX,
88         GUI_ID_TAB_CONTROL,
89 };
90
91 enum
92 {
93         TAB_SINGLEPLAYER=0,
94         TAB_MULTIPLAYER,
95         TAB_ADVANCED,
96         TAB_CREDITS
97 };
98
99 GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
100                 gui::IGUIElement* parent, s32 id,
101                 IMenuManager *menumgr,
102                 MainMenuData *data,
103                 IGameCallback *gamecallback
104 ):
105         GUIModalMenu(env, parent, id, menumgr),
106         m_data(data),
107         m_accepted(false),
108         m_gamecallback(gamecallback),
109         m_is_regenerating(false)
110 {
111         assert(m_data);
112         this->env = env;
113         this->parent = parent;
114         this->id = id;
115         this->menumgr = menumgr;
116 }
117
118 GUIMainMenu::~GUIMainMenu()
119 {
120         removeChildren();
121 }
122
123 void GUIMainMenu::removeChildren()
124 {
125         const core::list<gui::IGUIElement*> &children = getChildren();
126         core::list<gui::IGUIElement*> children_copy;
127         for(core::list<gui::IGUIElement*>::ConstIterator
128                         i = children.begin(); i != children.end(); i++)
129         {
130                 children_copy.push_back(*i);
131         }
132         for(core::list<gui::IGUIElement*>::Iterator
133                         i = children_copy.begin();
134                         i != children_copy.end(); i++)
135         {
136                 (*i)->remove();
137         }
138 }
139
140 void GUIMainMenu::regenerateGui(v2u32 screensize)
141 {
142         m_is_regenerating = true;
143         /*
144                 Read stuff from elements into m_data
145         */
146         readInput(m_data);
147
148         /*
149                 Remove stuff
150         */
151         removeChildren();
152         
153         /*
154                 Calculate new sizes and positions
155         */
156         
157         v2s32 size(screensize.X, screensize.Y);
158
159         core::rect<s32> rect(
160                         screensize.X/2 - size.X/2,
161                         screensize.Y/2 - size.Y/2,
162                         screensize.X/2 + size.X/2,
163                         screensize.Y/2 + size.Y/2
164         );
165
166         DesiredRect = rect;
167         recalculateAbsolutePosition(false);
168
169         //v2s32 size = rect.getSize();
170
171         /*
172                 Add stuff
173         */
174
175         changeCtype("");
176
177         // Version
178         if(m_data->selected_tab != TAB_CREDITS)
179         {
180                 core::rect<s32> rect(0, 0, size.X, 40);
181                 rect += v2s32(4, 0);
182                 Environment->addStaticText(narrow_to_wide(
183                                 "Minetest-c55 " VERSION_STRING).c_str(),
184                                 rect, false, true, this, -1);
185         }
186
187         //v2s32 center(size.X/2, size.Y/2);
188         v2s32 c800(size.X/2-400, size.Y/2-300);
189         
190         m_topleft_client = c800 + v2s32(90, 70+50+30);
191         m_size_client = v2s32(620, 270);
192
193         m_topleft_server = c800 + v2s32(90, 70+30+50+290);
194         m_size_server = v2s32(620, 140);
195         
196         if(m_data->selected_tab == TAB_ADVANCED)
197         {
198                 m_topleft_client = c800 + v2s32(90, 20+50+30);
199                 m_size_client = v2s32(620, 270);
200
201                 m_topleft_server = c800 + v2s32(90, 20+30+50+290);
202                 m_size_server = v2s32(620, 140);
203         }
204
205         // Tabs
206 #if 1
207         {
208                 core::rect<s32> rect(0, 0, m_size_client.X, 30);
209                 rect += m_topleft_client + v2s32(0, -30);
210                 gui::IGUITabControl *e = Environment->addTabControl(
211                                 rect, this, true, true, GUI_ID_TAB_CONTROL);
212                 e->addTab(L"Singleplayer");
213                 e->addTab(L"Multiplayer");
214                 e->addTab(L"Advanced");
215                 e->addTab(L"Credits");
216                 e->setActiveTab(m_data->selected_tab);
217         }
218 #endif
219         
220         if(m_data->selected_tab == TAB_SINGLEPLAYER)
221         {
222                 // HYBRID
223                 {
224                         /*core::rect<s32> rect(0, 0, 20, 125);
225                         rect += m_topleft_client + v2s32(15, 80);
226                         const wchar_t *text = L"H\nY\nB\nR\nI\nD";*/
227                         core::rect<s32> rect(0, 0, 20, 300);
228                         rect += m_topleft_client + v2s32(15, 15);
229                         const wchar_t *text = L"T\nA\nP\nE\n\nA\nN\nD\n\nG\nL\nU\nE";
230                         //gui::IGUIStaticText *t =
231                         Environment->addStaticText(text, rect, false, true, this, -1);
232                         //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
233                 }
234                 u32 bs = 5;
235                 // World selection listbox
236                 u32 world_sel_h = 160;
237                 u32 world_sel_w = 365;
238                 //s32 world_sel_x = 50;
239                 s32 world_sel_x = m_size_client.X-world_sel_w-30;
240                 s32 world_sel_y = 30;
241                 u32 world_button_count = 3;
242                 u32 world_button_w = (world_sel_w)/world_button_count - bs
243                                 + bs/(world_button_count-1);
244                 {
245                         core::rect<s32> rect(0, 0, world_sel_w-4, 20);
246                         rect += m_topleft_client + v2s32(world_sel_x+4, world_sel_y-20);
247                         /*gui::IGUIStaticText *e =*/ Environment->addStaticText(
248                                         wgettext("Select World:"), 
249                                         rect, false, true, this, -1);
250                         /*e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);*/
251                 }
252                 {
253                         core::rect<s32> rect(0, 0, world_sel_w, world_sel_h);
254                         rect += m_topleft_client + v2s32(world_sel_x, world_sel_y);
255                         gui::IGUIListBox *e = Environment->addListBox(rect, this,
256                                         GUI_ID_WORLD_LISTBOX);
257                         e->setDrawBackground(true);
258                         for(std::vector<WorldSpec>::const_iterator i = m_data->worlds.begin();
259                                         i != m_data->worlds.end(); i++){
260                                 e->addItem(narrow_to_wide(i->name+" ["+i->gameid+"]").c_str());
261                         }
262                         e->setSelected(m_data->selected_world);
263                         Environment->setFocus(e);
264                 }
265                 // Delete world button
266                 {
267                         core::rect<s32> rect(0, 0, world_button_w, 30);
268                         rect += m_topleft_client + v2s32(world_sel_x, world_sel_y+world_sel_h+0);
269                         Environment->addButton(rect, this, GUI_ID_DELETE_WORLD_BUTTON,
270                                   wgettext("Delete"));
271                 }
272                 // Create world button
273                 {
274                         core::rect<s32> rect(0, 0, world_button_w, 30);
275                         rect += m_topleft_client + v2s32(world_sel_x+world_button_w+bs, world_sel_y+world_sel_h+0);
276                         Environment->addButton(rect, this, GUI_ID_CREATE_WORLD_BUTTON,
277                                   wgettext("New"));
278                 }
279                 // Configure world button
280                 {
281                         core::rect<s32> rect(0, 0, world_button_w, 30);
282                         rect += m_topleft_client + v2s32(world_sel_x+(world_button_w+bs)*2,
283                                         world_sel_y+world_sel_h+0);
284                         Environment->addButton(rect, this, GUI_ID_CONFIGURE_WORLD_BUTTON,
285                                   wgettext("Configure"));
286                 }
287                 // Start game button
288                 {
289                         /*core::rect<s32> rect(0, 0, 120, 30);
290                         rect += m_topleft_client + v2s32(m_size_client.X-120-30,
291                                         m_size_client.Y-30-20-30-5);*/
292                         /*core::rect<s32> rect(0, 0, world_button_w, 30);
293                         rect += m_topleft_client + v2s32(world_sel_x+(world_button_w+bs)*3,
294                                         world_sel_y+world_sel_h+0);*/
295                         u32 bw = 160;
296                         core::rect<s32> rect(0, 0, bw, 30);
297                         rect += m_topleft_client + v2s32(world_sel_x+world_sel_w-bw,
298                                         world_sel_y+world_sel_h+30+bs);
299                         Environment->addButton(rect, this,
300                                         GUI_ID_JOIN_GAME_BUTTON, wgettext("Play"));
301                 }
302                 // Options
303                 s32 option_x = 50;
304                 s32 option_y = 30;
305                 u32 option_w = 150;
306                 {
307                         core::rect<s32> rect(0, 0, option_w, 30);
308                         rect += m_topleft_client + v2s32(option_x, option_y);
309                         Environment->addCheckBox(m_data->fancy_trees, rect, this,
310                                         GUI_ID_FANCYTREE_CB, wgettext("Fancy trees")); 
311                 }
312                 {
313                         core::rect<s32> rect(0, 0, option_w, 30);
314                         rect += m_topleft_client + v2s32(option_x, option_y+20);
315                         Environment->addCheckBox(m_data->smooth_lighting, rect, this,
316                                         GUI_ID_SMOOTH_LIGHTING_CB, wgettext("Smooth Lighting"));
317                 }
318                 {
319                         core::rect<s32> rect(0, 0, option_w, 30);
320                         rect += m_topleft_client + v2s32(option_x, option_y+20*2);
321                         Environment->addCheckBox(m_data->clouds_3d, rect, this,
322                                         GUI_ID_3D_CLOUDS_CB, wgettext("3D Clouds"));
323                 }
324                 {
325                         core::rect<s32> rect(0, 0, option_w, 30);
326                         rect += m_topleft_client + v2s32(option_x, option_y+20*3);
327                         Environment->addCheckBox(m_data->opaque_water, rect, this,
328                                         GUI_ID_OPAQUE_WATER_CB, wgettext("Opaque water"));
329                 }
330                 {
331                         core::rect<s32> rect(0, 0, option_w, 30);
332                         rect += m_topleft_client + v2s32(option_x, option_y+20*4);
333                         Environment->addCheckBox(m_data->creative_mode, rect, this,
334                                         GUI_ID_CREATIVE_CB, wgettext("Creative Mode"));
335                 }
336                 {
337                         core::rect<s32> rect(0, 0, option_w, 30);
338                         rect += m_topleft_client + v2s32(option_x, option_y+20*5);
339                         Environment->addCheckBox(m_data->enable_damage, rect, this,
340                                         GUI_ID_DAMAGE_CB, wgettext("Enable Damage"));
341                 }
342                 // Key change button
343                 {
344                         core::rect<s32> rect(0, 0, 120, 30);
345                         /*rect += m_topleft_client + v2s32(m_size_client.X-120-30,
346                                         m_size_client.Y-30-20);*/
347                         rect += m_topleft_client + v2s32(option_x, option_y+20*6+20);
348                         Environment->addButton(rect, this,
349                                         GUI_ID_CHANGE_KEYS_BUTTON, wgettext("Change keys"));
350                 }
351                 changeCtype("C");
352         }
353         else if(m_data->selected_tab == TAB_MULTIPLAYER)
354         {
355                 changeCtype("");
356                 // CLIENT
357                 {
358                         core::rect<s32> rect(0, 0, 20, 125);
359                         rect += m_topleft_client + v2s32(15, 80);
360                         const wchar_t *text = L"C\nL\nI\nE\nN\nT";
361                         //gui::IGUIStaticText *t =
362                         Environment->addStaticText(text, rect, false, true, this, -1);
363                         //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
364                 }
365                 // Nickname + password
366                 {
367                         core::rect<s32> rect(0, 0, 110, 20);
368                         rect += m_topleft_client + v2s32(35+30, 50+6);
369                         Environment->addStaticText(wgettext("Name/Password"), 
370                                 rect, false, true, this, -1);
371                 }
372                 changeCtype("C");
373                 {
374                         core::rect<s32> rect(0, 0, 230, 30);
375                         rect += m_topleft_client + v2s32(160+30, 50);
376                         gui::IGUIElement *e = 
377                         Environment->addEditBox(m_data->name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
378                         if(m_data->name == L"")
379                                 Environment->setFocus(e);
380                 }
381                 {
382                         core::rect<s32> rect(0, 0, 120, 30);
383                         rect += m_topleft_client + v2s32(m_size_client.X-60-100, 50);
384                         gui::IGUIEditBox *e =
385                         Environment->addEditBox(L"", rect, true, this, 264);
386                         e->setPasswordBox(true);
387                         if(m_data->name != L"" && m_data->address != L"")
388                                 Environment->setFocus(e);
389
390                 }
391                 changeCtype("");
392                 // Address + port
393                 {
394                         core::rect<s32> rect(0, 0, 110, 20);
395                         rect += m_topleft_client + v2s32(35+30, 100+6);
396                         Environment->addStaticText(wgettext("Address/Port"),
397                                 rect, false, true, this, -1);
398                 }
399                 changeCtype("C");
400                 {
401                         core::rect<s32> rect(0, 0, 230, 30);
402                         rect += m_topleft_client + v2s32(160+30, 100);
403                         gui::IGUIElement *e = 
404                         Environment->addEditBox(m_data->address.c_str(), rect, true,
405                                         this, GUI_ID_ADDRESS_INPUT);
406                         if(m_data->name != L"" && m_data->address == L"")
407                                 Environment->setFocus(e);
408                 }
409                 {
410                         core::rect<s32> rect(0, 0, 120, 30);
411                         rect += m_topleft_client + v2s32(m_size_client.X-60-100, 100);
412                         Environment->addEditBox(m_data->port.c_str(), rect, true,
413                                         this, GUI_ID_PORT_INPUT);
414                 }
415                 changeCtype("");
416                 {
417                         core::rect<s32> rect(0, 0, 250, 30);
418                         rect += m_topleft_client + v2s32(35+30, 150);
419                         Environment->addCheckBox(m_data->fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
420                                 wgettext("Fancy trees")); 
421                 }
422                 {
423                         core::rect<s32> rect(0, 0, 250, 30);
424                         rect += m_topleft_client + v2s32(35+30, 150+20);
425                         Environment->addCheckBox(m_data->smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
426                                         wgettext("Smooth Lighting"));
427                 }
428                 {
429                         core::rect<s32> rect(0, 0, 250, 30);
430                         rect += m_topleft_client + v2s32(35+30, 150+40);
431                         Environment->addCheckBox(m_data->clouds_3d, rect, this, GUI_ID_3D_CLOUDS_CB,
432                                         wgettext("3D Clouds"));
433                 }
434                 {
435                         core::rect<s32> rect(0, 0, 250, 30);
436                         rect += m_topleft_client + v2s32(35+30, 150+60);
437                         Environment->addCheckBox(m_data->opaque_water, rect, this, GUI_ID_OPAQUE_WATER_CB,
438                                         wgettext("Opaque water"));
439                 }
440                 // Key change button
441                 {
442                         core::rect<s32> rect(0, 0, 120, 30);
443                         rect += m_topleft_client + v2s32(m_size_client.X-180-30-120-20,
444                                         m_size_client.Y-30-20);
445                         Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
446                                 wgettext("Change keys"));
447                 }
448                 // Start game button
449                 {
450                         core::rect<s32> rect(0, 0, 180, 30);
451                         rect += m_topleft_client + v2s32(m_size_client.X-180-30,
452                                         m_size_client.Y-30-20);
453                         Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
454                                 wgettext("Start Game / Connect"));
455                 }
456                 changeCtype("C");
457         }
458         else if(m_data->selected_tab == TAB_ADVANCED)
459         {
460                 changeCtype("");
461                 // CLIENT
462                 {
463                         core::rect<s32> rect(0, 0, 20, 125);
464                         rect += m_topleft_client + v2s32(15, 80);
465                         const wchar_t *text = L"C\nL\nI\nE\nN\nT";
466                         //gui::IGUIStaticText *t =
467                         Environment->addStaticText(text, rect, false, true, this, -1);
468                         //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
469                 }
470                 // Nickname + password
471                 {
472                         core::rect<s32> rect(0, 0, 110, 20);
473                         rect += m_topleft_client + v2s32(35+30, 50+6);
474                         Environment->addStaticText(wgettext("Name/Password"), 
475                                 rect, false, true, this, -1);
476                 }
477                 changeCtype("C");
478                 {
479                         core::rect<s32> rect(0, 0, 230, 30);
480                         rect += m_topleft_client + v2s32(160+30, 50);
481                         gui::IGUIElement *e = 
482                         Environment->addEditBox(m_data->name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
483                         if(m_data->name == L"")
484                                 Environment->setFocus(e);
485                 }
486                 {
487                         core::rect<s32> rect(0, 0, 120, 30);
488                         rect += m_topleft_client + v2s32(m_size_client.X-60-100, 50);
489                         gui::IGUIEditBox *e =
490                         Environment->addEditBox(L"", rect, true, this, 264);
491                         e->setPasswordBox(true);
492                         if(m_data->name != L"" && m_data->address != L"")
493                                 Environment->setFocus(e);
494
495                 }
496                 changeCtype("");
497                 // Address + port
498                 {
499                         core::rect<s32> rect(0, 0, 110, 20);
500                         rect += m_topleft_client + v2s32(35+30, 100+6);
501                         Environment->addStaticText(wgettext("Address/Port"),
502                                 rect, false, true, this, -1);
503                 }
504                 changeCtype("C");
505                 {
506                         core::rect<s32> rect(0, 0, 230, 30);
507                         rect += m_topleft_client + v2s32(160+30, 100);
508                         gui::IGUIElement *e = 
509                         Environment->addEditBox(m_data->address.c_str(), rect, true,
510                                         this, GUI_ID_ADDRESS_INPUT);
511                         if(m_data->name != L"" && m_data->address == L"")
512                                 Environment->setFocus(e);
513                 }
514                 {
515                         core::rect<s32> rect(0, 0, 120, 30);
516                         rect += m_topleft_client + v2s32(m_size_client.X-60-100, 100);
517                         Environment->addEditBox(m_data->port.c_str(), rect, true,
518                                         this, GUI_ID_PORT_INPUT);
519                 }
520                 changeCtype("");
521                 {
522                         core::rect<s32> rect(0, 0, 400, 20);
523                         rect += m_topleft_client + v2s32(160+30, 100+35);
524                         Environment->addStaticText(wgettext("Leave address blank to start a local server."),
525                                 rect, false, true, this, -1);
526                 }
527                 {
528                         core::rect<s32> rect(0, 0, 250, 30);
529                         rect += m_topleft_client + v2s32(35+30, 150);
530                         Environment->addCheckBox(m_data->fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
531                                 wgettext("Fancy trees")); 
532                 }
533                 {
534                         core::rect<s32> rect(0, 0, 250, 30);
535                         rect += m_topleft_client + v2s32(35+30, 150+20);
536                         Environment->addCheckBox(m_data->smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
537                                         wgettext("Smooth Lighting"));
538                 }
539                 {
540                         core::rect<s32> rect(0, 0, 250, 30);
541                         rect += m_topleft_client + v2s32(35+30, 150+40);
542                         Environment->addCheckBox(m_data->clouds_3d, rect, this, GUI_ID_3D_CLOUDS_CB,
543                                         wgettext("3D Clouds"));
544                 }
545                 {
546                         core::rect<s32> rect(0, 0, 250, 30);
547                         rect += m_topleft_client + v2s32(35+30, 150+60);
548                         Environment->addCheckBox(m_data->opaque_water, rect, this, GUI_ID_OPAQUE_WATER_CB,
549                                         wgettext("Opaque water"));
550                 }
551                 // Key change button
552                 {
553                         core::rect<s32> rect(0, 0, 120, 30);
554                         rect += m_topleft_client + v2s32(m_size_client.X-180-30-120-20,
555                                         m_size_client.Y-30-20);
556                         Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
557                                 wgettext("Change keys"));
558                 }
559                 // Start game button
560                 {
561                         core::rect<s32> rect(0, 0, 180, 30);
562                         rect += m_topleft_client + v2s32(m_size_client.X-180-30,
563                                         m_size_client.Y-30-20);
564                         Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
565                                 wgettext("Start Game / Connect"));
566                 }
567                 /*
568                         Server section
569                 */
570                 // SERVER
571                 {
572                         core::rect<s32> rect(0, 0, 20, 125);
573                         rect += m_topleft_server + v2s32(15, 15);
574                         const wchar_t *text = L"S\nE\nR\nV\nE\nR";
575                         //gui::IGUIStaticText *t =
576                         Environment->addStaticText(text, rect, false, true, this, -1);
577                         //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
578                 }
579                 // Server parameters
580                 {
581                         core::rect<s32> rect(0, 0, 250, 30);
582                         rect += m_topleft_server + v2s32(30+20+250+20, 20);
583                         Environment->addCheckBox(m_data->creative_mode, rect, this, GUI_ID_CREATIVE_CB,
584                                 wgettext("Creative Mode"));
585                 }
586                 {
587                         core::rect<s32> rect(0, 0, 250, 30);
588                         rect += m_topleft_server + v2s32(30+20+250+20, 40);
589                         Environment->addCheckBox(m_data->enable_damage, rect, this, GUI_ID_DAMAGE_CB,
590                                 wgettext("Enable Damage"));
591                 }
592                 // Delete world button
593                 {
594                         core::rect<s32> rect(0, 0, 130, 30);
595                         rect += m_topleft_server + v2s32(30+20+250+20, 90);
596                         Environment->addButton(rect, this, GUI_ID_DELETE_WORLD_BUTTON,
597                                   wgettext("Delete world"));
598                 }
599                 // Create world button
600                 {
601                         core::rect<s32> rect(0, 0, 130, 30);
602                         rect += m_topleft_server + v2s32(30+20+250+20+140, 90);
603                         Environment->addButton(rect, this, GUI_ID_CREATE_WORLD_BUTTON,
604                                   wgettext("Create world"));
605                 }
606                 // World selection listbox
607                 {
608                         core::rect<s32> rect(0, 0, 250, 120);
609                         rect += m_topleft_server + v2s32(30+20, 10);
610                         gui::IGUIListBox *e = Environment->addListBox(rect, this,
611                                         GUI_ID_WORLD_LISTBOX);
612                         e->setDrawBackground(true);
613                         for(std::vector<WorldSpec>::const_iterator i = m_data->worlds.begin();
614                                         i != m_data->worlds.end(); i++){
615                                 e->addItem(narrow_to_wide(i->name+" ["+i->gameid+"]").c_str());
616                         }
617                         e->setSelected(m_data->selected_world);
618                 }
619                 changeCtype("C");
620         }
621         else if(m_data->selected_tab == TAB_CREDITS)
622         {
623                 // CREDITS
624                 {
625                         core::rect<s32> rect(0, 0, 20, 200);
626                         rect += m_topleft_client + v2s32(15, 60);
627                         const wchar_t *text = L"C\nR\nE\nD\nI\nT\nS";
628                         //gui::IGUIStaticText *t =
629                         Environment->addStaticText(text, rect, false, true, this, -1);
630                         //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
631                 }
632                 {
633                         core::rect<s32> rect(0, 0, 620, 250);
634                         rect += m_topleft_client + v2s32(150+14, 50+35);
635                         Environment->addStaticText(narrow_to_wide(
636                         "Minetest-c55 " VERSION_STRING "\n"
637                         "http://c55.me/minetest/\n"
638                         "\n"
639                         "by Perttu Ahola <celeron55@gmail.com>\n"
640                         "and contributors"
641                         ).c_str(), rect, false, true, this, -1);
642                 }
643         }
644
645         m_is_regenerating = false;
646 }
647
648 void GUIMainMenu::drawMenu()
649 {
650         gui::IGUISkin* skin = Environment->getSkin();
651         if (!skin)
652                 return;
653         video::IVideoDriver* driver = Environment->getVideoDriver();
654         
655         /*video::SColor bgcolor(140,0,0,0);
656         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
657
658         video::SColor bgcolor(140,0,0,0);
659
660         if(getTab() == TAB_SINGLEPLAYER)
661         {
662                 {
663                         core::rect<s32> rect(0, 0, m_size_client.X, m_size_client.Y);
664                         rect += AbsoluteRect.UpperLeftCorner + m_topleft_client;
665                         driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
666                 }
667         }
668         else if(getTab() == TAB_MULTIPLAYER)
669         {
670                 {
671                         core::rect<s32> rect(0, 0, m_size_client.X, m_size_client.Y);
672                         rect += AbsoluteRect.UpperLeftCorner + m_topleft_client;
673                         driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
674                 }
675         }
676         else if(getTab() == TAB_ADVANCED)
677         {
678                 {
679                         core::rect<s32> rect(0, 0, m_size_client.X, m_size_client.Y);
680                         rect += AbsoluteRect.UpperLeftCorner + m_topleft_client;
681                         driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
682                 }
683                 {
684                         core::rect<s32> rect(0, 0, m_size_server.X, m_size_server.Y);
685                         rect += AbsoluteRect.UpperLeftCorner + m_topleft_server;
686                         driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
687                 }
688         }
689         else if(getTab() == TAB_CREDITS)
690         {
691                 {
692                         core::rect<s32> rect(0, 0, m_size_client.X, m_size_client.Y);
693                         rect += AbsoluteRect.UpperLeftCorner + m_topleft_client;
694                         driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
695                 }
696                 video::ITexture *logotexture =
697                                 driver->getTexture(getTexturePath("menulogo.png").c_str());
698                 if(logotexture)
699                 {
700                         v2s32 logosize(logotexture->getOriginalSize().Width,
701                                         logotexture->getOriginalSize().Height);
702                         logosize *= 2;
703                         core::rect<s32> rect(0,0,logosize.X,logosize.Y);
704                         rect += AbsoluteRect.UpperLeftCorner + m_topleft_client;
705                         rect += v2s32(150, 50);
706                         driver->draw2DImage(logotexture, rect,
707                                 core::rect<s32>(core::position2d<s32>(0,0),
708                                 core::dimension2di(logotexture->getSize())),
709                                 NULL, NULL, true);
710                 }
711         }
712
713         gui::IGUIElement::draw();
714 }
715
716 void GUIMainMenu::readInput(MainMenuData *dst)
717 {
718         {
719                 gui::IGUIElement *e = getElementFromId(GUI_ID_TAB_CONTROL);
720                 if(e != NULL && e->getType() == gui::EGUIET_TAB_CONTROL)
721                         dst->selected_tab = ((gui::IGUITabControl*)e)->getActiveTab();
722         }
723         if(dst->selected_tab == TAB_SINGLEPLAYER)
724         {
725                 dst->simple_singleplayer_mode = true;
726         }
727         else
728         {
729                 dst->simple_singleplayer_mode = false;
730                 {
731                         gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
732                         if(e != NULL)
733                                 dst->name = e->getText();
734                 }
735                 {
736                         gui::IGUIElement *e = getElementFromId(264);
737                         if(e != NULL)
738                                 dst->password = e->getText();
739                 }
740                 {
741                         gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
742                         if(e != NULL)
743                                 dst->address = e->getText();
744                 }
745                 {
746                         gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
747                         if(e != NULL)
748                                 dst->port = e->getText();
749                 }
750         }
751         {
752                 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
753                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
754                         dst->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
755         }
756         {
757                 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
758                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
759                         dst->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
760         }
761         {
762                 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
763                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
764                         dst->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
765         }
766         {
767                 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
768                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
769                         dst->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
770         }
771         {
772                 gui::IGUIElement *e = getElementFromId(GUI_ID_3D_CLOUDS_CB);
773                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
774                         dst->clouds_3d = ((gui::IGUICheckBox*)e)->isChecked();
775         }
776         {
777                 gui::IGUIElement *e = getElementFromId(GUI_ID_OPAQUE_WATER_CB);
778                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
779                         dst->opaque_water = ((gui::IGUICheckBox*)e)->isChecked();
780         }
781
782         {
783                 gui::IGUIElement *e = getElementFromId(GUI_ID_WORLD_LISTBOX);
784                 if(e != NULL && e->getType() == gui::EGUIET_LIST_BOX)
785                         dst->selected_world = ((gui::IGUIListBox*)e)->getSelected();
786         }
787 }
788
789 void GUIMainMenu::acceptInput()
790 {
791         readInput(m_data);
792         m_accepted = true;
793 }
794
795 bool GUIMainMenu::OnEvent(const SEvent& event)
796 {
797         if(event.EventType==EET_KEY_INPUT_EVENT)
798         {
799                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
800                 {
801                         m_gamecallback->exitToOS();
802                         quitMenu();
803                         return true;
804                 }
805                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
806                 {
807                         acceptInput();
808                         quitMenu();
809                         return true;
810                 }
811         }
812         if(event.EventType==EET_GUI_EVENT)
813         {
814                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
815                                 && isVisible())
816                 {
817                         if(!canTakeFocus(event.GUIEvent.Element))
818                         {
819                                 dstream<<"GUIMainMenu: Not allowing focus change."
820                                                 <<std::endl;
821                                 // Returning true disables focus change
822                                 return true;
823                         }
824                 }
825                 if(event.GUIEvent.EventType==gui::EGET_TAB_CHANGED)
826                 {
827                         if(!m_is_regenerating)
828                                 regenerateGui(m_screensize_old);
829                         return true;
830                 }
831                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
832                 {
833                         switch(event.GUIEvent.Caller->getID())
834                         {
835                         case GUI_ID_JOIN_GAME_BUTTON: {
836                                 MainMenuData cur;
837                                 readInput(&cur);
838                                 if(cur.address == L"" && getTab() == TAB_MULTIPLAYER){
839                                         (new GUIMessageMenu(env, parent, -1, menumgr,
840                                                         wgettext("Address required."))
841                                                         )->drop();
842                                         return true;
843                                 }
844                                 acceptInput();
845                                 quitMenu();
846                                 return true;
847                         }
848                         case GUI_ID_CHANGE_KEYS_BUTTON: {
849                                 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
850                                 kmenu->drop();
851                                 return true;
852                         }
853                         case GUI_ID_DELETE_WORLD_BUTTON: {
854                                 MainMenuData cur;
855                                 readInput(&cur);
856                                 if(cur.selected_world == -1){
857                                         (new GUIMessageMenu(env, parent, -1, menumgr,
858                                                         wgettext("Cannot delete world: Nothing selected"))
859                                                         )->drop();
860                                 } else {
861                                         WorldSpec spec = m_data->worlds[cur.selected_world];
862                                         ConfirmDestDeleteWorld *dest = new
863                                                         ConfirmDestDeleteWorld(spec, this);
864                                         (new GUIConfirmMenu(env, parent, -1, menumgr, dest,
865                                                         (std::wstring(wgettext("Delete world "))
866                                                         +L"\""+narrow_to_wide(spec.name)+L"\"?").c_str()
867                                                         ))->drop();
868                                 }
869                                 return true;
870                         }
871                         case GUI_ID_CREATE_WORLD_BUTTON: {
872                                 std::vector<SubgameSpec> games = getAvailableGames();
873                                 if(games.size() == 0){
874                                         GUIMessageMenu *menu = new GUIMessageMenu(env, parent,
875                                                         -1, menumgr,
876                                                         wgettext("Cannot create world: No games found"));
877                                         menu->drop();
878                                 } else {
879                                         CreateWorldDest *dest = new CreateWorldDestMainMenu(this);
880                                         GUICreateWorld *menu = new GUICreateWorld(env, parent, -1,
881                                                         menumgr, dest, games);
882                                         menu->drop();
883                                 }
884                                 return true;
885                         }
886                         case GUI_ID_CONFIGURE_WORLD_BUTTON: {
887                                 GUIMessageMenu *menu = new GUIMessageMenu(env, parent,
888                                                 -1, menumgr,
889                                                 wgettext("Nothing here"));
890                                 menu->drop();
891                                 return true;
892                         }
893                         }
894                 }
895                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
896                 {
897                         switch(event.GUIEvent.Caller->getID())
898                         {
899                                 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
900                                 acceptInput();
901                                 quitMenu();
902                                 return true;
903                         }
904                 }
905                 if(event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN)
906                 {
907                         switch(event.GUIEvent.Caller->getID())
908                         {
909                                 case GUI_ID_WORLD_LISTBOX:
910                                 acceptInput();
911                                 m_data->address = L""; // Force local game
912                                 quitMenu();
913                                 return true;
914                         }
915                 }
916         }
917
918         return Parent ? Parent->OnEvent(event) : false;
919 }
920
921 void GUIMainMenu::createNewWorld(std::wstring name, std::string gameid)
922 {
923         if(name == L"")
924                 return;
925         acceptInput();
926         m_data->create_world_name = name;
927         m_data->create_world_gameid = gameid;
928         quitMenu();
929 }
930
931 void GUIMainMenu::deleteWorld(WorldSpec spec)
932 {
933         if(!spec.isValid())
934                 return;
935         acceptInput();
936         m_data->delete_world_spec = spec;
937         quitMenu();
938 }
939         
940 int GUIMainMenu::getTab()
941 {
942         gui::IGUIElement *e = getElementFromId(GUI_ID_TAB_CONTROL);
943         if(e != NULL && e->getType() == gui::EGUIET_TAB_CONTROL)
944                 return ((gui::IGUITabControl*)e)->getActiveTab();
945         return TAB_SINGLEPLAYER; // Default
946 }
947