3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 stujones11, Stuart Jones <stujones111@gmail.com>
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.
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.
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.
22 #include "modalMenu.h"
27 #ifdef HAVE_TOUCHSCREENGUI
28 #include "touchscreengui.h"
29 #include "client/renderingengine.h"
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)),
41 m_remap_dbl_click(remap_dbl_click)
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;
49 Environment->setFocus(this);
50 m_menumgr->createdMenu(this);
52 m_doubleclickdetect[0].time = 0;
53 m_doubleclickdetect[1].time = 0;
55 m_doubleclickdetect[0].pos = v2s32(0, 0);
56 m_doubleclickdetect[1].pos = v2s32(0, 0);
60 GUIModalMenu::~GUIModalMenu()
62 m_menumgr->deletingMenu(this);
65 void GUIModalMenu::allowFocusRemoval(bool allow)
67 m_allow_focus_removal = allow;
70 bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e)
72 return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
75 void GUIModalMenu::draw()
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);
91 This should be called when the menu wants to quit.
93 WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
94 immediately if you call this from the menu itself.
96 (More precisely, this decrements the reference count.)
98 void GUIModalMenu::quitMenu()
100 allowFocusRemoval(true);
101 // This removes Environment's grab on us
102 Environment->removeFocus(this);
103 m_menumgr->deletingMenu(this);
105 #ifdef HAVE_TOUCHSCREENGUI
106 if (g_touchscreengui && m_touchscreen_visible)
107 g_touchscreengui->show();
112 bool GUIModalMenu::DoubleClickDetection(const SEvent &event)
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.
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.
124 if (!m_remap_dbl_click)
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;
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());
139 double squaredistance = m_doubleclickdetect[0].pos.
140 getDistanceFromSQ(m_doubleclickdetect[1].pos);
142 if (squaredistance > (30 * 30)) {
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;
163 static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
166 if (tocheck == parent) {
169 tocheck = tocheck->getParent();
174 #ifdef HAVE_TOUCHSCREENGUI
176 bool GUIModalMenu::simulateMouseEvent(
177 gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event)
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;
189 mouse_event.MouseInput.Event = EMIE_MOUSE_MOVED;
190 mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
193 mouse_event.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
194 mouse_event.MouseInput.ButtonStates = 0;
199 if (preprocessEvent(mouse_event))
203 return target->OnEvent(mouse_event);
206 void GUIModalMenu::enter(gui::IGUIElement *hovered)
210 sanity_check(!m_hovered);
211 m_hovered.grab(hovered);
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);
220 void GUIModalMenu::leave()
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);
234 bool GUIModalMenu::preprocessEvent(const SEvent &event)
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);
247 Environment->setFocus(hovered);
249 std::string field_name = getNameByID(hovered->getID());
251 if (field_name.empty())
254 m_jni_field_name = field_name;
255 /*~ Imperative, as in "Enter/type in text".
256 Don't forget the space. */
257 std::string message = gettext("Enter ");
258 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
261 message += strgettext(label) + ":";
263 // single line text input
266 // multi line text input
267 if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
270 // passwords are always single line
271 if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
274 porting::showInputDialog(gettext("OK"), "",
275 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
281 #ifdef HAVE_TOUCHSCREENGUI
282 if (event.EventType == EET_TOUCH_INPUT_EVENT) {
283 irr_ptr<GUIModalMenu> holder;
284 holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
286 switch ((int)event.TouchInput.touchedCount) {
288 if (event.TouchInput.Event == ETIE_PRESSED_DOWN || event.TouchInput.Event == ETIE_MOVED)
289 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
290 if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
291 m_down_pos = m_pointer;
292 gui::IGUIElement *hovered = Environment->getRootGUIElement()->getElementFromPoint(core::position2d<s32>(m_pointer));
293 if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
294 Environment->setFocus(hovered);
295 if (m_hovered != hovered) {
299 gui::IGUIElement *focused = Environment->getFocus();
300 bool ret = simulateMouseEvent(focused, event.TouchInput.Event);
301 if (!ret && m_hovered != focused)
302 ret = simulateMouseEvent(m_hovered.get(), event.TouchInput.Event);
303 if (event.TouchInput.Event == ETIE_LEFT_UP)
308 if (event.TouchInput.Event != ETIE_PRESSED_DOWN)
309 return true; // ignore
310 auto focused = Environment->getFocus();
313 SEvent rclick_event{};
314 rclick_event.EventType = EET_MOUSE_INPUT_EVENT;
315 rclick_event.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
316 rclick_event.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
317 rclick_event.MouseInput.X = m_pointer.X;
318 rclick_event.MouseInput.Y = m_pointer.Y;
319 focused->OnEvent(rclick_event);
320 rclick_event.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
321 rclick_event.MouseInput.ButtonStates = EMBSM_LEFT;
322 focused->OnEvent(rclick_event);
331 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
332 s32 x = event.MouseInput.X;
333 s32 y = event.MouseInput.Y;
334 gui::IGUIElement *hovered =
335 Environment->getRootGUIElement()->getElementFromPoint(
336 core::position2d<s32>(x, y));
337 if (!isChild(hovered, this)) {
338 if (DoubleClickDetection(event)) {
347 bool GUIModalMenu::hasAndroidUIInput()
350 if (m_jni_field_name.empty())
354 if (porting::getInputDialogState() == -1)
357 // no value abort dialog processing
358 if (porting::getInputDialogState() != 0) {
359 m_jni_field_name.clear();