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"
26 #ifdef HAVE_TOUCHSCREENGUI
27 #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)),
41 Environment->setFocus(this);
42 m_menumgr->createdMenu(this);
46 GUIModalMenu::~GUIModalMenu()
48 m_menumgr->deletingMenu(this);
51 void GUIModalMenu::allowFocusRemoval(bool allow)
53 m_allow_focus_removal = allow;
56 bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e)
58 return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
61 void GUIModalMenu::draw()
66 video::IVideoDriver *driver = Environment->getVideoDriver();
67 v2u32 screensize = driver->getScreenSize();
68 if (screensize != m_screensize_old) {
69 m_screensize_old = screensize;
70 regenerateGui(screensize);
77 This should be called when the menu wants to quit.
79 WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
80 immediately if you call this from the menu itself.
82 (More precisely, this decrements the reference count.)
84 void GUIModalMenu::quitMenu()
86 allowFocusRemoval(true);
87 // This removes Environment's grab on us
88 Environment->removeFocus(this);
89 m_menumgr->deletingMenu(this);
91 #ifdef HAVE_TOUCHSCREENGUI
92 if (g_touchscreengui && m_touchscreen_visible)
93 g_touchscreengui->show();
97 void GUIModalMenu::removeChildren()
99 const core::list<gui::IGUIElement *> &children = getChildren();
100 core::list<gui::IGUIElement *> children_copy;
101 for (gui::IGUIElement *i : children) {
102 children_copy.push_back(i);
105 for (gui::IGUIElement *i : children_copy) {
110 bool GUIModalMenu::preprocessEvent(const SEvent &event)
114 // display software keyboard when clicking edit boxes
115 if (event.EventType == EET_MOUSE_INPUT_EVENT &&
116 event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
117 gui::IGUIElement *hovered =
118 Environment->getRootGUIElement()->getElementFromPoint(
119 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
120 if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
121 bool retval = hovered->OnEvent(event);
123 Environment->setFocus(hovered);
125 std::string field_name = getNameByID(hovered->getID());
127 if (field_name.empty())
130 m_jni_field_name = field_name;
131 std::string message = gettext("Enter ");
132 std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
135 message += gettext(label) + ":";
137 // single line text input
140 // multi line text input
141 if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
144 // passwords are always single line
145 if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
148 porting::showInputDialog(gettext("ok"), "",
149 wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
154 if (event.EventType == EET_TOUCH_INPUT_EVENT) {
156 memset(&translated, 0, sizeof(SEvent));
157 translated.EventType = EET_MOUSE_INPUT_EVENT;
158 gui::IGUIElement *root = Environment->getRootGUIElement();
161 errorstream << "GUIModalMenu::preprocessEvent"
162 << " unable to get root element" << std::endl;
165 gui::IGUIElement *hovered = root->getElementFromPoint(
166 core::position2d<s32>(event.TouchInput.X, event.TouchInput.Y));
168 translated.MouseInput.X = event.TouchInput.X;
169 translated.MouseInput.Y = event.TouchInput.Y;
170 translated.MouseInput.Control = false;
172 bool dont_send_event = false;
174 if (event.TouchInput.touchedCount == 1) {
175 switch (event.TouchInput.Event) {
176 case ETIE_PRESSED_DOWN:
177 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
178 translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
179 translated.MouseInput.ButtonStates = EMBSM_LEFT;
180 m_down_pos = m_pointer;
183 m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
184 translated.MouseInput.Event = EMIE_MOUSE_MOVED;
185 translated.MouseInput.ButtonStates = EMBSM_LEFT;
188 translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
189 translated.MouseInput.ButtonStates = 0;
190 hovered = root->getElementFromPoint(m_down_pos);
191 // we don't have a valid pointer element use last
193 translated.MouseInput.X = m_pointer.X;
194 translated.MouseInput.Y = m_pointer.Y;
197 m_down_pos = v2s32(0, 0);
200 dont_send_event = true;
201 // this is not supposed to happen
202 errorstream << "GUIModalMenu::preprocessEvent"
203 << " unexpected usecase Event="
204 << event.TouchInput.Event << std::endl;
206 } else if ((event.TouchInput.touchedCount == 2) &&
207 (event.TouchInput.Event == ETIE_PRESSED_DOWN)) {
208 hovered = root->getElementFromPoint(m_down_pos);
210 translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
211 translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
212 translated.MouseInput.X = m_pointer.X;
213 translated.MouseInput.Y = m_pointer.Y;
215 hovered->OnEvent(translated);
218 translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
219 translated.MouseInput.ButtonStates = EMBSM_LEFT;
222 hovered->OnEvent(translated);
224 dont_send_event = true;
226 // ignore unhandled 2 touch events ... accidental moving for example
227 else if (event.TouchInput.touchedCount == 2) {
228 dont_send_event = true;
230 else if (event.TouchInput.touchedCount > 2) {
231 errorstream << "GUIModalMenu::preprocessEvent"
232 << " to many multitouch events "
233 << event.TouchInput.touchedCount << " ignoring them"
237 if (dont_send_event) {
241 // check if translated event needs to be preprocessed again
242 if (preprocessEvent(translated)) {
247 bool retval = hovered->OnEvent(translated);
249 if (event.TouchInput.Event == ETIE_LEFT_UP) {
251 m_pointer = v2s32(0, 0);
263 bool GUIModalMenu::hasAndroidUIInput()
266 if (m_jni_field_name.empty()) {
271 if (porting::getInputDialogState() == -1) {
275 // no value abort dialog processing
276 if (porting::getInputDialogState() != 0) {
277 m_jni_field_name.clear();