cleanup
[waspsaliva.git] / src / gui / modalMenu.cpp
blob9b1e6dd9c7c84a109d2bc639e0cb47634214d9d3
1 /*
2 Minetest
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.
21 #include <cstdlib>
22 #include "modalMenu.h"
23 #include "gettext.h"
24 #include "porting.h"
25 #include "settings.h"
27 #ifdef HAVE_TOUCHSCREENGUI
28 #include "touchscreengui.h"
29 #endif
31 // clang-format off
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)),
36 #ifdef __ANDROID__
37 m_jni_field_name(""),
38 #endif
39 m_menumgr(menumgr),
40 m_remap_dbl_click(remap_dbl_click)
42 m_gui_scale = g_settings->getFloat("gui_scaling");
43 #ifdef __ANDROID__
44 float d = porting::getDisplayDensity();
45 m_gui_scale *= 1.1 - 0.3 * d + 0.2 * d * d;
46 #endif
47 setVisible(true);
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);
57 // clang-format on
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()
76 if (!IsVisible)
77 return;
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);
86 drawMenu();
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);
103 this->remove();
104 #ifdef HAVE_TOUCHSCREENGUI
105 if (g_touchscreengui && m_touchscreen_visible)
106 g_touchscreengui->show();
107 #endif
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) {
119 i->remove();
123 // clang-format off
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)
137 return false;
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());
148 if (delta > 400)
149 return false;
151 double squaredistance = m_doubleclickdetect[0].pos.
152 getDistanceFromSQ(m_doubleclickdetect[1].pos);
154 if (squaredistance > (30 * 30)) {
155 return false;
158 SEvent translated{};
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;
166 OnEvent(translated);
168 return true;
171 return false;
173 // clang-format on
175 static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
177 while (tocheck) {
178 if (tocheck == parent) {
179 return true;
181 tocheck = tocheck->getParent();
183 return false;
186 bool GUIModalMenu::preprocessEvent(const SEvent &event)
188 #ifdef __ANDROID__
189 // clang-format off
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);
198 if (retval)
199 Environment->setFocus(hovered);
201 std::string field_name = getNameByID(hovered->getID());
202 // read-only field
203 if (field_name.empty())
204 return retval;
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()));
211 if (label.empty())
212 label = "text";
213 message += gettext(label) + ":";
215 // single line text input
216 int type = 2;
218 // multi line text input
219 if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
220 type = 1;
222 // passwords are always single line
223 if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
224 type = 3;
226 porting::showInputDialog(gettext("OK"), "",
227 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
228 return retval;
232 if (event.EventType == EET_TOUCH_INPUT_EVENT) {
233 SEvent translated;
234 memset(&translated, 0, sizeof(SEvent));
235 translated.EventType = EET_MOUSE_INPUT_EVENT;
236 gui::IGUIElement *root = Environment->getRootGUIElement();
238 if (!root) {
239 errorstream << "GUIModalMenu::preprocessEvent"
240 << " unable to get root element" << std::endl;
241 return false;
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;
258 break;
259 case ETIE_MOVED:
260 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
261 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
262 translated.MouseInput.ButtonStates = EMBSM_LEFT;
263 break;
264 case ETIE_LEFT_UP:
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
269 // known pointer pos
270 translated.MouseInput.X = m_pointer.X;
271 translated.MouseInput.Y = m_pointer.Y;
273 // reset down pos
274 m_down_pos = v2s32(0, 0);
275 break;
276 default:
277 break;
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;
287 if (hovered)
288 hovered->OnEvent(translated);
290 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
291 translated.MouseInput.ButtonStates = EMBSM_LEFT;
293 if (hovered)
294 hovered->OnEvent(translated);
296 return true;
297 } else {
298 // ignore unhandled 2 touch events (accidental moving for example)
299 return true;
302 // check if translated event needs to be preprocessed again
303 if (preprocessEvent(translated))
304 return true;
306 if (hovered) {
307 grab();
308 bool retval = hovered->OnEvent(translated);
310 if (event.TouchInput.Event == ETIE_LEFT_UP)
311 // reset pointer
312 m_pointer = v2s32(0, 0);
314 drop();
315 return retval;
318 #endif
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)) {
328 return true;
332 return false;
335 #ifdef __ANDROID__
336 bool GUIModalMenu::hasAndroidUIInput()
338 // no dialog shown
339 if (m_jni_field_name.empty())
340 return false;
342 // still waiting
343 if (porting::getInputDialogState() == -1)
344 return true;
346 // no value abort dialog processing
347 if (porting::getInputDialogState() != 0) {
348 m_jni_field_name.clear();
349 return false;
352 return true;
354 #endif