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