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