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 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
258 /*~ Imperative, as in "Type in text" */
259 std::string message = fmtgettext("Enter %s:");
261 // single line text input
264 // multi line text input
265 if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
268 // passwords are always single line
269 if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
272 porting::showInputDialog(gettext("OK"), "",
273 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
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 [?])
284 switch ((int)event.TouchInput.touchedCount) {
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) {
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)
306 if (event.TouchInput.Event != ETIE_PRESSED_DOWN)
307 return true; // ignore
308 auto focused = Environment->getFocus();
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);
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)) {
345 bool GUIModalMenu::hasAndroidUIInput()
348 if (m_jni_field_name.empty())
352 if (porting::getInputDialogState() == -1)
355 // no value abort dialog processing
356 if (porting::getInputDialogState() != 0) {
357 m_jni_field_name.clear();