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;
73 gui::IGUIElement *e = getElementFromId(258);
75 text_name = e->getText();
77 text_name = m_data->name;
80 gui::IGUIElement *e = getElementFromId(256);
82 text_address = e->getText();
84 text_address = m_data->address;
87 gui::IGUIElement *e = getElementFromId(257);
89 text_port = e->getText();
91 text_port = m_data->port;
94 gui::IGUIElement *e = getElementFromId(263);
95 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
96 fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
98 fancy_trees = m_data->fancy_trees;
101 gui::IGUIElement *e = getElementFromId(262);
102 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
103 smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
105 smooth_lighting = m_data->smooth_lighting;
110 gui::IGUIElement *e = getElementFromId(259);
111 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
112 creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
114 creative_mode = m_data->creative_mode;
117 gui::IGUIElement *e = getElementFromId(261);
118 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
119 enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
121 enable_damage = m_data->enable_damage;
130 Calculate new sizes and positions
133 v2s32 size(620, 430);
135 core::rect<s32> rect(
136 screensize.X/2 - size.X/2,
137 screensize.Y/2 - size.Y/2,
138 screensize.X/2 + size.X/2,
139 screensize.Y/2 + size.Y/2
143 recalculateAbsolutePosition(false);
145 //v2s32 size = rect.getSize();
155 v2s32 topleft_client(40, 0);
156 v2s32 size_client = size - v2s32(40, 0);
159 core::rect<s32> rect(0, 0, 20, 125);
160 rect += topleft_client + v2s32(-15, 60);
161 const wchar_t *text = L"C\nL\nI\nE\nN\nT";
162 //gui::IGUIStaticText *t =
163 Environment->addStaticText(text, rect, false, true, this, -1);
164 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
167 // Nickname + password
169 core::rect<s32> rect(0, 0, 110, 20);
170 rect += topleft_client + v2s32(35, 50+6);
171 const wchar_t *text = L"Name/Password";
172 Environment->addStaticText(text, rect, false, true, this, -1);
175 core::rect<s32> rect(0, 0, 230, 30);
176 rect += topleft_client + v2s32(160, 50);
177 gui::IGUIElement *e =
178 Environment->addEditBox(text_name.c_str(), rect, true, this, 258);
180 Environment->setFocus(e);
183 core::rect<s32> rect(0, 0, 120, 30);
184 rect += topleft_client + v2s32(size_client.X-60-100, 50);
185 gui::IGUIEditBox *e =
186 Environment->addEditBox(L"", rect, true, this, 264);
187 e->setPasswordBox(true);
192 core::rect<s32> rect(0, 0, 110, 20);
193 rect += topleft_client + v2s32(35, 100+6);
194 const wchar_t *text = L"Address/Port";
195 Environment->addStaticText(text, rect, false, true, this, -1);
198 core::rect<s32> rect(0, 0, 230, 30);
199 rect += topleft_client + v2s32(160, 100);
200 gui::IGUIElement *e =
201 Environment->addEditBox(text_address.c_str(), rect, true, this, 256);
203 Environment->setFocus(e);
206 core::rect<s32> rect(0, 0, 120, 30);
207 //rect += topleft_client + v2s32(160+250+20, 125);
208 rect += topleft_client + v2s32(size_client.X-60-100, 100);
209 Environment->addEditBox(text_port.c_str(), rect, true, this, 257);
212 core::rect<s32> rect(0, 0, 400, 20);
213 rect += topleft_client + v2s32(160, 100+35);
214 const wchar_t *text = L"Leave address blank to start a local server.";
215 Environment->addStaticText(text, rect, false, true, this, -1);
218 core::rect<s32> rect(0, 0, 250, 30);
219 rect += topleft_client + v2s32(35, 150);
220 Environment->addCheckBox(fancy_trees, rect, this, 263,
224 core::rect<s32> rect(0, 0, 250, 30);
225 rect += topleft_client + v2s32(35, 150+30);
226 Environment->addCheckBox(smooth_lighting, rect, this, 262,
231 core::rect<s32> rect(0, 0, 180, 30);
232 //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
233 rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
234 Environment->addButton(rect, this, 257, L"Start Game / Connect");
241 v2s32 topleft_server(40, 250);
242 v2s32 size_server = size - v2s32(40, 0);
245 core::rect<s32> rect(0, 0, 20, 125);
246 rect += topleft_server + v2s32(-15, 40);
247 const wchar_t *text = L"S\nE\nR\nV\nE\nR";
248 //gui::IGUIStaticText *t =
249 Environment->addStaticText(text, rect, false, true, this, -1);
250 //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
255 core::rect<s32> rect(0, 0, 250, 30);
256 rect += topleft_server + v2s32(35, 30);
257 Environment->addCheckBox(creative_mode, rect, this, 259, L"Creative Mode");
260 core::rect<s32> rect(0, 0, 250, 30);
261 rect += topleft_server + v2s32(35, 60);
262 Environment->addCheckBox(enable_damage, rect, this, 261, L"Enable Damage");
266 core::rect<s32> rect(0, 0, 130, 30);
267 //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
268 rect += topleft_server + v2s32(40, 100+25);
269 Environment->addButton(rect, this, 260, L"Delete world");
273 void GUIMainMenu::drawMenu()
275 gui::IGUISkin* skin = Environment->getSkin();
278 video::IVideoDriver* driver = Environment->getVideoDriver();
280 /*video::SColor bgcolor(140,0,0,0);
281 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
283 video::SColor bgcolor(140,0,0,0);
286 core::rect<s32> rect(0, 0, 620, 230);
287 rect += AbsoluteRect.UpperLeftCorner;
288 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
292 core::rect<s32> rect(0, 250, 620, 430);
293 rect += AbsoluteRect.UpperLeftCorner;
294 driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
297 gui::IGUIElement::draw();
300 void GUIMainMenu::acceptInput()
303 gui::IGUIElement *e = getElementFromId(258);
305 m_data->name = e->getText();
308 gui::IGUIElement *e = getElementFromId(264);
310 m_data->password = e->getText();
313 gui::IGUIElement *e = getElementFromId(256);
315 m_data->address = e->getText();
318 gui::IGUIElement *e = getElementFromId(257);
320 m_data->port = e->getText();
323 gui::IGUIElement *e = getElementFromId(259);
324 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
325 m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
328 gui::IGUIElement *e = getElementFromId(261);
329 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
330 m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
333 gui::IGUIElement *e = getElementFromId(262);
334 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
335 m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
338 gui::IGUIElement *e = getElementFromId(263);
339 if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
340 m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
346 bool GUIMainMenu::OnEvent(const SEvent& event)
348 if(event.EventType==EET_KEY_INPUT_EVENT)
350 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
352 m_gamecallback->exitToOS();
356 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
363 if(event.EventType==EET_GUI_EVENT)
365 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
368 if(!canTakeFocus(event.GUIEvent.Element))
370 dstream<<"GUIMainMenu: Not allowing focus change."
372 // Returning true disables focus change
376 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
378 switch(event.GUIEvent.Caller->getID())
380 case 257: // Start game
384 case 260: // Delete map
385 // Don't accept input data, just set deletion request
386 m_data->delete_map = true;
392 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
394 switch(event.GUIEvent.Caller->getID())
396 case 256: case 257: case 258: case 264:
404 return Parent ? Parent->OnEvent(event) : false;