Merge branch 'upstream/master'
[oweals/minetest.git] / src / guiMainMenu.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "guiMainMenu.h"
21 #include "guiKeyChangeMenu.h"
22 #include "debug.h"
23 #include "serialization.h"
24 #include <string>
25
26
27
28 #include "gettext.h"
29
30 GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
31                 gui::IGUIElement* parent, s32 id,
32                 IMenuManager *menumgr,
33                 MainMenuData *data,
34                 IGameCallback *gamecallback
35 ):
36         GUIModalMenu(env, parent, id, menumgr),
37         m_data(data),
38         m_accepted(false),
39         m_gamecallback(gamecallback)
40 {
41         assert(m_data);
42         this->env = env;
43         this->parent = parent;
44         this->id = id;
45         this->menumgr = menumgr;
46 }
47
48 GUIMainMenu::~GUIMainMenu()
49 {
50         removeChildren();
51 }
52
53 void GUIMainMenu::removeChildren()
54 {
55         const core::list<gui::IGUIElement*> &children = getChildren();
56         core::list<gui::IGUIElement*> children_copy;
57         for(core::list<gui::IGUIElement*>::ConstIterator
58                         i = children.begin(); i != children.end(); i++)
59         {
60                 children_copy.push_back(*i);
61         }
62         for(core::list<gui::IGUIElement*>::Iterator
63                         i = children_copy.begin();
64                         i != children_copy.end(); i++)
65         {
66                 (*i)->remove();
67         }
68 }
69
70 void GUIMainMenu::regenerateGui(v2u32 screensize)
71 {
72         std::wstring text_name;
73         std::wstring text_address;
74         std::wstring text_port;
75         bool creative_mode;
76         bool enable_damage;
77         bool fancy_trees;
78         bool smooth_lighting;
79         
80         // Client options
81         {
82                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
83                 if(e != NULL)
84                         text_name = e->getText();
85                 else
86                         text_name = m_data->name;
87         }
88         {
89                 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
90                 if(e != NULL)
91                         text_address = e->getText();
92                 else
93                         text_address = m_data->address;
94         }
95         {
96                 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
97                 if(e != NULL)
98                         text_port = e->getText();
99                 else
100                         text_port = m_data->port;
101         }
102         {
103                 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
104                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
105                         fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
106                 else
107                         fancy_trees = m_data->fancy_trees;
108         }
109         {
110                 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
111                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
112                         smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
113                 else
114                         smooth_lighting = m_data->smooth_lighting;
115         }
116         
117         // Server options
118         {
119                 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
120                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
121                         creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
122                 else
123                         creative_mode = m_data->creative_mode;
124         }
125         {
126                 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
127                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
128                         enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
129                 else
130                         enable_damage = m_data->enable_damage;
131         }
132
133         /*
134                 Remove stuff
135         */
136         removeChildren();
137         
138         /*
139                 Calculate new sizes and positions
140         */
141         
142         v2s32 size(620, 430);
143
144         core::rect<s32> rect(
145                         screensize.X/2 - size.X/2,
146                         screensize.Y/2 - size.Y/2,
147                         screensize.X/2 + size.X/2,
148                         screensize.Y/2 + size.Y/2
149         );
150
151         DesiredRect = rect;
152         recalculateAbsolutePosition(false);
153
154         //v2s32 size = rect.getSize();
155
156         /*
157                 Add stuff
158         */
159
160         /*
161                 Client section
162         */
163
164         v2s32 topleft_client(40, 0);
165         v2s32 size_client = size - v2s32(40, 0);
166         
167         {
168                 core::rect<s32> rect(0, 0, 20, 125);
169                 rect += topleft_client + v2s32(-15, 60);
170                 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
171                 //gui::IGUIStaticText *t =
172                 Environment->addStaticText(text, rect, false, true, this, -1);
173                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
174         }
175
176         // Nickname + password
177         {
178                 core::rect<s32> rect(0, 0, 110, 20);
179                 rect += topleft_client + v2s32(35, 50+6);
180                 Environment->addStaticText(chartowchar_t(gettext("Name/Password")), 
181                         rect, false, true, this, -1);
182         }
183         {
184                 core::rect<s32> rect(0, 0, 230, 30);
185                 rect += topleft_client + v2s32(160, 50);
186                 gui::IGUIElement *e = 
187                 Environment->addEditBox(text_name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
188                 if(text_name == L"")
189                         Environment->setFocus(e);
190         }
191         {
192                 core::rect<s32> rect(0, 0, 120, 30);
193                 rect += topleft_client + v2s32(size_client.X-60-100, 50);
194                 gui::IGUIEditBox *e =
195                 Environment->addEditBox(L"", rect, true, this, 264);
196                 e->setPasswordBox(true);
197
198         }
199         // Address + port
200         {
201                 core::rect<s32> rect(0, 0, 110, 20);
202                 rect += topleft_client + v2s32(35, 100+6);
203                 Environment->addStaticText(chartowchar_t(gettext("Address/Port")),
204                         rect, false, true, this, -1);
205         }
206         {
207                 core::rect<s32> rect(0, 0, 230, 30);
208                 rect += topleft_client + v2s32(160, 100);
209                 gui::IGUIElement *e = 
210                 Environment->addEditBox(text_address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
211                 if(text_name != L"")
212                         Environment->setFocus(e);
213         }
214         {
215                 core::rect<s32> rect(0, 0, 120, 30);
216                 //rect += topleft_client + v2s32(160+250+20, 125);
217                 rect += topleft_client + v2s32(size_client.X-60-100, 100);
218                 Environment->addEditBox(text_port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
219         }
220         {
221                 core::rect<s32> rect(0, 0, 400, 20);
222                 rect += topleft_client + v2s32(160, 100+35);
223                 Environment->addStaticText(chartowchar_t(gettext("Leave address blank to start a local server.")),
224                         rect, false, true, this, -1);
225         }
226         {
227                 core::rect<s32> rect(0, 0, 250, 30);
228                 rect += topleft_client + v2s32(35, 150);
229                 Environment->addCheckBox(fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
230                         chartowchar_t(gettext("Fancy trees"))); 
231         }
232         {
233                 core::rect<s32> rect(0, 0, 250, 30);
234                 rect += topleft_client + v2s32(35, 150+30);
235                 Environment->addCheckBox(smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
236                                 chartowchar_t(gettext("Smooth Lighting")));
237         }
238         // Start game button
239         {
240                 core::rect<s32> rect(0, 0, 180, 30);
241                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
242                 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
243                 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
244                         chartowchar_t(gettext("Start Game / Connect")));
245         }
246
247         // Key change button
248         {
249                 core::rect<s32> rect(0, 0, 100, 30);
250                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
251                 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
252                 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
253                         chartowchar_t(gettext("Change keys")));
254         }
255         /*
256                 Server section
257         */
258
259         v2s32 topleft_server(40, 250);
260         v2s32 size_server = size - v2s32(40, 0);
261         
262         {
263                 core::rect<s32> rect(0, 0, 20, 125);
264                 rect += topleft_server + v2s32(-15, 40);
265                 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
266                 //gui::IGUIStaticText *t =
267                 Environment->addStaticText(text, rect, false, true, this, -1);
268                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
269         }
270
271         // Server parameters
272         {
273                 core::rect<s32> rect(0, 0, 250, 30);
274                 rect += topleft_server + v2s32(35, 30);
275                 Environment->addCheckBox(creative_mode, rect, this, GUI_ID_CREATIVE_CB,
276                         chartowchar_t(gettext("Creative Mode")));
277         }
278         {
279                 core::rect<s32> rect(0, 0, 250, 30);
280                 rect += topleft_server + v2s32(35, 60);
281                 Environment->addCheckBox(enable_damage, rect, this, GUI_ID_DAMAGE_CB,
282                         chartowchar_t(gettext("Enable Damage")));
283         }
284         // Map delete button
285         {
286                 core::rect<s32> rect(0, 0, 130, 30);
287                 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
288                 rect += topleft_server + v2s32(40, 100+25);
289                 Environment->addButton(rect, this, GUI_ID_DELETE_MAP_BUTTON,
290                           chartowchar_t(gettext("Delete map")));
291         }
292 }
293
294 void GUIMainMenu::drawMenu()
295 {
296         gui::IGUISkin* skin = Environment->getSkin();
297         if (!skin)
298                 return;
299         video::IVideoDriver* driver = Environment->getVideoDriver();
300         
301         /*video::SColor bgcolor(140,0,0,0);
302         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
303
304         video::SColor bgcolor(140,0,0,0);
305
306         {
307                 core::rect<s32> rect(0, 0, 620, 230);
308                 rect += AbsoluteRect.UpperLeftCorner;
309                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
310         }
311
312         {
313                 core::rect<s32> rect(0, 250, 620, 430);
314                 rect += AbsoluteRect.UpperLeftCorner;
315                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
316         }
317
318         gui::IGUIElement::draw();
319 }
320
321 void GUIMainMenu::acceptInput()
322 {
323         {
324                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
325                 if(e != NULL)
326                         m_data->name = e->getText();
327         }
328         {
329                 gui::IGUIElement *e = getElementFromId(264);
330                 if(e != NULL)
331                         m_data->password = e->getText();
332         }
333         {
334                 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
335                 if(e != NULL)
336                         m_data->address = e->getText();
337         }
338         {
339                 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
340                 if(e != NULL)
341                         m_data->port = e->getText();
342         }
343         {
344                 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
345                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
346                         m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
347         }
348         {
349                 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
350                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
351                         m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
352         }
353         {
354                 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
355                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
356                         m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
357         }
358         {
359                 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
360                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
361                         m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
362         }
363         
364         m_accepted = true;
365 }
366
367 bool GUIMainMenu::OnEvent(const SEvent& event)
368 {
369         if(event.EventType==EET_KEY_INPUT_EVENT)
370         {
371                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
372                 {
373                         m_gamecallback->exitToOS();
374                         quitMenu();
375                         return true;
376                 }
377                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
378                 {
379                         acceptInput();
380                         quitMenu();
381                         return true;
382                 }
383         }
384         if(event.EventType==EET_GUI_EVENT)
385         {
386                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
387                                 && isVisible())
388                 {
389                         if(!canTakeFocus(event.GUIEvent.Element))
390                         {
391                                 dstream<<"GUIMainMenu: Not allowing focus change."
392                                                 <<std::endl;
393                                 // Returning true disables focus change
394                                 return true;
395                         }
396                 }
397                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
398                 {
399                         switch(event.GUIEvent.Caller->getID())
400                         {
401                         case GUI_ID_JOIN_GAME_BUTTON: // Start game
402                                 acceptInput();
403                                 quitMenu();
404                                 return true;
405                         case GUI_ID_CHANGE_KEYS_BUTTON: {
406                                 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
407                                 kmenu->drop();
408                                 return true;
409                         }
410                         case GUI_ID_DELETE_MAP_BUTTON: // Delete map
411                                 // Don't accept input data, just set deletion request
412                                 m_data->delete_map = true;
413                                 m_accepted = true;
414                                 quitMenu();
415                                 return true;
416                         }
417                 }
418                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
419                 {
420                         switch(event.GUIEvent.Caller->getID())
421                         {
422                                 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
423                                 acceptInput();
424                                 quitMenu();
425                                 return true;
426                         }
427                 }
428         }
429
430         return Parent ? Parent->OnEvent(event) : false;
431 }
432