SuperTuxKart
screen.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2010-2015 Marianne Gagnon
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 3
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 
19 #ifndef HEADER_SCREEN_HPP
20 #define HEADER_SCREEN_HPP
21 
22 #include <map>
23 #include <string>
24 #include <typeinfo>
25 #include "utils/cpp2011.hpp"
26 
27 #include <irrString.h>
28 #include <IXMLReader.h>
29 
30 namespace irr
31 {
32  namespace gui { class IGUIElement; }
33 }
34 using namespace irr;
35 
36 #include "config/stk_config.hpp"
37 #include "guiengine/abstract_top_level_container.hpp"
38 #include "guiengine/engine.hpp"
39 #include "guiengine/event_handler.hpp"
40 #include "guiengine/widget.hpp"
41 #include "input/input.hpp"
42 #include "utils/leak_check.hpp"
43 #include "utils/ptr_vector.hpp"
44 
45 #include <functional>
46 
50 namespace GUIEngine
51 {
57  template<typename SCREEN>
59  {
60  protected:
61  static SCREEN* singleton;
62  public:
63 
65  {
66  singleton = NULL;
67  }
68 
69  static SCREEN* getInstance()
70  {
71  if (singleton == NULL)
72  {
73  singleton = new SCREEN();
74  GUIEngine::addScreenToList(singleton);
75  }
76 
77  return singleton;
78  }
79  };
80  template <typename SCREEN> SCREEN*
82 
94  {
95  private:
100 
101  bool m_loaded;
102 
107 
112 
114  unsigned int m_magic_number;
115 
116  unsigned m_width, m_height;
117 
118  friend class Skin;
119 
120  std::string m_filename;
122  std::function<Screen*()> m_screen_func;
123  public:
124 
125  LEAK_CHECK()
126 
127 
135  static void parseScreenFileDiv(irr::io::IXMLReader* xml,
136  PtrVector<Widget>& append_to,
137  irr::gui::IGUIElement* parent = NULL);
138 
139  Screen(bool pause_race=true);
140 
141  Screen(const char* filename, bool pause_race=true);
142 
143  virtual ~Screen();
144 
145  bool operator ==(const char* filename) const { return m_filename == filename; }
146 
147  void loadFromFile();
148 
150  bool isLoaded() const { return m_loaded; }
151 
152  void addWidgets();
153 
154  void calculateLayout();
155 
156  void manualAddWidget(Widget* w);
157 
158  void manualRemoveWidget(Widget* w);
159 
163  void setUpdateInBackground(bool value) {m_update_in_background = value;}
164  bool getUpdateInBackground() {return m_update_in_background;}
165 
167  const std::string& getName() const { return m_filename; }
168 
169  virtual void unload();
170 
174  bool needs3D() { return m_render_3d; }
175 
186  void setNeeds3D(bool needs3D) { m_render_3d = needs3D; }
187 
203  virtual void loadedFromFile() = 0;
204 
215  virtual void unloaded() {}
216 
222  virtual void beforeAddingWidget() {}
223 
233  virtual void init();
234 
237  void push();
238 
247  virtual void tearDown();
248 
254  virtual bool onEscapePressed() { return true; }
255 
266  virtual void eventCallback(Widget* widget, const std::string& name, const int playerID) = 0;
267 
271  virtual void onUpdate(float dt) { };
272 
276  virtual void onDraw(float dt) { };
277 
281  virtual void onResize();
282 
286  virtual MusicInformation* getMusic() const { return stk_config->m_title_music; }
287 
291  virtual MusicInformation* getInGameMenuMusic() const { return NULL; }
292 
293  virtual int getWidth() { return m_width; }
294 
295  virtual int getHeight() { return m_height; }
296 
301  virtual EventPropagation filterActions(PlayerAction action,
302  int deviceID,
303  const unsigned int value,
304  Input::InputType type,
305  int playerId)
306  { return EVENT_LET; }
307 
312  virtual void onDisabledItemClicked(const std::string& item) {}
313 
318  virtual void filterInput(Input::InputType type,
319  int deviceID,
320  int btnID,
321  int axisDir,
322  int value) {}
323 
327  virtual void onDialogClose() {}
328 
330  virtual void onFocusChanged(Widget* previous, Widget* focus, int playerID) {}
331  };
332 
333  class CutsceneScreen : public Screen
334  {
335  public:
336  CutsceneScreen(const char* name) : Screen(name, false)
337  {
338  setNeeds3D(true);
339  }
340 
341  virtual void onCutsceneEnd() = 0;
342  };
343 }
344 
345 #endif
Represents a GUI widgets container.
Definition: abstract_top_level_container.hpp:43
Definition: screen.hpp:334
Declares a class to be a singleton.
Definition: screen.hpp:59
Represents a single GUI screen.
Definition: screen.hpp:94
virtual void onDialogClose()
Callback that gets called when a dialog is closed.
Definition: screen.hpp:327
bool m_pause_race
True if the race (if it is running) should be paused when this screen is shown.
Definition: screen.hpp:99
virtual void eventCallback(Widget *widget, const std::string &name, const int playerID)=0
will be called everytime something happens.
unsigned int m_magic_number
to catch errors as early as possible, for debugging purposes only
Definition: screen.hpp:114
bool m_update_in_background
When set to true it updates the screen even if modal dialog is opened.
Definition: screen.hpp:111
virtual void filterInput(Input::InputType type, int deviceID, int btnID, int axisDir, int value)
Override this if you need to be notified of raw input in subclasses.
Definition: screen.hpp:318
virtual void onDisabledItemClicked(const std::string &item)
Callback you can use if you want to know when the user pressed on a disabled ribbon item.
Definition: screen.hpp:312
bool isLoaded() const
Definition: screen.hpp:150
virtual void beforeAddingWidget()
Optional callback invoked very early, before widgets have been added (contrast with init(),...
Definition: screen.hpp:222
virtual void onFocusChanged(Widget *previous, Widget *focus, int playerID)
Callback called when focus changes.
Definition: screen.hpp:330
virtual void loadedFromFile()=0
Callback invoked when loading this menu.
virtual void onUpdate(float dt)
optional callback you can override to be notified at every frame.
Definition: screen.hpp:271
virtual MusicInformation * getInGameMenuMusic() const
Definition: screen.hpp:291
const std::string & getName() const
Definition: screen.hpp:167
virtual bool onEscapePressed()
Called when escape is pressed.
Definition: screen.hpp:254
void setNeeds3D(bool needs3D)
Invoke this method for screens that use a 3D scene as background.
Definition: screen.hpp:186
void setUpdateInBackground(bool value)
When set to true it updates the screen even if modal dialog is opened.
Definition: screen.hpp:163
virtual MusicInformation * getMusic() const
Definition: screen.hpp:286
virtual void unloaded()
Callback invoked when this screen is being unloaded.
Definition: screen.hpp:215
bool needs3D()
Will be called to determine if the 3D scene must be rendered when at this screen.
Definition: screen.hpp:174
virtual void onDraw(float dt)
optional callback you can override to be notified at every frame.
Definition: screen.hpp:276
virtual EventPropagation filterActions(PlayerAction action, int deviceID, const unsigned int value, Input::InputType type, int playerId)
Override this if you need to be notified of player actions in subclasses.
Definition: screen.hpp:301
bool m_render_3d
Will be called to determine if the 3D scene must be rendered when at this screen.
Definition: screen.hpp:106
Object used to render the GUI widgets see Overview of GUI skin for more information about skinning in...
Definition: skin.hpp:280
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:147
Wrapper around an instance of the Music interface Adds information like composer, song title,...
Definition: music_information.hpp:43
Definition: ptr_vector.hpp:44
MusicInformation * m_title_music
Filename of the title music to play.
Definition: stk_config.hpp:170
PlayerAction
types of input events / what actions the players can do
Definition: input.hpp:117
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:33
void addScreenToList(Screen *cutscene)
Add a screen to the list of screens known by the gui engine.
Definition: engine.cpp:964