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;
69 gui::IGUIElement *e = getElementFromId(258);
71 text_name = e->getText();
73 text_name = m_data->name;
76 gui::IGUIElement *e = getElementFromId(256);
78 text_address = e->getText();
80 text_address = m_data->address;
83 gui::IGUIElement *e = getElementFromId(257);
85 text_port = e->getText();
87 text_port = m_data->port;
90 gui::IGUIElement *e = getElementFromId(259);
91 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
92 creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
94 creative_mode = m_data->creative_mode;
103 Calculate new sizes and positions
105 core::rect<s32> rect(
106 screensize.X/2 - 580/2,
107 screensize.Y/2 - 300/2,
108 screensize.X/2 + 580/2,
109 screensize.Y/2 + 300/2
113 recalculateAbsolutePosition(false);
115 v2s32 size = rect.getSize();
123 core::rect<s32> rect(0, 0, 100, 20);
124 rect = rect + v2s32(size.X/2 - 250, size.Y/2 - 100 + 6);
125 const wchar_t *text = L"Nickname";
126 Environment->addStaticText(text, rect, false, true, this, -1);
129 core::rect<s32> rect(0, 0, 250, 30);
130 rect = rect + v2s32(size.X/2 - 130, size.Y/2 - 100);
131 gui::IGUIElement *e =
132 Environment->addEditBox(text_name.c_str(), rect, true, this, 258);
134 Environment->setFocus(e);
138 core::rect<s32> rect(0, 0, 100, 20);
139 rect = rect + v2s32(size.X/2 - 250, size.Y/2 - 50 + 6);
140 const wchar_t *text = L"Address + Port";
141 Environment->addStaticText(text, rect, false, true, this, -1);
144 core::rect<s32> rect(0, 0, 250, 30);
145 rect = rect + v2s32(size.X/2 - 130, size.Y/2 - 50);
146 gui::IGUIElement *e =
147 Environment->addEditBox(text_address.c_str(), rect, true, this, 256);
149 Environment->setFocus(e);
152 core::rect<s32> rect(0, 0, 100, 30);
153 rect = rect + v2s32(size.X/2 - 130 + 250 + 20, size.Y/2 - 50);
154 Environment->addEditBox(text_port.c_str(), rect, true, this, 257);
157 core::rect<s32> rect(0, 0, 400, 20);
158 rect = rect + v2s32(size.X/2 - 130, size.Y/2 - 50 + 35);
159 const wchar_t *text = L"Leave address blank to start a local server.";
160 Environment->addStaticText(text, rect, false, true, this, -1);
164 core::rect<s32> rect(0, 0, 100, 20);
165 rect = rect + v2s32(size.X/2 - 250, size.Y/2 + 25 + 6);
166 const wchar_t *text = L"Server params";
167 Environment->addStaticText(text, rect, false, true, this, -1);
170 core::rect<s32> rect(0, 0, 250, 30);
171 rect = rect + v2s32(size.X/2 - 130, size.Y/2 + 25);
172 Environment->addCheckBox(creative_mode, rect, this, 259, L"Creative Mode");
176 core::rect<s32> rect(0, 0, 180, 30);
177 rect = rect + v2s32(size.X/2-180/2, size.Y/2-30/2 + 100);
178 Environment->addButton(rect, this, 257, L"Start Game / Connect");
182 core::rect<s32> rect(0, 0, 130, 30);
183 rect = rect + v2s32(size.X/2-130/2+200, size.Y/2-30/2 + 100);
184 Environment->addButton(rect, this, 260, L"Delete map");
188 void GUIMainMenu::drawMenu()
190 gui::IGUISkin* skin = Environment->getSkin();
193 video::IVideoDriver* driver = Environment->getVideoDriver();
195 video::SColor bgcolor(140,0,0,0);
196 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
198 gui::IGUIElement::draw();
201 void GUIMainMenu::acceptInput()
204 gui::IGUIElement *e = getElementFromId(258);
206 m_data->name = e->getText();
209 gui::IGUIElement *e = getElementFromId(256);
211 m_data->address = e->getText();
214 gui::IGUIElement *e = getElementFromId(257);
216 m_data->port = e->getText();
219 gui::IGUIElement *e = getElementFromId(259);
220 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
221 m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
227 bool GUIMainMenu::OnEvent(const SEvent& event)
229 if(event.EventType==EET_KEY_INPUT_EVENT)
231 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
233 m_gamecallback->exitToOS();
237 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
244 if(event.EventType==EET_GUI_EVENT)
246 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
249 if(!canTakeFocus(event.GUIEvent.Element))
251 dstream<<"GUIMainMenu: Not allowing focus change."
253 // Returning true disables focus change
257 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
259 switch(event.GUIEvent.Caller->getID())
261 case 257: // Start game
265 case 260: // Delete map
266 // Don't accept input data, just set deletion request
267 m_data->delete_map = true;
273 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
275 switch(event.GUIEvent.Caller->getID())
277 case 256: case 257: case 258:
285 return Parent ? Parent->OnEvent(event) : false;