Elevate Engine 1
Loading...
Searching...
No Matches
ComponentRegistry.h File Reference
#include <initializer_list>
#include <map>
#include <string>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <variant>
#include <entt/entt.hpp>
#include <glm/fwd.hpp>
#include <ElevateEngine/Core/GameObject.h>
#include <ElevateEngine/Core/Data.h>
#include <ElevateEngine/Core/Log.h>
#include <ElevateEngine/Core/Component.h>
#include <ElevateEngine/Core/EEObject.h>
#include <ElevateEngine/Editor/Serialization/ComponentLayout.h>
#include "ComponentRegistry.inl"

Go to the source code of this file.

Classes

struct  Elevate::has_super< T, typename >
 
struct  Elevate::has_super< T, std::void_t< typename T::Super > >
 
struct  Elevate::ParentFieldsHelper< T, bool >
 
struct  Elevate::ParentFieldsHelper< T, true >
 
struct  Elevate::HideInInspectorTag
 
struct  Elevate::FlattenTag
 
struct  Elevate::DisplayNameTag
 
struct  Elevate::TooltipTag
 
struct  Elevate::ReadOnlyTag
 
struct  Elevate::ColorTag
 
struct  Elevate::EditorIconTag
 
struct  Elevate::FieldMeta
 
struct  Elevate::EngineDataTypeTrait< T >
 
struct  Elevate::EngineDataTypeTrait< float >
 
struct  Elevate::EngineDataTypeTrait< int >
 
struct  Elevate::EngineDataTypeTrait< bool >
 
struct  Elevate::EngineDataTypeTrait< glm::vec2 >
 
struct  Elevate::EngineDataTypeTrait< glm::vec3 >
 
struct  Elevate::EngineDataTypeTrait< glm::vec4 >
 
class  Elevate::ComponentRegistry
 
struct  Elevate::ComponentRegistry::Entry
 

Namespaces

namespace  Elevate
 

Macros

#define HideInInspector   HideInInspectorTag{}
 
#define Flatten   FlattenTag{}
 
#define DisplayName(x)   DisplayNameTag{x}
 
#define Tooltip(x)   TooltipTag{x}
 
#define ReadOnly   ReadOnlyTag{}
 
#define ColorPicker   ColorTag{}
 
#define EditorIcon(path)   EditorIconTag{path}
 
#define EECATEGORY(name)
 
#define BEGIN_COMPONENT(T, ...)
 
#define EXPOSE(param, ...)
 
#define END_COMPONENT()
 
#define DECLARE_BASE(BaseType)
 
#define BEGIN_STRUCT(T)
 
#define END_STRUCT()
 

Typedefs

using Elevate::FieldOption = std::variant< HideInInspectorTag, EditorIconTag, FlattenTag, DisplayNameTag, TooltipTag, ReadOnlyTag, ColorTag >
 

Macro Definition Documentation

◆ BEGIN_COMPONENT

#define BEGIN_COMPONENT (   T,
  ... 
)
Value:
private: \
using ThisType = T; \
public: \
inline static struct T##ClassEntry { \
T##ClassEntry() { \
::Elevate::ComponentRegistry::AddClassToStack(#T); \
HasBaseClass = false; \
Options = { __VA_ARGS__ }; \
} \
std::vector<FieldOption> Options; \
EECategory Category; \
size_t FieldStartIndex = 0; \
std::string ClassName; \
std::vector<Elevate::ComponentField> ClassFieldStack; \
bool HasBaseClass = false; \
} generated_classEntry; \
public: \
virtual bool RemoveFromGameObject() override { \
if (gameObject) { \
gameObject->RemoveComponent<T>(); \
return true; \
} \
return false; \
}
static std::string GetCleanedName(std::string rawName)
static std::vector< ComponentField > & CompilationClassFieldStack()

Definition at line 187 of file ComponentRegistry.h.

188 : \
189using ThisType = T; \
190public: \
191inline static struct T##ClassEntry { \
192 T##ClassEntry() { \
194 ::Elevate::ComponentRegistry::AddClassToStack(#T); \
196 HasBaseClass = false; \
197 Options = { __VA_ARGS__ }; \
198 } \
199 std::vector<FieldOption> Options; \
200 EECategory Category; \
201 size_t FieldStartIndex = 0; \
202 std::string ClassName; \
203 std::vector<Elevate::ComponentField> ClassFieldStack; \
204 bool HasBaseClass = false; \
205} generated_classEntry; \
206public: \
207 virtual bool RemoveFromGameObject() override { \
208 if (gameObject) { \
209 gameObject->RemoveComponent<T>(); \
210 return true; \
211 } \
212 return false; \
213 }

◆ BEGIN_STRUCT

#define BEGIN_STRUCT (   T)
Value:
private: \
using ThisType = T; \
inline static struct T##StructEntry { \
T##StructEntry() { \
::Elevate::ComponentRegistry::AddClassToStack(#T); \
StructTypeName = typeid(T).name(); \
} \
size_t FieldStartIndex = 0; \
std::string StructName; \
std::string StructTypeName; \
std::vector<Elevate::ComponentField> StructFieldStack; \
} generated_structEntry; \
public:

Definition at line 331 of file ComponentRegistry.h.

332 : \
333 using ThisType = T; \
334 inline static struct T##StructEntry { \
335 T##StructEntry() { \
336 ::Elevate::ComponentRegistry::AddClassToStack(#T); \
339 StructTypeName = typeid(T).name(); \
340 } \
341 size_t FieldStartIndex = 0; \
342 std::string StructName; \
343 std::string StructTypeName; \
344 std::vector<Elevate::ComponentField> StructFieldStack; \
345 } generated_structEntry; \
346 public:

◆ ColorPicker

#define ColorPicker   ColorTag{}

Definition at line 69 of file ComponentRegistry.h.

◆ DECLARE_BASE

#define DECLARE_BASE (   BaseType)
Value:
using Super = BaseType; \
inline static struct BaseType##BaseClassDeclaration { \
BaseType##BaseClassDeclaration() { \
generated_classEntry.HasBaseClass = true; \
} \
} generated_baseDeclaration;

Definition at line 319 of file ComponentRegistry.h.

321 { \
322 BaseType##BaseClassDeclaration() { \
323 generated_classEntry.HasBaseClass = true; \
324 } \
325} generated_baseDeclaration;

◆ DisplayName

#define DisplayName (   x)    DisplayNameTag{x}

Definition at line 60 of file ComponentRegistry.h.

◆ EditorIcon

#define EditorIcon (   path)    EditorIconTag{path}

Definition at line 75 of file ComponentRegistry.h.

◆ EECATEGORY

#define EECATEGORY (   name)
Value:
private: \
inline static struct categoryRegistrar { \
categoryRegistrar() { \
generated_classEntry.Category = EECategory(name); \
} \
} generated_categoryRegistrar; \
virtual EECategory GetCategory() const override { return generated_classEntry.Category; } \
public:

Definition at line 172 of file ComponentRegistry.h.

173 : \
174 inline static struct categoryRegistrar { \
175 categoryRegistrar() { \
176 generated_classEntry.Category = EECategory(name); \
177 } \
178 } generated_categoryRegistrar; \
179 virtual EECategory GetCategory() const override { return generated_classEntry.Category; } \
180 public:

◆ END_COMPONENT

#define END_COMPONENT ( )

Definition at line 228 of file ComponentRegistry.h.

229 : \
230 inline static struct ClassEntryEnd { \
231 ClassEntryEnd() { \
232 ::Elevate::ComponentRegistry::Register<ThisType>( \
233 generated_classEntry.ClassName, \
234 generated_classEntry.Category, \
235 generated_classEntry.Options \
236 ); \
237 ::Elevate::ComponentRegistry::PopClassStack(); \
239 size_t start = generated_classEntry.FieldStartIndex; \
240 for (size_t i = start; i < global.size(); ++i) { \
241 generated_classEntry.ClassFieldStack.push_back(global[i]); \
242 } \
243 if (start < global.size()) { \
244 global.erase(global.begin() + start, global.end()); \
245 } \
246 } \
247 } generated_classEntryEnd; \
248public: \
249 inline virtual std::string GetName() const override { return generated_classEntry.ClassName; } \
250 inline virtual Elevate::ComponentLayout GetLayout() const override { \
251 std::vector<Elevate::ComponentField> instanceFields; \
252 if (generated_classEntry.HasBaseClass) { \
253 auto parentFields = ParentFieldsHelper<ThisType>::Get(); \
254 for (const Elevate::ComponentField& field : parentFields) { \
255 const void* fieldPtr = reinterpret_cast<const char*>(this) + field.offset; \
256 instanceFields.push_back(Elevate::ComponentField( \
257 field, fieldPtr \
258 )); \
259 } \
260 } \
261 for (const Elevate::ComponentField& field : generated_classEntry.ClassFieldStack) { \
262 const void* fieldPtr = reinterpret_cast<const char*>(this) + field.offset; \
263 instanceFields.push_back(Elevate::ComponentField(field, fieldPtr)); \
264 } \
265 return Elevate::ComponentLayout(generated_classEntry.ClassName, instanceFields); \
266 } \
267 virtual Component* Clone() override { \
268 ThisType* clone = new ThisType(); \
269 for (auto& field : ComponentRegistry::GetCustomComponentFields()[typeid(ThisType).name()]) { \
270 field.CopyValue(this, clone); \
271 } \
272 return clone; \
273 } \
274 virtual void CopyFrom(Component* other) override { \
275 if (auto o = dynamic_cast<ThisType*>(other)) { \
276 if (generated_classEntry.HasBaseClass) { \
277 auto parentFields = ParentFieldsHelper<ThisType>::Get(); \
278 for (const Elevate::ComponentField& field : parentFields) { \
279 field.CopyValue(o, this); \
280 } \
281 } \
282 for (const Elevate::ComponentField& field : generated_classEntry.ClassFieldStack) { \
283 field.CopyValue(o, this); \
284 } \
285 } \
286 else { \
287 EE_ERROR("Error: Tried setting a %s from a %s component in CopyFrom(Component*)", \
288 this->GetName(), other ? other->GetName() : "null"); \
289 } \
290 } \
291 virtual Elevate::GameObjectComponentFactory GetFactory() const override { \
292 auto& entries = ComponentRegistry::GetEntries(); \
293 auto it = entries.find(typeid(ThisType)); \
294 if (it != entries.end()) { \
295 return it->second.factory; \
296 } \
297 return nullptr; \
298 } \
299 virtual Elevate::GameObjectComponentDestructor GetDestructor() const override { \
300 auto& entries = ComponentRegistry::GetEntries(); \
301 auto it = entries.find(typeid(ThisType)); \
302 if (it != entries.end()) { \
303 return it->second.destructor; \
304 } \
305 return nullptr; \
306 } \
307 virtual const void* GetEditorIconHandle() const override { \
308 auto& entries = ComponentRegistry::GetEntries(); \
309 auto it = entries.find(typeid(ThisType)); \
310 if (it != entries.end()) { \
311 if(!it->second.editorIconPath.empty()) { \
312 return Texture::CreateFromFile(it->second.editorIconPath)->GetNativeHandle(); \
313 } \
314 } \
315 return nullptr; \
316 } \
317 virtual std::type_index GetTypeIndex() const override { return typeid(ThisType); }
std::function< Component *(std::weak_ptr< GameObject >)> GameObjectComponentFactory
Definition Component.h:21
std::function< void(std::weak_ptr< GameObject >)> GameObjectComponentDestructor
Definition Component.h:22

◆ END_STRUCT

#define END_STRUCT ( )
Value:
private: \
inline static struct StructEntryEnd { \
StructEntryEnd() { \
size_t start = generated_structEntry.FieldStartIndex; \
for (size_t i = start; i < global.size(); ++i) { \
generated_structEntry.StructFieldStack.push_back(global[i]); \
} \
if (start < global.size()) { \
global.erase(global.begin() + start, global.end()); \
} \
::Elevate::ComponentRegistry::GetCustomComponentFields()[generated_structEntry.StructTypeName] = generated_structEntry.StructFieldStack; \
} \
} generated_structEntryEnd;

Definition at line 348 of file ComponentRegistry.h.

349 : \
350 inline static struct StructEntryEnd { \
351 StructEntryEnd() { \
353 size_t start = generated_structEntry.FieldStartIndex; \
354 for (size_t i = start; i < global.size(); ++i) { \
355 generated_structEntry.StructFieldStack.push_back(global[i]); \
356 } \
357 if (start < global.size()) { \
358 global.erase(global.begin() + start, global.end()); \
359 } \
360 ::Elevate::ComponentRegistry::GetCustomComponentFields()[generated_structEntry.StructTypeName] = generated_structEntry.StructFieldStack; \
361 } \
362 } generated_structEntryEnd;

◆ EXPOSE

#define EXPOSE (   param,
  ... 
)
Value:
public: \
inline static struct param##PropertyEntry { \
param##PropertyEntry() { \
using MemberT = decltype(ThisType::param); \
::Elevate::ComponentRegistry::AddProperty<ThisType, MemberT>( \
&ThisType::param, \
#param, \
{ __VA_ARGS__ } \
); \
} \
} generated_##param##PropertyEntry;

Definition at line 215 of file ComponentRegistry.h.

216 : \
217inline static struct param##PropertyEntry { \
218 param##PropertyEntry() { \
219 using MemberT = decltype(ThisType::param); \
220 ::Elevate::ComponentRegistry::AddProperty<ThisType, MemberT>( \
221 &ThisType::param, \
222 #param, \
223 { __VA_ARGS__ } \
224 ); \
225 } \
226} generated_##param##PropertyEntry;

◆ Flatten

#define Flatten   FlattenTag{}

Definition at line 57 of file ComponentRegistry.h.

◆ HideInInspector

#define HideInInspector   HideInInspectorTag{}

Definition at line 54 of file ComponentRegistry.h.

◆ ReadOnly

#define ReadOnly   ReadOnlyTag{}

Definition at line 66 of file ComponentRegistry.h.

◆ Tooltip

#define Tooltip (   x)    TooltipTag{x}

Definition at line 63 of file ComponentRegistry.h.