]> git.lizzy.rs Git - minetest.git/blob - src/guiCreateWorld.cpp
World creation button and dialog and functionality
[minetest.git] / src / guiCreateWorld.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "guiCreateWorld.h"
21 #include "debug.h"
22 #include "serialization.h"
23 #include <string>
24 #include <IGUICheckBox.h>
25 #include <IGUIEditBox.h>
26 #include <IGUIButton.h>
27 #include <IGUIStaticText.h>
28 #include <IGUIFont.h>
29 #include <IGUIListBox.h>
30
31 #include "gettext.h"
32
33 enum
34 {
35         GUI_ID_NAME_INPUT = 101,
36         GUI_ID_GAME_LISTBOX,
37         GUI_ID_CREATE,
38         GUI_ID_CANCEL
39 };
40
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 ):
47         GUIModalMenu(env, parent, id, menumgr),
48         m_dest(dest),
49         m_games(games)
50 {
51         assert(games.size() > 0);
52 }
53
54 GUICreateWorld::~GUICreateWorld()
55 {
56         removeChildren();
57         if(m_dest)
58                 delete m_dest;
59 }
60
61 void GUICreateWorld::removeChildren()
62 {
63         const core::list<gui::IGUIElement*> &children = getChildren();
64         core::list<gui::IGUIElement*> children_copy;
65         for(core::list<gui::IGUIElement*>::ConstIterator
66                         i = children.begin(); i != children.end(); i++)
67         {
68                 children_copy.push_back(*i);
69         }
70         for(core::list<gui::IGUIElement*>::Iterator
71                         i = children_copy.begin();
72                         i != children_copy.end(); i++)
73         {
74                 (*i)->remove();
75         }
76 }
77
78 void GUICreateWorld::regenerateGui(v2u32 screensize)
79 {
80         std::wstring name = L"";
81
82         {
83                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
84                 if(e != NULL)
85                         name = e->getText();
86         }
87
88         /*
89                 Remove stuff
90         */
91         removeChildren();
92         
93         /*
94                 Calculate new sizes and positions
95         */
96         core::rect<s32> rect(
97                         screensize.X/2 - 580/2,
98                         screensize.Y/2 - 300/2,
99                         screensize.X/2 + 580/2,
100                         screensize.Y/2 + 300/2
101         );
102         
103         DesiredRect = rect;
104         recalculateAbsolutePosition(false);
105
106         v2s32 size = rect.getSize();
107
108         v2s32 topleft = v2s32(10+80, 10+70);
109
110         /*
111                 Add stuff
112         */
113         {
114                 core::rect<s32> rect(0, 0, 100, 20);
115                 rect += v2s32(0, 5) + topleft;
116                 Environment->addStaticText(wgettext("World name"),
117                         rect, false, true, this, -1);
118         }
119         {
120                 core::rect<s32> rect(0, 0, 300, 30);
121                 rect = rect + v2s32(100, 0) + topleft;
122                 gui::IGUIElement *e = 
123                 Environment->addEditBox(name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
124                 Environment->setFocus(e);
125
126                 irr::SEvent evt;
127                 evt.EventType = EET_KEY_INPUT_EVENT;
128                 evt.KeyInput.Key = KEY_END;
129                 evt.KeyInput.PressedDown = true;
130                 e->OnEvent(evt);
131         }
132         {
133                 core::rect<s32> rect(0, 0, 100, 20);
134                 rect += v2s32(0, 40+5) + topleft;
135                 Environment->addStaticText(wgettext("Game"),
136                         rect, false, true, this, -1);
137         }
138         {
139                 core::rect<s32> rect(0, 0, 300, 80);
140                 rect += v2s32(100, 40) + topleft;
141                 gui::IGUIListBox *e = Environment->addListBox(rect, this,
142                                 GUI_ID_GAME_LISTBOX);
143                 e->setDrawBackground(true);
144                 for(u32 i=0; i<m_games.size(); i++){
145                         std::wostringstream os(std::ios::binary);
146                         os<<narrow_to_wide(m_games[i].name).c_str();
147                         os<<L" [";
148                         os<<narrow_to_wide(m_games[i].id).c_str();
149                         os<<L"]";
150                         e->addItem(os.str().c_str());
151                 }
152                 e->setSelected(0);
153         }
154         changeCtype("");
155         {
156                 core::rect<s32> rect(0, 0, 120, 30);
157                 rect = rect + v2s32(170, 140) + topleft;
158                 Environment->addButton(rect, this, GUI_ID_CANCEL,
159                         wgettext("Cancel"));
160         }
161         {
162                 core::rect<s32> rect(0, 0, 120, 30);
163                 rect = rect + v2s32(300, 140) + topleft;
164                 Environment->addButton(rect, this, GUI_ID_CREATE,
165                         wgettext("Create"));
166         }
167         changeCtype("C");
168 }
169
170 void GUICreateWorld::drawMenu()
171 {
172         gui::IGUISkin* skin = Environment->getSkin();
173         if (!skin)
174                 return;
175         video::IVideoDriver* driver = Environment->getVideoDriver();
176         
177         video::SColor bgcolor(140,0,0,0);
178         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
179
180         gui::IGUIElement::draw();
181 }
182
183 void GUICreateWorld::acceptInput()
184 {
185         if(m_dest)
186         {
187                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
188                 if(e != NULL)
189                 {
190                         m_dest->accepted(e->getText(), m_games[0].id);
191                 }
192                 delete m_dest;
193                 m_dest = NULL;
194         }
195 }
196
197 bool GUICreateWorld::OnEvent(const SEvent& event)
198 {
199         if(event.EventType==EET_KEY_INPUT_EVENT)
200         {
201                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
202                 {
203                         quitMenu();
204                         return true;
205                 }
206                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
207                 {
208                         acceptInput();
209                         quitMenu();
210                         return true;
211                 }
212         }
213         if(event.EventType==EET_GUI_EVENT)
214         {
215                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
216                                 && isVisible())
217                 {
218                         if(!canTakeFocus(event.GUIEvent.Element))
219                         {
220                                 dstream<<"GUICreateWorld: Not allowing focus change."
221                                                 <<std::endl;
222                                 // Returning true disables focus change
223                                 return true;
224                         }
225                 }
226                 bool accept_input = false;
227                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED){
228                         switch(event.GUIEvent.Caller->getID()){
229                         case GUI_ID_CANCEL:
230                                 quitMenu();
231                                 return true;
232                                 break;
233                         case GUI_ID_CREATE:
234                                 accept_input = true;
235                                 break;
236                         }
237                 }
238                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER){
239                         switch(event.GUIEvent.Caller->getID()){
240                         case GUI_ID_NAME_INPUT:
241                                 accept_input = true;
242                                 break;
243                         }
244                 }
245                 if(accept_input){
246                         acceptInput();
247                         quitMenu();
248                         // quitMenu deallocates menu
249                         return true;
250                 }
251         }
252
253         return Parent ? Parent->OnEvent(event) : false;
254 }
255