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();
186 bool GUIModalMenu::preprocessEvent(const SEvent
&event
)
190 // display software keyboard when clicking edit boxes
191 if (event
.EventType
== EET_MOUSE_INPUT_EVENT
&&
192 event
.MouseInput
.Event
== EMIE_LMOUSE_PRESSED_DOWN
) {
193 gui::IGUIElement
*hovered
=
194 Environment
->getRootGUIElement()->getElementFromPoint(
195 core::position2d
<s32
>(event
.MouseInput
.X
, event
.MouseInput
.Y
));
196 if ((hovered
) && (hovered
->getType() == irr::gui::EGUIET_EDIT_BOX
)) {
197 bool retval
= hovered
->OnEvent(event
);
199 Environment
->setFocus(hovered
);
201 std::string field_name
= getNameByID(hovered
->getID());
203 if (field_name
.empty())
206 m_jni_field_name
= field_name
;
207 /*~ Imperative, as in "Enter/type in text".
208 Don't forget the space. */
209 std::string message
= gettext("Enter ");
210 std::string label
= wide_to_utf8(getLabelByID(hovered
->getID()));
213 message
+= gettext(label
) + ":";
215 // single line text input
218 // multi line text input
219 if (((gui::IGUIEditBox
*)hovered
)->isMultiLineEnabled())
222 // passwords are always single line
223 if (((gui::IGUIEditBox
*)hovered
)->isPasswordBox())
226 porting::showInputDialog(gettext("OK"), "",
227 wide_to_utf8(((gui::IGUIEditBox
*)hovered
)->getText()), type
);
232 if (event
.EventType
== EET_TOUCH_INPUT_EVENT
) {
234 memset(&translated
, 0, sizeof(SEvent
));
235 translated
.EventType
= EET_MOUSE_INPUT_EVENT
;
236 gui::IGUIElement
*root
= Environment
->getRootGUIElement();
239 errorstream
<< "GUIModalMenu::preprocessEvent"
240 << " unable to get root element" << std::endl
;
243 gui::IGUIElement
*hovered
=
244 root
->getElementFromPoint(core::position2d
<s32
>(
245 event
.TouchInput
.X
, event
.TouchInput
.Y
));
247 translated
.MouseInput
.X
= event
.TouchInput
.X
;
248 translated
.MouseInput
.Y
= event
.TouchInput
.Y
;
249 translated
.MouseInput
.Control
= false;
251 if (event
.TouchInput
.touchedCount
== 1) {
252 switch (event
.TouchInput
.Event
) {
253 case ETIE_PRESSED_DOWN
:
254 m_pointer
= v2s32(event
.TouchInput
.X
, event
.TouchInput
.Y
);
255 translated
.MouseInput
.Event
= EMIE_LMOUSE_PRESSED_DOWN
;
256 translated
.MouseInput
.ButtonStates
= EMBSM_LEFT
;
257 m_down_pos
= m_pointer
;
260 m_pointer
= v2s32(event
.TouchInput
.X
, event
.TouchInput
.Y
);
261 translated
.MouseInput
.Event
= EMIE_MOUSE_MOVED
;
262 translated
.MouseInput
.ButtonStates
= EMBSM_LEFT
;
265 translated
.MouseInput
.Event
= EMIE_LMOUSE_LEFT_UP
;
266 translated
.MouseInput
.ButtonStates
= 0;
267 hovered
= root
->getElementFromPoint(m_down_pos
);
268 // we don't have a valid pointer element use last
270 translated
.MouseInput
.X
= m_pointer
.X
;
271 translated
.MouseInput
.Y
= m_pointer
.Y
;
274 m_down_pos
= v2s32(0, 0);
279 } else if ((event
.TouchInput
.touchedCount
== 2) &&
280 (event
.TouchInput
.Event
== ETIE_PRESSED_DOWN
)) {
281 hovered
= root
->getElementFromPoint(m_down_pos
);
283 translated
.MouseInput
.Event
= EMIE_RMOUSE_PRESSED_DOWN
;
284 translated
.MouseInput
.ButtonStates
= EMBSM_LEFT
| EMBSM_RIGHT
;
285 translated
.MouseInput
.X
= m_pointer
.X
;
286 translated
.MouseInput
.Y
= m_pointer
.Y
;
288 hovered
->OnEvent(translated
);
290 translated
.MouseInput
.Event
= EMIE_RMOUSE_LEFT_UP
;
291 translated
.MouseInput
.ButtonStates
= EMBSM_LEFT
;
294 hovered
->OnEvent(translated
);
298 // ignore unhandled 2 touch events (accidental moving for example)
302 // check if translated event needs to be preprocessed again
303 if (preprocessEvent(translated
))
308 bool retval
= hovered
->OnEvent(translated
);
310 if (event
.TouchInput
.Event
== ETIE_LEFT_UP
)
312 m_pointer
= v2s32(0, 0);
320 if (event
.EventType
== EET_MOUSE_INPUT_EVENT
) {
321 s32 x
= event
.MouseInput
.X
;
322 s32 y
= event
.MouseInput
.Y
;
323 gui::IGUIElement
*hovered
=
324 Environment
->getRootGUIElement()->getElementFromPoint(
325 core::position2d
<s32
>(x
, y
));
326 if (!isChild(hovered
, this)) {
327 if (DoubleClickDetection(event
)) {
336 bool GUIModalMenu::hasAndroidUIInput()
339 if (m_jni_field_name
.empty())
343 if (porting::getInputDialogState() == -1)
346 // no value abort dialog processing
347 if (porting::getInputDialogState() != 0) {
348 m_jni_field_name
.clear();