]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/modalMenu.cpp
TouchScreen Control: fix some bugs, cleanup
[dragonfireclient.git] / src / gui / modalMenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 stujones11, Stuart Jones <stujones111@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <cstdlib>
22 #include "modalMenu.h"
23 #include "gettext.h"
24 #include "porting.h"
25 #include "settings.h"
26
27 #ifdef HAVE_TOUCHSCREENGUI
28 #include "touchscreengui.h"
29 #endif
30
31 GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment *env, gui::IGUIElement *parent, s32 id,
32                 IMenuManager *menumgr) :
33                 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
34                                 core::rect<s32>(0, 0, 100, 100)),
35 #ifdef __ANDROID__
36                 m_jni_field_name(""),
37 #endif
38                 m_menumgr(menumgr)
39 {
40         m_gui_scale = g_settings->getFloat("gui_scaling");
41 #ifdef __ANDROID__
42         float d = porting::getDisplayDensity();
43         m_gui_scale *= 1.1 - 0.3 * d + 0.2 * d * d;
44 #endif
45         setVisible(true);
46         Environment->setFocus(this);
47         m_menumgr->createdMenu(this);
48 }
49 // clang-format on
50
51 GUIModalMenu::~GUIModalMenu()
52 {
53         m_menumgr->deletingMenu(this);
54 }
55
56 void GUIModalMenu::allowFocusRemoval(bool allow)
57 {
58         m_allow_focus_removal = allow;
59 }
60
61 bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e)
62 {
63         return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
64 }
65
66 void GUIModalMenu::draw()
67 {
68         if (!IsVisible)
69                 return;
70
71         video::IVideoDriver *driver = Environment->getVideoDriver();
72         v2u32 screensize = driver->getScreenSize();
73         if (screensize != m_screensize_old) {
74                 m_screensize_old = screensize;
75                 regenerateGui(screensize);
76         }
77
78         drawMenu();
79 }
80
81 /*
82         This should be called when the menu wants to quit.
83
84         WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
85         immediately if you call this from the menu itself.
86
87         (More precisely, this decrements the reference count.)
88 */
89 void GUIModalMenu::quitMenu()
90 {
91         allowFocusRemoval(true);
92         // This removes Environment's grab on us
93         Environment->removeFocus(this);
94         m_menumgr->deletingMenu(this);
95         this->remove();
96 #ifdef HAVE_TOUCHSCREENGUI
97         if (g_touchscreengui && m_touchscreen_visible)
98                 g_touchscreengui->show();
99 #endif
100 }
101
102 void GUIModalMenu::removeChildren()
103 {
104         const core::list<gui::IGUIElement *> &children = getChildren();
105         core::list<gui::IGUIElement *> children_copy;
106         for (gui::IGUIElement *i : children) {
107                 children_copy.push_back(i);
108         }
109
110         for (gui::IGUIElement *i : children_copy) {
111                 i->remove();
112         }
113 }
114
115 bool GUIModalMenu::preprocessEvent(const SEvent &event)
116 {
117 #ifdef __ANDROID__
118         // clang-format off
119         // display software keyboard when clicking edit boxes
120         if (event.EventType == EET_MOUSE_INPUT_EVENT &&
121                         event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
122                 gui::IGUIElement *hovered =
123                         Environment->getRootGUIElement()->getElementFromPoint(
124                                 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
125                 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
126                         bool retval = hovered->OnEvent(event);
127                         if (retval)
128                                 Environment->setFocus(hovered);
129
130                         std::string field_name = getNameByID(hovered->getID());
131                         // read-only field
132                         if (field_name.empty())
133                                 return retval;
134
135                         m_jni_field_name = field_name;
136                         /*~ Imperative, as in "Enter/type in text".
137                         Don't forget the space. */
138                         std::string message = gettext("Enter ");
139                         std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
140                         if (label.empty())
141                                 label = "text";
142                         message += gettext(label) + ":";
143
144                         // single line text input
145                         int type = 2;
146
147                         // multi line text input
148                         if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
149                                 type = 1;
150
151                         // passwords are always single line
152                         if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
153                                 type = 3;
154
155                         porting::showInputDialog(gettext("OK"), "",
156                                 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
157                         return retval;
158                 }
159         }
160
161         if (event.EventType == EET_TOUCH_INPUT_EVENT) {
162                 SEvent translated;
163                 memset(&translated, 0, sizeof(SEvent));
164                 translated.EventType = EET_MOUSE_INPUT_EVENT;
165                 gui::IGUIElement *root = Environment->getRootGUIElement();
166
167                 if (!root) {
168                         errorstream << "GUIModalMenu::preprocessEvent"
169                                     << " unable to get root element" << std::endl;
170                         return false;
171                 }
172                 gui::IGUIElement *hovered =
173                                 root->getElementFromPoint(core::position2d<s32>(
174                                                 event.TouchInput.X, event.TouchInput.Y));
175
176                 translated.MouseInput.X = event.TouchInput.X;
177                 translated.MouseInput.Y = event.TouchInput.Y;
178                 translated.MouseInput.Control = false;
179
180                 if (event.TouchInput.touchedCount == 1) {
181                         switch (event.TouchInput.Event) {
182                         case ETIE_PRESSED_DOWN:
183                                 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
184                                 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
185                                 translated.MouseInput.ButtonStates = EMBSM_LEFT;
186                                 m_down_pos = m_pointer;
187                                 break;
188                         case ETIE_MOVED:
189                                 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
190                                 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
191                                 translated.MouseInput.ButtonStates = EMBSM_LEFT;
192                                 break;
193                         case ETIE_LEFT_UP:
194                                 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
195                                 translated.MouseInput.ButtonStates = 0;
196                                 hovered = root->getElementFromPoint(m_down_pos);
197                                 // we don't have a valid pointer element use last
198                                 // known pointer pos
199                                 translated.MouseInput.X = m_pointer.X;
200                                 translated.MouseInput.Y = m_pointer.Y;
201
202                                 // reset down pos
203                                 m_down_pos = v2s32(0, 0);
204                                 break;
205                         default:
206                                 break;
207                         }
208                 } else if ((event.TouchInput.touchedCount == 2) &&
209                                 (event.TouchInput.Event == ETIE_PRESSED_DOWN)) {
210                         hovered = root->getElementFromPoint(m_down_pos);
211
212                         translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
213                         translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
214                         translated.MouseInput.X = m_pointer.X;
215                         translated.MouseInput.Y = m_pointer.Y;
216                         if (hovered)
217                                 hovered->OnEvent(translated);
218
219                         translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
220                         translated.MouseInput.ButtonStates = EMBSM_LEFT;
221
222                         if (hovered)
223                                 hovered->OnEvent(translated);
224
225                         return true;
226                 } else {
227                         // ignore unhandled 2 touch events (accidental moving for example)
228                         return true;
229                 }
230
231                 // check if translated event needs to be preprocessed again
232                 if (preprocessEvent(translated))
233                         return true;
234
235                 if (hovered) {
236                         grab();
237                         bool retval = hovered->OnEvent(translated);
238
239                         if (event.TouchInput.Event == ETIE_LEFT_UP)
240                                 // reset pointer
241                                 m_pointer = v2s32(0, 0);
242
243                         drop();
244                         return retval;
245                 }
246         }
247 #endif
248         return false;
249 }
250
251 #ifdef __ANDROID__
252 bool GUIModalMenu::hasAndroidUIInput()
253 {
254         // no dialog shown
255         if (m_jni_field_name.empty())
256                 return false;
257
258         // still waiting
259         if (porting::getInputDialogState() == -1)
260                 return true;
261
262         // no value abort dialog processing
263         if (porting::getInputDialogState() != 0) {
264                 m_jni_field_name.clear();
265                 return false;
266         }
267
268         return true;
269 }
270 #endif