3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "guiMainMenu.h"
21 #include "guiKeyChangeMenu.h"
23 #include "serialization.h"
25 #include <IGUICheckBox.h>
26 #include <IGUIEditBox.h>
27 #include <IGUIButton.h>
28 #include <IGUIStaticText.h>
34 GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
35 gui::IGUIElement* parent, s32 id,
36 IMenuManager *menumgr,
38 IGameCallback *gamecallback
40 GUIModalMenu(env, parent, id, menumgr),
43 m_gamecallback(gamecallback)
47 this->parent = parent;
49 this->menumgr = menumgr;
52 GUIMainMenu::~GUIMainMenu()
57 void GUIMainMenu::removeChildren()
59 const core::list<gui::IGUIElement*> &children = getChildren();
60 core::list<gui::IGUIElement*> children_copy;
61 for(core::list<gui::IGUIElement*>::ConstIterator
62 i = children.begin(); i != children.end(); i++)
64 children_copy.push_back(*i);
66 for(core::list<gui::IGUIElement*>::Iterator
67 i = children_copy.begin();
68 i != children_copy.end(); i++)
74 void GUIMainMenu::regenerateGui(v2u32 screensize)
76 std::wstring text_name;
77 std::wstring text_address;
78 std::wstring text_port;
88 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
90 text_name = e->getText();
92 text_name = m_data->name;
95 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
97 text_address = e->getText();
99 text_address = m_data->address;
102 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
104 text_port = e->getText();
106 text_port = m_data->port;
109 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
110 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
111 fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
113 fancy_trees = m_data->fancy_trees;
116 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
117 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
118 smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
120 smooth_lighting = m_data->smooth_lighting;
123 gui::IGUIElement *e = getElementFromId(GUI_ID_3D_CLOUDS_CB);
124 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
125 clouds_3d = ((gui::IGUICheckBox*)e)->isChecked();
127 clouds_3d = m_data->clouds_3d;
130 gui::IGUIElement *e = getElementFromId(GUI_ID_OPAQUE_WATER_CB);
131 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
132 opaque_water = ((gui::IGUICheckBox*)e)->isChecked();
134 opaque_water = m_data->opaque_water;
139 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
140 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
141 creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
143 creative_mode = m_data->creative_mode;
146 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
147 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
148 enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
150 enable_damage = m_data->enable_damage;
159 Calculate new sizes and positions
162 v2s32 size(620, 430);
164 core::rect<s32> rect(
165 screensize.X/2 - size.X/2,
166 screensize.Y/2 - size.Y/2,
167 screensize.X/2 + size.X/2,
168 screensize.Y/2 + size.Y/2
172 recalculateAbsolutePosition(false);
174 //v2s32 size = rect.getSize();
184 v2s32 topleft_client(40, 0);
185 v2s32 size_client = size - v2s32(40, 0);
189 core::rect<s32> rect(0, 0, 20, 125);
190 rect += topleft_client + v2s32(-15, 60);
191 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
192 //gui::IGUIStaticText *t =
193 Environment->addStaticText(text, rect, false, true, this, -1);
194 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
197 // Nickname + password
199 core::rect<s32> rect(0, 0, 110, 20);
200 rect += topleft_client + v2s32(35, 50+6);
201 Environment->addStaticText(wgettext("Name/Password"),
202 rect, false, true, this, -1);
206 core::rect<s32> rect(0, 0, 230, 30);
207 rect += topleft_client + v2s32(160, 50);
208 gui::IGUIElement *e =
209 Environment->addEditBox(text_name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
211 Environment->setFocus(e);
214 core::rect<s32> rect(0, 0, 120, 30);
215 rect += topleft_client + v2s32(size_client.X-60-100, 50);
216 gui::IGUIEditBox *e =
217 Environment->addEditBox(L"", rect, true, this, 264);
218 e->setPasswordBox(true);
219 if(text_name != L"" && text_address != L"")
220 Environment->setFocus(e);
226 core::rect<s32> rect(0, 0, 110, 20);
227 rect += topleft_client + v2s32(35, 100+6);
228 Environment->addStaticText(wgettext("Address/Port"),
229 rect, false, true, this, -1);
233 core::rect<s32> rect(0, 0, 230, 30);
234 rect += topleft_client + v2s32(160, 100);
235 gui::IGUIElement *e =
236 Environment->addEditBox(text_address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
237 if(text_name != L"" && text_address == L"")
238 Environment->setFocus(e);
241 core::rect<s32> rect(0, 0, 120, 30);
242 //rect += topleft_client + v2s32(160+250+20, 125);
243 rect += topleft_client + v2s32(size_client.X-60-100, 100);
244 Environment->addEditBox(text_port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
248 core::rect<s32> rect(0, 0, 400, 20);
249 rect += topleft_client + v2s32(160, 100+35);
250 Environment->addStaticText(wgettext("Leave address blank to start a local server."),
251 rect, false, true, this, -1);
254 core::rect<s32> rect(0, 0, 250, 30);
255 rect += topleft_client + v2s32(35, 150);
256 Environment->addCheckBox(fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
257 wgettext("Fancy trees"));
260 core::rect<s32> rect(0, 0, 250, 30);
261 rect += topleft_client + v2s32(35, 150+20);
262 Environment->addCheckBox(smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
263 wgettext("Smooth Lighting"));
266 core::rect<s32> rect(0, 0, 250, 30);
267 rect += topleft_client + v2s32(35, 150+40);
268 Environment->addCheckBox(clouds_3d, rect, this, GUI_ID_3D_CLOUDS_CB,
269 wgettext("3D Clouds"));
272 core::rect<s32> rect(0, 0, 250, 30);
273 rect += topleft_client + v2s32(35, 150+60);
274 Environment->addCheckBox(opaque_water, rect, this, GUI_ID_OPAQUE_WATER_CB,
275 wgettext("Opaque water"));
279 core::rect<s32> rect(0, 0, 180, 30);
280 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
281 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
282 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
283 wgettext("Start Game / Connect"));
288 core::rect<s32> rect(0, 0, 100, 30);
289 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
290 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
291 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
292 wgettext("Change keys"));
298 v2s32 topleft_server(40, 250);
299 v2s32 size_server = size - v2s32(40, 0);
302 core::rect<s32> rect(0, 0, 20, 125);
303 rect += topleft_server + v2s32(-15, 40);
304 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
305 //gui::IGUIStaticText *t =
306 Environment->addStaticText(text, rect, false, true, this, -1);
307 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
312 core::rect<s32> rect(0, 0, 250, 30);
313 rect += topleft_server + v2s32(35, 30);
314 Environment->addCheckBox(creative_mode, rect, this, GUI_ID_CREATIVE_CB,
315 wgettext("Creative Mode"));
318 core::rect<s32> rect(0, 0, 250, 30);
319 rect += topleft_server + v2s32(35, 60);
320 Environment->addCheckBox(enable_damage, rect, this, GUI_ID_DAMAGE_CB,
321 wgettext("Enable Damage"));
325 core::rect<s32> rect(0, 0, 130, 30);
326 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
327 rect += topleft_server + v2s32(40, 100+25);
328 Environment->addButton(rect, this, GUI_ID_DELETE_MAP_BUTTON,
329 wgettext("Delete map"));
334 void GUIMainMenu::drawMenu()
336 gui::IGUISkin* skin = Environment->getSkin();
339 video::IVideoDriver* driver = Environment->getVideoDriver();
341 /*video::SColor bgcolor(140,0,0,0);
342 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
344 video::SColor bgcolor(140,0,0,0);
347 core::rect<s32> rect(0, 0, 620, 230);
348 rect += AbsoluteRect.UpperLeftCorner;
349 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
353 core::rect<s32> rect(0, 250, 620, 430);
354 rect += AbsoluteRect.UpperLeftCorner;
355 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
358 gui::IGUIElement::draw();
361 void GUIMainMenu::acceptInput()
364 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
366 m_data->name = e->getText();
369 gui::IGUIElement *e = getElementFromId(264);
371 m_data->password = e->getText();
374 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
376 m_data->address = e->getText();
379 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
381 m_data->port = e->getText();
384 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
385 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
386 m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
389 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
390 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
391 m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
394 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
395 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
396 m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
399 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
400 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
401 m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
404 gui::IGUIElement *e = getElementFromId(GUI_ID_3D_CLOUDS_CB);
405 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
406 m_data->clouds_3d = ((gui::IGUICheckBox*)e)->isChecked();
409 gui::IGUIElement *e = getElementFromId(GUI_ID_OPAQUE_WATER_CB);
410 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
411 m_data->opaque_water = ((gui::IGUICheckBox*)e)->isChecked();
417 bool GUIMainMenu::OnEvent(const SEvent& event)
419 if(event.EventType==EET_KEY_INPUT_EVENT)
421 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
423 m_gamecallback->exitToOS();
427 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
434 if(event.EventType==EET_GUI_EVENT)
436 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
439 if(!canTakeFocus(event.GUIEvent.Element))
441 dstream<<"GUIMainMenu: Not allowing focus change."
443 // Returning true disables focus change
447 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
449 switch(event.GUIEvent.Caller->getID())
451 case GUI_ID_JOIN_GAME_BUTTON: // Start game
455 case GUI_ID_CHANGE_KEYS_BUTTON: {
456 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
460 case GUI_ID_DELETE_MAP_BUTTON: // Delete map
461 // Don't accept input data, just set deletion request
462 m_data->delete_map = true;
468 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
470 switch(event.GUIEvent.Caller->getID())
472 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
480 return Parent ? Parent->OnEvent(event) : false;