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