]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiTextInputMenu.cpp
Masterserver show privs and js autoload
[dragonfireclient.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         changeCtype("");
137         {
138                 core::rect<s32> rect(0, 0, 140, 30);
139                 rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+25);
140                 wchar_t* text = wgettext("Proceed");
141                 Environment->addButton(rect, this, 257,
142                         text);
143                 delete[] text;
144         }
145         changeCtype("C");
146 }
147
148 void GUITextInputMenu::drawMenu()
149 {
150         gui::IGUISkin* skin = Environment->getSkin();
151         if (!skin)
152                 return;
153         video::IVideoDriver* driver = Environment->getVideoDriver();
154         
155         video::SColor bgcolor(140,0,0,0);
156         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
157
158         gui::IGUIElement::draw();
159 }
160
161 void GUITextInputMenu::acceptInput()
162 {
163         if(m_dest)
164         {
165                 gui::IGUIElement *e = getElementFromId(256);
166                 if(e != NULL)
167                 {
168                         m_dest->gotText(e->getText());
169                 }
170                 delete m_dest;
171                 m_dest = NULL;
172         }
173 }
174
175 bool GUITextInputMenu::OnEvent(const SEvent& event)
176 {
177         if(event.EventType==EET_KEY_INPUT_EVENT)
178         {
179                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
180                 {
181                         quitMenu();
182                         return true;
183                 }
184                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
185                 {
186                         acceptInput();
187                         quitMenu();
188                         return true;
189                 }
190         }
191         if(event.EventType==EET_GUI_EVENT)
192         {
193                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
194                                 && isVisible())
195                 {
196                         if(!canTakeFocus(event.GUIEvent.Element))
197                         {
198                                 dstream<<"GUITextInputMenu: Not allowing focus change."
199                                                 <<std::endl;
200                                 // Returning true disables focus change
201                                 return true;
202                         }
203                 }
204                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
205                 {
206                         switch(event.GUIEvent.Caller->getID())
207                         {
208                         case 257:
209                                 acceptInput();
210                                 quitMenu();
211                                 // quitMenu deallocates menu
212                                 return true;
213                         }
214                 }
215                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
216                 {
217                         switch(event.GUIEvent.Caller->getID())
218                         {
219                         case 256:
220                                 acceptInput();
221                                 quitMenu();
222                                 // quitMenu deallocates menu
223                                 return true;
224                         }
225                 }
226         }
227
228         return Parent ? Parent->OnEvent(event) : false;
229 }
230