]> git.lizzy.rs Git - minetest.git/blob - src/guiTextInputMenu.cpp
Fix lost pause support in singleplayer
[minetest.git] / src / guiTextInputMenu.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 "guiTextInputMenu.h"
21 #include "debug.h"
22 #include "serialization.h"
23 #include "main.h" // for g_settings
24 #include "settings.h"
25 #include <string>
26 #include <IGUICheckBox.h>
27 #include <IGUIEditBox.h>
28 #include <IGUIButton.h>
29 #include <IGUIStaticText.h>
30 #include <IGUIFont.h>
31
32 #include "gettext.h"
33
34 #if USE_FREETYPE
35 #include "intlGUIEditBox.h"
36 #endif
37
38 GUITextInputMenu::GUITextInputMenu(gui::IGUIEnvironment* env,
39                 gui::IGUIElement* parent, s32 id,
40                 IMenuManager *menumgr,
41                 TextDest *dest,
42                 std::wstring initial_text
43 ):
44         GUIModalMenu(env, parent, id, menumgr),
45         m_dest(dest),
46         m_initial_text(initial_text)
47 {
48 }
49
50 GUITextInputMenu::~GUITextInputMenu()
51 {
52         removeChildren();
53         if(m_dest)
54                 delete m_dest;
55 }
56
57 void GUITextInputMenu::removeChildren()
58 {
59         {
60                 gui::IGUIElement *e = getElementFromId(256);
61                 if(e != NULL)
62                         e->remove();
63         }
64         {
65                 gui::IGUIElement *e = getElementFromId(257);
66                 if(e != NULL)
67                         e->remove();
68         }
69 }
70
71 void GUITextInputMenu::regenerateGui(v2u32 screensize)
72 {
73         std::wstring text;
74
75         {
76                 gui::IGUIElement *e = getElementFromId(256);
77                 if(e != NULL)
78                 {
79                         text = e->getText();
80                 }
81                 else
82                 {
83                         text = m_initial_text;
84                         m_initial_text = L"";
85                 }
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         /*
109                 Add stuff
110         */
111         {
112                 core::rect<s32> rect(0, 0, 300, 30);
113                 rect = rect + v2s32(size.X/2-300/2, size.Y/2-30/2-25);
114                 gui::IGUIElement *e;
115                 #if USE_FREETYPE
116                 if (g_settings->getBool("freetype")) {
117                         e = (gui::IGUIElement *) new gui::intlGUIEditBox(text.c_str(), true, Environment, this, 256, rect);
118                         e->drop();
119                 } else {
120                         e = Environment->addEditBox(text.c_str(), rect, true, this, 256);
121                 }
122                 #else
123                         e = Environment->addEditBox(text.c_str(), rect, true, this, 256);
124                 #endif
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                 evt.KeyInput.Char = 0;
132                 evt.KeyInput.Control = 0;
133                 evt.KeyInput.Shift = 0;
134                 e->OnEvent(evt);
135         }
136         {
137                 core::rect<s32> rect(0, 0, 140, 30);
138                 rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+25);
139                 wchar_t* text = wgettext("Proceed");
140                 Environment->addButton(rect, this, 257,
141                         text);
142                 delete[] text;
143         }
144 }
145
146 void GUITextInputMenu::drawMenu()
147 {
148         gui::IGUISkin* skin = Environment->getSkin();
149         if (!skin)
150                 return;
151         video::IVideoDriver* driver = Environment->getVideoDriver();
152         
153         video::SColor bgcolor(140,0,0,0);
154         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
155
156         gui::IGUIElement::draw();
157 }
158
159 void GUITextInputMenu::acceptInput()
160 {
161         if(m_dest)
162         {
163                 gui::IGUIElement *e = getElementFromId(256);
164                 if(e != NULL)
165                 {
166                         m_dest->gotText(e->getText());
167                 }
168                 delete m_dest;
169                 m_dest = NULL;
170         }
171 }
172
173 bool GUITextInputMenu::OnEvent(const SEvent& event)
174 {
175         if(event.EventType==EET_KEY_INPUT_EVENT)
176         {
177                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
178                 {
179                         quitMenu();
180                         return true;
181                 }
182                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
183                 {
184                         acceptInput();
185                         quitMenu();
186                         return true;
187                 }
188         }
189         if(event.EventType==EET_GUI_EVENT)
190         {
191                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
192                                 && isVisible())
193                 {
194                         if(!canTakeFocus(event.GUIEvent.Element))
195                         {
196                                 dstream<<"GUITextInputMenu: Not allowing focus change."
197                                                 <<std::endl;
198                                 // Returning true disables focus change
199                                 return true;
200                         }
201                 }
202                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
203                 {
204                         switch(event.GUIEvent.Caller->getID())
205                         {
206                         case 257:
207                                 acceptInput();
208                                 quitMenu();
209                                 // quitMenu deallocates menu
210                                 return true;
211                         }
212                 }
213                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
214                 {
215                         switch(event.GUIEvent.Caller->getID())
216                         {
217                         case 256:
218                                 acceptInput();
219                                 quitMenu();
220                                 // quitMenu deallocates menu
221                                 return true;
222                         }
223                 }
224         }
225
226         return Parent ? Parent->OnEvent(event) : false;
227 }
228