]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/modalMenu.cpp
Fix revoke debug privs not reliably turn off stuff (#11409)
[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 // clang-format off
32 GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent,
33         s32 id, IMenuManager *menumgr, bool remap_dbl_click) :
34                 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
35                                 core::rect<s32>(0, 0, 100, 100)),
36 #ifdef __ANDROID__
37                 m_jni_field_name(""),
38 #endif
39                 m_menumgr(menumgr),
40                 m_remap_dbl_click(remap_dbl_click)
41 {
42         m_gui_scale = g_settings->getFloat("gui_scaling");
43 #ifdef __ANDROID__
44         float d = porting::getDisplayDensity();
45         m_gui_scale *= 1.1 - 0.3 * d + 0.2 * d * d;
46 #endif
47         setVisible(true);
48         Environment->setFocus(this);
49         m_menumgr->createdMenu(this);
50
51         m_doubleclickdetect[0].time = 0;
52         m_doubleclickdetect[1].time = 0;
53
54         m_doubleclickdetect[0].pos = v2s32(0, 0);
55         m_doubleclickdetect[1].pos = v2s32(0, 0);
56 }
57 // clang-format on
58
59 GUIModalMenu::~GUIModalMenu()
60 {
61         m_menumgr->deletingMenu(this);
62 }
63
64 void GUIModalMenu::allowFocusRemoval(bool allow)
65 {
66         m_allow_focus_removal = allow;
67 }
68
69 bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e)
70 {
71         return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
72 }
73
74 void GUIModalMenu::draw()
75 {
76         if (!IsVisible)
77                 return;
78
79         video::IVideoDriver *driver = Environment->getVideoDriver();
80         v2u32 screensize = driver->getScreenSize();
81         if (screensize != m_screensize_old) {
82                 m_screensize_old = screensize;
83                 regenerateGui(screensize);
84         }
85
86         drawMenu();
87 }
88
89 /*
90         This should be called when the menu wants to quit.
91
92         WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
93         immediately if you call this from the menu itself.
94
95         (More precisely, this decrements the reference count.)
96 */
97 void GUIModalMenu::quitMenu()
98 {
99         allowFocusRemoval(true);
100         // This removes Environment's grab on us
101         Environment->removeFocus(this);
102         m_menumgr->deletingMenu(this);
103         this->remove();
104 #ifdef HAVE_TOUCHSCREENGUI
105         if (g_touchscreengui && m_touchscreen_visible)
106                 g_touchscreengui->show();
107 #endif
108 }
109
110 void GUIModalMenu::removeChildren()
111 {
112         const core::list<gui::IGUIElement *> &children = getChildren();
113         core::list<gui::IGUIElement *> children_copy;
114         for (gui::IGUIElement *i : children) {
115                 children_copy.push_back(i);
116         }
117
118         for (gui::IGUIElement *i : children_copy) {
119                 i->remove();
120         }
121 }
122
123 // clang-format off
124 bool GUIModalMenu::DoubleClickDetection(const SEvent &event)
125 {
126         /* The following code is for capturing double-clicks of the mouse button
127          * and translating the double-click into an EET_KEY_INPUT_EVENT event
128          * -- which closes the form -- under some circumstances.
129          *
130          * There have been many github issues reporting this as a bug even though it
131          * was an intended feature.  For this reason, remapping the double-click as
132          * an ESC must be explicitly set when creating this class via the
133          * /p remap_dbl_click parameter of the constructor.
134          */
135
136         if (!m_remap_dbl_click)
137                 return false;
138
139         if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
140                 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
141                 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
142
143                 m_doubleclickdetect[1].pos = m_pointer;
144                 m_doubleclickdetect[1].time = porting::getTimeMs();
145         } else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
146                 u64 delta = porting::getDeltaMs(
147                         m_doubleclickdetect[0].time, porting::getTimeMs());
148                 if (delta > 400)
149                         return false;
150
151                 double squaredistance = m_doubleclickdetect[0].pos.
152                         getDistanceFromSQ(m_doubleclickdetect[1].pos);
153
154                 if (squaredistance > (30 * 30)) {
155                         return false;
156                 }
157
158                 SEvent translated{};
159                 // translate doubleclick to escape
160                 translated.EventType            = EET_KEY_INPUT_EVENT;
161                 translated.KeyInput.Key         = KEY_ESCAPE;
162                 translated.KeyInput.Control     = false;
163                 translated.KeyInput.Shift       = false;
164                 translated.KeyInput.PressedDown = true;
165                 translated.KeyInput.Char        = 0;
166                 OnEvent(translated);
167
168                 return true;
169         }
170
171         return false;
172 }
173 // clang-format on
174
175 static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
176 {
177         while (tocheck) {
178                 if (tocheck == parent) {
179                         return true;
180                 }
181                 tocheck = tocheck->getParent();
182         }
183         return false;
184 }
185
186 #ifdef __ANDROID__
187
188 bool GUIModalMenu::simulateMouseEvent(
189                 gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event)
190 {
191         SEvent mouse_event{}; // value-initialized, not unitialized
192         mouse_event.EventType = EET_MOUSE_INPUT_EVENT;
193         mouse_event.MouseInput.X = m_pointer.X;
194         mouse_event.MouseInput.Y = m_pointer.Y;
195         switch (touch_event) {
196         case ETIE_PRESSED_DOWN:
197                 mouse_event.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
198                 mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
199                 break;
200         case ETIE_MOVED:
201                 mouse_event.MouseInput.Event = EMIE_MOUSE_MOVED;
202                 mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
203                 break;
204         case ETIE_LEFT_UP:
205                 mouse_event.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
206                 mouse_event.MouseInput.ButtonStates = 0;
207                 break;
208         default:
209                 return false;
210         }
211         if (preprocessEvent(mouse_event))
212                 return true;
213         if (!target)
214                 return false;
215         return target->OnEvent(mouse_event);
216 }
217
218 void GUIModalMenu::enter(gui::IGUIElement *hovered)
219 {
220         sanity_check(!m_hovered);
221         m_hovered.grab(hovered);
222         SEvent gui_event{};
223         gui_event.EventType = EET_GUI_EVENT;
224         gui_event.GUIEvent.Caller = m_hovered.get();
225         gui_event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
226         gui_event.GUIEvent.Element = gui_event.GUIEvent.Caller;
227         m_hovered->OnEvent(gui_event);
228 }
229
230 void GUIModalMenu::leave()
231 {
232         if (!m_hovered)
233                 return;
234         SEvent gui_event{};
235         gui_event.EventType = EET_GUI_EVENT;
236         gui_event.GUIEvent.Caller = m_hovered.get();
237         gui_event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
238         m_hovered->OnEvent(gui_event);
239         m_hovered.reset();
240 }
241
242 #endif
243
244 bool GUIModalMenu::preprocessEvent(const SEvent &event)
245 {
246 #ifdef __ANDROID__
247         // clang-format off
248         // display software keyboard when clicking edit boxes
249         if (event.EventType == EET_MOUSE_INPUT_EVENT &&
250                         event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
251                 gui::IGUIElement *hovered =
252                         Environment->getRootGUIElement()->getElementFromPoint(
253                                 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
254                 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
255                         bool retval = hovered->OnEvent(event);
256                         if (retval)
257                                 Environment->setFocus(hovered);
258
259                         std::string field_name = getNameByID(hovered->getID());
260                         // read-only field
261                         if (field_name.empty())
262                                 return retval;
263
264                         m_jni_field_name = field_name;
265                         /*~ Imperative, as in "Enter/type in text".
266                         Don't forget the space. */
267                         std::string message = gettext("Enter ");
268                         std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
269                         if (label.empty())
270                                 label = "text";
271                         message += gettext(label) + ":";
272
273                         // single line text input
274                         int type = 2;
275
276                         // multi line text input
277                         if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
278                                 type = 1;
279
280                         // passwords are always single line
281                         if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
282                                 type = 3;
283
284                         porting::showInputDialog(gettext("OK"), "",
285                                 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
286                         return retval;
287                 }
288         }
289
290         if (event.EventType == EET_TOUCH_INPUT_EVENT) {
291                 irr_ptr<GUIModalMenu> holder;
292                 holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
293
294                 switch ((int)event.TouchInput.touchedCount) {
295                 case 1: {
296                         if (event.TouchInput.Event == ETIE_PRESSED_DOWN || event.TouchInput.Event == ETIE_MOVED)
297                                 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
298                         if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
299                                 m_down_pos = m_pointer;
300                         gui::IGUIElement *hovered = Environment->getRootGUIElement()->getElementFromPoint(core::position2d<s32>(m_pointer));
301                         if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
302                                 Environment->setFocus(hovered);
303                         if (m_hovered != hovered) {
304                                 leave();
305                                 enter(hovered);
306                         }
307                         gui::IGUIElement *focused = Environment->getFocus();
308                         bool ret = simulateMouseEvent(focused, event.TouchInput.Event);
309                         if (!ret && m_hovered != focused)
310                                 ret = simulateMouseEvent(m_hovered.get(), event.TouchInput.Event);
311                         if (event.TouchInput.Event == ETIE_LEFT_UP)
312                                 leave();
313                         return ret;
314                 }
315                 case 2: {
316                         if (event.TouchInput.Event != ETIE_PRESSED_DOWN)
317                                 return true; // ignore
318                         auto focused = Environment->getFocus();
319                         if (!focused)
320                                 return true;
321                         SEvent rclick_event{};
322                         rclick_event.EventType = EET_MOUSE_INPUT_EVENT;
323                         rclick_event.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
324                         rclick_event.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
325                         rclick_event.MouseInput.X = m_pointer.X;
326                         rclick_event.MouseInput.Y = m_pointer.Y;
327                         focused->OnEvent(rclick_event);
328                         rclick_event.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
329                         rclick_event.MouseInput.ButtonStates = EMBSM_LEFT;
330                         focused->OnEvent(rclick_event);
331                         return true;
332                 }
333                 default: // ignored
334                         return true;
335                 }
336         }
337 #endif
338
339         if (event.EventType == EET_MOUSE_INPUT_EVENT) {
340                 s32 x = event.MouseInput.X;
341                 s32 y = event.MouseInput.Y;
342                 gui::IGUIElement *hovered =
343                                 Environment->getRootGUIElement()->getElementFromPoint(
344                                                 core::position2d<s32>(x, y));
345                 if (!isChild(hovered, this)) {
346                         if (DoubleClickDetection(event)) {
347                                 return true;
348                         }
349                 }
350         }
351         return false;
352 }
353
354 #ifdef __ANDROID__
355 bool GUIModalMenu::hasAndroidUIInput()
356 {
357         // no dialog shown
358         if (m_jni_field_name.empty())
359                 return false;
360
361         // still waiting
362         if (porting::getInputDialogState() == -1)
363                 return true;
364
365         // no value abort dialog processing
366         if (porting::getInputDialogState() != 0) {
367                 m_jni_field_name.clear();
368                 return false;
369         }
370
371         return true;
372 }
373 #endif