]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/modalMenu.cpp
Add Custom version string
[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 bool GUIModalMenu::preprocessEvent(const SEvent &event)
187 {
188 #ifdef __ANDROID__
189         // clang-format off
190         // display software keyboard when clicking edit boxes
191         if (event.EventType == EET_MOUSE_INPUT_EVENT &&
192                         event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
193                 gui::IGUIElement *hovered =
194                         Environment->getRootGUIElement()->getElementFromPoint(
195                                 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
196                 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
197                         bool retval = hovered->OnEvent(event);
198                         if (retval)
199                                 Environment->setFocus(hovered);
200
201                         std::string field_name = getNameByID(hovered->getID());
202                         // read-only field
203                         if (field_name.empty())
204                                 return retval;
205
206                         m_jni_field_name = field_name;
207                         /*~ Imperative, as in "Enter/type in text".
208                         Don't forget the space. */
209                         std::string message = gettext("Enter ");
210                         std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
211                         if (label.empty())
212                                 label = "text";
213                         message += gettext(label) + ":";
214
215                         // single line text input
216                         int type = 2;
217
218                         // multi line text input
219                         if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
220                                 type = 1;
221
222                         // passwords are always single line
223                         if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
224                                 type = 3;
225
226                         porting::showInputDialog(gettext("OK"), "",
227                                 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
228                         return retval;
229                 }
230         }
231
232         if (event.EventType == EET_TOUCH_INPUT_EVENT) {
233                 SEvent translated;
234                 memset(&translated, 0, sizeof(SEvent));
235                 translated.EventType = EET_MOUSE_INPUT_EVENT;
236                 gui::IGUIElement *root = Environment->getRootGUIElement();
237
238                 if (!root) {
239                         errorstream << "GUIModalMenu::preprocessEvent"
240                                     << " unable to get root element" << std::endl;
241                         return false;
242                 }
243                 gui::IGUIElement *hovered =
244                                 root->getElementFromPoint(core::position2d<s32>(
245                                                 event.TouchInput.X, event.TouchInput.Y));
246
247                 translated.MouseInput.X = event.TouchInput.X;
248                 translated.MouseInput.Y = event.TouchInput.Y;
249                 translated.MouseInput.Control = false;
250
251                 if (event.TouchInput.touchedCount == 1) {
252                         switch (event.TouchInput.Event) {
253                         case ETIE_PRESSED_DOWN:
254                                 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
255                                 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
256                                 translated.MouseInput.ButtonStates = EMBSM_LEFT;
257                                 m_down_pos = m_pointer;
258                                 break;
259                         case ETIE_MOVED:
260                                 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
261                                 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
262                                 translated.MouseInput.ButtonStates = EMBSM_LEFT;
263                                 break;
264                         case ETIE_LEFT_UP:
265                                 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
266                                 translated.MouseInput.ButtonStates = 0;
267                                 hovered = root->getElementFromPoint(m_down_pos);
268                                 // we don't have a valid pointer element use last
269                                 // known pointer pos
270                                 translated.MouseInput.X = m_pointer.X;
271                                 translated.MouseInput.Y = m_pointer.Y;
272
273                                 // reset down pos
274                                 m_down_pos = v2s32(0, 0);
275                                 break;
276                         default:
277                                 break;
278                         }
279                 } else if ((event.TouchInput.touchedCount == 2) &&
280                                 (event.TouchInput.Event == ETIE_PRESSED_DOWN)) {
281                         hovered = root->getElementFromPoint(m_down_pos);
282
283                         translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
284                         translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
285                         translated.MouseInput.X = m_pointer.X;
286                         translated.MouseInput.Y = m_pointer.Y;
287                         if (hovered)
288                                 hovered->OnEvent(translated);
289
290                         translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
291                         translated.MouseInput.ButtonStates = EMBSM_LEFT;
292
293                         if (hovered)
294                                 hovered->OnEvent(translated);
295
296                         return true;
297                 } else {
298                         // ignore unhandled 2 touch events (accidental moving for example)
299                         return true;
300                 }
301
302                 // check if translated event needs to be preprocessed again
303                 if (preprocessEvent(translated))
304                         return true;
305
306                 if (hovered) {
307                         grab();
308                         bool retval = hovered->OnEvent(translated);
309
310                         if (event.TouchInput.Event == ETIE_LEFT_UP)
311                                 // reset pointer
312                                 m_pointer = v2s32(0, 0);
313
314                         drop();
315                         return retval;
316                 }
317         }
318 #endif
319
320         if (event.EventType == EET_MOUSE_INPUT_EVENT) {
321                 s32 x = event.MouseInput.X;
322                 s32 y = event.MouseInput.Y;
323                 gui::IGUIElement *hovered =
324                                 Environment->getRootGUIElement()->getElementFromPoint(
325                                                 core::position2d<s32>(x, y));
326                 if (!isChild(hovered, this)) {
327                         if (DoubleClickDetection(event)) {
328                                 return true;
329                         }
330                 }
331         }
332         return false;
333 }
334
335 #ifdef __ANDROID__
336 bool GUIModalMenu::hasAndroidUIInput()
337 {
338         // no dialog shown
339         if (m_jni_field_name.empty())
340                 return false;
341
342         // still waiting
343         if (porting::getInputDialogState() == -1)
344                 return true;
345
346         // no value abort dialog processing
347         if (porting::getInputDialogState() != 0) {
348                 m_jni_field_name.clear();
349                 return false;
350         }
351
352         return true;
353 }
354 #endif