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"
31 GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment *env, gui::IGUIElement *parent, s32 id,
32 IMenuManager *menumgr) :
33 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
34 core::rect<s32>(0, 0, 100, 100)),
40 m_gui_scale = g_settings->getFloat("gui_scaling");
42 float d = porting::getDisplayDensity();
43 m_gui_scale *= 1.1 - 0.3 * d + 0.2 * d * d;
46 Environment->setFocus(this);
47 m_menumgr->createdMenu(this);
51 GUIModalMenu::~GUIModalMenu()
53 m_menumgr->deletingMenu(this);
56 void GUIModalMenu::allowFocusRemoval(bool allow)
58 m_allow_focus_removal = allow;
61 bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e)
63 return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
66 void GUIModalMenu::draw()
71 video::IVideoDriver *driver = Environment->getVideoDriver();
72 v2u32 screensize = driver->getScreenSize();
73 if (screensize != m_screensize_old) {
74 m_screensize_old = screensize;
75 regenerateGui(screensize);
82 This should be called when the menu wants to quit.
84 WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
85 immediately if you call this from the menu itself.
87 (More precisely, this decrements the reference count.)
89 void GUIModalMenu::quitMenu()
91 allowFocusRemoval(true);
92 // This removes Environment's grab on us
93 Environment->removeFocus(this);
94 m_menumgr->deletingMenu(this);
96 #ifdef HAVE_TOUCHSCREENGUI
97 if (g_touchscreengui && m_touchscreen_visible)
98 g_touchscreengui->show();
102 void GUIModalMenu::removeChildren()
104 const core::list<gui::IGUIElement *> &children = getChildren();
105 core::list<gui::IGUIElement *> children_copy;
106 for (gui::IGUIElement *i : children) {
107 children_copy.push_back(i);
110 for (gui::IGUIElement *i : children_copy) {
115 bool GUIModalMenu::preprocessEvent(const SEvent &event)
119 // display software keyboard when clicking edit boxes
120 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
121 event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
122 gui::IGUIElement *hovered =
123 Environment->getRootGUIElement()->getElementFromPoint(
124 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
125 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
126 bool retval = hovered->OnEvent(event);
128 Environment->setFocus(hovered);
130 std::string field_name = getNameByID(hovered->getID());
132 if (field_name.empty())
135 m_jni_field_name = field_name;
136 /*~ Imperative, as in "Enter/type in text".
137 Don't forget the space. */
138 std::string message = gettext("Enter ");
139 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
142 message += gettext(label) + ":";
144 // single line text input
147 // multi line text input
148 if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
151 // passwords are always single line
152 if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
155 porting::showInputDialog(gettext("OK"), "",
156 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
161 if (event.EventType == EET_TOUCH_INPUT_EVENT) {
163 memset(&translated, 0, sizeof(SEvent));
164 translated.EventType = EET_MOUSE_INPUT_EVENT;
165 gui::IGUIElement *root = Environment->getRootGUIElement();
168 errorstream << "GUIModalMenu::preprocessEvent"
169 << " unable to get root element" << std::endl;
172 gui::IGUIElement *hovered =
173 root->getElementFromPoint(core::position2d<s32>(
174 event.TouchInput.X, event.TouchInput.Y));
176 translated.MouseInput.X = event.TouchInput.X;
177 translated.MouseInput.Y = event.TouchInput.Y;
178 translated.MouseInput.Control = false;
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;
189 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
190 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
191 translated.MouseInput.ButtonStates = EMBSM_LEFT;
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
199 translated.MouseInput.X = m_pointer.X;
200 translated.MouseInput.Y = m_pointer.Y;
203 m_down_pos = v2s32(0, 0);
208 } else if ((event.TouchInput.touchedCount == 2) &&
209 (event.TouchInput.Event == ETIE_PRESSED_DOWN)) {
210 hovered = root->getElementFromPoint(m_down_pos);
212 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
213 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
214 translated.MouseInput.X = m_pointer.X;
215 translated.MouseInput.Y = m_pointer.Y;
217 hovered->OnEvent(translated);
219 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
220 translated.MouseInput.ButtonStates = EMBSM_LEFT;
223 hovered->OnEvent(translated);
227 // ignore unhandled 2 touch events (accidental moving for example)
231 // check if translated event needs to be preprocessed again
232 if (preprocessEvent(translated))
237 bool retval = hovered->OnEvent(translated);
239 if (event.TouchInput.Event == ETIE_LEFT_UP)
241 m_pointer = v2s32(0, 0);
252 bool GUIModalMenu::hasAndroidUIInput()
255 if (m_jni_field_name.empty())
259 if (porting::getInputDialogState() == -1)
262 // no value abort dialog processing
263 if (porting::getInputDialogState() != 0) {
264 m_jni_field_name.clear();