SuperTuxKart
abstract_state_manager.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2010-2015 SuperTuxKart-Team
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (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 General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_ABSTRACT_STATE_MANAGER_HPP
20 #define HEADER_ABSTRACT_STATE_MANAGER_HPP
21 
22 #include <vector>
23 #include <string>
24 #include "guiengine/engine.hpp"
25 #include "guiengine/screen.hpp"
26 #include "utils/leak_check.hpp"
27 
31 namespace GUIEngine
32 {
33  class Widget;
34  class Screen;
35 
39  enum GameState
40  {
41  MENU,
42  GAME,
43  INGAME_MENU,
46  }; // GameState
47 
53  {
54  protected:
59 
64  std::vector<std::pair<std::string, Screen*> > m_menu_stack;
65 
66  void pushMenu(Screen* screen);
67 
68  void setGameState(GameState state);
69 
70  public:
71 
72  LEAK_CHECK()
73 
74 
76 
77  virtual ~AbstractStateManager() { }
78 
80  void pushScreen(Screen* screen);
81 
88 
95  void popMenu();
96 
101  void resetAndGoToScreen(Screen* screen);
102 
111  void resetAndSetStack(Screen* screens[]);
112 
118  void enterGameState();
119 
122 
124  void reshowTopMostMenu();
125 
126  template<typename T>
127  void hardResetAndGoToScreen()
128  {
130 
131  GUIEngine::clearScreenCache();
132 
133  T* instance = T::getInstance();
134 
135  m_menu_stack.emplace_back(instance->getName(), instance);
136  setGameState(MENU);
137 
138  switchToScreen(instance);
139  getCurrentScreen()->init();
140 
141  onTopMostScreenChanged();
142  }
143 
144  /* ***********************************
145  * methods to override in children *
146  *********************************** */
147 
152  virtual void escapePressed() = 0;
153 
158  virtual void onGameStateChange(GameState new_state) = 0;
159 
165  virtual void onStackEmptied() = 0;
166 
167  virtual void onTopMostScreenChanged() = 0;
168 
169  // --------------------------------------------------------------------
172  unsigned int getMenuStackSize() const
173  {
174  return (unsigned int)m_menu_stack.size();
175  }
176  }; // Class AbstractStateManager
177 
178 } // GUIEngine
179 #endif
GameState getGameState()
Definition: abstract_state_manager.cpp:67
void reshowTopMostMenu()
to be called after e.g.
Definition: abstract_state_manager.cpp:185
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:31
void resetAndGoToScreen(Screen *screen)
clears the menu stack and starts afresh with a new stack containing only the given screen
Definition: abstract_state_manager.cpp:256
void resetAndSetStack(Screen *screens[])
Sets the whole menu stack.
Definition: abstract_state_manager.cpp:283
virtual void escapePressed()=0
callback invoked whenever escape was pressed (or any similar cancel operation)
Dummy GameState e.
Definition: abstract_state_manager.hpp:45
virtual void onStackEmptied()=0
callback invoked when the stack is emptied (all menus are popped out).
virtual void init()
Callback invoked when entering this menu (after the widgets have been added).
Definition: screen.cpp:94
GameState
Definition: abstract_state_manager.hpp:39
void enterGameState()
call to make the state manager enter game mode.
Definition: abstract_state_manager.cpp:50
void replaceTopMostScreen(Screen *screen, GUIEngine::GameState gameState=GUIEngine::CURRENT)
replaces the menu at the top of the screens stack (i.e.
Definition: abstract_state_manager.cpp:148
void switchToScreen(Screen *screen)
Low-level mean to change current screen.
Definition: engine.cpp:899
virtual void tearDown()
Callback invoked before leaving this menu.
Definition: screen.cpp:116
unsigned int getMenuStackSize() const
Returns the number of screens on the stack.
Definition: abstract_state_manager.hpp:172
Represents a single GUI screen.
Definition: screen.hpp:94
void popMenu()
removes the menu at the top of the screens stack If the stack becomes empty after performing the pop ...
Definition: abstract_state_manager.cpp:211
virtual void onGameStateChange(GameState new_state)=0
callback invoked when game mode changes (e.g.
std::vector< std::pair< std::string, Screen * > > m_menu_stack
This stack will contain menu names (e.g.
Definition: abstract_state_manager.hpp:64
void pushScreen(Screen *screen)
adds a menu at the top of the screens stack
Definition: abstract_state_manager.cpp:127
GameState m_game_mode
Whether we are in game mode.
Definition: abstract_state_manager.hpp:58
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:138
Screen * getCurrentScreen()
Definition: engine.hpp:166
Abstract base class you must override from to use the GUI engine.
Definition: abstract_state_manager.hpp:52