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  std::function<SCREEN*()> new_screen_function = []()
75  { return ScreenSingleton::getInstance(); };
76  singleton->setScreenPointerFunction(new_screen_function);
77  GUIEngine::addScreenToList(singleton);
78  }
79 
80  return singleton;
81  }
82  };
83  template <typename SCREEN> SCREEN*
85 
97  {
98 protected:
102  private:
107 
108  bool m_loaded;
109 
114 
119 
121  unsigned int m_magic_number;
122 
123  unsigned m_width, m_height;
124 
125  friend class Skin;
126 
127  std::string m_filename;
129  std::function<Screen*()> m_screen_func;
130  public:
131 
132  LEAK_CHECK()
133 
134 
142  static void parseScreenFileDiv(irr::io::IXMLReader* xml,
143  PtrVector<Widget>& append_to,
144  irr::gui::IGUIElement* parent = NULL);
145 
149  std::function<Screen*()> getNewScreenPointer() const { return m_screen_func; }
150 
151  void setScreenPointerFunction(const std::function<Screen*()>& f) { m_screen_func = f; }
152 
153  Screen(bool pause_race=true);
154 
155  Screen(const char* filename, bool pause_race=true);
156 
157  virtual ~Screen();
158 
159  bool operator ==(const char* filename) const { return m_filename == filename; }
160 
161  void loadFromFile();
162 
164  bool isLoaded() const { return m_loaded; }
165 
166  void addWidgets();
167 
168  void calculateLayout();
169 
170  void manualAddWidget(Widget* w);
171 
172  void manualRemoveWidget(Widget* w);
173 
177  void setUpdateInBackground(bool value) {m_update_in_background = value;}
178  bool getUpdateInBackground() {return m_update_in_background;}
179 
181  const std::string& getName() const { return m_filename; }
182 
183  virtual void unload();
184 
188  bool needs3D() { return m_render_3d; }
189 
200  void setNeeds3D(bool needs3D) { m_render_3d = needs3D; }
201 
217  virtual void loadedFromFile() = 0;
218 
229  virtual void unloaded() {}
230 
236  virtual void beforeAddingWidget() {}
237 
247  virtual void init();
248 
251  void push();
252 
261  virtual void tearDown();
262 
268  virtual bool onEscapePressed() { return true; }
269 
280  virtual void eventCallback(Widget* widget, const std::string& name, const int playerID) = 0;
281 
285  virtual void onUpdate(float dt) { };
286 
290  virtual void onDraw(float dt) { };
291 
295  virtual MusicInformation* getMusic() const { return stk_config->m_title_music; }
296 
300  virtual MusicInformation* getInGameMenuMusic() const { return NULL; }
301 
302  virtual int getWidth() { return m_width; }
303 
304  virtual int getHeight() { return m_height; }
305 
306  virtual bool isResizable() const { return m_resizable; }
311  virtual EventPropagation filterActions(PlayerAction action,
312  int deviceID,
313  const unsigned int value,
314  Input::InputType type,
315  int playerId)
316  { return EVENT_LET; }
317 
322  virtual void onDisabledItemClicked(const std::string& item) {}
323 
328  virtual void filterInput(Input::InputType type,
329  int deviceID,
330  int btnID,
331  int axisDir,
332  int value) {}
333 
337  virtual void onDialogClose() {}
338 
340  virtual void onFocusChanged(Widget* previous, Widget* focus, int playerID) {}
341  };
342 
343  class CutsceneScreen : public Screen
344  {
345  public:
346  CutsceneScreen(const char* name) : Screen(name, false)
347  {
348  setNeeds3D(true);
349  }
350 
351  virtual void onCutsceneEnd() = 0;
352  };
353 }
354 
355 #endif
bool m_resizable
True if this screen is resizable.
Definition: screen.hpp:101
Wrapper around an instance of the Music interface Adds information like composer, song title...
Definition: music_information.hpp:42
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:31
void addScreenToList(Screen *cutscene)
Add a screen to the list of screens known by the gui engine.
Definition: engine.cpp:959
PlayerAction
types of input events / what actions the players can do
Definition: input.hpp:116
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:311
Definition: three_d_animation.hpp:32
Represents a GUI widgets container.
Definition: abstract_top_level_container.hpp:42
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:322
void setUpdateInBackground(bool value)
When set to true it updates the screen even if modal dialog is opened.
Definition: screen.hpp:177
bool m_update_in_background
When set to true it updates the screen even if modal dialog is opened.
Definition: screen.hpp:118
virtual void onFocusChanged(Widget *previous, Widget *focus, int playerID)
Callback called when focus changes.
Definition: screen.hpp:340
bool isLoaded() const
Definition: screen.hpp:164
virtual void onUpdate(float dt)
optional callback you can override to be notified at every frame.
Definition: screen.hpp:285
virtual bool onEscapePressed()
Called when escape is pressed.
Definition: screen.hpp:268
bool m_pause_race
True if the race (if it is running) should be paused when this screen is shown.
Definition: screen.hpp:106
virtual void onDialogClose()
Callback that gets called when a dialog is closed.
Definition: screen.hpp:337
const std::string & getName() const
Definition: screen.hpp:181
virtual void beforeAddingWidget()
Optional callback invoked very early, before widgets have been added (contrast with init()...
Definition: screen.hpp:236
virtual MusicInformation * getMusic() const
Definition: screen.hpp:295
Definition: screen.hpp:343
Object used to render the GUI widgets see Overview of GUI skin for more information about skinning in...
Definition: skin.hpp:268
std::function< Screen *()> m_screen_func
For runtime screen reloading without template.
Definition: screen.hpp:129
bool needs3D()
Will be called to determine if the 3D scene must be rendered when at this screen. ...
Definition: screen.hpp:188
virtual void unloaded()
Callback invoked when this screen is being unloaded.
Definition: screen.hpp:229
Definition: ptr_vector.hpp:43
Represents a single GUI screen.
Definition: screen.hpp:96
Declares a class to be a singleton.
Definition: screen.hpp:58
virtual void onDraw(float dt)
optional callback you can override to be notified at every frame.
Definition: screen.hpp:290
virtual MusicInformation * getInGameMenuMusic() const
Definition: screen.hpp:300
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:328
void setNeeds3D(bool needs3D)
Invoke this method for screens that use a 3D scene as background.
Definition: screen.hpp:200
unsigned int m_magic_number
to catch errors as early as possible, for debugging purposes only
Definition: screen.hpp:121
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:142
bool m_render_3d
Will be called to determine if the 3D scene must be rendered when at this screen. ...
Definition: screen.hpp:113
MusicInformation * m_title_music
Filename of the title music to play.
Definition: stk_config.hpp:170