]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/modalMenu.cpp
LINT fixes since recent tooling update
[dragonfireclient.git] / src / gui / modalMenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 stujones11, Stuart Jones <stujones111@gmail.com>
5
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.
10
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.
15
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.
19 */
20
21 #include <cstdlib>
22 #include "modalMenu.h"
23 #include "gettext.h"
24 #include "porting.h"
25 #include "settings.h"
26
27 #ifdef HAVE_TOUCHSCREENGUI
28 #include "touchscreengui.h"
29 #endif
30
31 // clang-format off
32 GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent, s32 id,
33                 IMenuManager *menumgr) :
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 {
41         m_gui_scale = g_settings->getFloat("gui_scaling");
42 #ifdef __ANDROID__
43         float d = porting::getDisplayDensity();
44         m_gui_scale *= 1.1 - 0.3 * d + 0.2 * d * d;
45 #endif
46         setVisible(true);
47         Environment->setFocus(this);
48         m_menumgr->createdMenu(this);
49 }
50 // clang-format on
51
52 GUIModalMenu::~GUIModalMenu()
53 {
54         m_menumgr->deletingMenu(this);
55 }
56
57 void GUIModalMenu::allowFocusRemoval(bool allow)
58 {
59         m_allow_focus_removal = allow;
60 }
61
62 bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e)
63 {
64         return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
65 }
66
67 void GUIModalMenu::draw()
68 {
69         if (!IsVisible)
70                 return;
71
72         video::IVideoDriver *driver = Environment->getVideoDriver();
73         v2u32 screensize = driver->getScreenSize();
74         if (screensize != m_screensize_old) {
75                 m_screensize_old = screensize;
76                 regenerateGui(screensize);
77         }
78
79         drawMenu();
80 }
81
82 /*
83         This should be called when the menu wants to quit.
84
85         WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
86         immediately if you call this from the menu itself.
87
88         (More precisely, this decrements the reference count.)
89 */
90 void GUIModalMenu::quitMenu()
91 {
92         allowFocusRemoval(true);
93         // This removes Environment's grab on us
94         Environment->removeFocus(this);
95         m_menumgr->deletingMenu(this);
96         this->remove();
97 #ifdef HAVE_TOUCHSCREENGUI
98         if (g_touchscreengui && m_touchscreen_visible)
99                 g_touchscreengui->show();
100 #endif
101 }
102
103 void GUIModalMenu::removeChildren()
104 {
105         const core::list<gui::IGUIElement *> &children = getChildren();
106         core::list<gui::IGUIElement *> children_copy;
107         for (gui::IGUIElement *i : children) {
108                 children_copy.push_back(i);
109         }
110
111         for (gui::IGUIElement *i : children_copy) {
112                 i->remove();
113         }
114 }
115
116 bool GUIModalMenu::preprocessEvent(const SEvent &event)
117 {
118 #ifdef __ANDROID__
119         // clang-format off
120         // display software keyboard when clicking edit boxes
121         if (event.EventType == EET_MOUSE_INPUT_EVENT &&
122                         event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
123                 gui::IGUIElement *hovered =
124                         Environment->getRootGUIElement()->getElementFromPoint(
125                                 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
126                 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
127                         bool retval = hovered->OnEvent(event);
128                         if (retval)
129                                 Environment->setFocus(hovered);
130
131                         std::string field_name = getNameByID(hovered->getID());
132                         // read-only field
133                         if (field_name.empty())
134                                 return retval;
135
136                         m_jni_field_name = field_name;
137                         std::string message = gettext("Enter ");
138                         std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
139                         if (label.empty())
140                                 label = "text";
141                         message += gettext(label) + ":";
142
143                         // single line text input
144                         int type = 2;
145
146                         // multi line text input
147                         if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
148                                 type = 1;
149
150                         // passwords are always single line
151                         if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
152                                 type = 3;
153
154                         porting::showInputDialog(gettext("ok"), "",
155                                 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
156                         return retval;
157                 }
158         }
159
160         if (event.EventType == EET_TOUCH_INPUT_EVENT) {
161                 SEvent translated;
162                 memset(&translated, 0, sizeof(SEvent));
163                 translated.EventType = EET_MOUSE_INPUT_EVENT;
164                 gui::IGUIElement *root = Environment->getRootGUIElement();
165
166                 if (!root) {
167                         errorstream << "GUIModalMenu::preprocessEvent"
168                                     << " unable to get root element" << std::endl;
169                         return false;
170                 }
171                 gui::IGUIElement *hovered = root->getElementFromPoint(
172                         core::position2d<s32>(event.TouchInput.X, event.TouchInput.Y));
173
174                 translated.MouseInput.X = event.TouchInput.X;
175                 translated.MouseInput.Y = event.TouchInput.Y;
176                 translated.MouseInput.Control = false;
177
178                 bool dont_send_event = false;
179
180                 if (event.TouchInput.touchedCount == 1) {
181                         switch (event.TouchInput.Event) {
182                         case ETIE_PRESSED_DOWN:
183                                 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
184                                 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
185                                 translated.MouseInput.ButtonStates = EMBSM_LEFT;
186                                 m_down_pos = m_pointer;
187                                 break;
188                         case ETIE_MOVED:
189                                 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
190                                 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
191                                 translated.MouseInput.ButtonStates = EMBSM_LEFT;
192                                 break;
193                         case ETIE_LEFT_UP:
194                                 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
195                                 translated.MouseInput.ButtonStates = 0;
196                                 hovered = root->getElementFromPoint(m_down_pos);
197                                 // we don't have a valid pointer element use last
198                                 // known pointer pos
199                                 translated.MouseInput.X = m_pointer.X;
200                                 translated.MouseInput.Y = m_pointer.Y;
201
202                                 // reset down pos
203                                 m_down_pos = v2s32(0, 0);
204                                 break;
205                         default:
206                                 dont_send_event = true;
207                                 // this is not supposed to happen
208                                 errorstream << "GUIModalMenu::preprocessEvent"
209                                             << " unexpected usecase Event="
210                                             << event.TouchInput.Event << std::endl;
211                         }
212                 } else if ((event.TouchInput.touchedCount == 2) &&
213                                 (event.TouchInput.Event == ETIE_PRESSED_DOWN)) {
214                         hovered = root->getElementFromPoint(m_down_pos);
215
216                         translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
217                         translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
218                         translated.MouseInput.X = m_pointer.X;
219                         translated.MouseInput.Y = m_pointer.Y;
220                         if (hovered) {
221                                 hovered->OnEvent(translated);
222                         }
223
224                         translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
225                         translated.MouseInput.ButtonStates = EMBSM_LEFT;
226
227                         if (hovered) {
228                                 hovered->OnEvent(translated);
229                         }
230                         dont_send_event = true;
231                 }
232                 // ignore unhandled 2 touch events ... accidental moving for example
233                 else if (event.TouchInput.touchedCount == 2) {
234                         dont_send_event = true;
235                 }
236                 else if (event.TouchInput.touchedCount > 2) {
237                         errorstream << "GUIModalMenu::preprocessEvent"
238                                     << " to many multitouch events "
239                                     << event.TouchInput.touchedCount << " ignoring them"
240                                     << std::endl;
241                 }
242
243                 if (dont_send_event) {
244                         return true;
245                 }
246
247                 // check if translated event needs to be preprocessed again
248                 if (preprocessEvent(translated)) {
249                         return true;
250                 }
251                 if (hovered) {
252                         grab();
253                         bool retval = hovered->OnEvent(translated);
254
255                         if (event.TouchInput.Event == ETIE_LEFT_UP) {
256                                 // reset pointer
257                                 m_pointer = v2s32(0, 0);
258                         }
259                         drop();
260                         return retval;
261                 }
262         }
263         // clang-format on
264 #endif
265         return false;
266 }
267
268 #ifdef __ANDROID__
269 bool GUIModalMenu::hasAndroidUIInput()
270 {
271         // no dialog shown
272         if (m_jni_field_name.empty()) {
273                 return false;
274         }
275
276         // still waiting
277         if (porting::getInputDialogState() == -1) {
278                 return true;
279         }
280
281         // no value abort dialog processing
282         if (porting::getInputDialogState() != 0) {
283                 m_jni_field_name.clear();
284                 return false;
285         }
286
287         return true;
288 }
289 #endif