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();
111 void GUIModalMenu::removeChildren()
113 const core::list<gui::IGUIElement *> &children = getChildren();
114 core::list<gui::IGUIElement *> children_copy;
115 for (gui::IGUIElement *i : children) {
116 children_copy.push_back(i);
119 for (gui::IGUIElement *i : children_copy) {
125 bool GUIModalMenu::DoubleClickDetection(const SEvent &event)
127 /* The following code is for capturing double-clicks of the mouse button
128 * and translating the double-click into an EET_KEY_INPUT_EVENT event
129 * -- which closes the form -- under some circumstances.
131 * There have been many github issues reporting this as a bug even though it
132 * was an intended feature. For this reason, remapping the double-click as
133 * an ESC must be explicitly set when creating this class via the
134 * /p remap_dbl_click parameter of the constructor.
137 if (!m_remap_dbl_click)
140 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
141 m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
142 m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
144 m_doubleclickdetect[1].pos = m_pointer;
145 m_doubleclickdetect[1].time = porting::getTimeMs();
146 } else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
147 u64 delta = porting::getDeltaMs(
148 m_doubleclickdetect[0].time, porting::getTimeMs());
152 double squaredistance = m_doubleclickdetect[0].pos.
153 getDistanceFromSQ(m_doubleclickdetect[1].pos);
155 if (squaredistance > (30 * 30)) {
160 // translate doubleclick to escape
161 translated.EventType = EET_KEY_INPUT_EVENT;
162 translated.KeyInput.Key = KEY_ESCAPE;
163 translated.KeyInput.Control = false;
164 translated.KeyInput.Shift = false;
165 translated.KeyInput.PressedDown = true;
166 translated.KeyInput.Char = 0;
176 static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
179 if (tocheck == parent) {
182 tocheck = tocheck->getParent();
187 #ifdef HAVE_TOUCHSCREENGUI
189 bool GUIModalMenu::simulateMouseEvent(
190 gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event)
192 SEvent mouse_event{}; // value-initialized, not unitialized
193 mouse_event.EventType = EET_MOUSE_INPUT_EVENT;
194 mouse_event.MouseInput.X = m_pointer.X;
195 mouse_event.MouseInput.Y = m_pointer.Y;
196 switch (touch_event) {
197 case ETIE_PRESSED_DOWN:
198 mouse_event.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
199 mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
202 mouse_event.MouseInput.Event = EMIE_MOUSE_MOVED;
203 mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
206 mouse_event.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
207 mouse_event.MouseInput.ButtonStates = 0;
212 if (preprocessEvent(mouse_event))
216 return target->OnEvent(mouse_event);
219 void GUIModalMenu::enter(gui::IGUIElement *hovered)
223 sanity_check(!m_hovered);
224 m_hovered.grab(hovered);
226 gui_event.EventType = EET_GUI_EVENT;
227 gui_event.GUIEvent.Caller = m_hovered.get();
228 gui_event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
229 gui_event.GUIEvent.Element = gui_event.GUIEvent.Caller;
230 m_hovered->OnEvent(gui_event);
233 void GUIModalMenu::leave()
238 gui_event.EventType = EET_GUI_EVENT;
239 gui_event.GUIEvent.Caller = m_hovered.get();
240 gui_event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
241 m_hovered->OnEvent(gui_event);
247 bool GUIModalMenu::preprocessEvent(const SEvent &event)
251 // display software keyboard when clicking edit boxes
252 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
253 event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
254 gui::IGUIElement *hovered =
255 Environment->getRootGUIElement()->getElementFromPoint(
256 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
257 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
258 bool retval = hovered->OnEvent(event);
260 Environment->setFocus(hovered);
262 std::string field_name = getNameByID(hovered->getID());
264 if (field_name.empty())
267 m_jni_field_name = field_name;
268 /*~ Imperative, as in "Enter/type in text".
269 Don't forget the space. */
270 std::string message = gettext("Enter ");
271 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
274 message += strgettext(label) + ":";
276 // single line text input
279 // multi line text input
280 if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
283 // passwords are always single line
284 if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
287 porting::showInputDialog(gettext("OK"), "",
288 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
294 #ifdef HAVE_TOUCHSCREENGUI
295 if (event.EventType == EET_TOUCH_INPUT_EVENT) {
296 irr_ptr<GUIModalMenu> holder;
297 holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
299 switch ((int)event.TouchInput.touchedCount) {
301 if (event.TouchInput.Event == ETIE_PRESSED_DOWN || event.TouchInput.Event == ETIE_MOVED)
302 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
303 if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
304 m_down_pos = m_pointer;
305 gui::IGUIElement *hovered = Environment->getRootGUIElement()->getElementFromPoint(core::position2d<s32>(m_pointer));
306 if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
307 Environment->setFocus(hovered);
308 if (m_hovered != hovered) {
312 gui::IGUIElement *focused = Environment->getFocus();
313 bool ret = simulateMouseEvent(focused, event.TouchInput.Event);
314 if (!ret && m_hovered != focused)
315 ret = simulateMouseEvent(m_hovered.get(), event.TouchInput.Event);
316 if (event.TouchInput.Event == ETIE_LEFT_UP)
321 if (event.TouchInput.Event != ETIE_PRESSED_DOWN)
322 return true; // ignore
323 auto focused = Environment->getFocus();
326 SEvent rclick_event{};
327 rclick_event.EventType = EET_MOUSE_INPUT_EVENT;
328 rclick_event.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
329 rclick_event.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
330 rclick_event.MouseInput.X = m_pointer.X;
331 rclick_event.MouseInput.Y = m_pointer.Y;
332 focused->OnEvent(rclick_event);
333 rclick_event.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
334 rclick_event.MouseInput.ButtonStates = EMBSM_LEFT;
335 focused->OnEvent(rclick_event);
344 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
345 s32 x = event.MouseInput.X;
346 s32 y = event.MouseInput.Y;
347 gui::IGUIElement *hovered =
348 Environment->getRootGUIElement()->getElementFromPoint(
349 core::position2d<s32>(x, y));
350 if (!isChild(hovered, this)) {
351 if (DoubleClickDetection(event)) {
360 bool GUIModalMenu::hasAndroidUIInput()
363 if (m_jni_field_name.empty())
367 if (porting::getInputDialogState() == -1)
370 // no value abort dialog processing
371 if (porting::getInputDialogState() != 0) {
372 m_jni_field_name.clear();