]> git.lizzy.rs Git - minetest.git/blob - src/guiCreateWorld.cpp
Remove useless recalculation of bounding box (mapblock_mesh)
[minetest.git] / src / guiCreateWorld.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 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.
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 Lesser General Public License for more details.
14
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.
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 #include "gettext.h"
31 #include "util/string.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                 const std::string &initial_game
47 ):
48         GUIModalMenu(env, parent, id, menumgr),
49         m_dest(dest),
50         m_games(games),
51         m_initial_game_i(0)
52 {
53         assert(games.size() > 0);
54
55         for(size_t i=0; i<games.size(); i++){
56                 if(games[i].id == initial_game){
57                         m_initial_game_i = i;
58                         break;
59                 }
60         }
61 }
62
63 GUICreateWorld::~GUICreateWorld()
64 {
65         removeChildren();
66         if(m_dest)
67                 delete m_dest;
68 }
69
70 void GUICreateWorld::removeChildren()
71 {
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++)
76         {
77                 children_copy.push_back(*i);
78         }
79         for(core::list<gui::IGUIElement*>::Iterator
80                         i = children_copy.begin();
81                         i != children_copy.end(); i++)
82         {
83                 (*i)->remove();
84         }
85 }
86
87 void GUICreateWorld::regenerateGui(v2u32 screensize)
88 {
89         std::wstring name = L"";
90
91         {
92                 gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
93                 if(e != NULL)
94                         name = e->getText();
95         }
96
97         /*
98                 Remove stuff
99         */
100         removeChildren();
101         
102         /*
103                 Calculate new sizes and positions
104         */
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
110         );
111         
112         DesiredRect = rect;
113         recalculateAbsolutePosition(false);
114
115         v2s32 topleft = v2s32(10+80, 10+70);
116
117         /*
118                 Add stuff
119         */
120         {
121                 core::rect<s32> rect(0, 0, 100, 20);
122                 rect += v2s32(0, 5) + topleft;
123                 wchar_t* text = wgettext("World name");
124                 Environment->addStaticText(text, rect, false, true, this, -1);
125                 delete[] text;
126         }
127         {
128                 core::rect<s32> rect(0, 0, 300, 30);
129                 rect = rect + v2s32(100, 0) + topleft;
130                 gui::IGUIElement *e = 
131                 Environment->addEditBox(name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
132                 Environment->setFocus(e);
133
134                 irr::SEvent evt;
135                 evt.EventType = EET_KEY_INPUT_EVENT;
136                 evt.KeyInput.Key = KEY_END;
137                 evt.KeyInput.PressedDown = true;
138                 evt.KeyInput.Char = 0;
139                 evt.KeyInput.Control = 0;
140                 evt.KeyInput.Shift = 0;
141                 e->OnEvent(evt);
142         }
143         {
144                 core::rect<s32> rect(0, 0, 100, 20);
145                 rect += v2s32(0, 40+5) + topleft;
146                 wchar_t* text = wgettext("Game");
147                 Environment->addStaticText(text, rect, false, true, this, -1);
148                 delete[] text;
149         }
150         {
151                 core::rect<s32> rect(0, 0, 300, 80);
152                 rect += v2s32(100, 40) + topleft;
153                 gui::IGUIListBox *e = Environment->addListBox(rect, this,
154                                 GUI_ID_GAME_LISTBOX);
155                 e->setDrawBackground(true);
156                 for(u32 i=0; i<m_games.size(); i++){
157                         std::wostringstream os(std::ios::binary);
158                         os<<narrow_to_wide(m_games[i].name).c_str();
159                         os<<L" [";
160                         os<<narrow_to_wide(m_games[i].id).c_str();
161                         os<<L"]";
162                         e->addItem(os.str().c_str());
163                 }
164                 e->setSelected(m_initial_game_i);
165         }
166         changeCtype("");
167         {
168                 core::rect<s32> rect(0, 0, 120, 30);
169                 rect = rect + v2s32(170, 140) + topleft;
170                 wchar_t* text = wgettext("Create");
171                 Environment->addButton(rect, this, GUI_ID_CREATE,
172                         text);
173                 delete[] text;
174         }
175         {
176                 core::rect<s32> rect(0, 0, 120, 30);
177                 rect = rect + v2s32(300, 140) + topleft;
178                 wchar_t* text = wgettext("Cancel");
179                 Environment->addButton(rect, this, GUI_ID_CANCEL,
180                         text);
181                 delete [] text;
182         }
183         changeCtype("C");
184 }
185
186 void GUICreateWorld::drawMenu()
187 {
188         gui::IGUISkin* skin = Environment->getSkin();
189         if (!skin)
190                 return;
191         video::IVideoDriver* driver = Environment->getVideoDriver();
192         
193         video::SColor bgcolor(140,0,0,0);
194         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
195
196         gui::IGUIElement::draw();
197 }
198
199 void GUICreateWorld::acceptInput()
200 {
201         if(m_dest)
202         {
203                 int selected = -1;
204                 {
205                         gui::IGUIElement *e = getElementFromId(GUI_ID_GAME_LISTBOX);
206                         if(e != NULL && e->getType() == gui::EGUIET_LIST_BOX)
207                                 selected = ((gui::IGUIListBox*)e)->getSelected();
208                 }
209                 std::wstring name;
210                 {
211                         gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
212                         if(e != NULL)
213                                 name = e->getText();
214                 }
215                 if(selected != -1 && name != L"")
216                         m_dest->accepted(name, m_games[selected].id);
217                 delete m_dest;
218                 m_dest = NULL;
219         }
220 }
221
222 bool GUICreateWorld::OnEvent(const SEvent& event)
223 {
224         if(event.EventType==EET_KEY_INPUT_EVENT)
225         {
226                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
227                 {
228                         quitMenu();
229                         return true;
230                 }
231                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
232                 {
233                         acceptInput();
234                         quitMenu();
235                         return true;
236                 }
237         }
238         if(event.EventType==EET_GUI_EVENT)
239         {
240                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
241                                 && isVisible())
242                 {
243                         if(!canTakeFocus(event.GUIEvent.Element))
244                         {
245                                 dstream<<"GUICreateWorld: Not allowing focus change."
246                                                 <<std::endl;
247                                 // Returning true disables focus change
248                                 return true;
249                         }
250                 }
251                 bool accept_input = false;
252                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED){
253                         switch(event.GUIEvent.Caller->getID()){
254                         case GUI_ID_CANCEL:
255                                 quitMenu();
256                                 return true;
257                                 break;
258                         case GUI_ID_CREATE:
259                                 accept_input = true;
260                                 break;
261                         }
262                 }
263                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER){
264                         switch(event.GUIEvent.Caller->getID()){
265                         case GUI_ID_NAME_INPUT:
266                                 accept_input = true;
267                                 break;
268                         }
269                 }
270                 if(accept_input){
271                         acceptInput();
272                         quitMenu();
273                         // quitMenu deallocates menu
274                         return true;
275                 }
276         }
277
278         return Parent ? Parent->OnEvent(event) : false;
279 }
280