mainmenumanager.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /*
  18. All kinds of stuff that needs to be exposed from main.cpp
  19. */
  20. #include "modalMenu.h"
  21. #include <cassert>
  22. #include <list>
  23. class IGameCallback
  24. {
  25. public:
  26. virtual void exitToOS() = 0;
  27. virtual void keyConfig() = 0;
  28. virtual void disconnect() = 0;
  29. virtual void changePassword() = 0;
  30. virtual void changeVolume() = 0;
  31. virtual void signalKeyConfigChange() = 0;
  32. };
  33. extern gui::IGUIEnvironment *guienv;
  34. extern gui::IGUIStaticText *guiroot;
  35. // Handler for the modal menus
  36. class MainMenuManager : public IMenuManager
  37. {
  38. public:
  39. virtual void createdMenu(gui::IGUIElement *menu)
  40. {
  41. #ifndef NDEBUG
  42. for (gui::IGUIElement *i : m_stack) {
  43. assert(i != menu);
  44. }
  45. #endif
  46. if(!m_stack.empty())
  47. m_stack.back()->setVisible(false);
  48. m_stack.push_back(menu);
  49. guienv->setFocus(m_stack.back());
  50. }
  51. virtual void deletingMenu(gui::IGUIElement *menu)
  52. {
  53. // Remove all entries if there are duplicates
  54. m_stack.remove(menu);
  55. if(!m_stack.empty()) {
  56. m_stack.back()->setVisible(true);
  57. guienv->setFocus(m_stack.back());
  58. }
  59. }
  60. // Returns true to prevent further processing
  61. virtual bool preprocessEvent(const SEvent& event)
  62. {
  63. if (m_stack.empty())
  64. return false;
  65. GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(m_stack.back());
  66. return mm && mm->preprocessEvent(event);
  67. }
  68. u32 menuCount()
  69. {
  70. return m_stack.size();
  71. }
  72. bool pausesGame()
  73. {
  74. for (gui::IGUIElement *i : m_stack) {
  75. GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(i);
  76. if (mm && mm->pausesGame())
  77. return true;
  78. }
  79. return false;
  80. }
  81. std::list<gui::IGUIElement*> m_stack;
  82. };
  83. extern MainMenuManager g_menumgr;
  84. extern bool isMenuActive();
  85. class MainGameCallback : public IGameCallback
  86. {
  87. public:
  88. MainGameCallback() = default;
  89. virtual ~MainGameCallback() = default;
  90. virtual void exitToOS()
  91. {
  92. shutdown_requested = true;
  93. }
  94. virtual void disconnect()
  95. {
  96. disconnect_requested = true;
  97. }
  98. virtual void changePassword()
  99. {
  100. changepassword_requested = true;
  101. }
  102. virtual void changeVolume()
  103. {
  104. changevolume_requested = true;
  105. }
  106. virtual void keyConfig()
  107. {
  108. keyconfig_requested = true;
  109. }
  110. virtual void signalKeyConfigChange()
  111. {
  112. keyconfig_changed = true;
  113. }
  114. bool disconnect_requested = false;
  115. bool changepassword_requested = false;
  116. bool changevolume_requested = false;
  117. bool keyconfig_requested = false;
  118. bool shutdown_requested = false;
  119. bool keyconfig_changed = false;
  120. };
  121. extern MainGameCallback *g_gamecallback;