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, s32 id,
33 IMenuManager *menumgr) :
34 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
35 core::rect<s32>(0, 0, 100, 100)),
41 m_gui_scale = g_settings->getFloat("gui_scaling");
43 float d = porting::getDisplayDensity();
44 m_gui_scale *= 1.1 - 0.3 * d + 0.2 * d * d;
47 Environment->setFocus(this);
48 m_menumgr->createdMenu(this);
52 GUIModalMenu::~GUIModalMenu()
54 m_menumgr->deletingMenu(this);
57 void GUIModalMenu::allowFocusRemoval(bool allow)
59 m_allow_focus_removal = allow;
62 bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e)
64 return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
67 void GUIModalMenu::draw()
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);
83 This should be called when the menu wants to quit.
85 WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
86 immediately if you call this from the menu itself.
88 (More precisely, this decrements the reference count.)
90 void GUIModalMenu::quitMenu()
92 allowFocusRemoval(true);
93 // This removes Environment's grab on us
94 Environment->removeFocus(this);
95 m_menumgr->deletingMenu(this);
97 #ifdef HAVE_TOUCHSCREENGUI
98 if (g_touchscreengui && m_touchscreen_visible)
99 g_touchscreengui->show();
103 void GUIModalMenu::removeChildren()
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);
111 for (gui::IGUIElement *i : children_copy) {
116 bool GUIModalMenu::preprocessEvent(const SEvent &event)
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);
129 Environment->setFocus(hovered);
131 std::string field_name = getNameByID(hovered->getID());
133 if (field_name.empty())
136 m_jni_field_name = field_name;
137 std::string message = gettext("Enter ");
138 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
141 message += gettext(label) + ":";
143 // single line text input
146 // multi line text input
147 if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
150 // passwords are always single line
151 if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
154 porting::showInputDialog(gettext("ok"), "",
155 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
160 if (event.EventType == EET_TOUCH_INPUT_EVENT) {
162 memset(&translated, 0, sizeof(SEvent));
163 translated.EventType = EET_MOUSE_INPUT_EVENT;
164 gui::IGUIElement *root = Environment->getRootGUIElement();
167 errorstream << "GUIModalMenu::preprocessEvent"
168 << " unable to get root element" << std::endl;
171 gui::IGUIElement *hovered = root->getElementFromPoint(
172 core::position2d<s32>(event.TouchInput.X, event.TouchInput.Y));
174 translated.MouseInput.X = event.TouchInput.X;
175 translated.MouseInput.Y = event.TouchInput.Y;
176 translated.MouseInput.Control = false;
178 bool dont_send_event = 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);
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;
212 } else if ((event.TouchInput.touchedCount == 2) &&
213 (event.TouchInput.Event == ETIE_PRESSED_DOWN)) {
214 hovered = root->getElementFromPoint(m_down_pos);
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;
221 hovered->OnEvent(translated);
224 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
225 translated.MouseInput.ButtonStates = EMBSM_LEFT;
228 hovered->OnEvent(translated);
230 dont_send_event = true;
232 // ignore unhandled 2 touch events ... accidental moving for example
233 else if (event.TouchInput.touchedCount == 2) {
234 dont_send_event = true;
236 else if (event.TouchInput.touchedCount > 2) {
237 errorstream << "GUIModalMenu::preprocessEvent"
238 << " to many multitouch events "
239 << event.TouchInput.touchedCount << " ignoring them"
243 if (dont_send_event) {
247 // check if translated event needs to be preprocessed again
248 if (preprocessEvent(translated)) {
253 bool retval = hovered->OnEvent(translated);
255 if (event.TouchInput.Event == ETIE_LEFT_UP) {
257 m_pointer = v2s32(0, 0);
269 bool GUIModalMenu::hasAndroidUIInput()
272 if (m_jni_field_name.empty()) {
277 if (porting::getInputDialogState() == -1) {
281 // no value abort dialog processing
282 if (porting::getInputDialogState() != 0) {
283 m_jni_field_name.clear();