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"
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)),
40 m_remap_dbl_click(remap_dbl_click)
42 m_gui_scale = g_settings->getFloat("gui_scaling");
44 float d = porting::getDisplayDensity();
45 m_gui_scale *= 1.1 - 0.3 * d + 0.2 * d * d;
48 Environment->setFocus(this);
49 m_menumgr->createdMenu(this);
51 m_doubleclickdetect[0].time = 0;
52 m_doubleclickdetect[1].time = 0;
54 m_doubleclickdetect[0].pos = v2s32(0, 0);
55 m_doubleclickdetect[1].pos = v2s32(0, 0);
59 GUIModalMenu::~GUIModalMenu()
61 m_menumgr->deletingMenu(this);
64 void GUIModalMenu::allowFocusRemoval(bool allow)
66 m_allow_focus_removal = allow;
69 bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e)
71 return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
74 void GUIModalMenu::draw()
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);
90 This should be called when the menu wants to quit.
92 WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
93 immediately if you call this from the menu itself.
95 (More precisely, this decrements the reference count.)
97 void GUIModalMenu::quitMenu()
99 allowFocusRemoval(true);
100 // This removes Environment's grab on us
101 Environment->removeFocus(this);
102 m_menumgr->deletingMenu(this);
104 #ifdef HAVE_TOUCHSCREENGUI
105 if (g_touchscreengui && m_touchscreen_visible)
106 g_touchscreengui->show();
110 void GUIModalMenu::removeChildren()
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);
118 for (gui::IGUIElement *i : children_copy) {
124 bool GUIModalMenu::DoubleClickDetection(const SEvent &event)
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.
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.
136 if (!m_remap_dbl_click)
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;
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());
151 double squaredistance = m_doubleclickdetect[0].pos.
152 getDistanceFromSQ(m_doubleclickdetect[1].pos);
154 if (squaredistance > (30 * 30)) {
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;
175 static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
178 if (tocheck == parent) {
181 tocheck = tocheck->getParent();
188 bool GUIModalMenu::simulateMouseEvent(
189 gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event)
191 SEvent mouse_event{}; // value-initialized, not unitialized
192 mouse_event.EventType = EET_MOUSE_INPUT_EVENT;
193 mouse_event.MouseInput.X = m_pointer.X;
194 mouse_event.MouseInput.Y = m_pointer.Y;
195 switch (touch_event) {
196 case ETIE_PRESSED_DOWN:
197 mouse_event.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
198 mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
201 mouse_event.MouseInput.Event = EMIE_MOUSE_MOVED;
202 mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
205 mouse_event.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
206 mouse_event.MouseInput.ButtonStates = 0;
211 if (preprocessEvent(mouse_event))
215 return target->OnEvent(mouse_event);
218 void GUIModalMenu::enter(gui::IGUIElement *hovered)
220 sanity_check(!m_hovered);
221 m_hovered.grab(hovered);
223 gui_event.EventType = EET_GUI_EVENT;
224 gui_event.GUIEvent.Caller = m_hovered.get();
225 gui_event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
226 gui_event.GUIEvent.Element = gui_event.GUIEvent.Caller;
227 m_hovered->OnEvent(gui_event);
230 void GUIModalMenu::leave()
235 gui_event.EventType = EET_GUI_EVENT;
236 gui_event.GUIEvent.Caller = m_hovered.get();
237 gui_event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
238 m_hovered->OnEvent(gui_event);
244 bool GUIModalMenu::preprocessEvent(const SEvent &event)
248 // display software keyboard when clicking edit boxes
249 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
250 event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
251 gui::IGUIElement *hovered =
252 Environment->getRootGUIElement()->getElementFromPoint(
253 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
254 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
255 bool retval = hovered->OnEvent(event);
257 Environment->setFocus(hovered);
259 std::string field_name = getNameByID(hovered->getID());
261 if (field_name.empty())
264 m_jni_field_name = field_name;
265 /*~ Imperative, as in "Enter/type in text".
266 Don't forget the space. */
267 std::string message = gettext("Enter ");
268 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
271 message += gettext(label) + ":";
273 // single line text input
276 // multi line text input
277 if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
280 // passwords are always single line
281 if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
284 porting::showInputDialog(gettext("OK"), "",
285 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
290 if (event.EventType == EET_TOUCH_INPUT_EVENT) {
291 irr_ptr<GUIModalMenu> holder;
292 holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
294 switch ((int)event.TouchInput.touchedCount) {
296 if (event.TouchInput.Event == ETIE_PRESSED_DOWN || event.TouchInput.Event == ETIE_MOVED)
297 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
298 if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
299 m_down_pos = m_pointer;
300 gui::IGUIElement *hovered = Environment->getRootGUIElement()->getElementFromPoint(core::position2d<s32>(m_pointer));
301 if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
302 Environment->setFocus(hovered);
303 if (m_hovered != hovered) {
307 gui::IGUIElement *focused = Environment->getFocus();
308 bool ret = simulateMouseEvent(focused, event.TouchInput.Event);
309 if (!ret && m_hovered != focused)
310 ret = simulateMouseEvent(m_hovered.get(), event.TouchInput.Event);
311 if (event.TouchInput.Event == ETIE_LEFT_UP)
316 if (event.TouchInput.Event != ETIE_PRESSED_DOWN)
317 return true; // ignore
318 auto focused = Environment->getFocus();
321 SEvent rclick_event{};
322 rclick_event.EventType = EET_MOUSE_INPUT_EVENT;
323 rclick_event.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
324 rclick_event.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
325 rclick_event.MouseInput.X = m_pointer.X;
326 rclick_event.MouseInput.Y = m_pointer.Y;
327 focused->OnEvent(rclick_event);
328 rclick_event.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
329 rclick_event.MouseInput.ButtonStates = EMBSM_LEFT;
330 focused->OnEvent(rclick_event);
339 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
340 s32 x = event.MouseInput.X;
341 s32 y = event.MouseInput.Y;
342 gui::IGUIElement *hovered =
343 Environment->getRootGUIElement()->getElementFromPoint(
344 core::position2d<s32>(x, y));
345 if (!isChild(hovered, this)) {
346 if (DoubleClickDetection(event)) {
355 bool GUIModalMenu::hasAndroidUIInput()
358 if (m_jni_field_name.empty())
362 if (porting::getInputDialogState() == -1)
365 // no value abort dialog processing
366 if (porting::getInputDialogState() != 0) {
367 m_jni_field_name.clear();