ezEngine  Milestone 7
Component.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <Foundation/Communication/Message.h>
7 
8 #include <Core/World/Declarations.h>
9 
18 class EZ_CORE_DLL ezComponent : public ezReflectedClass
19 {
21 
22 protected:
24  ezComponent();
25  virtual ~ezComponent();
26 
27 public:
29  bool IsDynamic() const;
30 
32  void SetActive(bool bActive);
33 
35  void Activate();
36 
38  void Deactivate();
39 
41  bool IsActive() const;
42 
44  ezComponentManagerBase* GetManager() const;
45 
47  ezGameObject* GetOwner();
48 
50  const ezGameObject* GetOwner() const;
51 
53  ezComponentHandle GetHandle() const;
54 
56  static ezUInt16 TypeId();
57 
59  static ezUInt16 GetNextTypeId();
60 
61  ezUInt32 m_uiEditorPickingID;
62 
63 protected:
64  friend class ezWorld;
65  friend class ezGameObject;
66  friend class ezComponentManagerBase;
67 
68  template <typename T>
69  ezComponentHandle GetHandle() const;
70 
72 
73 private:
74  virtual ezUInt16 GetTypeId() const = 0;
75 
77  virtual ezResult Initialize();
78 
80  virtual ezResult Deinitialize();
81 
83  bool IsInitialized() const;
84 
86  virtual void OnAfterAttachedToObject();
87 
89  virtual void OnBeforeDetachedFromObject();
90 
91  void OnMessage(ezMessage& msg);
92  void OnMessage(ezMessage& msg) const;
93 
94  ezGenericComponentId m_InternalId;
95 
96  ezComponentManagerBase* m_pManager;
97  ezGameObject* m_pOwner;
98 
99  static ezUInt16 TYPE_ID;
100  static ezUInt16 s_uiNextTypeId;
101 };
102 
103 #include <Core/World/Implementation/Component_inl.h>
104 
106 #define EZ_DECLARE_COMPONENT_TYPE(componentType, managerType) \
107  EZ_ADD_DYNAMIC_REFLECTION(componentType); \
108  public: \
109  ezComponentHandle GetHandle() const; \
110  managerType* GetManager() const; \
111  EZ_FORCE_INLINE ezWorld* GetWorld() const { return GetManager()->GetWorld(); } \
112  virtual ezUInt16 GetTypeId() const override { return TYPE_ID; } \
113  static EZ_FORCE_INLINE ezUInt16 TypeId() { return TYPE_ID; } \
114  static ezComponentHandle CreateComponent(ezWorld* pWorld, componentType*& pComponent) { return pWorld->GetComponentManager<managerType>()->CreateComponent(pComponent); } \
115  private: \
116  friend managerType; \
117  static ezUInt16 TYPE_ID;
118 
122 #define EZ_BEGIN_COMPONENT_TYPE(componentType, baseType, version, managerType) \
123  ezUInt16 componentType::TYPE_ID = ezComponent::GetNextTypeId(); \
124  ezComponentHandle componentType::GetHandle() const { return ezComponent::GetHandle<componentType>(); } \
125  managerType* componentType::GetManager() const { return static_cast<managerType*>(ezComponent::GetManager()); } \
126  EZ_BEGIN_DYNAMIC_REFLECTED_TYPE(componentType, baseType, version, ezRTTINoAllocator);
127 
129 #define EZ_END_COMPONENT_TYPE EZ_END_DYNAMIC_REFLECTED_TYPE
130 
131