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;
86 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
88 text_name = e->getText();
90 text_name = m_data->name;
93 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
95 text_address = e->getText();
97 text_address = m_data->address;
100 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
102 text_port = e->getText();
104 text_port = m_data->port;
107 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
108 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
109 fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
111 fancy_trees = m_data->fancy_trees;
114 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
115 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
116 smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
118 smooth_lighting = m_data->smooth_lighting;
123 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
124 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
125 creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
127 creative_mode = m_data->creative_mode;
130 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
131 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
132 enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
134 enable_damage = m_data->enable_damage;
143 Calculate new sizes and positions
146 v2s32 size(620, 430);
148 core::rect<s32> rect(
149 screensize.X/2 - size.X/2,
150 screensize.Y/2 - size.Y/2,
151 screensize.X/2 + size.X/2,
152 screensize.Y/2 + size.Y/2
156 recalculateAbsolutePosition(false);
158 //v2s32 size = rect.getSize();
168 v2s32 topleft_client(40, 0);
169 v2s32 size_client = size - v2s32(40, 0);
173 core::rect<s32> rect(0, 0, 20, 125);
174 rect += topleft_client + v2s32(-15, 60);
175 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
176 //gui::IGUIStaticText *t =
177 Environment->addStaticText(text, rect, false, true, this, -1);
178 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
181 // Nickname + password
183 core::rect<s32> rect(0, 0, 110, 20);
184 rect += topleft_client + v2s32(35, 50+6);
185 Environment->addStaticText(wgettext("Name/Password"),
186 rect, false, true, this, -1);
190 core::rect<s32> rect(0, 0, 230, 30);
191 rect += topleft_client + v2s32(160, 50);
192 gui::IGUIElement *e =
193 Environment->addEditBox(text_name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
195 Environment->setFocus(e);
198 core::rect<s32> rect(0, 0, 120, 30);
199 rect += topleft_client + v2s32(size_client.X-60-100, 50);
200 gui::IGUIEditBox *e =
201 Environment->addEditBox(L"", rect, true, this, 264);
202 e->setPasswordBox(true);
203 if(text_name != L"" && text_address != L"")
204 Environment->setFocus(e);
210 core::rect<s32> rect(0, 0, 110, 20);
211 rect += topleft_client + v2s32(35, 100+6);
212 Environment->addStaticText(wgettext("Address/Port"),
213 rect, false, true, this, -1);
217 core::rect<s32> rect(0, 0, 230, 30);
218 rect += topleft_client + v2s32(160, 100);
219 gui::IGUIElement *e =
220 Environment->addEditBox(text_address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
221 if(text_name != L"" && text_address == L"")
222 Environment->setFocus(e);
225 core::rect<s32> rect(0, 0, 120, 30);
226 //rect += topleft_client + v2s32(160+250+20, 125);
227 rect += topleft_client + v2s32(size_client.X-60-100, 100);
228 Environment->addEditBox(text_port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
232 core::rect<s32> rect(0, 0, 400, 20);
233 rect += topleft_client + v2s32(160, 100+35);
234 Environment->addStaticText(wgettext("Leave address blank to start a local server."),
235 rect, false, true, this, -1);
238 core::rect<s32> rect(0, 0, 250, 30);
239 rect += topleft_client + v2s32(35, 150);
240 Environment->addCheckBox(fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
241 wgettext("Fancy trees"));
244 core::rect<s32> rect(0, 0, 250, 30);
245 rect += topleft_client + v2s32(35, 150+30);
246 Environment->addCheckBox(smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
247 wgettext("Smooth Lighting"));
251 core::rect<s32> rect(0, 0, 180, 30);
252 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
253 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
254 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
255 wgettext("Start Game / Connect"));
260 core::rect<s32> rect(0, 0, 100, 30);
261 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
262 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
263 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
264 wgettext("Change keys"));
270 v2s32 topleft_server(40, 250);
271 v2s32 size_server = size - v2s32(40, 0);
274 core::rect<s32> rect(0, 0, 20, 125);
275 rect += topleft_server + v2s32(-15, 40);
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);
284 core::rect<s32> rect(0, 0, 250, 30);
285 rect += topleft_server + v2s32(35, 30);
286 Environment->addCheckBox(creative_mode, rect, this, GUI_ID_CREATIVE_CB,
287 wgettext("Creative Mode"));
290 core::rect<s32> rect(0, 0, 250, 30);
291 rect += topleft_server + v2s32(35, 60);
292 Environment->addCheckBox(enable_damage, rect, this, GUI_ID_DAMAGE_CB,
293 wgettext("Enable Damage"));
297 core::rect<s32> rect(0, 0, 130, 30);
298 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
299 rect += topleft_server + v2s32(40, 100+25);
300 Environment->addButton(rect, this, GUI_ID_DELETE_MAP_BUTTON,
301 wgettext("Delete map"));
306 void GUIMainMenu::drawMenu()
308 gui::IGUISkin* skin = Environment->getSkin();
311 video::IVideoDriver* driver = Environment->getVideoDriver();
313 /*video::SColor bgcolor(140,0,0,0);
314 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
316 video::SColor bgcolor(140,0,0,0);
319 core::rect<s32> rect(0, 0, 620, 230);
320 rect += AbsoluteRect.UpperLeftCorner;
321 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
325 core::rect<s32> rect(0, 250, 620, 430);
326 rect += AbsoluteRect.UpperLeftCorner;
327 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
330 gui::IGUIElement::draw();
333 void GUIMainMenu::acceptInput()
336 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
338 m_data->name = e->getText();
341 gui::IGUIElement *e = getElementFromId(264);
343 m_data->password = e->getText();
346 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
348 m_data->address = e->getText();
351 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
353 m_data->port = e->getText();
356 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
357 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
358 m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
361 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
362 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
363 m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
366 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
367 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
368 m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
371 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
372 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
373 m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
379 bool GUIMainMenu::OnEvent(const SEvent& event)
381 if(event.EventType==EET_KEY_INPUT_EVENT)
383 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
385 m_gamecallback->exitToOS();
389 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
396 if(event.EventType==EET_GUI_EVENT)
398 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
401 if(!canTakeFocus(event.GUIEvent.Element))
403 dstream<<"GUIMainMenu: Not allowing focus change."
405 // Returning true disables focus change
409 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
411 switch(event.GUIEvent.Caller->getID())
413 case GUI_ID_JOIN_GAME_BUTTON: // Start game
417 case GUI_ID_CHANGE_KEYS_BUTTON: {
418 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
422 case GUI_ID_DELETE_MAP_BUTTON: // Delete map
423 // Don't accept input data, just set deletion request
424 m_data->delete_map = true;
430 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
432 switch(event.GUIEvent.Caller->getID())
434 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
442 return Parent ? Parent->OnEvent(event) : false;