modalMenu.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2018 stujones11, Stuart Jones <stujones111@gmail.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include <cstdlib>
  18. #include "client/renderingengine.h"
  19. #include "modalMenu.h"
  20. #include "gettext.h"
  21. #include "porting.h"
  22. #include "settings.h"
  23. #ifdef HAVE_TOUCHSCREENGUI
  24. #include "touchscreengui.h"
  25. #endif
  26. // clang-format off
  27. GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent,
  28. s32 id, IMenuManager *menumgr, bool remap_dbl_click) :
  29. IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
  30. core::rect<s32>(0, 0, 100, 100)),
  31. #ifdef __ANDROID__
  32. m_jni_field_name(""),
  33. #endif
  34. m_menumgr(menumgr),
  35. m_remap_dbl_click(remap_dbl_click)
  36. {
  37. m_gui_scale = std::max(g_settings->getFloat("gui_scaling"), 0.5f);
  38. const float screen_dpi_scale = RenderingEngine::getDisplayDensity();
  39. #ifdef HAVE_TOUCHSCREENGUI
  40. m_gui_scale *= 1.1f - 0.3f * screen_dpi_scale + 0.2f * screen_dpi_scale * screen_dpi_scale;
  41. #else
  42. m_gui_scale *= screen_dpi_scale;
  43. #endif
  44. setVisible(true);
  45. Environment->setFocus(this);
  46. m_menumgr->createdMenu(this);
  47. m_doubleclickdetect[0].time = 0;
  48. m_doubleclickdetect[1].time = 0;
  49. m_doubleclickdetect[0].pos = v2s32(0, 0);
  50. m_doubleclickdetect[1].pos = v2s32(0, 0);
  51. }
  52. // clang-format on
  53. GUIModalMenu::~GUIModalMenu()
  54. {
  55. m_menumgr->deletingMenu(this);
  56. }
  57. void GUIModalMenu::allowFocusRemoval(bool allow)
  58. {
  59. m_allow_focus_removal = allow;
  60. }
  61. bool GUIModalMenu::canTakeFocus(gui::IGUIElement *e)
  62. {
  63. return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
  64. }
  65. void GUIModalMenu::draw()
  66. {
  67. if (!IsVisible)
  68. return;
  69. video::IVideoDriver *driver = Environment->getVideoDriver();
  70. v2u32 screensize = driver->getScreenSize();
  71. if (screensize != m_screensize_old) {
  72. m_screensize_old = screensize;
  73. regenerateGui(screensize);
  74. }
  75. drawMenu();
  76. }
  77. /*
  78. 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.
  81. (More precisely, this decrements the reference count.)
  82. */
  83. void GUIModalMenu::quitMenu()
  84. {
  85. allowFocusRemoval(true);
  86. // This removes Environment's grab on us
  87. Environment->removeFocus(this);
  88. m_menumgr->deletingMenu(this);
  89. this->remove();
  90. #ifdef HAVE_TOUCHSCREENGUI
  91. if (g_touchscreengui && m_touchscreen_visible)
  92. g_touchscreengui->show();
  93. #endif
  94. }
  95. // clang-format off
  96. bool GUIModalMenu::DoubleClickDetection(const SEvent &event)
  97. {
  98. /* The following code is for capturing double-clicks of the mouse button
  99. * and translating the double-click into an EET_KEY_INPUT_EVENT event
  100. * -- which closes the form -- under some circumstances.
  101. *
  102. * There have been many github issues reporting this as a bug even though it
  103. * was an intended feature. For this reason, remapping the double-click as
  104. * an ESC must be explicitly set when creating this class via the
  105. * /p remap_dbl_click parameter of the constructor.
  106. */
  107. if (!m_remap_dbl_click)
  108. return false;
  109. if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  110. m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
  111. m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
  112. m_doubleclickdetect[1].pos = m_pointer;
  113. m_doubleclickdetect[1].time = porting::getTimeMs();
  114. } else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
  115. u64 delta = porting::getDeltaMs(
  116. m_doubleclickdetect[0].time, porting::getTimeMs());
  117. if (delta > 400)
  118. return false;
  119. double squaredistance = m_doubleclickdetect[0].pos.
  120. getDistanceFromSQ(m_doubleclickdetect[1].pos);
  121. if (squaredistance > (30 * 30)) {
  122. return false;
  123. }
  124. SEvent translated{};
  125. // translate doubleclick to escape
  126. translated.EventType = EET_KEY_INPUT_EVENT;
  127. translated.KeyInput.Key = KEY_ESCAPE;
  128. translated.KeyInput.Control = false;
  129. translated.KeyInput.Shift = false;
  130. translated.KeyInput.PressedDown = true;
  131. translated.KeyInput.Char = 0;
  132. OnEvent(translated);
  133. return true;
  134. }
  135. return false;
  136. }
  137. // clang-format on
  138. static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
  139. {
  140. while (tocheck) {
  141. if (tocheck == parent) {
  142. return true;
  143. }
  144. tocheck = tocheck->getParent();
  145. }
  146. return false;
  147. }
  148. #ifdef HAVE_TOUCHSCREENGUI
  149. bool GUIModalMenu::simulateMouseEvent(
  150. gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event)
  151. {
  152. SEvent mouse_event{}; // value-initialized, not unitialized
  153. mouse_event.EventType = EET_MOUSE_INPUT_EVENT;
  154. mouse_event.MouseInput.X = m_pointer.X;
  155. mouse_event.MouseInput.Y = m_pointer.Y;
  156. switch (touch_event) {
  157. case ETIE_PRESSED_DOWN:
  158. mouse_event.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
  159. mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
  160. break;
  161. case ETIE_MOVED:
  162. mouse_event.MouseInput.Event = EMIE_MOUSE_MOVED;
  163. mouse_event.MouseInput.ButtonStates = EMBSM_LEFT;
  164. break;
  165. case ETIE_LEFT_UP:
  166. mouse_event.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
  167. mouse_event.MouseInput.ButtonStates = 0;
  168. break;
  169. default:
  170. return false;
  171. }
  172. if (preprocessEvent(mouse_event))
  173. return true;
  174. if (!target)
  175. return false;
  176. return target->OnEvent(mouse_event);
  177. }
  178. void GUIModalMenu::enter(gui::IGUIElement *hovered)
  179. {
  180. if (!hovered)
  181. return;
  182. sanity_check(!m_hovered);
  183. m_hovered.grab(hovered);
  184. SEvent gui_event{};
  185. gui_event.EventType = EET_GUI_EVENT;
  186. gui_event.GUIEvent.Caller = m_hovered.get();
  187. gui_event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
  188. gui_event.GUIEvent.Element = gui_event.GUIEvent.Caller;
  189. m_hovered->OnEvent(gui_event);
  190. }
  191. void GUIModalMenu::leave()
  192. {
  193. if (!m_hovered)
  194. return;
  195. SEvent gui_event{};
  196. gui_event.EventType = EET_GUI_EVENT;
  197. gui_event.GUIEvent.Caller = m_hovered.get();
  198. gui_event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
  199. m_hovered->OnEvent(gui_event);
  200. m_hovered.reset();
  201. }
  202. #endif
  203. bool GUIModalMenu::preprocessEvent(const SEvent &event)
  204. {
  205. #ifdef __ANDROID__
  206. // clang-format off
  207. // display software keyboard when clicking edit boxes
  208. if (event.EventType == EET_MOUSE_INPUT_EVENT &&
  209. event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  210. gui::IGUIElement *hovered =
  211. Environment->getRootGUIElement()->getElementFromPoint(
  212. core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
  213. if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
  214. bool retval = hovered->OnEvent(event);
  215. if (retval)
  216. Environment->setFocus(hovered);
  217. std::string field_name = getNameByID(hovered->getID());
  218. // read-only field
  219. if (field_name.empty())
  220. return retval;
  221. m_jni_field_name = field_name;
  222. // single line text input
  223. int type = 2;
  224. // multi line text input
  225. if (((gui::IGUIEditBox *)hovered)->isMultiLineEnabled())
  226. type = 1;
  227. // passwords are always single line
  228. if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
  229. type = 3;
  230. porting::showInputDialog(gettext("OK"), "",
  231. wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
  232. return retval;
  233. }
  234. }
  235. #endif
  236. #ifdef HAVE_TOUCHSCREENGUI
  237. if (event.EventType == EET_TOUCH_INPUT_EVENT) {
  238. irr_ptr<GUIModalMenu> holder;
  239. holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
  240. switch ((int)event.TouchInput.touchedCount) {
  241. case 1: {
  242. if (event.TouchInput.Event == ETIE_PRESSED_DOWN || event.TouchInput.Event == ETIE_MOVED)
  243. m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
  244. if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
  245. m_down_pos = m_pointer;
  246. gui::IGUIElement *hovered = Environment->getRootGUIElement()->getElementFromPoint(core::position2d<s32>(m_pointer));
  247. if (event.TouchInput.Event == ETIE_PRESSED_DOWN)
  248. Environment->setFocus(hovered);
  249. if (m_hovered != hovered) {
  250. leave();
  251. enter(hovered);
  252. }
  253. gui::IGUIElement *focused = Environment->getFocus();
  254. bool ret = simulateMouseEvent(focused, event.TouchInput.Event);
  255. if (!ret && m_hovered != focused)
  256. ret = simulateMouseEvent(m_hovered.get(), event.TouchInput.Event);
  257. if (event.TouchInput.Event == ETIE_LEFT_UP)
  258. leave();
  259. return ret;
  260. }
  261. case 2: {
  262. if (event.TouchInput.Event != ETIE_PRESSED_DOWN)
  263. return true; // ignore
  264. auto focused = Environment->getFocus();
  265. if (!focused)
  266. return true;
  267. SEvent rclick_event{};
  268. rclick_event.EventType = EET_MOUSE_INPUT_EVENT;
  269. rclick_event.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
  270. rclick_event.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
  271. rclick_event.MouseInput.X = m_pointer.X;
  272. rclick_event.MouseInput.Y = m_pointer.Y;
  273. focused->OnEvent(rclick_event);
  274. rclick_event.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
  275. rclick_event.MouseInput.ButtonStates = EMBSM_LEFT;
  276. focused->OnEvent(rclick_event);
  277. return true;
  278. }
  279. default: // ignored
  280. return true;
  281. }
  282. }
  283. #endif
  284. if (event.EventType == EET_MOUSE_INPUT_EVENT) {
  285. s32 x = event.MouseInput.X;
  286. s32 y = event.MouseInput.Y;
  287. gui::IGUIElement *hovered =
  288. Environment->getRootGUIElement()->getElementFromPoint(
  289. core::position2d<s32>(x, y));
  290. if (!isChild(hovered, this)) {
  291. if (DoubleClickDetection(event)) {
  292. return true;
  293. }
  294. }
  295. }
  296. return false;
  297. }
  298. #ifdef __ANDROID__
  299. bool GUIModalMenu::hasAndroidUIInput()
  300. {
  301. // no dialog shown
  302. if (m_jni_field_name.empty())
  303. return false;
  304. // still waiting
  305. if (porting::getInputDialogState() == -1)
  306. return true;
  307. // no value abort dialog processing
  308. if (porting::getInputDialogState() != 0) {
  309. m_jni_field_name.clear();
  310. return false;
  311. }
  312. return true;
  313. }
  314. #endif