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);
191 core::rect<s32> rect(0, 0, 300, 30);
192 rect += topleft_client + v2s32(-36, 0);
193 Environment->addStaticText(narrow_to_wide(VERSION_STRING).c_str(),
194 rect, false, true, this, -1);
198 core::rect<s32> rect(0, 0, 20, 125);
199 rect += topleft_client + v2s32(-15, 80);
200 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
201 //gui::IGUIStaticText *t =
202 Environment->addStaticText(text, rect, false, true, this, -1);
203 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
205 // Nickname + password
207 core::rect<s32> rect(0, 0, 110, 20);
208 rect += topleft_client + v2s32(35, 50+6);
209 Environment->addStaticText(wgettext("Name/Password"),
210 rect, false, true, this, -1);
214 core::rect<s32> rect(0, 0, 230, 30);
215 rect += topleft_client + v2s32(160, 50);
216 gui::IGUIElement *e =
217 Environment->addEditBox(text_name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
219 Environment->setFocus(e);
222 core::rect<s32> rect(0, 0, 120, 30);
223 rect += topleft_client + v2s32(size_client.X-60-100, 50);
224 gui::IGUIEditBox *e =
225 Environment->addEditBox(L"", rect, true, this, 264);
226 e->setPasswordBox(true);
227 if(text_name != L"" && text_address != L"")
228 Environment->setFocus(e);
234 core::rect<s32> rect(0, 0, 110, 20);
235 rect += topleft_client + v2s32(35, 100+6);
236 Environment->addStaticText(wgettext("Address/Port"),
237 rect, false, true, this, -1);
241 core::rect<s32> rect(0, 0, 230, 30);
242 rect += topleft_client + v2s32(160, 100);
243 gui::IGUIElement *e =
244 Environment->addEditBox(text_address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
245 if(text_name != L"" && text_address == L"")
246 Environment->setFocus(e);
249 core::rect<s32> rect(0, 0, 120, 30);
250 //rect += topleft_client + v2s32(160+250+20, 125);
251 rect += topleft_client + v2s32(size_client.X-60-100, 100);
252 Environment->addEditBox(text_port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
256 core::rect<s32> rect(0, 0, 400, 20);
257 rect += topleft_client + v2s32(160, 100+35);
258 Environment->addStaticText(wgettext("Leave address blank to start a local server."),
259 rect, false, true, this, -1);
262 core::rect<s32> rect(0, 0, 250, 30);
263 rect += topleft_client + v2s32(35, 150);
264 Environment->addCheckBox(fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
265 wgettext("Fancy trees"));
268 core::rect<s32> rect(0, 0, 250, 30);
269 rect += topleft_client + v2s32(35, 150+20);
270 Environment->addCheckBox(smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
271 wgettext("Smooth Lighting"));
274 core::rect<s32> rect(0, 0, 250, 30);
275 rect += topleft_client + v2s32(35, 150+40);
276 Environment->addCheckBox(clouds_3d, rect, this, GUI_ID_3D_CLOUDS_CB,
277 wgettext("3D Clouds"));
280 core::rect<s32> rect(0, 0, 250, 30);
281 rect += topleft_client + v2s32(35, 150+60);
282 Environment->addCheckBox(opaque_water, rect, this, GUI_ID_OPAQUE_WATER_CB,
283 wgettext("Opaque water"));
287 core::rect<s32> rect(0, 0, 180, 30);
288 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
289 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
290 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
291 wgettext("Start Game / Connect"));
296 core::rect<s32> rect(0, 0, 100, 30);
297 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
298 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
299 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
300 wgettext("Change keys"));
306 v2s32 topleft_server(40, 290);
307 v2s32 size_server = size - v2s32(40, 0);
311 core::rect<s32> rect(0, 0, 20, 125);
312 rect += topleft_server + v2s32(-15, 15);
313 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
314 //gui::IGUIStaticText *t =
315 Environment->addStaticText(text, rect, false, true, this, -1);
316 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
320 core::rect<s32> rect(0, 0, 250, 30);
321 rect += topleft_server + v2s32(35, 20);
322 Environment->addCheckBox(creative_mode, rect, this, GUI_ID_CREATIVE_CB,
323 wgettext("Creative Mode"));
326 core::rect<s32> rect(0, 0, 250, 30);
327 rect += topleft_server + v2s32(35, 40);
328 Environment->addCheckBox(enable_damage, rect, this, GUI_ID_DAMAGE_CB,
329 wgettext("Enable Damage"));
333 core::rect<s32> rect(0, 0, 130, 30);
334 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
335 rect += topleft_server + v2s32(40, 90);
336 Environment->addButton(rect, this, GUI_ID_DELETE_MAP_BUTTON,
337 wgettext("Delete map"));
342 void GUIMainMenu::drawMenu()
344 gui::IGUISkin* skin = Environment->getSkin();
347 video::IVideoDriver* driver = Environment->getVideoDriver();
349 /*video::SColor bgcolor(140,0,0,0);
350 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
352 video::SColor bgcolor(140,0,0,0);
355 core::rect<s32> rect(0, 0, 620, 270);
356 rect += AbsoluteRect.UpperLeftCorner;
357 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
361 core::rect<s32> rect(0, 290, 620, 430);
362 rect += AbsoluteRect.UpperLeftCorner;
363 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
366 gui::IGUIElement::draw();
369 void GUIMainMenu::acceptInput()
372 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
374 m_data->name = e->getText();
377 gui::IGUIElement *e = getElementFromId(264);
379 m_data->password = e->getText();
382 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
384 m_data->address = e->getText();
387 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
389 m_data->port = e->getText();
392 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
393 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
394 m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
397 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
398 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
399 m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
402 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
403 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
404 m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
407 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
408 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
409 m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
412 gui::IGUIElement *e = getElementFromId(GUI_ID_3D_CLOUDS_CB);
413 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
414 m_data->clouds_3d = ((gui::IGUICheckBox*)e)->isChecked();
417 gui::IGUIElement *e = getElementFromId(GUI_ID_OPAQUE_WATER_CB);
418 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
419 m_data->opaque_water = ((gui::IGUICheckBox*)e)->isChecked();
425 bool GUIMainMenu::OnEvent(const SEvent& event)
427 if(event.EventType==EET_KEY_INPUT_EVENT)
429 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
431 m_gamecallback->exitToOS();
435 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
442 if(event.EventType==EET_GUI_EVENT)
444 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
447 if(!canTakeFocus(event.GUIEvent.Element))
449 dstream<<"GUIMainMenu: Not allowing focus change."
451 // Returning true disables focus change
455 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
457 switch(event.GUIEvent.Caller->getID())
459 case GUI_ID_JOIN_GAME_BUTTON: // Start game
463 case GUI_ID_CHANGE_KEYS_BUTTON: {
464 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
468 case GUI_ID_DELETE_MAP_BUTTON: // Delete map
469 // Don't accept input data, just set deletion request
470 m_data->delete_map = true;
476 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
478 switch(event.GUIEvent.Caller->getID())
480 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
488 return Parent ? Parent->OnEvent(event) : false;