2618d3ccfb948d8389a498cfa66ba0e233fb417a
[oweals/minetest.git] / src / mainmenumanager.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 /*
23         All kinds of stuff that needs to be exposed from main.cpp
24 */
25 #include "debug.h" // assert
26 #include "modalMenu.h"
27 #include <list>
28
29 class IGameCallback
30 {
31 public:
32         virtual void exitToOS() = 0;
33         virtual void keyConfig() = 0;
34         virtual void disconnect() = 0;
35         virtual void changePassword() = 0;
36         virtual void changeVolume() = 0;
37
38         virtual void signalKeyConfigChange() = 0;
39 };
40
41 extern gui::IGUIEnvironment *guienv;
42 extern gui::IGUIStaticText *guiroot;
43
44 // Handler for the modal menus
45
46 class MainMenuManager : public IMenuManager
47 {
48 public:
49         virtual void createdMenu(gui::IGUIElement *menu)
50         {
51                 for(std::list<gui::IGUIElement*>::iterator
52                                 i = m_stack.begin();
53                                 i != m_stack.end(); ++i)
54                 {
55                         assert(*i != menu);
56                 }
57
58                 if(!m_stack.empty())
59                         m_stack.back()->setVisible(false);
60                 m_stack.push_back(menu);
61         }
62
63         virtual void deletingMenu(gui::IGUIElement *menu)
64         {
65                 // Remove all entries if there are duplicates
66                 bool removed_entry;
67                 do{
68                         removed_entry = false;
69                         for(std::list<gui::IGUIElement*>::iterator
70                                         i = m_stack.begin();
71                                         i != m_stack.end(); ++i)
72                         {
73                                 if(*i == menu)
74                                 {
75                                         m_stack.erase(i);
76                                         removed_entry = true;
77                                         break;
78                                 }
79                         }
80                 }while(removed_entry);
81
82                 /*core::list<GUIModalMenu*>::Iterator i = m_stack.getLast();
83                 assert(*i == menu);
84                 m_stack.erase(i);*/
85
86                 if(!m_stack.empty())
87                         m_stack.back()->setVisible(true);
88         }
89
90         // Returns true to prevent further processing
91         virtual bool preprocessEvent(const SEvent& event)
92         {
93                 if (m_stack.empty())
94                         return false;
95                 GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(m_stack.back());
96                 return mm && mm->preprocessEvent(event);
97         }
98
99         u32 menuCount()
100         {
101                 return m_stack.size();
102         }
103
104         bool pausesGame()
105         {
106                 for(std::list<gui::IGUIElement*>::iterator
107                                 i = m_stack.begin(); i != m_stack.end(); ++i)
108                 {
109                         GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(*i);
110                         if (mm && mm->pausesGame())
111                                 return true;
112                 }
113                 return false;
114         }
115
116         std::list<gui::IGUIElement*> m_stack;
117 };
118
119 extern MainMenuManager g_menumgr;
120
121 extern bool isMenuActive();
122
123 class MainGameCallback : public IGameCallback
124 {
125 public:
126         MainGameCallback() {}
127         virtual ~MainGameCallback() {}
128
129         virtual void exitToOS()
130         {
131                 shutdown_requested = true;
132         }
133
134         virtual void disconnect()
135         {
136                 disconnect_requested = true;
137         }
138
139         virtual void changePassword()
140         {
141                 changepassword_requested = true;
142         }
143
144         virtual void changeVolume()
145         {
146                 changevolume_requested = true;
147         }
148
149         virtual void keyConfig()
150         {
151                 keyconfig_requested = true;
152         }
153
154         virtual void signalKeyConfigChange()
155         {
156                 keyconfig_changed = true;
157         }
158
159
160         bool disconnect_requested = false;
161         bool changepassword_requested = false;
162         bool changevolume_requested = false;
163         bool keyconfig_requested = false;
164         bool shutdown_requested = false;
165
166         bool keyconfig_changed = false;
167 };
168
169 extern MainGameCallback *g_gamecallback;