Cafu Engine
CompBase.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_COMPONENT_BASE_HPP_INCLUDED
8 #define CAFU_GUISYS_COMPONENT_BASE_HPP_INCLUDED
9 
10 #include "Variables.hpp"
11 #include "Templates/Pointer.hpp"
12 
13 // This macro is introduced by some header (gtk?) under Linux...
14 #undef CurrentTime
15 
16 
17 namespace cf { namespace TypeSys { class TypeInfoT; } }
18 namespace cf { namespace TypeSys { class CreateParamsT; } }
19 namespace cf { namespace TypeSys { class MethsDocT; } }
20 namespace cf { namespace TypeSys { class VarsDocT; } }
21 
22 struct CaKeyboardEventT;
23 struct CaMouseEventT;
24 struct lua_State;
25 struct luaL_Reg;
26 
27 
28 namespace cf
29 {
30  namespace GuiSys
31  {
32  class WindowT;
33 
34  /// This is the base class for the components that a window is composed/aggregated of.
35  ///
36  /// Components are the basic building blocks of a window: their composition defines
37  /// the properties, the behaviour, and thus virtually every aspect of the window.
38  ///
39  /// Components can exist in two invariants:
40  /// - Stand-alone, independent and not a part of any window.
41  /// - Normally, as an active part of a window.
42  ///
43  /// Stand-alone components typically occur when they're newly instantiated, for example
44  /// when they are loaded from disk, when they are instantiated in scripts, or when they
45  /// are kept in the clipboard or managed in the Undo/Redo system of the GUI Editor.
46  /// Newly created, copied or cloned components are initially stand-alone.
47  ///
48  /// A component becomes a part of a window via the WindowT::AddComponent() method.
49  /// The window then knows the component, because it hosts it, and reversely, the
50  /// component then knows the parent window that it is a component of.
51  class ComponentBaseT : public RefCountedT
52  {
53  public:
54 
55  /// The constructor.
56  /// The newly created component is initially not a part of any window.
58 
59  /// The copy constructor.
60  /// The newly copied component is initially not a part of any window, even if the source component was.
61  /// @param Comp The component to create a copy of.
62  ComponentBaseT(const ComponentBaseT& Comp);
63 
64  /// The virtual copy constructor.
65  /// Callers can use this method to create a copy of this component without knowing its concrete type.
66  /// Overrides in derived classes use a covariant return type to facilitate use when the concrete type is known.
67  /// The newly cloned component is initially not a part of any window, even if the source component was.
68  virtual ComponentBaseT* Clone() const;
69 
70  /// The virtual destructor.
71  virtual ~ComponentBaseT() { }
72 
73 
74  /// Returns the parent window that contains this component,
75  /// or `NULL` if this component is currently not a part of any window.
76  WindowT* GetWindow() const { return m_Window; }
77 
78  /// Returns the variable manager that keeps generic references to our member variables,
79  /// providing a simple kind of "reflection" or "type introspection" feature.
80  TypeSys::VarManT& GetMemberVars() { return m_MemberVars; }
81 
82  /// Calls the given Lua method of this component.
83  /// This method is analogous to UniScriptStateT::CallMethod(), see there for details.
84  /// @param MethodName The name of the Lua method to call.
85  /// @param Signature See UniScriptStateT::Call() for details.
86  bool CallLuaMethod(const char* MethodName, const char* Signature="", ...);
87 
88  /// Returns the name of this component.
89  virtual const char* GetName() const { return "Base"; }
90 
91 
92  /// This method is called whenever something "external" to this component has changed:
93  /// - if the parent window has changed, because this component was added to or removed from it,
94  /// - if other components in the parent window have changed.
95  /// The component can use the opportunity to search the window for "sibling" components
96  /// that it depends on, and store direct pointers to them.
97  /// Note however that dependencies among components must not be cyclic, or else the deletion
98  /// of a window will leave a memory leak.
99  /// @param Window The parent window that contains this component, or `NULL` to indicate that this component is removed from the window that it used to be a part of.
100  virtual void UpdateDependencies(WindowT* Window);
101 
102  /// This method implements the graphical output of this component.
103  virtual void Render() const { }
104 
105  /// This method is called after all windows and their components have been loaded.
106  ///
107  /// It is called only once when the static part of GUI initializatzion is complete, i.e. after the initial
108  /// values of all windows and their components have been set.
109  /// Components can override this method in order act / do something / add custom behaviour at that time.
110  ///
111  /// For example, a choice component can use it to set the associated text component to the initial
112  /// selection, a script component can forward it to the script by calling a related script function,
113  /// a component that for backwards-compatibility supports reading old variables can convert to new ones, etc.
114  ///
115  /// @param OnlyStatic `true` if only the loading of static data is desired, e.g.
116  /// when the world is instantiated in the GUI Editor, `false` if also
117  /// user-defined scripts with custom, initial behaviour should be loaded.
118  virtual void OnPostLoad(bool OnlyStatic) { }
119 
120  /// This method handles keyboard input events.
121  /// @param KE Keyboard event instance.
122  /// @returns Whether the component handled ("consumed") the event.
123  virtual bool OnInputEvent(const CaKeyboardEventT& KE) { return false; }
124 
125  /// This method handles mouse input events.
126  /// @param ME Mouse event instance.
127  /// @param PosX x-coordinate of the mouse cursor position.
128  /// @param PosY y-coordinate of the mouse cursor position.
129  /// @returns Whether the component handled ("consumed") the event.
130  virtual bool OnInputEvent(const CaMouseEventT& ME, float PosX, float PosY) { return false; }
131 
132  /// This method handles clock-tick events.
133  /// @param t The time in seconds since the last clock-tick.
134  virtual void OnClockTickEvent(float t);
135 
136 
137  // The TypeSys related declarations for this class.
138  virtual const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
139  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
140  static const cf::TypeSys::TypeInfoT TypeInfo;
141 
142 
143  protected:
144 
145  // The Lua API methods of this class.
146  static int Get(lua_State* LuaState);
147  static int Set(lua_State* LuaState);
148  static int GetExtraMessage(lua_State* LuaState);
149  static int Interpolate(lua_State* LuaState);
150  static int toString(lua_State* LuaState);
151 
152  static const luaL_Reg MethodsList[]; ///< The list of Lua methods for this class.
153  static const char* DocClass;
154  static const cf::TypeSys::MethsDocT DocMethods[];
155 
156 
157  private:
158 
159  /// A helper structure for interpolations.
160  struct InterpolationT
161  {
162  cf::TypeSys::VarBaseT* Var; ///< The variable whose value is being interpolated.
163  unsigned int Suffix; ///< If the variable is composed of several values, this is the index of the one being interpolated.
164  float StartValue; ///< Start value of the interpolation.
165  float EndValue; ///< End value of the interpolation.
166  float CurrentTime; ///< Current time between 0 and TotalTime.
167  float TotalTime; ///< Duration of the interpolation.
168 
169  float GetCurrentValue() const { return StartValue + (EndValue-StartValue)*CurrentTime/TotalTime; }
170  };
171 
172 
173  void operator = (const ComponentBaseT&); ///< Use of the Assignment Operator is not allowed.
174 
175  WindowT* m_Window; ///< The parent window that contains this component, or `NULL` if this component is currently not a part of any window.
176  TypeSys::VarManT m_MemberVars; ///< The variable manager that keeps generic references to our member variables.
177  ArrayT<InterpolationT*> m_PendingInterp; ///< The currently pending interpolations.
178  };
179  }
180 }
181 
182 #endif
virtual void OnPostLoad(bool OnlyStatic)
This method is called after all windows and their components have been loaded.
Definition: CompBase.hpp:118
This class is a simple container for pointers to VarBaseTs.
Definition: Variables.hpp:326
virtual ComponentBaseT * Clone() const
The virtual copy constructor.
Definition: CompBase.cpp:44
TypeSys::VarManT & GetMemberVars()
Returns the variable manager that keeps generic references to our member variables, providing a simple kind of "reflection" or "type introspection" feature.
Definition: CompBase.hpp:80
virtual void Render() const
This method implements the graphical output of this component.
Definition: CompBase.hpp:103
This struct describes a mouse event.
Definition: OpenGLWindow.hpp:185
virtual bool OnInputEvent(const CaMouseEventT &ME, float PosX, float PosY)
This method handles mouse input events.
Definition: CompBase.hpp:130
bool CallLuaMethod(const char *MethodName, const char *Signature="",...)
Calls the given Lua method of this component.
Definition: CompBase.cpp:50
This is the base class for the components that a window is composed/aggregated of.
Definition: CompBase.hpp:51
ComponentBaseT()
The constructor.
Definition: CompBase.cpp:30
virtual ~ComponentBaseT()
The virtual destructor.
Definition: CompBase.hpp:71
This struct describes a keyboard event.
Definition: OpenGLWindow.hpp:20
static const luaL_Reg MethodsList[]
The list of Lua methods for this class.
Definition: CompBase.hpp:152
virtual void OnClockTickEvent(float t)
This method handles clock-tick events.
Definition: CompBase.cpp:68
This class represents a window of the GuiSys.
Definition: Window.hpp:54
WindowT * GetWindow() const
Returns the parent window that contains this component, or NULL if this component is currently not a ...
Definition: CompBase.hpp:76
This is the common base class for the VarT classes.
Definition: Variables.hpp:113
virtual const char * GetName() const
Returns the name of this component.
Definition: CompBase.hpp:89
virtual bool OnInputEvent(const CaKeyboardEventT &KE)
This method handles keyboard input events.
Definition: CompBase.hpp:123
virtual void UpdateDependencies(WindowT *Window)
This method is called whenever something "external" to this component has changed: ...
Definition: CompBase.cpp:62
Definition: TypeSys.hpp:52
Definition: TypeSys.hpp:57
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
A base class for objects that are reference-counted with IntrusivePtrTs.
Definition: Pointer.hpp:13