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