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"
22 #include "serialization.h"
25 GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
26 gui::IGUIElement* parent, s32 id,
27 IMenuManager *menumgr,
29 IGameCallback *gamecallback
31 GUIModalMenu(env, parent, id, menumgr),
34 m_gamecallback(gamecallback)
39 GUIMainMenu::~GUIMainMenu()
44 void GUIMainMenu::removeChildren()
46 const core::list<gui::IGUIElement*> &children = getChildren();
47 core::list<gui::IGUIElement*> children_copy;
48 for(core::list<gui::IGUIElement*>::ConstIterator
49 i = children.begin(); i != children.end(); i++)
51 children_copy.push_back(*i);
53 for(core::list<gui::IGUIElement*>::Iterator
54 i = children_copy.begin();
55 i != children_copy.end(); i++)
61 void GUIMainMenu::regenerateGui(v2u32 screensize)
63 std::wstring text_name;
64 std::wstring text_address;
65 std::wstring text_port;
70 gui::IGUIElement *e = getElementFromId(258);
72 text_name = e->getText();
74 text_name = m_data->name;
77 gui::IGUIElement *e = getElementFromId(256);
79 text_address = e->getText();
81 text_address = m_data->address;
84 gui::IGUIElement *e = getElementFromId(257);
86 text_port = e->getText();
88 text_port = m_data->port;
91 gui::IGUIElement *e = getElementFromId(259);
92 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
93 creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
95 creative_mode = m_data->creative_mode;
98 gui::IGUIElement *e = getElementFromId(261);
99 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
100 enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
102 enable_damage = m_data->enable_damage;
111 Calculate new sizes and positions
114 v2s32 size(620, 430);
116 core::rect<s32> rect(
117 screensize.X/2 - size.X/2,
118 screensize.Y/2 - size.Y/2,
119 screensize.X/2 + size.X/2,
120 screensize.Y/2 + size.Y/2
124 recalculateAbsolutePosition(false);
126 //v2s32 size = rect.getSize();
136 v2s32 topleft_client(40, 0);
137 v2s32 size_client = size - v2s32(40, 0);
140 core::rect<s32> rect(0, 0, 20, 125);
141 rect += topleft_client + v2s32(-15, 60);
142 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
143 //gui::IGUIStaticText *t =
144 Environment->addStaticText(text, rect, false, true, this, -1);
145 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
150 core::rect<s32> rect(0, 0, 100, 20);
151 rect += topleft_client + v2s32(40, 50+6);
152 const wchar_t *text = L"Nickname";
153 Environment->addStaticText(text, rect, false, true, this, -1);
156 core::rect<s32> rect(0, 0, 250, 30);
157 rect += topleft_client + v2s32(160, 50);
158 gui::IGUIElement *e =
159 Environment->addEditBox(text_name.c_str(), rect, true, this, 258);
161 Environment->setFocus(e);
165 core::rect<s32> rect(0, 0, 100, 20);
166 rect += topleft_client + v2s32(40, 100+6);
167 const wchar_t *text = L"Address + Port";
168 Environment->addStaticText(text, rect, false, true, this, -1);
171 core::rect<s32> rect(0, 0, 250, 30);
172 rect += topleft_client + v2s32(160, 100);
173 gui::IGUIElement *e =
174 Environment->addEditBox(text_address.c_str(), rect, true, this, 256);
176 Environment->setFocus(e);
179 core::rect<s32> rect(0, 0, 100, 30);
180 //rect += topleft_client + v2s32(160+250+20, 125);
181 rect += topleft_client + v2s32(size_client.X-40-100, 100);
182 Environment->addEditBox(text_port.c_str(), rect, true, this, 257);
185 core::rect<s32> rect(0, 0, 400, 20);
186 rect += topleft_client + v2s32(160, 100+35);
187 const wchar_t *text = L"Leave address blank to start a local server.";
188 Environment->addStaticText(text, rect, false, true, this, -1);
192 core::rect<s32> rect(0, 0, 180, 30);
193 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
194 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
195 Environment->addButton(rect, this, 257, L"Start Game / Connect");
202 v2s32 topleft_server(40, 250);
203 v2s32 size_server = size - v2s32(40, 0);
206 core::rect<s32> rect(0, 0, 20, 125);
207 rect += topleft_server + v2s32(-15, 40);
208 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
209 //gui::IGUIStaticText *t =
210 Environment->addStaticText(text, rect, false, true, this, -1);
211 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
216 core::rect<s32> rect(0, 0, 250, 30);
217 rect += topleft_server + v2s32(40, 30);
218 Environment->addCheckBox(creative_mode, rect, this, 259, L"Creative Mode");
221 core::rect<s32> rect(0, 0, 250, 30);
222 rect += topleft_server + v2s32(40, 60);
223 Environment->addCheckBox(enable_damage, rect, this, 261, L"Enable Damage");
227 core::rect<s32> rect(0, 0, 130, 30);
228 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
229 rect += topleft_server + v2s32(40, 100+25);
230 Environment->addButton(rect, this, 260, L"Delete map");
234 void GUIMainMenu::drawMenu()
236 gui::IGUISkin* skin = Environment->getSkin();
239 video::IVideoDriver* driver = Environment->getVideoDriver();
241 /*video::SColor bgcolor(140,0,0,0);
242 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
244 video::SColor bgcolor(140,0,0,0);
247 core::rect<s32> rect(0, 0, 620, 230);
248 rect += AbsoluteRect.UpperLeftCorner;
249 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
253 core::rect<s32> rect(0, 250, 620, 430);
254 rect += AbsoluteRect.UpperLeftCorner;
255 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
258 gui::IGUIElement::draw();
261 void GUIMainMenu::acceptInput()
264 gui::IGUIElement *e = getElementFromId(258);
266 m_data->name = e->getText();
269 gui::IGUIElement *e = getElementFromId(256);
271 m_data->address = e->getText();
274 gui::IGUIElement *e = getElementFromId(257);
276 m_data->port = e->getText();
279 gui::IGUIElement *e = getElementFromId(259);
280 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
281 m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
284 gui::IGUIElement *e = getElementFromId(261);
285 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
286 m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
292 bool GUIMainMenu::OnEvent(const SEvent& event)
294 if(event.EventType==EET_KEY_INPUT_EVENT)
296 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
298 m_gamecallback->exitToOS();
302 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
309 if(event.EventType==EET_GUI_EVENT)
311 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
314 if(!canTakeFocus(event.GUIEvent.Element))
316 dstream<<"GUIMainMenu: Not allowing focus change."
318 // Returning true disables focus change
322 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
324 switch(event.GUIEvent.Caller->getID())
326 case 257: // Start game
330 case 260: // Delete map
331 // Don't accept input data, just set deletion request
332 m_data->delete_map = true;
338 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
340 switch(event.GUIEvent.Caller->getID())
342 case 256: case 257: case 258:
350 return Parent ? Parent->OnEvent(event) : false;