3 Copyright (C) 2010-12 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"
22 #include "guiCreateWorld.h"
23 #include "guiMessageMenu.h"
24 #include "guiConfirmMenu.h"
26 #include "serialization.h"
28 #include <IGUICheckBox.h>
29 #include <IGUIEditBox.h>
30 #include <IGUIButton.h>
31 #include <IGUIStaticText.h>
33 #include <IGUIListBox.h>
35 #include "guiPauseMenu.h"
39 struct CreateWorldDestMainMenu : public CreateWorldDest
41 CreateWorldDestMainMenu(GUIMainMenu *menu):
44 void accepted(std::wstring name, std::string gameid)
46 m_menu->createNewWorld(name, gameid);
51 struct ConfirmDestDeleteWorld : public ConfirmDest
53 ConfirmDestDeleteWorld(WorldSpec spec, GUIMainMenu *menu):
57 void answer(bool answer)
61 m_menu->deleteWorld(m_spec);
69 GUI_ID_QUIT_BUTTON = 101,
74 GUI_ID_SMOOTH_LIGHTING_CB,
76 GUI_ID_OPAQUE_WATER_CB,
79 GUI_ID_JOIN_GAME_BUTTON,
80 GUI_ID_CHANGE_KEYS_BUTTON,
81 GUI_ID_DELETE_WORLD_BUTTON,
82 GUI_ID_CREATE_WORLD_BUTTON,
86 GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
87 gui::IGUIElement* parent, s32 id,
88 IMenuManager *menumgr,
90 IGameCallback *gamecallback
92 GUIModalMenu(env, parent, id, menumgr),
95 m_gamecallback(gamecallback)
99 this->parent = parent;
101 this->menumgr = menumgr;
104 GUIMainMenu::~GUIMainMenu()
109 void GUIMainMenu::removeChildren()
111 const core::list<gui::IGUIElement*> &children = getChildren();
112 core::list<gui::IGUIElement*> children_copy;
113 for(core::list<gui::IGUIElement*>::ConstIterator
114 i = children.begin(); i != children.end(); i++)
116 children_copy.push_back(*i);
118 for(core::list<gui::IGUIElement*>::Iterator
119 i = children_copy.begin();
120 i != children_copy.end(); i++)
126 void GUIMainMenu::regenerateGui(v2u32 screensize)
129 Read stuff from elements into m_data
139 Calculate new sizes and positions
142 v2s32 size(620, 430);
144 core::rect<s32> rect(
145 screensize.X/2 - size.X/2,
146 screensize.Y/2 - size.Y/2,
147 screensize.X/2 + size.X/2,
148 screensize.Y/2 + size.Y/2
152 recalculateAbsolutePosition(false);
154 //v2s32 size = rect.getSize();
164 v2s32 topleft_client(40, 0);
165 v2s32 size_client = size - v2s32(40, 0);
171 core::rect<s32> rect(0, 0, 300, 30);
172 rect += topleft_client + v2s32(-36, 0);
173 Environment->addStaticText(narrow_to_wide(VERSION_STRING).c_str(),
174 rect, false, true, this, -1);
178 core::rect<s32> rect(0, 0, 20, 125);
179 rect += topleft_client + v2s32(-15, 80);
180 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
181 //gui::IGUIStaticText *t =
182 Environment->addStaticText(text, rect, false, true, this, -1);
183 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
185 // Nickname + password
187 core::rect<s32> rect(0, 0, 110, 20);
188 rect += topleft_client + v2s32(35, 50+6);
189 Environment->addStaticText(wgettext("Name/Password"),
190 rect, false, true, this, -1);
194 core::rect<s32> rect(0, 0, 230, 30);
195 rect += topleft_client + v2s32(160, 50);
196 gui::IGUIElement *e =
197 Environment->addEditBox(m_data->name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
198 if(m_data->name == L"")
199 Environment->setFocus(e);
202 core::rect<s32> rect(0, 0, 120, 30);
203 rect += topleft_client + v2s32(size_client.X-60-100, 50);
204 gui::IGUIEditBox *e =
205 Environment->addEditBox(L"", rect, true, this, 264);
206 e->setPasswordBox(true);
207 if(m_data->name != L"" && m_data->address != L"")
208 Environment->setFocus(e);
214 core::rect<s32> rect(0, 0, 110, 20);
215 rect += topleft_client + v2s32(35, 100+6);
216 Environment->addStaticText(wgettext("Address/Port"),
217 rect, false, true, this, -1);
221 core::rect<s32> rect(0, 0, 230, 30);
222 rect += topleft_client + v2s32(160, 100);
223 gui::IGUIElement *e =
224 Environment->addEditBox(m_data->address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
225 if(m_data->name != L"" && m_data->address == L"")
226 Environment->setFocus(e);
229 core::rect<s32> rect(0, 0, 120, 30);
230 //rect += topleft_client + v2s32(160+250+20, 125);
231 rect += topleft_client + v2s32(size_client.X-60-100, 100);
232 Environment->addEditBox(m_data->port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
236 core::rect<s32> rect(0, 0, 400, 20);
237 rect += topleft_client + v2s32(160, 100+35);
238 Environment->addStaticText(wgettext("Leave address blank to start a local server."),
239 rect, false, true, this, -1);
242 core::rect<s32> rect(0, 0, 250, 30);
243 rect += topleft_client + v2s32(35, 150);
244 Environment->addCheckBox(m_data->fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
245 wgettext("Fancy trees"));
248 core::rect<s32> rect(0, 0, 250, 30);
249 rect += topleft_client + v2s32(35, 150+20);
250 Environment->addCheckBox(m_data->smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
251 wgettext("Smooth Lighting"));
254 core::rect<s32> rect(0, 0, 250, 30);
255 rect += topleft_client + v2s32(35, 150+40);
256 Environment->addCheckBox(m_data->clouds_3d, rect, this, GUI_ID_3D_CLOUDS_CB,
257 wgettext("3D Clouds"));
260 core::rect<s32> rect(0, 0, 250, 30);
261 rect += topleft_client + v2s32(35, 150+60);
262 Environment->addCheckBox(m_data->opaque_water, rect, this, GUI_ID_OPAQUE_WATER_CB,
263 wgettext("Opaque water"));
267 core::rect<s32> rect(0, 0, 180, 30);
268 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
269 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
270 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
271 wgettext("Start Game / Connect"));
276 core::rect<s32> rect(0, 0, 100, 30);
277 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
278 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
279 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
280 wgettext("Change keys"));
286 v2s32 topleft_server(40, 290);
287 v2s32 size_server = size - v2s32(40, 0);
291 core::rect<s32> rect(0, 0, 20, 125);
292 rect += topleft_server + v2s32(-15, 15);
293 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
294 //gui::IGUIStaticText *t =
295 Environment->addStaticText(text, rect, false, true, this, -1);
296 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
300 core::rect<s32> rect(0, 0, 250, 30);
301 rect += topleft_server + v2s32(20+250+20, 20);
302 Environment->addCheckBox(m_data->creative_mode, rect, this, GUI_ID_CREATIVE_CB,
303 wgettext("Creative Mode"));
306 core::rect<s32> rect(0, 0, 250, 30);
307 rect += topleft_server + v2s32(20+250+20, 40);
308 Environment->addCheckBox(m_data->enable_damage, rect, this, GUI_ID_DAMAGE_CB,
309 wgettext("Enable Damage"));
311 // Delete world button
313 core::rect<s32> rect(0, 0, 130, 30);
314 rect += topleft_server + v2s32(20+250+20, 90);
315 Environment->addButton(rect, this, GUI_ID_DELETE_WORLD_BUTTON,
316 wgettext("Delete world"));
318 // Create world button
320 core::rect<s32> rect(0, 0, 130, 30);
321 rect += topleft_server + v2s32(20+250+20+140, 90);
322 Environment->addButton(rect, this, GUI_ID_CREATE_WORLD_BUTTON,
323 wgettext("Create world"));
325 // World selection listbox
327 core::rect<s32> rect(0, 0, 250, 120);
328 rect += topleft_server + v2s32(20, 10);
329 gui::IGUIListBox *e = Environment->addListBox(rect, this,
330 GUI_ID_WORLD_LISTBOX);
331 e->setDrawBackground(true);
332 for(std::vector<WorldSpec>::const_iterator i = m_data->worlds.begin();
333 i != m_data->worlds.end(); i++){
334 e->addItem(narrow_to_wide(i->name+" ["+i->gameid+"]").c_str());
336 e->setSelected(m_data->selected_world);
341 void GUIMainMenu::drawMenu()
343 gui::IGUISkin* skin = Environment->getSkin();
346 video::IVideoDriver* driver = Environment->getVideoDriver();
348 /*video::SColor bgcolor(140,0,0,0);
349 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
351 video::SColor bgcolor(140,0,0,0);
354 core::rect<s32> rect(0, 0, 620, 270);
355 rect += AbsoluteRect.UpperLeftCorner;
356 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
360 core::rect<s32> rect(0, 290, 620, 430);
361 rect += AbsoluteRect.UpperLeftCorner;
362 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
365 gui::IGUIElement::draw();
368 void GUIMainMenu::readInput(MainMenuData *dst)
371 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
373 dst->name = e->getText();
376 gui::IGUIElement *e = getElementFromId(264);
378 dst->password = e->getText();
381 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
383 dst->address = e->getText();
386 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
388 dst->port = e->getText();
391 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
392 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
393 dst->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
396 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
397 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
398 dst->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
401 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
402 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
403 dst->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
406 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
407 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
408 dst->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
411 gui::IGUIElement *e = getElementFromId(GUI_ID_3D_CLOUDS_CB);
412 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
413 dst->clouds_3d = ((gui::IGUICheckBox*)e)->isChecked();
416 gui::IGUIElement *e = getElementFromId(GUI_ID_OPAQUE_WATER_CB);
417 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
418 dst->opaque_water = ((gui::IGUICheckBox*)e)->isChecked();
422 gui::IGUIElement *e = getElementFromId(GUI_ID_WORLD_LISTBOX);
423 if(e != NULL && e->getType() == gui::EGUIET_LIST_BOX)
424 dst->selected_world = ((gui::IGUIListBox*)e)->getSelected();
428 void GUIMainMenu::acceptInput()
434 bool GUIMainMenu::OnEvent(const SEvent& event)
436 if(event.EventType==EET_KEY_INPUT_EVENT)
438 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
440 m_gamecallback->exitToOS();
444 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
451 if(event.EventType==EET_GUI_EVENT)
453 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
456 if(!canTakeFocus(event.GUIEvent.Element))
458 dstream<<"GUIMainMenu: Not allowing focus change."
460 // Returning true disables focus change
464 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
466 switch(event.GUIEvent.Caller->getID())
468 case GUI_ID_JOIN_GAME_BUTTON:
472 case GUI_ID_CHANGE_KEYS_BUTTON: {
473 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
477 case GUI_ID_DELETE_WORLD_BUTTON: {
480 if(cur.selected_world == -1){
481 (new GUIMessageMenu(env, parent, -1, menumgr,
482 wgettext("Cannot delete world: Nothing selected"))
485 WorldSpec spec = m_data->worlds[cur.selected_world];
486 ConfirmDestDeleteWorld *dest = new
487 ConfirmDestDeleteWorld(spec, this);
488 (new GUIConfirmMenu(env, parent, -1, menumgr, dest,
489 (std::wstring(wgettext("Delete world "))
490 +L"\""+narrow_to_wide(spec.name)+L"\"?").c_str()
495 case GUI_ID_CREATE_WORLD_BUTTON: {
496 std::vector<SubgameSpec> games = getAvailableGames();
497 if(games.size() == 0){
498 GUIMessageMenu *menu = new GUIMessageMenu(env, parent,
500 wgettext("Cannot create world: No games found"));
503 CreateWorldDest *dest = new CreateWorldDestMainMenu(this);
504 GUICreateWorld *menu = new GUICreateWorld(env, parent, -1,
505 menumgr, dest, games);
512 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
514 switch(event.GUIEvent.Caller->getID())
516 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
522 if(event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN)
524 switch(event.GUIEvent.Caller->getID())
526 case GUI_ID_WORLD_LISTBOX:
528 m_data->address = L""; // Force local game
535 return Parent ? Parent->OnEvent(event) : false;
538 void GUIMainMenu::createNewWorld(std::wstring name, std::string gameid)
543 m_data->create_world_name = name;
544 m_data->create_world_gameid = gameid;
548 void GUIMainMenu::deleteWorld(WorldSpec spec)
553 m_data->delete_world_spec = spec;