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