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