]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/modalMenu.cpp
Improve a translation 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 #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 // clang-format off
112 bool GUIModalMenu::DoubleClickDetection(const SEvent &event)
113 {
114         /* The following code is for capturing double-clicks of the mouse button
115          * and translating the double-click into an EET_KEY_INPUT_EVENT event
116          * -- which closes the form -- under some circumstances.
117          *
118          * There have been many github issues reporting this as a bug even though it
119          * was an intended feature.  For this reason, remapping the double-click as
120          * an ESC must be explicitly set when creating this class via the
121          * /p remap_dbl_click parameter of the constructor.
122          */
123
124         if (!m_remap_dbl_click)
125                 return false;
126
127         if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
128                 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
129                 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
130
131                 m_doubleclickdetect[1].pos = m_pointer;
132                 m_doubleclickdetect[1].time = porting::getTimeMs();
133         } else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
134                 u64 delta = porting::getDeltaMs(
135                         m_doubleclickdetect[0].time, porting::getTimeMs());
136                 if (delta > 400)
137                         return false;
138
139                 double squaredistance = m_doubleclickdetect[0].pos.
140                         getDistanceFromSQ(m_doubleclickdetect[1].pos);
141
142                 if (squaredistance > (30 * 30)) {
143                         return false;
144                 }
145
146                 SEvent translated{};
147                 // translate doubleclick to escape
148                 translated.EventType            = EET_KEY_INPUT_EVENT;
149                 translated.KeyInput.Key         = KEY_ESCAPE;
150                 translated.KeyInput.Control     = false;
151                 translated.KeyInput.Shift       = false;
152                 translated.KeyInput.PressedDown = true;
153                 translated.KeyInput.Char        = 0;
154                 OnEvent(translated);
155
156                 return true;
157         }
158
159         return false;
160 }
161 // clang-format on
162
163 static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
164 {
165         while (tocheck) {
166                 if (tocheck == parent) {
167                         return true;
168                 }
169                 tocheck = tocheck->getParent();
170         }
171         return false;
172 }
173
174 #ifdef HAVE_TOUCHSCREENGUI
175
176 bool GUIModalMenu::simulateMouseEvent(
177                 gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event)
178 {
179         SEvent mouse_event{}; // value-initialized, not unitialized
180         mouse_event.EventType = EET_MOUSE_INPUT_EVENT;
181         mouse_event.MouseInput.X = m_pointer.X;
182         mouse_event.MouseInput.Y = m_pointer.Y;
183         switch (touch_event) {
184         case ETIE_PRESSED_DOWN:
185                 mouse_event.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
186                 mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
187                 break;
188         case ETIE_MOVED:
189                 mouse_event.MouseInput.Event = EMIE_MOUSE_MOVED;
190                 mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
191                 break;
192         case ETIE_LEFT_UP:
193                 mouse_event.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
194                 mouse_event.MouseInput.ButtonStates = 0;
195                 break;
196         default:
197                 return false;
198         }
199         if (preprocessEvent(mouse_event))
200                 return true;
201         if (!target)
202                 return false;
203         return target->OnEvent(mouse_event);
204 }
205
206 void GUIModalMenu::enter(gui::IGUIElement *hovered)
207 {
208         if (!hovered)
209                 return;
210         sanity_check(!m_hovered);
211         m_hovered.grab(hovered);
212         SEvent gui_event{};
213         gui_event.EventType = EET_GUI_EVENT;
214         gui_event.GUIEvent.Caller = m_hovered.get();
215         gui_event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
216         gui_event.GUIEvent.Element = gui_event.GUIEvent.Caller;
217         m_hovered->OnEvent(gui_event);
218 }
219
220 void GUIModalMenu::leave()
221 {
222         if (!m_hovered)
223                 return;
224         SEvent gui_event{};
225         gui_event.EventType = EET_GUI_EVENT;
226         gui_event.GUIEvent.Caller = m_hovered.get();
227         gui_event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
228         m_hovered->OnEvent(gui_event);
229         m_hovered.reset();
230 }
231
232 #endif
233
234 bool GUIModalMenu::preprocessEvent(const SEvent &event)
235 {
236 #ifdef __ANDROID__
237         // clang-format off
238         // display software keyboard when clicking edit boxes
239         if (event.EventType == EET_MOUSE_INPUT_EVENT &&
240                         event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
241                 gui::IGUIElement *hovered =
242                         Environment->getRootGUIElement()->getElementFromPoint(
243                                 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
244                 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
245                         bool retval = hovered->OnEvent(event);
246                         if (retval)
247                                 Environment->setFocus(hovered);
248
249                         std::string field_name = getNameByID(hovered->getID());
250                         // read-only field
251                         if (field_name.empty())
252                                 return retval;
253
254                         m_jni_field_name = field_name;
255                         std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
256                         if (label.empty())
257                                 label = "text";
258                         /*~ Imperative, as in "Type in text" */
259                         std::string message = fmtgettext("Enter %s:");
260
261                         // single line text input
262                         int type = 2;
263
264                         // multi line text input
265                         if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
266                                 type = 1;
267
268                         // passwords are always single line
269                         if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
270                                 type = 3;
271
272                         porting::showInputDialog(gettext("OK"), "",
273                                 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
274                         return retval;
275                 }
276         }
277 #endif
278
279 #ifdef HAVE_TOUCHSCREENGUI
280         if (event.EventType == EET_TOUCH_INPUT_EVENT) {
281                 irr_ptr<GUIModalMenu> holder;
282                 holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
283
284                 switch ((int)event.TouchInput.touchedCount) {
285                 case 1: {
286                         if (event.TouchInput.Event == ETIE_PRESSED_DOWN || event.TouchInput.Event == ETIE_MOVED)
287                                 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
288                         if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
289                                 m_down_pos = m_pointer;
290                         gui::IGUIElement *hovered = Environment->getRootGUIElement()->getElementFromPoint(core::position2d<s32>(m_pointer));
291                         if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
292                                 Environment->setFocus(hovered);
293                         if (m_hovered != hovered) {
294                                 leave();
295                                 enter(hovered);
296                         }
297                         gui::IGUIElement *focused = Environment->getFocus();
298                         bool ret = simulateMouseEvent(focused, event.TouchInput.Event);
299                         if (!ret && m_hovered != focused)
300                                 ret = simulateMouseEvent(m_hovered.get(), event.TouchInput.Event);
301                         if (event.TouchInput.Event == ETIE_LEFT_UP)
302                                 leave();
303                         return ret;
304                 }
305                 case 2: {
306                         if (event.TouchInput.Event != ETIE_PRESSED_DOWN)
307                                 return true; // ignore
308                         auto focused = Environment->getFocus();
309                         if (!focused)
310                                 return true;
311                         SEvent rclick_event{};
312                         rclick_event.EventType = EET_MOUSE_INPUT_EVENT;
313                         rclick_event.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
314                         rclick_event.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
315                         rclick_event.MouseInput.X = m_pointer.X;
316                         rclick_event.MouseInput.Y = m_pointer.Y;
317                         focused->OnEvent(rclick_event);
318                         rclick_event.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
319                         rclick_event.MouseInput.ButtonStates = EMBSM_LEFT;
320                         focused->OnEvent(rclick_event);
321                         return true;
322                 }
323                 default: // ignored
324                         return true;
325                 }
326         }
327 #endif
328
329         if (event.EventType == EET_MOUSE_INPUT_EVENT) {
330                 s32 x = event.MouseInput.X;
331                 s32 y = event.MouseInput.Y;
332                 gui::IGUIElement *hovered =
333                                 Environment->getRootGUIElement()->getElementFromPoint(
334                                                 core::position2d<s32>(x, y));
335                 if (!isChild(hovered, this)) {
336                         if (DoubleClickDetection(event)) {
337                                 return true;
338                         }
339                 }
340         }
341         return false;
342 }
343
344 #ifdef __ANDROID__
345 bool GUIModalMenu::hasAndroidUIInput()
346 {
347         // no dialog shown
348         if (m_jni_field_name.empty())
349                 return false;
350
351         // still waiting
352         if (porting::getInputDialogState() == -1)
353                 return true;
354
355         // no value abort dialog processing
356         if (porting::getInputDialogState() != 0) {
357                 m_jni_field_name.clear();
358                 return false;
359         }
360
361         return true;
362 }
363 #endif