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"
30 GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
31 gui::IGUIElement* parent, s32 id,
32 IMenuManager *menumgr,
34 IGameCallback *gamecallback
36 GUIModalMenu(env, parent, id, menumgr),
39 m_gamecallback(gamecallback)
43 this->parent = parent;
45 this->menumgr = menumgr;
48 GUIMainMenu::~GUIMainMenu()
53 void GUIMainMenu::removeChildren()
55 const core::list<gui::IGUIElement*> &children = getChildren();
56 core::list<gui::IGUIElement*> children_copy;
57 for(core::list<gui::IGUIElement*>::ConstIterator
58 i = children.begin(); i != children.end(); i++)
60 children_copy.push_back(*i);
62 for(core::list<gui::IGUIElement*>::Iterator
63 i = children_copy.begin();
64 i != children_copy.end(); i++)
70 void GUIMainMenu::regenerateGui(v2u32 screensize)
72 std::wstring text_name;
73 std::wstring text_address;
74 std::wstring text_port;
82 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
84 text_name = e->getText();
86 text_name = m_data->name;
89 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
91 text_address = e->getText();
93 text_address = m_data->address;
96 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
98 text_port = e->getText();
100 text_port = m_data->port;
103 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
104 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
105 fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
107 fancy_trees = m_data->fancy_trees;
110 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
111 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
112 smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
114 smooth_lighting = m_data->smooth_lighting;
119 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
120 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
121 creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
123 creative_mode = m_data->creative_mode;
126 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
127 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
128 enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
130 enable_damage = m_data->enable_damage;
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);
169 core::rect<s32> rect(0, 0, 20, 125);
170 rect += topleft_client + v2s32(-15, 60);
171 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
172 //gui::IGUIStaticText *t =
173 Environment->addStaticText(text, rect, false, true, this, -1);
174 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
177 // Nickname + password
179 core::rect<s32> rect(0, 0, 110, 20);
180 rect += topleft_client + v2s32(35, 50+6);
181 Environment->addStaticText(chartowchar_t(gettext("Name/Password")),
182 rect, false, true, this, -1);
186 core::rect<s32> rect(0, 0, 230, 30);
187 rect += topleft_client + v2s32(160, 50);
188 gui::IGUIElement *e =
189 Environment->addEditBox(text_name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
191 Environment->setFocus(e);
194 core::rect<s32> rect(0, 0, 120, 30);
195 rect += topleft_client + v2s32(size_client.X-60-100, 50);
196 gui::IGUIEditBox *e =
197 Environment->addEditBox(L"", rect, true, this, 264);
198 e->setPasswordBox(true);
204 core::rect<s32> rect(0, 0, 110, 20);
205 rect += topleft_client + v2s32(35, 100+6);
206 Environment->addStaticText(chartowchar_t(gettext("Address/Port")),
207 rect, false, true, this, -1);
211 core::rect<s32> rect(0, 0, 230, 30);
212 rect += topleft_client + v2s32(160, 100);
213 gui::IGUIElement *e =
214 Environment->addEditBox(text_address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
216 Environment->setFocus(e);
219 core::rect<s32> rect(0, 0, 120, 30);
220 //rect += topleft_client + v2s32(160+250+20, 125);
221 rect += topleft_client + v2s32(size_client.X-60-100, 100);
222 Environment->addEditBox(text_port.c_str(), rect, true, this, GUI_ID_PORT_INPUT);
226 core::rect<s32> rect(0, 0, 400, 20);
227 rect += topleft_client + v2s32(160, 100+35);
228 Environment->addStaticText(chartowchar_t(gettext("Leave address blank to start a local server.")),
229 rect, false, true, this, -1);
232 core::rect<s32> rect(0, 0, 250, 30);
233 rect += topleft_client + v2s32(35, 150);
234 Environment->addCheckBox(fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
235 chartowchar_t(gettext("Fancy trees")));
238 core::rect<s32> rect(0, 0, 250, 30);
239 rect += topleft_client + v2s32(35, 150+30);
240 Environment->addCheckBox(smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
241 chartowchar_t(gettext("Smooth Lighting")));
245 core::rect<s32> rect(0, 0, 180, 30);
246 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
247 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
248 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
249 chartowchar_t(gettext("Start Game / Connect")));
254 core::rect<s32> rect(0, 0, 100, 30);
255 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
256 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
257 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON,
258 chartowchar_t(gettext("Change keys")));
264 v2s32 topleft_server(40, 250);
265 v2s32 size_server = size - v2s32(40, 0);
268 core::rect<s32> rect(0, 0, 20, 125);
269 rect += topleft_server + v2s32(-15, 40);
270 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
271 //gui::IGUIStaticText *t =
272 Environment->addStaticText(text, rect, false, true, this, -1);
273 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
278 core::rect<s32> rect(0, 0, 250, 30);
279 rect += topleft_server + v2s32(35, 30);
280 Environment->addCheckBox(creative_mode, rect, this, GUI_ID_CREATIVE_CB,
281 chartowchar_t(gettext("Creative Mode")));
284 core::rect<s32> rect(0, 0, 250, 30);
285 rect += topleft_server + v2s32(35, 60);
286 Environment->addCheckBox(enable_damage, rect, this, GUI_ID_DAMAGE_CB,
287 chartowchar_t(gettext("Enable Damage")));
291 core::rect<s32> rect(0, 0, 130, 30);
292 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
293 rect += topleft_server + v2s32(40, 100+25);
294 Environment->addButton(rect, this, GUI_ID_DELETE_MAP_BUTTON,
295 chartowchar_t(gettext("Delete map")));
300 void GUIMainMenu::drawMenu()
302 gui::IGUISkin* skin = Environment->getSkin();
305 video::IVideoDriver* driver = Environment->getVideoDriver();
307 /*video::SColor bgcolor(140,0,0,0);
308 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
310 video::SColor bgcolor(140,0,0,0);
313 core::rect<s32> rect(0, 0, 620, 230);
314 rect += AbsoluteRect.UpperLeftCorner;
315 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
319 core::rect<s32> rect(0, 250, 620, 430);
320 rect += AbsoluteRect.UpperLeftCorner;
321 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
324 gui::IGUIElement::draw();
327 void GUIMainMenu::acceptInput()
330 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
332 m_data->name = e->getText();
335 gui::IGUIElement *e = getElementFromId(264);
337 m_data->password = e->getText();
340 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
342 m_data->address = e->getText();
345 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
347 m_data->port = e->getText();
350 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
351 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
352 m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
355 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
356 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
357 m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
360 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
361 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
362 m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
365 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
366 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
367 m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
373 bool GUIMainMenu::OnEvent(const SEvent& event)
375 if(event.EventType==EET_KEY_INPUT_EVENT)
377 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
379 m_gamecallback->exitToOS();
383 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
390 if(event.EventType==EET_GUI_EVENT)
392 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
395 if(!canTakeFocus(event.GUIEvent.Element))
397 dstream<<"GUIMainMenu: Not allowing focus change."
399 // Returning true disables focus change
403 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
405 switch(event.GUIEvent.Caller->getID())
407 case GUI_ID_JOIN_GAME_BUTTON: // Start game
411 case GUI_ID_CHANGE_KEYS_BUTTON: {
412 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
416 case GUI_ID_DELETE_MAP_BUTTON: // Delete map
417 // Don't accept input data, just set deletion request
418 m_data->delete_map = true;
424 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
426 switch(event.GUIEvent.Caller->getID())
428 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
436 return Parent ? Parent->OnEvent(event) : false;