Cafu Engine
GuiImpl.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_GUISYS_GUI_HPP_INCLUDED
8 #define CAFU_GUISYS_GUI_HPP_INCLUDED
9 
10 #include "MaterialSystem/MaterialManagerImpl.hpp"
11 #include "Templates/Pointer.hpp"
12 #include "TypeSys.hpp"
13 
14 #include <stdexcept>
15 
16 
17 namespace cf { class UniScriptStateT; }
18 namespace MatSys { class RenderMaterialT; }
19 struct CaKeyboardEventT;
20 struct CaMouseEventT;
21 struct lua_State;
22 
23 
24 namespace cf
25 {
26  namespace GuiSys
27  {
28  /// The TypeInfoTs of all GuiImplT-derived classes must register with this TypeInfoManT instance.
29  cf::TypeSys::TypeInfoManT& GetGuiTIM();
30 
31 
32  class GuiResourcesT;
33  class WindowT;
34 
35  /// Note that it is very difficult to change these constants later, because then all GUI scripts
36  /// in the world had to be changed too (and in a non-trivial way)!
37  const float VIRTUAL_SCREEN_SIZE_X=640.0f;
38  const float VIRTUAL_SCREEN_SIZE_Y=480.0f;
39 
40 
41  /// This class implements a Graphical User Interface (GUI).
42  class GuiImplT : public RefCountedT
43  {
44  public:
45 
46  class InitErrorT;
47 
48  /// Flags for initializing a GUI from a script.
50  {
51  InitFlag_InlineCode = 1, ///< Normally, the `GuiScriptName` parameter to the GuiImplT ctor is a filename. If this is set, it is treated as inline script code.
52  InitFlag_InGuiEditor = 2 ///< Whether the GUI is instantiated in the GUI Editor. If set, only the static data will be loaded, initial behaviour is *not* run.
53  };
54 
55  /// Initializes the given script state for use with GuiImplT instances.
56  static void InitScriptState(UniScriptStateT& ScriptState);
57 
58 
59  /// Constructor for creating a window hierarchy (=="a GUI") from the GUI script file GuiScriptName.
60  /// @param ScriptState The caller will use this GUI with this script state (binds the GUI to it).
61  /// @param GuiRes The provider for resources (fonts and models) that are used in this GUI.
62  GuiImplT(UniScriptStateT& ScriptState, GuiResourcesT& GuiRes);
63 
64  /// Constructor for creating a window hierarchy (=="a GUI") from the GUI script file GuiScriptName.
65  ///
66  /// This constructor is *DEPRECATED*. It only exists so that code that uses it can be updated to
67  /// the new constructor at a convenient time. It should not be used in any new code.
68  ///
69  /// @param GuiRes The provider for resources (fonts and models) that are used in this GUI.
70  GuiImplT(GuiResourcesT& GuiRes);
71 
72  /// The destructor.
73  ~GuiImplT();
74 
75  /// A method that is needed when the obsolete, deprecated ctor above is used.
76  /// Without this method, it is impossible to break cycles of IntrusivePtrT%s.
77  void ObsoleteForceKill();
78 
79  /// Assigns the given GUI to the global "gui" and loads the given script in order to initialize it.
80  /// @param ScriptName The file name of the script to load or inline script code (if InitFlag_InlineCode is set).
81  /// @param Flags A combination of the flags in InitFlagsT.
82  /// @throws Throws an InitErrorT object on problems initializing the GUI.
83  void LoadScript(const std::string& ScriptName, int Flags = 0);
84 
85  /// Returns the material manager instance of this GUI.
86  const MaterialManagerImplT& GetMaterialManager() const { return m_MaterialMan; }
87 
88  /// Returns the default RenderMaterialT that should be used for borders and backgrounds if no other material is specified for that window.
90 
91  /// Returns the (default) RenderMaterialT for the mouse pointer.
93 
94  /// Returns the resource provider for fonts and models that are used in this GUI.
95  GuiResourcesT& GetGuiResources() const { return m_GuiResources; }
96 
97  /// Returns the name of the script file of this GUI.
98  const std::string& GetScriptName() const;
99 
100  /// Returns the script state of this GUI.
101  UniScriptStateT& GetScriptState() { return *m_ScriptState; }
102 
103  /// Returns the root window of this GUI.
105 
106  /// Returns the window in this GUI that has the keyboard input focus.
108 
109  /// Activates or deactivates this GUI.
110  void Activate(bool doActivate=true);
111 
112  /// Returns whether this GUI is active or not. This is of importance mainly for the GuiMan, which doesn't send us events and doesn't draw us if we're not active.
113  bool GetIsActive() const { return IsActive; }
114 
115  /// Sets whether this GUI is interactive or not. See GetIsInteractive() for additional information.
116  void SetInteractive(bool IsInteractive_=true);
117 
118  /// Returns whether this GUI is interactive (reacts to device events) or not. This is of important mainly for the GuiMan, which doesn't send us device events if we are not interactive, and sends device events only to the top-most interactive GUI.
119  bool GetIsInteractive() const { return IsInteractive; }
120 
121  /// Returns whether this GUI is fullscreen and fully opaque, i.e. whether this GUI covers everything under it. If true, the GuiSys saves the rendering of the GUIs "below" this one. This can improve the GUI performance significantly if e.g. the player is at a point in the game where the world rendering FPS is low.
122  bool GetIsFullCover() const { return IsFullCover; }
123 
124  /// Returns the position of the mouse cursor.
125  void GetMousePos(float& MousePosX_, float& MousePosY_) const;
126 
127  /// Sets the position of the mouse cursor.
128  void SetMousePos(float MousePosX_, float MousePosY_);
129 
130  /// Returns the size of the mouse cursor.
131  float GetMouseCursorSize() const { return m_MouseCursorSize; }
132 
133  /// Sets the size of the mouse cursor.
134  void SetMouseCursorSize(float s) { m_MouseCursorSize = s; }
135 
136  /// Sets whether this GUI shows a mouse cursor.
137  void SetShowMouse(bool ShowMouse_);
138 
139  /// Returns whether this GUI shows a mouse cursor.
140  bool IsMouseShown() const { return MouseIsShown; }
141 
142  /// Renders this GUI.
143  /// Note that this method does *not* setup any of the MatSys's model, view or projection matrices:
144  /// it's up to the caller to do that.
145  /// @param zLayerCoating Whether a z-layer coating should be applied to the GUI screen when finishing the rendering.
146  /// This is useful whenever the z-ordering of scene elements can be imperfect, e.g. in the Map Editor.
147  /// Generally, 3D world GUIs should use \c true, 2D GUIs should use \c false.
148  void Render(bool zLayerCoating=false) const;
149 
150  /// Processes a keyboard event by forwarding it to the window that currently has the input focus.
151  /// The GuiMan should make the descision to call this method dependend on the result of the GetIsInteractive() method.
152  /// @param KE Keyboard event to process.
153  /// @returns true if the device has been successfully processed, false otherwise.
154  bool ProcessDeviceEvent(const CaKeyboardEventT& KE);
155 
156  /// Processes a mouse event by forwarding it to the window that currently has the input focus.
157  /// The GuiMan should make the descision to call this method dependend on the result of the GetIsInteractive() method.
158  /// @param ME Mouse event to process.
159  /// @returns true if the device has been successfully processed, false otherwise.
160  bool ProcessDeviceEvent(const CaMouseEventT& ME);
161 
162  /// "Creates" a time tick event for each window of the GUI (no matter whether its currently visible (shown) or not)
163  /// by calling its OnTimeTickEvent() methods.
164  /// @param t The time in seconds since the last clock-tick.
165  void DistributeClockTickEvents(float t);
166 
167 
168  // The TypeSys related declarations for this class.
169  /*virtual*/ const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
170  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
171  static const cf::TypeSys::TypeInfoT TypeInfo;
172 
173 
174  private:
175 
176  GuiImplT(const GuiImplT&); ///< Use of the Copy Constructor is not allowed.
177  void operator = (const GuiImplT&); ///< Use of the Assignment Operator is not allowed.
178 
179  void Init(); ///< Calls the OnInit() script methods of all windows.
180 
181 
182  std::string ScriptName; ///< The name of the *.cgui file that contains this GUI's script.
183  UniScriptStateT* m_ScriptState; ///< The script state of this GUI.
184  const bool m_IsOwnScriptSt; ///< Are we the owner of the m_ScriptState instance?
185  MaterialManagerImplT m_MaterialMan; ///< The material manager for the materials that are used in this GUI.
186  MatSys::RenderMaterialT* m_GuiDefaultRM; ///< Used for the window borders and the backgrounds if no other material is specified.
187  MatSys::RenderMaterialT* m_GuiPointerRM; ///< Used for the mouse pointer.
188  MatSys::RenderMaterialT* m_GuiFinishZRM; ///< Used for laying-down z-buffer values after all GUI elements have been rendered.
189  GuiResourcesT& m_GuiResources; ///< The provider for resources (fonts and models) that are used in this GUI.
190 
191  IntrusivePtrT<WindowT> RootWindow; ///< The root window of the window hierarchy that forms this GUI.
192  IntrusivePtrT<WindowT> FocusWindow; ///< The window in the hierachy that currently has the (keyboard) input focus.
193  IntrusivePtrT<WindowT> MouseOverWindow; ///< The window that the mouse is currently hovering over.
194 
195  bool m_IsInited; ///< Has the Init() method already been called?
196  bool IsActive; ///< Whether this GUI is active or not. This is of importance mainly for the GuiMan, which doesn't send us events and doesn't draw us if we're not active.
197  bool IsInteractive; ///< Whether this GUI is interactive (reacts to device events) or not. This is of importance mainly for the GuiMan, which doesn't send us device events if we are not interactive, and sends device events only to the top-most interactive GUI.
198  bool IsFullCover; ///< Whether this GUI is fullscreen and fully opaque, i.e. whether this GUI covers everything under it. If true, the GuiSys saves the rendering of the GUIs "below" this one. This can improve the GUI performance significantly if e.g. the player is at a point in the game where the world rendering FPS is low.
199  float MousePosX; ///< The x-coordinate of the position of the mouse cursor.
200  float MousePosY; ///< The y-coordinate of the position of the mouse cursor.
201  float m_MouseCursorSize; ///< The size of the mouse cursor.
202  bool MouseIsShown; ///< Whether the mouse cursor is shown. Non-interactive GUIs normally don't show a cursor.
203 
204  // Gui variables (general purpose)... (Maus-unabhängig, z.B. aktuelle Lift-Position............. übers Netzwerk sync'en!!)
205  // ...
206 
207 
208  // Methods called from Lua scripts on cf::GuiSys::GuiTs.
209  static int Activate(lua_State* LuaState); ///< Sets the IsActive flag of this GUI.
210  static int Close(lua_State* LuaState); ///< Same as calling "gui:activate(false);".
211  static int SetInteractive(lua_State* LuaState); ///< Sets the IsInteractive flag of this GUI.
212  static int SetFullCover(lua_State* LuaState); ///< Sets the IsFullCover flag of this GUI.
213  static int SetMousePos(lua_State* LuaState); ///< Sets the position of the mouse cursor.
214  static int SetMouseCursorSize(lua_State* LuaState); ///< Sets the size of the mouse cursor.
215  static int SetMouseMat(lua_State* LuaState); ///< Sets the material that is used to render the mouse cursor.
216  static int SetMouseIsShown(lua_State* LuaState); ///< Determines whether the mouse cursor is shown at all.
217  static int CreateNew(lua_State* LuaState); ///< Creates and returns a new window or component.
218  static int SetFocus(lua_State* LuaState); ///< Sets the keyboard input focus to the given window. Does *not* call the Lua OnFocusLose() or OnFocusGain() scripts!
219  static int GetRootWindow(lua_State* LuaState); ///< Returns the root window of this GUI.
220  static int SetRootWindow(lua_State* LuaState); ///< Sets the root window for this GUI.
221  static int Init(lua_State* LuaState); ///< Calls the OnInit() script methods of all windows.
222  static int toString(lua_State* LuaState); ///< Returns a string representation of this GUI.
223 
224  static const luaL_Reg MethodsList[]; ///< List of methods registered with Lua.
225  static const char* DocClass;
226  static const cf::TypeSys::MethsDocT DocMethods[];
227  };
228  }
229 }
230 
231 
232 /// A class that is thrown on GUI initialization errors.
233 class cf::GuiSys::GuiImplT::InitErrorT : public std::runtime_error
234 {
235  public:
236 
237  InitErrorT(const std::string& Message);
238 };
239 
240 #endif
bool GetIsFullCover() const
Returns whether this GUI is fullscreen and fully opaque, i.e. whether this GUI covers everything unde...
Definition: GuiImpl.hpp:122
void ObsoleteForceKill()
A method that is needed when the obsolete, deprecated ctor above is used.
Definition: GuiImpl.cpp:288
bool ProcessDeviceEvent(const CaKeyboardEventT &KE)
Processes a keyboard event by forwarding it to the window that currently has the input focus...
Definition: GuiImpl.cpp:453
void Render(bool zLayerCoating=false) const
Renders this GUI.
Definition: GuiImpl.cpp:410
This class represents a surface render material.
Definition: RenderMaterial.hpp:25
bool GetIsInteractive() const
Returns whether this GUI is interactive (reacts to device events) or not. This is of important mainly...
Definition: GuiImpl.hpp:119
~GuiImplT()
The destructor.
Definition: GuiImpl.cpp:267
This class implements smart (reference-counted) pointers.
Definition: Pointer.hpp:43
UniScriptStateT & GetScriptState()
Returns the script state of this GUI.
Definition: GuiImpl.hpp:101
bool IsMouseShown() const
Returns whether this GUI shows a mouse cursor.
Definition: GuiImpl.hpp:140
void LoadScript(const std::string &ScriptName, int Flags=0)
Assigns the given GUI to the global "gui" and loads the given script in order to initialize it...
Definition: GuiImpl.cpp:128
IntrusivePtrT< WindowT > GetFocusWindow() const
Returns the window in this GUI that has the keyboard input focus.
Definition: GuiImpl.cpp:343
void SetMouseCursorSize(float s)
Sets the size of the mouse cursor.
Definition: GuiImpl.hpp:134
This struct describes a mouse event.
Definition: OpenGLWindow.hpp:185
void Activate(bool doActivate=true)
Activates or deactivates this GUI.
Definition: GuiImpl.cpp:349
void DistributeClockTickEvents(float t)
"Creates" a time tick event for each window of the GUI (no matter whether its currently visible (show...
Definition: GuiImpl.cpp:550
const MaterialManagerImplT & GetMaterialManager() const
Returns the material manager instance of this GUI.
Definition: GuiImpl.hpp:86
float GetMouseCursorSize() const
Returns the size of the mouse cursor.
Definition: GuiImpl.hpp:131
This class manages the type infos.
Definition: TypeSys.hpp:145
InitFlagsT
Flags for initializing a GUI from a script.
Definition: GuiImpl.hpp:49
This class implements the MaterialManagerI interface.
Definition: MaterialManagerImpl.hpp:23
This class implements a Graphical User Interface (GUI).
Definition: GuiImpl.hpp:42
GuiResourcesT & GetGuiResources() const
Returns the resource provider for fonts and models that are used in this GUI.
Definition: GuiImpl.hpp:95
This struct describes a keyboard event.
Definition: OpenGLWindow.hpp:20
A class that is thrown on GUI initialization errors.
Definition: GuiImpl.hpp:233
const std::string & GetScriptName() const
Returns the name of the script file of this GUI.
Definition: GuiImpl.cpp:331
bool GetIsActive() const
Returns whether this GUI is active or not. This is of importance mainly for the GuiMan, which doesn't send us events and doesn't draw us if we're not active.
Definition: GuiImpl.hpp:113
Whether the GUI is instantiated in the GUI Editor. If set, only the static data will be loaded...
Definition: GuiImpl.hpp:52
IntrusivePtrT< WindowT > GetRootWindow() const
Returns the root window of this GUI.
Definition: GuiImpl.cpp:337
void SetShowMouse(bool ShowMouse_)
Sets whether this GUI shows a mouse cursor.
Definition: GuiImpl.cpp:404
MatSys::RenderMaterialT * GetDefaultRM() const
Returns the default RenderMaterialT that should be used for borders and backgrounds if no other mater...
Definition: GuiImpl.cpp:319
static void InitScriptState(UniScriptStateT &ScriptState)
Initializes the given script state for use with GuiImplT instances.
Definition: GuiImpl.cpp:58
MatSys::RenderMaterialT * GetPointerRM() const
Returns the (default) RenderMaterialT for the mouse pointer.
Definition: GuiImpl.cpp:325
void SetInteractive(bool IsInteractive_=true)
Sets whether this GUI is interactive or not. See GetIsInteractive() for additional information...
Definition: GuiImpl.cpp:364
This class represents the state of a script: the underlying Lua state, pending coroutines, metatables for C++ class hierarchies, etc.
Definition: UniScriptState.hpp:214
GuiImplT(UniScriptStateT &ScriptState, GuiResourcesT &GuiRes)
Constructor for creating a window hierarchy (=="a GUI") from the GUI script file GuiScriptName.
Definition: GuiImpl.cpp:79
Definition: TypeSys.hpp:52
Definition: TypeSys.hpp:57
void GetMousePos(float &MousePosX_, float &MousePosY_) const
Returns the position of the mouse cursor.
Definition: GuiImpl.cpp:370
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
Normally, the GuiScriptName parameter to the GuiImplT ctor is a filename. If this is set...
Definition: GuiImpl.hpp:51
void SetMousePos(float MousePosX_, float MousePosY_)
Sets the position of the mouse cursor.
Definition: GuiImpl.cpp:377
A base class for objects that are reference-counted with IntrusivePtrTs.
Definition: Pointer.hpp:13
This class manages and provides resources (fonts and models) for GuiImplT instances.
Definition: GuiResources.hpp:26