]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiMainMenu.cpp
updated menu a bit, and some other small fixes
[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 "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
168         {
169                 core::rect<s32> rect(0, 0, 100, 20);
170                 rect += topleft_client + v2s32(40, 50+6);
171                 const wchar_t *text = L"Nickname";
172                 Environment->addStaticText(text, rect, false, true, this, -1);
173         }
174         {
175                 core::rect<s32> rect(0, 0, 250, 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         // Address + port
183         {
184                 core::rect<s32> rect(0, 0, 100, 20);
185                 rect += topleft_client + v2s32(40, 100+6);
186                 const wchar_t *text = L"Address + Port";
187                 Environment->addStaticText(text, rect, false, true, this, -1);
188         }
189         {
190                 core::rect<s32> rect(0, 0, 250, 30);
191                 rect += topleft_client + v2s32(160, 100);
192                 gui::IGUIElement *e = 
193                 Environment->addEditBox(text_address.c_str(), rect, true, this, 256);
194                 if(text_name != L"")
195                         Environment->setFocus(e);
196         }
197         {
198                 core::rect<s32> rect(0, 0, 100, 30);
199                 //rect += topleft_client + v2s32(160+250+20, 125);
200                 rect += topleft_client + v2s32(size_client.X-40-100, 100);
201                 Environment->addEditBox(text_port.c_str(), rect, true, this, 257);
202         }
203         {
204                 core::rect<s32> rect(0, 0, 400, 20);
205                 rect += topleft_client + v2s32(160, 100+35);
206                 const wchar_t *text = L"Leave address blank to start a local server.";
207                 Environment->addStaticText(text, rect, false, true, this, -1);
208         }
209         {
210                 core::rect<s32> rect(0, 0, 250, 30);
211                 rect += topleft_client + v2s32(40, 150);
212                 Environment->addCheckBox(fancy_trees, rect, this, 263,
213                                 L"Fancy trees");
214         }
215         {
216                 core::rect<s32> rect(0, 0, 250, 30);
217                 rect += topleft_client + v2s32(40, 150+30);
218                 Environment->addCheckBox(smooth_lighting, rect, this, 262,
219                                 L"Smooth Lighting");
220         }
221         // Start game button
222         {
223                 core::rect<s32> rect(0, 0, 180, 30);
224                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
225                 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
226                 Environment->addButton(rect, this, 257, L"Start Game / Connect");
227         }
228
229         /*
230                 Server section
231         */
232
233         v2s32 topleft_server(40, 250);
234         v2s32 size_server = size - v2s32(40, 0);
235         
236         {
237                 core::rect<s32> rect(0, 0, 20, 125);
238                 rect += topleft_server + v2s32(-15, 40);
239                 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
240                 //gui::IGUIStaticText *t =
241                 Environment->addStaticText(text, rect, false, true, this, -1);
242                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
243         }
244
245         // Server parameters
246         {
247                 core::rect<s32> rect(0, 0, 250, 30);
248                 rect += topleft_server + v2s32(40, 30);
249                 Environment->addCheckBox(creative_mode, rect, this, 259, L"Creative Mode");
250         }
251         {
252                 core::rect<s32> rect(0, 0, 250, 30);
253                 rect += topleft_server + v2s32(40, 60);
254                 Environment->addCheckBox(enable_damage, rect, this, 261, L"Enable Damage");
255         }
256         // Map delete button
257         {
258                 core::rect<s32> rect(0, 0, 130, 30);
259                 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
260                 rect += topleft_server + v2s32(40, 100+25);
261                 Environment->addButton(rect, this, 260, L"Delete map");
262         }
263 }
264
265 void GUIMainMenu::drawMenu()
266 {
267         gui::IGUISkin* skin = Environment->getSkin();
268         if (!skin)
269                 return;
270         video::IVideoDriver* driver = Environment->getVideoDriver();
271         
272         /*video::SColor bgcolor(140,0,0,0);
273         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
274
275         video::SColor bgcolor(140,0,0,0);
276
277         {
278                 core::rect<s32> rect(0, 0, 620, 230);
279                 rect += AbsoluteRect.UpperLeftCorner;
280                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
281         }
282
283         {
284                 core::rect<s32> rect(0, 250, 620, 430);
285                 rect += AbsoluteRect.UpperLeftCorner;
286                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
287         }
288
289         gui::IGUIElement::draw();
290 }
291
292 void GUIMainMenu::acceptInput()
293 {
294         {
295                 gui::IGUIElement *e = getElementFromId(258);
296                 if(e != NULL)
297                         m_data->name = e->getText();
298         }
299         {
300                 gui::IGUIElement *e = getElementFromId(256);
301                 if(e != NULL)
302                         m_data->address = e->getText();
303         }
304         {
305                 gui::IGUIElement *e = getElementFromId(257);
306                 if(e != NULL)
307                         m_data->port = e->getText();
308         }
309         {
310                 gui::IGUIElement *e = getElementFromId(259);
311                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
312                         m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
313         }
314         {
315                 gui::IGUIElement *e = getElementFromId(261);
316                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
317                         m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
318         }
319         {
320                 gui::IGUIElement *e = getElementFromId(262);
321                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
322                         m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
323         }
324         {
325                 gui::IGUIElement *e = getElementFromId(263);
326                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
327                         m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
328         }
329         
330         m_accepted = true;
331 }
332
333 bool GUIMainMenu::OnEvent(const SEvent& event)
334 {
335         if(event.EventType==EET_KEY_INPUT_EVENT)
336         {
337                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
338                 {
339                         m_gamecallback->exitToOS();
340                         quitMenu();
341                         return true;
342                 }
343                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
344                 {
345                         acceptInput();
346                         quitMenu();
347                         return true;
348                 }
349         }
350         if(event.EventType==EET_GUI_EVENT)
351         {
352                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
353                                 && isVisible())
354                 {
355                         if(!canTakeFocus(event.GUIEvent.Element))
356                         {
357                                 dstream<<"GUIMainMenu: Not allowing focus change."
358                                                 <<std::endl;
359                                 // Returning true disables focus change
360                                 return true;
361                         }
362                 }
363                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
364                 {
365                         switch(event.GUIEvent.Caller->getID())
366                         {
367                         case 257: // Start game
368                                 acceptInput();
369                                 quitMenu();
370                                 return true;
371                         case 260: // Delete map
372                                 // Don't accept input data, just set deletion request
373                                 m_data->delete_map = true;
374                                 m_accepted = true;
375                                 quitMenu();
376                                 return true;
377                         }
378                 }
379                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
380                 {
381                         switch(event.GUIEvent.Caller->getID())
382                         {
383                         case 256: case 257: case 258:
384                                 acceptInput();
385                                 quitMenu();
386                                 return true;
387                         }
388                 }
389         }
390
391         return Parent ? Parent->OnEvent(event) : false;
392 }
393