3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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 "guiCreateWorld.h"
22 #include "serialization.h"
24 #include <IGUICheckBox.h>
25 #include <IGUIEditBox.h>
26 #include <IGUIButton.h>
27 #include <IGUIStaticText.h>
29 #include <IGUIListBox.h>
31 #include "util/string.h"
35 GUI_ID_NAME_INPUT = 101,
41 GUICreateWorld::GUICreateWorld(gui::IGUIEnvironment* env,
42 gui::IGUIElement* parent, s32 id,
43 IMenuManager *menumgr,
44 CreateWorldDest *dest,
45 const std::vector<SubgameSpec> &games,
46 const std::string &initial_game
48 GUIModalMenu(env, parent, id, menumgr),
53 assert(games.size() > 0);
55 for(size_t i=0; i<games.size(); i++){
56 if(games[i].id == initial_game){
63 GUICreateWorld::~GUICreateWorld()
70 void GUICreateWorld::removeChildren()
72 const core::list<gui::IGUIElement*> &children = getChildren();
73 core::list<gui::IGUIElement*> children_copy;
74 for(core::list<gui::IGUIElement*>::ConstIterator
75 i = children.begin(); i != children.end(); i++)
77 children_copy.push_back(*i);
79 for(core::list<gui::IGUIElement*>::Iterator
80 i = children_copy.begin();
81 i != children_copy.end(); i++)
87 void GUICreateWorld::regenerateGui(v2u32 screensize)
89 std::wstring name = L"";
92 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
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();
117 v2s32 topleft = v2s32(10+80, 10+70);
123 core::rect<s32> rect(0, 0, 100, 20);
124 rect += v2s32(0, 5) + topleft;
125 wchar_t* text = wgettext("World name");
126 Environment->addStaticText(text, rect, false, true, this, -1);
130 core::rect<s32> rect(0, 0, 300, 30);
131 rect = rect + v2s32(100, 0) + topleft;
132 gui::IGUIElement *e =
133 Environment->addEditBox(name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
134 Environment->setFocus(e);
137 evt.EventType = EET_KEY_INPUT_EVENT;
138 evt.KeyInput.Key = KEY_END;
139 evt.KeyInput.PressedDown = true;
143 core::rect<s32> rect(0, 0, 100, 20);
144 rect += v2s32(0, 40+5) + topleft;
145 wchar_t* text = wgettext("Game");
146 Environment->addStaticText(text, rect, false, true, this, -1);
150 core::rect<s32> rect(0, 0, 300, 80);
151 rect += v2s32(100, 40) + topleft;
152 gui::IGUIListBox *e = Environment->addListBox(rect, this,
153 GUI_ID_GAME_LISTBOX);
154 e->setDrawBackground(true);
155 for(u32 i=0; i<m_games.size(); i++){
156 std::wostringstream os(std::ios::binary);
157 os<<narrow_to_wide(m_games[i].name).c_str();
159 os<<narrow_to_wide(m_games[i].id).c_str();
161 e->addItem(os.str().c_str());
163 e->setSelected(m_initial_game_i);
167 core::rect<s32> rect(0, 0, 120, 30);
168 rect = rect + v2s32(170, 140) + topleft;
169 wchar_t* text = wgettext("Create");
170 Environment->addButton(rect, this, GUI_ID_CREATE,
175 core::rect<s32> rect(0, 0, 120, 30);
176 rect = rect + v2s32(300, 140) + topleft;
177 wchar_t* text = wgettext("Cancel");
178 Environment->addButton(rect, this, GUI_ID_CANCEL,
185 void GUICreateWorld::drawMenu()
187 gui::IGUISkin* skin = Environment->getSkin();
190 video::IVideoDriver* driver = Environment->getVideoDriver();
192 video::SColor bgcolor(140,0,0,0);
193 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
195 gui::IGUIElement::draw();
198 void GUICreateWorld::acceptInput()
204 gui::IGUIElement *e = getElementFromId(GUI_ID_GAME_LISTBOX);
205 if(e != NULL && e->getType() == gui::EGUIET_LIST_BOX)
206 selected = ((gui::IGUIListBox*)e)->getSelected();
210 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
214 if(selected != -1 && name != L"")
215 m_dest->accepted(name, m_games[selected].id);
221 bool GUICreateWorld::OnEvent(const SEvent& event)
223 if(event.EventType==EET_KEY_INPUT_EVENT)
225 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
230 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
237 if(event.EventType==EET_GUI_EVENT)
239 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
242 if(!canTakeFocus(event.GUIEvent.Element))
244 dstream<<"GUICreateWorld: Not allowing focus change."
246 // Returning true disables focus change
250 bool accept_input = false;
251 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED){
252 switch(event.GUIEvent.Caller->getID()){
262 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER){
263 switch(event.GUIEvent.Caller->getID()){
264 case GUI_ID_NAME_INPUT:
272 // quitMenu deallocates menu
277 return Parent ? Parent->OnEvent(event) : false;