modalMenu.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes_extrabloated.h"
  18. #ifdef HAVE_TOUCHSCREENGUI
  19. #include "touchscreengui.h"
  20. #endif
  21. class GUIModalMenu;
  22. class IMenuManager
  23. {
  24. public:
  25. // A GUIModalMenu calls these when this class is passed as a parameter
  26. virtual void createdMenu(gui::IGUIElement *menu) = 0;
  27. virtual void deletingMenu(gui::IGUIElement *menu) = 0;
  28. };
  29. /*
  30. Remember to drop() the menu after creating, so that it can
  31. remove itself when it wants to.
  32. */
  33. class GUIModalMenu : public gui::IGUIElement
  34. {
  35. public:
  36. GUIModalMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent, s32 id,
  37. IMenuManager *menumgr):
  38. IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
  39. core::rect<s32>(0,0,100,100))
  40. {
  41. m_menumgr = menumgr;
  42. setVisible(true);
  43. Environment->setFocus(this);
  44. m_menumgr->createdMenu(this);
  45. }
  46. virtual ~GUIModalMenu()
  47. {
  48. m_menumgr->deletingMenu(this);
  49. }
  50. void allowFocusRemoval(bool allow)
  51. {
  52. m_allow_focus_removal = allow;
  53. }
  54. bool canTakeFocus(gui::IGUIElement *e)
  55. {
  56. return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
  57. }
  58. void draw()
  59. {
  60. if(!IsVisible)
  61. return;
  62. video::IVideoDriver* driver = Environment->getVideoDriver();
  63. v2u32 screensize = driver->getScreenSize();
  64. if(screensize != m_screensize_old /*|| m_force_regenerate_gui*/)
  65. {
  66. m_screensize_old = screensize;
  67. regenerateGui(screensize);
  68. //m_force_regenerate_gui = false;
  69. }
  70. drawMenu();
  71. }
  72. /*
  73. This should be called when the menu wants to quit.
  74. WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
  75. immediately if you call this from the menu itself.
  76. (More precisely, this decrements the reference count.)
  77. */
  78. void quitMenu()
  79. {
  80. allowFocusRemoval(true);
  81. // This removes Environment's grab on us
  82. Environment->removeFocus(this);
  83. m_menumgr->deletingMenu(this);
  84. this->remove();
  85. #ifdef HAVE_TOUCHSCREENGUI
  86. if (g_touchscreengui)
  87. g_touchscreengui->show();
  88. #endif
  89. }
  90. void removeChildren()
  91. {
  92. const core::list<gui::IGUIElement*> &children = getChildren();
  93. core::list<gui::IGUIElement*> children_copy;
  94. for (gui::IGUIElement *i : children) {
  95. children_copy.push_back(i);
  96. }
  97. for (gui::IGUIElement *i : children_copy) {
  98. i->remove();
  99. }
  100. }
  101. virtual void regenerateGui(v2u32 screensize) = 0;
  102. virtual void drawMenu() = 0;
  103. virtual bool preprocessEvent(const SEvent& event) { return false; };
  104. virtual bool OnEvent(const SEvent& event) { return false; };
  105. virtual bool pausesGame(){ return false; } // Used for pause menu
  106. protected:
  107. //bool m_force_regenerate_gui;
  108. v2u32 m_screensize_old;
  109. private:
  110. IMenuManager *m_menumgr;
  111. // This might be necessary to expose to the implementation if it
  112. // wants to launch other menus
  113. bool m_allow_focus_removal = false;
  114. };