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