2cb8cae6295b3e5417989408d650d959986e10b3
[oweals/minetest.git] / src / guiTextInputMenu.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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
25 GUITextInputMenu::GUITextInputMenu(gui::IGUIEnvironment* env,
26                 gui::IGUIElement* parent, s32 id,
27                 IMenuManager *menumgr,
28                 TextDest *dest,
29                 std::wstring initial_text
30 ):
31         GUIModalMenu(env, parent, id, menumgr),
32         m_dest(dest),
33         m_initial_text(initial_text)
34 {
35 }
36
37 GUITextInputMenu::~GUITextInputMenu()
38 {
39         removeChildren();
40         if(m_dest)
41                 delete m_dest;
42 }
43
44 void GUITextInputMenu::removeChildren()
45 {
46         {
47                 gui::IGUIElement *e = getElementFromId(256);
48                 if(e != NULL)
49                         e->remove();
50         }
51         {
52                 gui::IGUIElement *e = getElementFromId(257);
53                 if(e != NULL)
54                         e->remove();
55         }
56 }
57
58 void GUITextInputMenu::regenerateGui(v2u32 screensize)
59 {
60         std::wstring text;
61
62         {
63                 gui::IGUIElement *e = getElementFromId(256);
64                 if(e != NULL)
65                 {
66                         text = e->getText();
67                 }
68                 else
69                 {
70                         text = m_initial_text;
71                         m_initial_text = L"";
72                 }
73         }
74
75         /*
76                 Remove stuff
77         */
78         removeChildren();
79         
80         /*
81                 Calculate new sizes and positions
82         */
83         core::rect<s32> rect(
84                         screensize.X/2 - 580/2,
85                         screensize.Y/2 - 300/2,
86                         screensize.X/2 + 580/2,
87                         screensize.Y/2 + 300/2
88         );
89         
90         DesiredRect = rect;
91         recalculateAbsolutePosition(false);
92
93         v2s32 size = rect.getSize();
94
95         /*
96                 Add stuff
97         */
98         {
99                 core::rect<s32> rect(0, 0, 300, 30);
100                 rect = rect + v2s32(size.X/2-300/2, size.Y/2-30/2-25);
101                 gui::IGUIElement *e = 
102                 Environment->addEditBox(text.c_str(), rect, true, this, 256);
103                 Environment->setFocus(e);
104         }
105         {
106                 core::rect<s32> rect(0, 0, 140, 30);
107                 rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+25);
108                 Environment->addButton(rect, this, 257, L"Proceed");
109         }
110 }
111
112 void GUITextInputMenu::drawMenu()
113 {
114         gui::IGUISkin* skin = Environment->getSkin();
115         if (!skin)
116                 return;
117         video::IVideoDriver* driver = Environment->getVideoDriver();
118         
119         video::SColor bgcolor(140,0,0,0);
120         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
121
122         gui::IGUIElement::draw();
123 }
124
125 void GUITextInputMenu::acceptInput()
126 {
127         if(m_dest)
128         {
129                 gui::IGUIElement *e = getElementFromId(256);
130                 if(e != NULL)
131                 {
132                         m_dest->gotText(e->getText());
133                 }
134                 delete m_dest;
135                 m_dest = NULL;
136         }
137 }
138
139 bool GUITextInputMenu::OnEvent(const SEvent& event)
140 {
141         if(event.EventType==EET_KEY_INPUT_EVENT)
142         {
143                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
144                 {
145                         quitMenu();
146                         return true;
147                 }
148                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
149                 {
150                         acceptInput();
151                         quitMenu();
152                         return true;
153                 }
154         }
155         if(event.EventType==EET_GUI_EVENT)
156         {
157                 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
158                                 && isVisible())
159                 {
160                         if(!canTakeFocus(event.GUIEvent.Element))
161                         {
162                                 dstream<<"GUITextInputMenu: Not allowing focus change."
163                                                 <<std::endl;
164                                 // Returning true disables focus change
165                                 return true;
166                         }
167                 }
168                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
169                 {
170                         switch(event.GUIEvent.Caller->getID())
171                         {
172                         case 257:
173                                 acceptInput();
174                                 quitMenu();
175                                 // quitMenu deallocates menu
176                                 return true;
177                         }
178                 }
179                 if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
180                 {
181                         switch(event.GUIEvent.Caller->getID())
182                         {
183                         case 256:
184                                 acceptInput();
185                                 quitMenu();
186                                 // quitMenu deallocates menu
187                                 return true;
188                         }
189                 }
190         }
191
192         return Parent ? Parent->OnEvent(event) : false;
193 }
194