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"
25 #include "serialization.h"
27 #include <IGUICheckBox.h>
28 #include <IGUIEditBox.h>
29 #include <IGUIButton.h>
30 #include <IGUIStaticText.h>
32 #include <IGUIListBox.h>
34 #include "guiPauseMenu.h"
38 struct CreateWorldDestMainMenu : public CreateWorldDest
40 CreateWorldDestMainMenu(GUIMainMenu *menu):
43 void accepted(std::wstring name, std::string gameid)
45 m_menu->createNewWorld(name, gameid);
52 GUI_ID_QUIT_BUTTON = 101,
57 GUI_ID_SMOOTH_LIGHTING_CB,
59 GUI_ID_OPAQUE_WATER_CB,
62 GUI_ID_JOIN_GAME_BUTTON,
63 GUI_ID_CHANGE_KEYS_BUTTON,
64 GUI_ID_DELETE_WORLD_BUTTON,
65 GUI_ID_CREATE_WORLD_BUTTON,
69 GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
70 gui::IGUIElement* parent, s32 id,
71 IMenuManager *menumgr,
73 IGameCallback *gamecallback
75 GUIModalMenu(env, parent, id, menumgr),
78 m_gamecallback(gamecallback)
82 this->parent = parent;
84 this->menumgr = menumgr;
87 GUIMainMenu::~GUIMainMenu()
92 void GUIMainMenu::removeChildren()
94 const core::list<gui::IGUIElement*> &children = getChildren();
95 core::list<gui::IGUIElement*> children_copy;
96 for(core::list<gui::IGUIElement*>::ConstIterator
97 i = children.begin(); i != children.end(); i++)
99 children_copy.push_back(*i);
101 for(core::list<gui::IGUIElement*>::Iterator
102 i = children_copy.begin();
103 i != children_copy.end(); i++)
109 void GUIMainMenu::regenerateGui(v2u32 screensize)
112 Read stuff from elements into m_data
122 Calculate new sizes and positions
125 v2s32 size(620, 430);
127 core::rect<s32> rect(
128 screensize.X/2 - size.X/2,
129 screensize.Y/2 - size.Y/2,
130 screensize.X/2 + size.X/2,
131 screensize.Y/2 + size.Y/2
135 recalculateAbsolutePosition(false);
137 //v2s32 size = rect.getSize();
147 v2s32 topleft_client(40, 0);
148 v2s32 size_client = size - v2s32(40, 0);
154 core::rect<s32> rect(0, 0, 300, 30);
155 rect += topleft_client + v2s32(-36, 0);
156 Environment->addStaticText(narrow_to_wide(VERSION_STRING).c_str(),
157 rect, false, true, this, -1);
161 core::rect<s32> rect(0, 0, 20, 125);
162 rect += topleft_client + v2s32(-15, 80);
163 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
164 //gui::IGUIStaticText *t =
165 Environment->addStaticText(text, rect, false, true, this, -1);
166 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
168 // Nickname + password
170 core::rect<s32> rect(0, 0, 110, 20);
171 rect += topleft_client + v2s32(35, 50+6);
172 Environment->addStaticText(wgettext("Name/Password"),
173 rect, false, true, this, -1);
177 core::rect<s32> rect(0, 0, 230, 30);
178 rect += topleft_client + v2s32(160, 50);
179 gui::IGUIElement *e =
180 Environment->addEditBox(m_data->name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
181 if(m_data->name == L"")
182 Environment->setFocus(e);
185 core::rect<s32> rect(0, 0, 120, 30);
186 rect += topleft_client + v2s32(size_client.X-60-100, 50);
187 gui::IGUIEditBox *e =
188 Environment->addEditBox(L"", rect, true, this, 264);
189 e->setPasswordBox(true);
190 if(m_data->name != L"" && m_data->address != L"")
191 Environment->setFocus(e);
197 core::rect<s32> rect(0, 0, 110, 20);
198 rect += topleft_client + v2s32(35, 100+6);
199 Environment->addStaticText(wgettext("Address/Port"),
200 rect, false, true, this, -1);
204 core::rect<s32> rect(0, 0, 230, 30);
205 rect += topleft_client + v2s32(160, 100);
206 gui::IGUIElement *e =
207 Environment->addEditBox(m_data->address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
208 if(m_data->name != L"" && m_data->address == L"")
209 Environment->setFocus(e);
212 core::rect<s32> rect(0, 0, 120, 30);
213 //rect += topleft_client + v2s32(160+250+20, 125);
214 rect += topleft_client + v2s32(size_client.X-60-100, 100);
215 Environment->addEditBox(m_data->port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
219 core::rect<s32> rect(0, 0, 400, 20);
220 rect += topleft_client + v2s32(160, 100+35);
221 Environment->addStaticText(wgettext("Leave address blank to start a local server."),
222 rect, false, true, this, -1);
225 core::rect<s32> rect(0, 0, 250, 30);
226 rect += topleft_client + v2s32(35, 150);
227 Environment->addCheckBox(m_data->fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
228 wgettext("Fancy trees"));
231 core::rect<s32> rect(0, 0, 250, 30);
232 rect += topleft_client + v2s32(35, 150+20);
233 Environment->addCheckBox(m_data->smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
234 wgettext("Smooth Lighting"));
237 core::rect<s32> rect(0, 0, 250, 30);
238 rect += topleft_client + v2s32(35, 150+40);
239 Environment->addCheckBox(m_data->clouds_3d, rect, this, GUI_ID_3D_CLOUDS_CB,
240 wgettext("3D Clouds"));
243 core::rect<s32> rect(0, 0, 250, 30);
244 rect += topleft_client + v2s32(35, 150+60);
245 Environment->addCheckBox(m_data->opaque_water, rect, this, GUI_ID_OPAQUE_WATER_CB,
246 wgettext("Opaque water"));
250 core::rect<s32> rect(0, 0, 180, 30);
251 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
252 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
253 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
254 wgettext("Start Game / Connect"));
259 core::rect<s32> rect(0, 0, 100, 30);
260 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
261 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
262 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
263 wgettext("Change keys"));
269 v2s32 topleft_server(40, 290);
270 v2s32 size_server = size - v2s32(40, 0);
274 core::rect<s32> rect(0, 0, 20, 125);
275 rect += topleft_server + v2s32(-15, 15);
276 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
277 //gui::IGUIStaticText *t =
278 Environment->addStaticText(text, rect, false, true, this, -1);
279 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
283 core::rect<s32> rect(0, 0, 250, 30);
284 rect += topleft_server + v2s32(20+250+20, 20);
285 Environment->addCheckBox(m_data->creative_mode, rect, this, GUI_ID_CREATIVE_CB,
286 wgettext("Creative Mode"));
289 core::rect<s32> rect(0, 0, 250, 30);
290 rect += topleft_server + v2s32(20+250+20, 40);
291 Environment->addCheckBox(m_data->enable_damage, rect, this, GUI_ID_DAMAGE_CB,
292 wgettext("Enable Damage"));
294 // Delete world button
296 core::rect<s32> rect(0, 0, 130, 30);
297 rect += topleft_server + v2s32(20+250+20, 90);
298 Environment->addButton(rect, this, GUI_ID_DELETE_WORLD_BUTTON,
299 wgettext("Delete world"));
301 // Create world button
303 core::rect<s32> rect(0, 0, 130, 30);
304 rect += topleft_server + v2s32(20+250+20+140, 90);
305 Environment->addButton(rect, this, GUI_ID_CREATE_WORLD_BUTTON,
306 wgettext("Create world"));
308 // World selection listbox
310 core::rect<s32> rect(0, 0, 250, 120);
311 rect += topleft_server + v2s32(20, 10);
312 gui::IGUIListBox *e = Environment->addListBox(rect, this,
313 GUI_ID_WORLD_LISTBOX);
314 e->setDrawBackground(true);
315 for(std::list<std::wstring>::const_iterator i = m_data->worlds.begin();
316 i != m_data->worlds.end(); i++){
317 e->addItem(i->c_str());
319 e->setSelected(m_data->selected_world);
324 void GUIMainMenu::drawMenu()
326 gui::IGUISkin* skin = Environment->getSkin();
329 video::IVideoDriver* driver = Environment->getVideoDriver();
331 /*video::SColor bgcolor(140,0,0,0);
332 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
334 video::SColor bgcolor(140,0,0,0);
337 core::rect<s32> rect(0, 0, 620, 270);
338 rect += AbsoluteRect.UpperLeftCorner;
339 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
343 core::rect<s32> rect(0, 290, 620, 430);
344 rect += AbsoluteRect.UpperLeftCorner;
345 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
348 gui::IGUIElement::draw();
351 void GUIMainMenu::readInput(MainMenuData *dst)
354 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
356 dst->name = e->getText();
359 gui::IGUIElement *e = getElementFromId(264);
361 dst->password = e->getText();
364 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
366 dst->address = e->getText();
369 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
371 dst->port = e->getText();
374 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
375 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
376 dst->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
379 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
380 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
381 dst->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
384 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
385 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
386 dst->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
389 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
390 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
391 dst->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
394 gui::IGUIElement *e = getElementFromId(GUI_ID_3D_CLOUDS_CB);
395 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
396 dst->clouds_3d = ((gui::IGUICheckBox*)e)->isChecked();
399 gui::IGUIElement *e = getElementFromId(GUI_ID_OPAQUE_WATER_CB);
400 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
401 dst->opaque_water = ((gui::IGUICheckBox*)e)->isChecked();
405 gui::IGUIElement *e = getElementFromId(GUI_ID_WORLD_LISTBOX);
406 if(e != NULL && e->getType() == gui::EGUIET_LIST_BOX)
407 dst->selected_world = ((gui::IGUIListBox*)e)->getSelected();
411 void GUIMainMenu::acceptInput()
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:
455 case GUI_ID_CHANGE_KEYS_BUTTON: {
456 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
460 case GUI_ID_DELETE_WORLD_BUTTON: {
462 m_data->delete_world = true;
466 case GUI_ID_CREATE_WORLD_BUTTON: {
467 std::vector<SubgameSpec> games = getAvailableGames();
468 if(games.size() == 0){
469 GUIMessageMenu *menu = new GUIMessageMenu(env, parent,
471 wgettext("Cannot create world: No games found"));
474 CreateWorldDest *dest = new CreateWorldDestMainMenu(this);
475 GUICreateWorld *menu = new GUICreateWorld(env, parent, -1,
476 menumgr, dest, games);
483 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
485 switch(event.GUIEvent.Caller->getID())
487 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
493 if(event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN)
495 switch(event.GUIEvent.Caller->getID())
497 case GUI_ID_WORLD_LISTBOX:
499 m_data->address = L""; // Force local game
506 return Parent ? Parent->OnEvent(event) : false;
509 void GUIMainMenu::createNewWorld(std::wstring name, std::string gameid)
514 m_data->create_world_name = name;
515 m_data->create_world_gameid = gameid;