Cafu Engine
CompModel.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_GAMESYS_COMPONENT_MODEL_HPP_INCLUDED
8 #define CAFU_GAMESYS_COMPONENT_MODEL_HPP_INCLUDED
9 
10 #include "CompBase.hpp"
11 
12 
13 namespace cf { namespace GuiSys { class GuiImplT; } }
14 class AnimPoseT;
15 class CafuModelT;
16 
17 
18 namespace cf
19 {
20  namespace GameSys
21  {
22  /// This component adds a 3D model to its entity.
23  /// Models can be used to add geometric detail to a map. Some models also have ready-made
24  /// "GUI fixtures" where scripted GUIs can be attached that players can interact with.
25  /// Use the CaWE Model Editor in order to import mesh and animation data for models, and
26  /// to prepare them for use in game maps.
28  {
29  public:
30 
31  /// The constructor.
33 
34  /// The copy constructor.
35  /// @param Comp The component to create a copy of.
36  ComponentModelT(const ComponentModelT& Comp);
37 
38  /// The destructor.
40 
41  /// Returns the current pose of this model (or `NULL` if there is no pose (yet)).
42  AnimPoseT* GetPose() const;
43 
44  /// Returns the GUI instance of this model, if it has one (or `NULL` otherwise).
46 
47  // Base class overrides.
48  ComponentModelT* Clone() const;
49  const char* GetName() const { return "Model"; }
50  void UpdateDependencies(EntityT* Entity);
51  unsigned int GetEditorColor() const { return 0x00FFFF; }
54  bool Render(bool FirstPersonView, float LodDist) const;
55  void DoServerFrame(float t);
56  void DoClientFrame(float t);
57 
58 
59  // The TypeSys related declarations for this class.
60  const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
61  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
62  static const cf::TypeSys::TypeInfoT TypeInfo;
63 
64 
65  protected:
66 
67  // The Lua API methods of this class.
68  static int GetNumAnims(lua_State* LuaState);
69  static int GetNumSkins(lua_State* LuaState);
70  static int GetGui(lua_State* LuaState);
71  static int toString(lua_State* LuaState);
72 
73  static const luaL_Reg MethodsList[]; ///< The list of Lua methods for this class.
74  static const char* DocClass;
75  static const cf::TypeSys::MethsDocT DocMethods[];
76  static const cf::TypeSys::MethsDocT DocCallbacks[];
77  static const cf::TypeSys::VarsDocT DocVars[];
78 
79 
80  private:
81 
82  /// A variable of type std::string, specifically for model file names. It updates the related
83  /// model instance in the parent ComponentModelT whenever a new model file name is set.
84  class VarModelNameT : public TypeSys::VarT<std::string>
85  {
86  public:
87 
88  VarModelNameT(const char* Name, const std::string& Value, const char* Flags[], ComponentModelT& Comp);
89  VarModelNameT(const VarModelNameT& Var, ComponentModelT& Comp);
90 
91  // Base class overrides.
92  std::string GetExtraMessage() const { return m_ExtraMsg; }
93  void Serialize(cf::Network::OutStreamT& Stream) const; ///< See base class documentation for details!
94  void Deserialize(cf::Network::InStreamT& Stream);
95  void Set(const std::string& v);
96 
97 
98  private:
99 
100  ComponentModelT& m_Comp; ///< The parent ComponentModelT that contains this variable.
101  std::string m_ExtraMsg; ///< If the model could not be loaded in Set(), this message contains details about the problem.
102  };
103 
104 
105  /// A variable of type int, specifically for model animation sequence numbers. It updates the
106  /// related model pose in the parent ComponentModelT whenever a new sequence number is set.
107  class VarModelAnimNrT : public TypeSys::VarT<int>
108  {
109  public:
110 
111  VarModelAnimNrT(const char* Name, const int& Value, const char* Flags[], ComponentModelT& Comp);
112  VarModelAnimNrT(const VarModelAnimNrT& Var, ComponentModelT& Comp);
113 
114  // Base class overrides.
115  void Set(const int& v);
116  void GetChoices(ArrayT<std::string>& Strings, ArrayT<int>& Values) const;
117 
118 
119  private:
120 
121  void SetAnimNr(int AnimNr);
122 
123  ComponentModelT& m_Comp; ///< The parent ComponentModelT that contains this variable.
124  };
125 
126 
127  /// A variable of type int, specifically for model skin numbers.
128  class VarModelSkinNrT : public TypeSys::VarT<int>
129  {
130  public:
131 
132  VarModelSkinNrT(const char* Name, const int& Value, const char* Flags[], ComponentModelT& Comp);
133  VarModelSkinNrT(const VarModelSkinNrT& Var, ComponentModelT& Comp);
134 
135  // Base class overrides.
136  void GetChoices(ArrayT<std::string>& Strings, ArrayT<int>& Values) const;
137 
138 
139  private:
140 
141  ComponentModelT& m_Comp; ///< The parent ComponentModelT that contains this variable.
142  };
143 
144 
145  /// A variable of type std::string, specifically for GUI file names. It updates the related
146  /// GUI instance in the parent ComponentModelT whenever a new GUI file name is set.
147  class VarGuiNameT : public TypeSys::VarT<std::string>
148  {
149  public:
150 
151  VarGuiNameT(const char* Name, const std::string& Value, const char* Flags[], ComponentModelT& Comp);
152  VarGuiNameT(const VarGuiNameT& Var, ComponentModelT& Comp);
153 
154  // Base class overrides.
155  void Set(const std::string& v);
156 
157 
158  private:
159 
160  ComponentModelT& m_Comp; ///< The parent ComponentModelT that contains this variable.
161  };
162 
163 
164  void FillMemberVars(); ///< A helper method for the constructors.
165  void ReInit(std::string* ErrorMsg=NULL); ///< A helper method.
166 
167  TypeSys::VarT<bool> m_ModelShow; ///< Whether the model is currently shown.
168  VarModelNameT m_ModelName; ///< The file name of the model.
169  VarModelAnimNrT m_ModelAnimNr; ///< The animation sequence number of the model.
170  VarModelSkinNrT m_ModelSkinNr; ///< The skin used for rendering the model.
171  TypeSys::VarT<float> m_ModelScale; ///< The scale factor applied to the model coordinates when converted to world space.
172  VarGuiNameT m_GuiName; ///< The file name of the GUI to be used with the models GUI fixtures (if there are any).
173  // TypeSys::VarT<bool> m_CastShadows; ///< Does this model cast shadows when lit by a dynamic light source?
174  TypeSys::VarT<bool> m_IsSubmodel; ///< Is this model a submodel of another model?
175  TypeSys::VarT<bool> m_Is1stPerson; ///< Is this a 1st-person view model? If `true`, the model is rendered if the world is rendered from *this* entity's perspective. If `false`, the model is rendered when seen from the outside, i.e. in everybody else's view. The default is `false`, because `true` is normally only used with the human player's 1st-person carried weapon models.
176 
177  const CafuModelT* m_Model; ///< The model instance, updated by changes to m_ModelName.
178  mutable AnimPoseT* m_Pose; ///< The pose of the model, updated by changes to m_ModelAnimNr.
179  mutable IntrusivePtrT<GuiSys::GuiImplT> m_Gui; ///< The GUI instance, updated by changes to m_GuiName.
180  };
181  }
182 }
183 
184 #endif
BoundingBox3fT GetCullingBB() const
This method returns a bounding-box that encloses the visual representation of this component...
Definition: CompModel.cpp:570
This class represents a native Cafu model.
Definition: Model_cmdl.hpp:45
AnimPoseT * GetPose() const
Returns the current pose of this model (or NULL if there is no pose (yet)).
Definition: CompModel.cpp:363
unsigned int GetEditorColor() const
Returns a color that the Map Editor can use to render the representation of this component's entity...
Definition: CompModel.hpp:51
void Deserialize(cf::Network::InStreamT &Stream, bool IsIniting)
Reads the state of this component from the given stream, and updates the component accordingly...
Definition: CompBase.cpp:116
void Serialize(cf::Network::OutStreamT &Stream) const
Writes the current state of this component into the given stream.
Definition: CompBase.cpp:98
This class represents game entities, which are the basic elements of a world.
Definition: Entity.hpp:53
This is a "wrapper" around a normal C++ variable.
Definition: SetCompVar.hpp:15
ComponentModelT * Clone() const
The virtual copy constructor.
Definition: CompModel.cpp:488
~ComponentModelT()
The destructor.
Definition: CompModel.cpp:354
This class is used for reading data from a StateT instance (deserialization).
Definition: State.hpp:207
IntrusivePtrT< cf::GuiSys::GuiImplT > GetGui() const
Returns the GUI instance of this model, if it has one (or NULL otherwise).
Definition: CompModel.cpp:376
static const luaL_Reg MethodsList[]
The list of Lua methods for this class.
Definition: CompModel.hpp:73
ComponentModelT()
The constructor.
Definition: CompModel.cpp:299
This class is used for writing data into a StateT instance (serialization).
Definition: State.hpp:81
BoundingBox3fT GetEditorBB() const
Returns a bounding-box that the Map Editor can use to render the representation of this component's e...
Definition: CompModel.cpp:544
void UpdateDependencies(EntityT *Entity)
This method is called whenever something "external" to this component has changed: ...
Definition: CompModel.cpp:536
This class describes a specific pose of an associated model.
Definition: AnimPose.hpp:35
bool Render(bool FirstPersonView, float LodDist) const
This method implements the graphical output of this component.
Definition: CompModel.cpp:576
This component adds a 3D model to its entity.
Definition: CompModel.hpp:27
void DoClientFrame(float t)
Derived classes override this method in order to implement the real work proposed by OnClientFrame()...
Definition: CompModel.cpp:676
const char * GetName() const
Returns the name of this component.
Definition: CompModel.hpp:49
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
void DoServerFrame(float t)
Derived classes override this method in order to implement the real work proposed by OnServerFrame()...
Definition: CompModel.cpp:650
Definition: TypeSys.hpp:68
This is the base class for the components that an entity is composed/aggregated of.
Definition: CompBase.hpp:54