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"
28 GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
29 gui::IGUIElement* parent, s32 id,
30 IMenuManager *menumgr,
32 IGameCallback *gamecallback
34 GUIModalMenu(env, parent, id, menumgr),
37 m_gamecallback(gamecallback)
41 this->parent = parent;
43 this->menumgr = menumgr;
46 GUIMainMenu::~GUIMainMenu()
51 void GUIMainMenu::removeChildren()
53 const core::list<gui::IGUIElement*> &children = getChildren();
54 core::list<gui::IGUIElement*> children_copy;
55 for(core::list<gui::IGUIElement*>::ConstIterator
56 i = children.begin(); i != children.end(); i++)
58 children_copy.push_back(*i);
60 for(core::list<gui::IGUIElement*>::Iterator
61 i = children_copy.begin();
62 i != children_copy.end(); i++)
68 void GUIMainMenu::regenerateGui(v2u32 screensize)
70 std::wstring text_name;
71 std::wstring text_address;
72 std::wstring text_port;
80 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
82 text_name = e->getText();
84 text_name = m_data->name;
87 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
89 text_address = e->getText();
91 text_address = m_data->address;
94 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
96 text_port = e->getText();
98 text_port = m_data->port;
101 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
102 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
103 fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
105 fancy_trees = m_data->fancy_trees;
108 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
109 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
110 smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
112 smooth_lighting = m_data->smooth_lighting;
117 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
118 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
119 creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
121 creative_mode = m_data->creative_mode;
124 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
125 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
126 enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
128 enable_damage = m_data->enable_damage;
137 Calculate new sizes and positions
140 v2s32 size(620, 430);
142 core::rect<s32> rect(
143 screensize.X/2 - size.X/2,
144 screensize.Y/2 - size.Y/2,
145 screensize.X/2 + size.X/2,
146 screensize.Y/2 + size.Y/2
150 recalculateAbsolutePosition(false);
152 //v2s32 size = rect.getSize();
162 v2s32 topleft_client(40, 0);
163 v2s32 size_client = size - v2s32(40, 0);
166 core::rect<s32> rect(0, 0, 20, 125);
167 rect += topleft_client + v2s32(-15, 60);
168 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
169 //gui::IGUIStaticText *t =
170 Environment->addStaticText(text, rect, false, true, this, -1);
171 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
174 // Nickname + password
176 core::rect<s32> rect(0, 0, 110, 20);
177 rect += topleft_client + v2s32(35, 50+6);
178 const wchar_t *text = L"Name/Password";
179 Environment->addStaticText(text, rect, false, true, this, -1);
182 core::rect<s32> rect(0, 0, 230, 30);
183 rect += topleft_client + v2s32(160, 50);
184 gui::IGUIElement *e =
185 Environment->addEditBox(text_name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
187 Environment->setFocus(e);
190 core::rect<s32> rect(0, 0, 120, 30);
191 rect += topleft_client + v2s32(size_client.X-60-100, 50);
192 gui::IGUIEditBox *e =
193 Environment->addEditBox(L"", rect, true, this, 264);
194 e->setPasswordBox(true);
199 core::rect<s32> rect(0, 0, 110, 20);
200 rect += topleft_client + v2s32(35, 100+6);
201 const wchar_t *text = L"Address/Port";
202 Environment->addStaticText(text, rect, false, true, this, -1);
205 core::rect<s32> rect(0, 0, 230, 30);
206 rect += topleft_client + v2s32(160, 100);
207 gui::IGUIElement *e =
208 Environment->addEditBox(text_address.c_str(), rect, true, this, GUI_ID_ADDRESS_INPUT);
210 Environment->setFocus(e);
213 core::rect<s32> rect(0, 0, 120, 30);
214 //rect += topleft_client + v2s32(160+250+20, 125);
215 rect += topleft_client + v2s32(size_client.X-60-100, 100);
216 Environment->addEditBox(text_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 const wchar_t *text = L"Leave address blank to start a local server.";
222 Environment->addStaticText(text, rect, false, true, this, -1);
225 core::rect<s32> rect(0, 0, 250, 30);
226 rect += topleft_client + v2s32(35, 150);
227 Environment->addCheckBox(fancy_trees, rect, this, GUI_ID_FANCYTREE_CB,
231 core::rect<s32> rect(0, 0, 250, 30);
232 rect += topleft_client + v2s32(35, 150+30);
233 Environment->addCheckBox(smooth_lighting, rect, this, GUI_ID_SMOOTH_LIGHTING_CB,
238 core::rect<s32> rect(0, 0, 180, 30);
239 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
240 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
241 Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON, L"Start Game / Connect");
246 core::rect<s32> rect(0, 0, 100, 30);
247 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
248 rect += topleft_client + v2s32(size_client.X-180-40-100-20, 150+25);
249 Environment->addButton(rect, this, GUI_ID_CHANGE_KEYS_BUTTON, L"Change keys");
255 v2s32 topleft_server(40, 250);
256 v2s32 size_server = size - v2s32(40, 0);
259 core::rect<s32> rect(0, 0, 20, 125);
260 rect += topleft_server + v2s32(-15, 40);
261 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
262 //gui::IGUIStaticText *t =
263 Environment->addStaticText(text, rect, false, true, this, -1);
264 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
269 core::rect<s32> rect(0, 0, 250, 30);
270 rect += topleft_server + v2s32(35, 30);
271 Environment->addCheckBox(creative_mode, rect, this, GUI_ID_CREATIVE_CB, L"Creative Mode");
274 core::rect<s32> rect(0, 0, 250, 30);
275 rect += topleft_server + v2s32(35, 60);
276 Environment->addCheckBox(enable_damage, rect, this, GUI_ID_DAMAGE_CB, L"Enable Damage");
280 core::rect<s32> rect(0, 0, 130, 30);
281 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
282 rect += topleft_server + v2s32(40, 100+25);
283 Environment->addButton(rect, this, GUI_ID_DELETE_MAP_BUTTON, L"Delete map");
287 void GUIMainMenu::drawMenu()
289 gui::IGUISkin* skin = Environment->getSkin();
292 video::IVideoDriver* driver = Environment->getVideoDriver();
294 /*video::SColor bgcolor(140,0,0,0);
295 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
297 video::SColor bgcolor(140,0,0,0);
300 core::rect<s32> rect(0, 0, 620, 230);
301 rect += AbsoluteRect.UpperLeftCorner;
302 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
306 core::rect<s32> rect(0, 250, 620, 430);
307 rect += AbsoluteRect.UpperLeftCorner;
308 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
311 gui::IGUIElement::draw();
314 void GUIMainMenu::acceptInput()
317 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
319 m_data->name = e->getText();
322 gui::IGUIElement *e = getElementFromId(264);
324 m_data->password = e->getText();
327 gui::IGUIElement *e = getElementFromId(GUI_ID_ADDRESS_INPUT);
329 m_data->address = e->getText();
332 gui::IGUIElement *e = getElementFromId(GUI_ID_PORT_INPUT);
334 m_data->port = e->getText();
337 gui::IGUIElement *e = getElementFromId(GUI_ID_CREATIVE_CB);
338 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
339 m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
342 gui::IGUIElement *e = getElementFromId(GUI_ID_DAMAGE_CB);
343 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
344 m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
347 gui::IGUIElement *e = getElementFromId(GUI_ID_SMOOTH_LIGHTING_CB);
348 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
349 m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
352 gui::IGUIElement *e = getElementFromId(GUI_ID_FANCYTREE_CB);
353 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
354 m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
360 bool GUIMainMenu::OnEvent(const SEvent& event)
362 if(event.EventType==EET_KEY_INPUT_EVENT)
364 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
366 m_gamecallback->exitToOS();
370 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
377 if(event.EventType==EET_GUI_EVENT)
379 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
382 if(!canTakeFocus(event.GUIEvent.Element))
384 dstream<<"GUIMainMenu: Not allowing focus change."
386 // Returning true disables focus change
390 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
392 switch(event.GUIEvent.Caller->getID())
394 case GUI_ID_JOIN_GAME_BUTTON: // Start game
398 case GUI_ID_CHANGE_KEYS_BUTTON: {
399 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(env, parent, -1,menumgr);
403 case GUI_ID_DELETE_MAP_BUTTON: // Delete map
404 // Don't accept input data, just set deletion request
405 m_data->delete_map = true;
411 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
413 switch(event.GUIEvent.Caller->getID())
415 case GUI_ID_ADDRESS_INPUT: case GUI_ID_PORT_INPUT: case GUI_ID_NAME_INPUT: case 264:
423 return Parent ? Parent->OnEvent(event) : false;