mainmenumanager.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. }
  50. virtual void deletingMenu(gui::IGUIElement *menu)
  51. {
  52. // Remove all entries if there are duplicates
  53. bool removed_entry;
  54. do{
  55. removed_entry = false;
  56. for(std::list<gui::IGUIElement*>::iterator
  57. i = m_stack.begin();
  58. i != m_stack.end(); ++i)
  59. {
  60. if(*i == menu)
  61. {
  62. m_stack.erase(i);
  63. removed_entry = true;
  64. break;
  65. }
  66. }
  67. }while(removed_entry);
  68. /*core::list<GUIModalMenu*>::Iterator i = m_stack.getLast();
  69. assert(*i == menu);
  70. m_stack.erase(i);*/
  71. if(!m_stack.empty())
  72. m_stack.back()->setVisible(true);
  73. }
  74. // Returns true to prevent further processing
  75. virtual bool preprocessEvent(const SEvent& event)
  76. {
  77. if (m_stack.empty())
  78. return false;
  79. GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(m_stack.back());
  80. return mm && mm->preprocessEvent(event);
  81. }
  82. u32 menuCount()
  83. {
  84. return m_stack.size();
  85. }
  86. bool pausesGame()
  87. {
  88. for (gui::IGUIElement *i : m_stack) {
  89. GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(i);
  90. if (mm && mm->pausesGame())
  91. return true;
  92. }
  93. return false;
  94. }
  95. std::list<gui::IGUIElement*> m_stack;
  96. };
  97. extern MainMenuManager g_menumgr;
  98. extern bool isMenuActive();
  99. class MainGameCallback : public IGameCallback
  100. {
  101. public:
  102. MainGameCallback() = default;
  103. virtual ~MainGameCallback() = default;
  104. virtual void exitToOS()
  105. {
  106. shutdown_requested = true;
  107. }
  108. virtual void disconnect()
  109. {
  110. disconnect_requested = true;
  111. }
  112. virtual void changePassword()
  113. {
  114. changepassword_requested = true;
  115. }
  116. virtual void changeVolume()
  117. {
  118. changevolume_requested = true;
  119. }
  120. virtual void keyConfig()
  121. {
  122. keyconfig_requested = true;
  123. }
  124. virtual void signalKeyConfigChange()
  125. {
  126. keyconfig_changed = true;
  127. }
  128. bool disconnect_requested = false;
  129. bool changepassword_requested = false;
  130. bool changevolume_requested = false;
  131. bool keyconfig_requested = false;
  132. bool shutdown_requested = false;
  133. bool keyconfig_changed = false;
  134. };
  135. extern MainGameCallback *g_gamecallback;