|
| 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 | + |
| 26 | +#ifdef HAVE_TOUCHSCREENGUI |
| 27 | +#include "touchscreengui.h" |
| 28 | +#endif |
| 29 | + |
| 30 | +// clang-format off |
| 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)), |
| 35 | +#ifdef __ANDROID__ |
| 36 | + m_jni_field_name(""), |
| 37 | +#endif |
| 38 | + m_menumgr(menumgr) |
| 39 | +{ |
| 40 | + setVisible(true); |
| 41 | + Environment->setFocus(this); |
| 42 | + m_menumgr->createdMenu(this); |
| 43 | +} |
| 44 | +// clang-format on |
| 45 | + |
| 46 | +GUIModalMenu::~GUIModalMenu() |
| 47 | +{ |
| 48 | + m_menumgr->deletingMenu(this); |
| 49 | +} |
| 50 | + |
| 51 | +void GUIModalMenu::allowFocusRemoval(bool allow) |
| 52 | +{ |
| 53 | + m_allow_focus_removal = allow; |
| 54 | +} |
| 55 | + |
| 56 | +bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e) |
| 57 | +{ |
| 58 | + return (e && (e == this || isMyChild(e))) || m_allow_focus_removal; |
| 59 | +} |
| 60 | + |
| 61 | +void GUIModalMenu::draw() |
| 62 | +{ |
| 63 | + if (!IsVisible) |
| 64 | + return; |
| 65 | + |
| 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); |
| 71 | + } |
| 72 | + |
| 73 | + drawMenu(); |
| 74 | +} |
| 75 | + |
| 76 | +/* |
| 77 | + This should be called when the menu wants to quit. |
| 78 | +
|
| 79 | + WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return |
| 80 | + immediately if you call this from the menu itself. |
| 81 | +
|
| 82 | + (More precisely, this decrements the reference count.) |
| 83 | +*/ |
| 84 | +void GUIModalMenu::quitMenu() |
| 85 | +{ |
| 86 | + allowFocusRemoval(true); |
| 87 | + // This removes Environment's grab on us |
| 88 | + Environment->removeFocus(this); |
| 89 | + m_menumgr->deletingMenu(this); |
| 90 | + this->remove(); |
| 91 | +#ifdef HAVE_TOUCHSCREENGUI |
| 92 | + if (g_touchscreengui && m_touchscreen_visible) |
| 93 | + g_touchscreengui->show(); |
| 94 | +#endif |
| 95 | +} |
| 96 | + |
| 97 | +void GUIModalMenu::removeChildren() |
| 98 | +{ |
| 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); |
| 103 | + } |
| 104 | + |
| 105 | + for (gui::IGUIElement *i : children_copy) { |
| 106 | + i->remove(); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +bool GUIModalMenu::preprocessEvent(const SEvent &event) |
| 111 | +{ |
| 112 | +#ifdef __ANDROID__ |
| 113 | + // clang-format off |
| 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); |
| 122 | + if (retval) |
| 123 | + Environment->setFocus(hovered); |
| 124 | + |
| 125 | + std::string field_name = getNameByID(hovered->getID()); |
| 126 | + // read-only field |
| 127 | + if (field_name.empty()) |
| 128 | + return retval; |
| 129 | + |
| 130 | + m_jni_field_name = field_name; |
| 131 | + std::string message = gettext("Enter "); |
| 132 | + std::string label = wide_to_utf8(getLabelByID(hovered->getID())); |
| 133 | + if (label.empty()) |
| 134 | + label = "text"; |
| 135 | + message += gettext(label) + ":"; |
| 136 | + |
| 137 | + // single line text input |
| 138 | + int type = 2; |
| 139 | + |
| 140 | + // multi line text input |
| 141 | + if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled()) |
| 142 | + type = 1; |
| 143 | + |
| 144 | + // passwords are always single line |
| 145 | + if (((gui::IGUIEditBox *)hovered)->isPasswordBox()) |
| 146 | + type = 3; |
| 147 | + |
| 148 | + porting::showInputDialog(gettext("ok"), "", |
| 149 | + wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type); |
| 150 | + return retval; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if (event.EventType == EET_TOUCH_INPUT_EVENT) { |
| 155 | + SEvent translated; |
| 156 | + memset(&translated, 0, sizeof(SEvent)); |
| 157 | + translated.EventType = EET_MOUSE_INPUT_EVENT; |
| 158 | + gui::IGUIElement *root = Environment->getRootGUIElement(); |
| 159 | + |
| 160 | + if (!root) { |
| 161 | + errorstream << "GUIModalMenu::preprocessEvent" |
| 162 | + << " unable to get root element" << std::endl; |
| 163 | + return false; |
| 164 | + } |
| 165 | + gui::IGUIElement *hovered = root->getElementFromPoint( |
| 166 | + core::position2d<s32>(event.TouchInput.X, event.TouchInput.Y)); |
| 167 | + |
| 168 | + translated.MouseInput.X = event.TouchInput.X; |
| 169 | + translated.MouseInput.Y = event.TouchInput.Y; |
| 170 | + translated.MouseInput.Control = false; |
| 171 | + |
| 172 | + bool dont_send_event = false; |
| 173 | + |
| 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; |
| 181 | + break; |
| 182 | + case ETIE_MOVED: |
| 183 | + m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y); |
| 184 | + translated.MouseInput.Event = EMIE_MOUSE_MOVED; |
| 185 | + translated.MouseInput.ButtonStates = EMBSM_LEFT; |
| 186 | + break; |
| 187 | + case ETIE_LEFT_UP: |
| 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 |
| 192 | + // known pointer pos |
| 193 | + translated.MouseInput.X = m_pointer.X; |
| 194 | + translated.MouseInput.Y = m_pointer.Y; |
| 195 | + |
| 196 | + // reset down pos |
| 197 | + m_down_pos = v2s32(0, 0); |
| 198 | + break; |
| 199 | + default: |
| 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; |
| 205 | + } |
| 206 | + } else if ((event.TouchInput.touchedCount == 2) && |
| 207 | + (event.TouchInput.Event == ETIE_PRESSED_DOWN)) { |
| 208 | + hovered = root->getElementFromPoint(m_down_pos); |
| 209 | + |
| 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; |
| 214 | + if (hovered) { |
| 215 | + hovered->OnEvent(translated); |
| 216 | + } |
| 217 | + |
| 218 | + translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP; |
| 219 | + translated.MouseInput.ButtonStates = EMBSM_LEFT; |
| 220 | + |
| 221 | + if (hovered) { |
| 222 | + hovered->OnEvent(translated); |
| 223 | + } |
| 224 | + dont_send_event = true; |
| 225 | + } |
| 226 | + // ignore unhandled 2 touch events ... accidental moving for example |
| 227 | + else if (event.TouchInput.touchedCount == 2) { |
| 228 | + dont_send_event = true; |
| 229 | + } |
| 230 | + else if (event.TouchInput.touchedCount > 2) { |
| 231 | + errorstream << "GUIModalMenu::preprocessEvent" |
| 232 | + << " to many multitouch events " |
| 233 | + << event.TouchInput.touchedCount << " ignoring them" |
| 234 | + << std::endl; |
| 235 | + } |
| 236 | + |
| 237 | + if (dont_send_event) { |
| 238 | + return true; |
| 239 | + } |
| 240 | + |
| 241 | + // check if translated event needs to be preprocessed again |
| 242 | + if (preprocessEvent(translated)) { |
| 243 | + return true; |
| 244 | + } |
| 245 | + if (hovered) { |
| 246 | + grab(); |
| 247 | + bool retval = hovered->OnEvent(translated); |
| 248 | + |
| 249 | + if (event.TouchInput.Event == ETIE_LEFT_UP) { |
| 250 | + // reset pointer |
| 251 | + m_pointer = v2s32(0, 0); |
| 252 | + } |
| 253 | + drop(); |
| 254 | + return retval; |
| 255 | + } |
| 256 | + } |
| 257 | + // clang-format on |
| 258 | +#endif |
| 259 | + return false; |
| 260 | +} |
| 261 | + |
| 262 | +#ifdef __ANDROID__ |
| 263 | +bool GUIModalMenu::hasAndroidUIInput() |
| 264 | +{ |
| 265 | + // no dialog shown |
| 266 | + if (m_jni_field_name.empty()) { |
| 267 | + return false; |
| 268 | + } |
| 269 | + |
| 270 | + // still waiting |
| 271 | + if (porting::getInputDialogState() == -1) { |
| 272 | + return true; |
| 273 | + } |
| 274 | + |
| 275 | + // no value abort dialog processing |
| 276 | + if (porting::getInputDialogState() != 0) { |
| 277 | + m_jni_field_name.clear(); |
| 278 | + return false; |
| 279 | + } |
| 280 | + |
| 281 | + return true; |
| 282 | +} |
| 283 | +#endif |
0 commit comments