Translation to Portuguese of Brazil for Minetest
[oweals/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 ):
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                 wchar_t* text = wgettext("World name");
117                 Environment->addStaticText(text, rect, false, true, this, -1);
118                 delete[] text;
119         }
120         {
121                 core::rect<s32> rect(0, 0, 300, 30);
122                 rect = rect + v2s32(100, 0) + topleft;
123                 gui::IGUIElement *e = 
124                 Environment->addEditBox(name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
125                 Environment->setFocus(e);
126
127                 irr::SEvent evt;
128                 evt.EventType = EET_KEY_INPUT_EVENT;
129                 evt.KeyInput.Key = KEY_END;
130                 evt.KeyInput.PressedDown = true;
131                 e->OnEvent(evt);
132         }
133         {
134                 core::rect<s32> rect(0, 0, 100, 20);
135                 rect += v2s32(0, 40+5) + topleft;
136                 wchar_t* text = wgettext("Game");
137                 Environment->addStaticText(text, rect, false, true, this, -1);
138                 delete[] text;
139         }
140         {
141                 core::rect<s32> rect(0, 0, 300, 80);
142                 rect += v2s32(100, 40) + topleft;
143                 gui::IGUIListBox *e = Environment->addListBox(rect, this,
144                                 GUI_ID_GAME_LISTBOX);
145                 e->setDrawBackground(true);
146                 for(u32 i=0; i<m_games.size(); i++){
147                         std::wostringstream os(std::ios::binary);
148                         os<<narrow_to_wide(m_games[i].name).c_str();
149                         os<<L" [";
150                         os<<narrow_to_wide(m_games[i].id).c_str();
151                         os<<L"]";
152                         e->addItem(os.str().c_str());
153                 }
154                 e->setSelected(0);
155         }
156         changeCtype("");
157         {
158                 core::rect<s32> rect(0, 0, 120, 30);
159                 rect = rect + v2s32(170, 140) + topleft;
160                 wchar_t* text = wgettext("Create");
161                 Environment->addButton(rect, this, GUI_ID_CREATE,
162                         text);
163                 delete[] text;
164         }
165         {
166                 core::rect<s32> rect(0, 0, 120, 30);
167                 rect = rect + v2s32(300, 140) + topleft;
168                 wchar_t* text = wgettext("Cancel");
169                 Environment->addButton(rect, this, GUI_ID_CANCEL,
170                         text);
171                 delete [] text;
172         }
173         changeCtype("C");
174 }
175
176 void GUICreateWorld::drawMenu()
177 {
178         gui::IGUISkin* skin = Environment->getSkin();
179         if (!skin)
180                 return;
181         video::IVideoDriver* driver = Environment->getVideoDriver();
182         
183         video::SColor bgcolor(140,0,0,0);
184         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
185
186         gui::IGUIElement::draw();
187 }
188
189 void GUICreateWorld::acceptInput()
190 {
191         if(m_dest)
192         {
193                 int selected = -1;
194                 {
195                         gui::IGUIElement *e = getElementFromId(GUI_ID_GAME_LISTBOX);
196                         if(e != NULL && e->getType() == gui::EGUIET_LIST_BOX)
197                                 selected = ((gui::IGUIListBox*)e)->getSelected();
198                 }
199                 std::wstring name;
200                 {
201                         gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
202                         if(e != NULL)
203                                 name = e->getText();
204                 }
205                 if(selected != -1 && name != L"")
206                         m_dest->accepted(name, m_games[selected].id);
207                 delete m_dest;
208                 m_dest = NULL;
209         }
210 }
211
212 bool GUICreateWorld::OnEvent(const SEvent& event)
213 {
214         if(event.EventType==EET_KEY_INPUT_EVENT)
215         {
216                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
217                 {
218                         quitMenu();
219                         return true;
220                 }
221                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
222                 {
223                         acceptInput();
224                         quitMenu();
225                         return true;
226                 }
227         }
228         if(event.EventType==EET_GUI_EVENT)
229         {
230                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
231                                 && isVisible())
232                 {
233                         if(!canTakeFocus(event.GUIEvent.Element))
234                         {
235                                 dstream<<"GUICreateWorld: Not allowing focus change."
236                                                 <<std::endl;
237                                 // Returning true disables focus change
238                                 return true;
239                         }
240                 }
241                 bool accept_input = false;
242                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED){
243                         switch(event.GUIEvent.Caller->getID()){
244                         case GUI_ID_CANCEL:
245                                 quitMenu();
246                                 return true;
247                                 break;
248                         case GUI_ID_CREATE:
249                                 accept_input = true;
250                                 break;
251                         }
252                 }
253                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER){
254                         switch(event.GUIEvent.Caller->getID()){
255                         case GUI_ID_NAME_INPUT:
256                                 accept_input = true;
257                                 break;
258                         }
259                 }
260                 if(accept_input){
261                         acceptInput();
262                         quitMenu();
263                         // quitMenu deallocates menu
264                         return true;
265                 }
266         }
267
268         return Parent ? Parent->OnEvent(event) : false;
269 }
270