]> git.lizzy.rs Git - minetest.git/blob - src/guiMainMenu.cpp
Made the main menu a bit more unique looking
[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
69         {
70                 gui::IGUIElement *e = getElementFromId(258);
71                 if(e != NULL)
72                         text_name = e->getText();
73                 else
74                         text_name = m_data->name;
75         }
76         {
77                 gui::IGUIElement *e = getElementFromId(256);
78                 if(e != NULL)
79                         text_address = e->getText();
80                 else
81                         text_address = m_data->address;
82         }
83         {
84                 gui::IGUIElement *e = getElementFromId(257);
85                 if(e != NULL)
86                         text_port = e->getText();
87                 else
88                         text_port = m_data->port;
89         }
90         {
91                 gui::IGUIElement *e = getElementFromId(259);
92                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
93                         creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
94                 else
95                         creative_mode = m_data->creative_mode;
96         }
97         {
98                 gui::IGUIElement *e = getElementFromId(261);
99                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
100                         enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
101                 else
102                         enable_damage = m_data->enable_damage;
103         }
104
105         /*
106                 Remove stuff
107         */
108         removeChildren();
109         
110         /*
111                 Calculate new sizes and positions
112         */
113         
114         v2s32 size(620, 430);
115
116         core::rect<s32> rect(
117                         screensize.X/2 - size.X/2,
118                         screensize.Y/2 - size.Y/2,
119                         screensize.X/2 + size.X/2,
120                         screensize.Y/2 + size.Y/2
121         );
122
123         DesiredRect = rect;
124         recalculateAbsolutePosition(false);
125
126         //v2s32 size = rect.getSize();
127
128         /*
129                 Add stuff
130         */
131
132         /*
133                 Client section
134         */
135
136         v2s32 topleft_client(40, 0);
137         v2s32 size_client = size - v2s32(40, 0);
138         
139         {
140                 core::rect<s32> rect(0, 0, 20, 125);
141                 rect += topleft_client + v2s32(-15, 60);
142                 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
143                 //gui::IGUIStaticText *t =
144                 Environment->addStaticText(text, rect, false, true, this, -1);
145                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
146         }
147
148         // Nickname
149         {
150                 core::rect<s32> rect(0, 0, 100, 20);
151                 rect += topleft_client + v2s32(40, 50+6);
152                 const wchar_t *text = L"Nickname";
153                 Environment->addStaticText(text, rect, false, true, this, -1);
154         }
155         {
156                 core::rect<s32> rect(0, 0, 250, 30);
157                 rect += topleft_client + v2s32(160, 50);
158                 gui::IGUIElement *e = 
159                 Environment->addEditBox(text_name.c_str(), rect, true, this, 258);
160                 if(text_name == L"")
161                         Environment->setFocus(e);
162         }
163         // Address + port
164         {
165                 core::rect<s32> rect(0, 0, 100, 20);
166                 rect += topleft_client + v2s32(40, 100+6);
167                 const wchar_t *text = L"Address + Port";
168                 Environment->addStaticText(text, rect, false, true, this, -1);
169         }
170         {
171                 core::rect<s32> rect(0, 0, 250, 30);
172                 rect += topleft_client + v2s32(160, 100);
173                 gui::IGUIElement *e = 
174                 Environment->addEditBox(text_address.c_str(), rect, true, this, 256);
175                 if(text_name != L"")
176                         Environment->setFocus(e);
177         }
178         {
179                 core::rect<s32> rect(0, 0, 100, 30);
180                 //rect += topleft_client + v2s32(160+250+20, 125);
181                 rect += topleft_client + v2s32(size_client.X-40-100, 100);
182                 Environment->addEditBox(text_port.c_str(), rect, true, this, 257);
183         }
184         {
185                 core::rect<s32> rect(0, 0, 400, 20);
186                 rect += topleft_client + v2s32(160, 100+35);
187                 const wchar_t *text = L"Leave address blank to start a local server.";
188                 Environment->addStaticText(text, rect, false, true, this, -1);
189         }
190         // Start game button
191         {
192                 core::rect<s32> rect(0, 0, 180, 30);
193                 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
194                 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
195                 Environment->addButton(rect, this, 257, L"Start Game / Connect");
196         }
197
198         /*
199                 Server section
200         */
201
202         v2s32 topleft_server(40, 250);
203         v2s32 size_server = size - v2s32(40, 0);
204         
205         {
206                 core::rect<s32> rect(0, 0, 20, 125);
207                 rect += topleft_server + v2s32(-15, 40);
208                 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
209                 //gui::IGUIStaticText *t =
210                 Environment->addStaticText(text, rect, false, true, this, -1);
211                 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
212         }
213
214         // Server parameters
215         {
216                 core::rect<s32> rect(0, 0, 250, 30);
217                 rect += topleft_server + v2s32(40, 30);
218                 Environment->addCheckBox(creative_mode, rect, this, 259, L"Creative Mode");
219         }
220         {
221                 core::rect<s32> rect(0, 0, 250, 30);
222                 rect += topleft_server + v2s32(40, 60);
223                 Environment->addCheckBox(enable_damage, rect, this, 261, L"Enable Damage");
224         }
225         // Map delete button
226         {
227                 core::rect<s32> rect(0, 0, 130, 30);
228                 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
229                 rect += topleft_server + v2s32(40, 100+25);
230                 Environment->addButton(rect, this, 260, L"Delete map");
231         }
232 }
233
234 void GUIMainMenu::drawMenu()
235 {
236         gui::IGUISkin* skin = Environment->getSkin();
237         if (!skin)
238                 return;
239         video::IVideoDriver* driver = Environment->getVideoDriver();
240         
241         /*video::SColor bgcolor(140,0,0,0);
242         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
243
244         video::SColor bgcolor(140,0,0,0);
245
246         {
247                 core::rect<s32> rect(0, 0, 620, 230);
248                 rect += AbsoluteRect.UpperLeftCorner;
249                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
250         }
251
252         {
253                 core::rect<s32> rect(0, 250, 620, 430);
254                 rect += AbsoluteRect.UpperLeftCorner;
255                 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
256         }
257
258         gui::IGUIElement::draw();
259 }
260
261 void GUIMainMenu::acceptInput()
262 {
263         {
264                 gui::IGUIElement *e = getElementFromId(258);
265                 if(e != NULL)
266                         m_data->name = e->getText();
267         }
268         {
269                 gui::IGUIElement *e = getElementFromId(256);
270                 if(e != NULL)
271                         m_data->address = e->getText();
272         }
273         {
274                 gui::IGUIElement *e = getElementFromId(257);
275                 if(e != NULL)
276                         m_data->port = e->getText();
277         }
278         {
279                 gui::IGUIElement *e = getElementFromId(259);
280                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
281                         m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
282         }
283         {
284                 gui::IGUIElement *e = getElementFromId(261);
285                 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
286                         m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
287         }
288         
289         m_accepted = true;
290 }
291
292 bool GUIMainMenu::OnEvent(const SEvent& event)
293 {
294         if(event.EventType==EET_KEY_INPUT_EVENT)
295         {
296                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
297                 {
298                         m_gamecallback->exitToOS();
299                         quitMenu();
300                         return true;
301                 }
302                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
303                 {
304                         acceptInput();
305                         quitMenu();
306                         return true;
307                 }
308         }
309         if(event.EventType==EET_GUI_EVENT)
310         {
311                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
312                                 && isVisible())
313                 {
314                         if(!canTakeFocus(event.GUIEvent.Element))
315                         {
316                                 dstream<<"GUIMainMenu: Not allowing focus change."
317                                                 <<std::endl;
318                                 // Returning true disables focus change
319                                 return true;
320                         }
321                 }
322                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
323                 {
324                         switch(event.GUIEvent.Caller->getID())
325                         {
326                         case 257: // Start game
327                                 acceptInput();
328                                 quitMenu();
329                                 return true;
330                         case 260: // Delete map
331                                 // Don't accept input data, just set deletion request
332                                 m_data->delete_map = true;
333                                 m_accepted = true;
334                                 quitMenu();
335                                 return true;
336                         }
337                 }
338                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
339                 {
340                         switch(event.GUIEvent.Caller->getID())
341                         {
342                         case 256: case 257: case 258:
343                                 acceptInput();
344                                 quitMenu();
345                                 return true;
346                         }
347                 }
348         }
349
350         return Parent ? Parent->OnEvent(event) : false;
351 }
352