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