Elevate Engine 1
Loading...
Searching...
No Matches
Elevate::ComponentRegistry Class Reference

#include <ComponentRegistry.h>

Classes

struct  Entry
 

Static Public Member Functions

template<typename T >
static auto GetParentFieldsIfPossible (const T *obj) -> std::vector< ComponentField >
 
template<typename T >
static void Register (const std::string &name, EECategory category, std::vector< FieldOption > &options)
 
static std::unordered_map< std::type_index, Entry > & GetEntries ()
 
static std::string GetName (const std::type_info &type)
 
static std::vector< std::string > & ClassPaths ()
 
static std::vector< std::string > & CompilationClassStack ()
 
static std::vector< ComponentField > & CompilationClassFieldStack ()
 
template<typename T >
static constexpr EngineDataType DeduceEngineDataType ()
 
static std::string GetCleanedName (std::string rawName)
 
static void AddClassToStack (std::string newClass)
 
static void PopClassStack ()
 
static std::map< std::string, std::vector< ComponentField > > & GetCustomComponentFields ()
 
template<typename Class , typename FieldType >
static void AddProperty (FieldType Class::*member, const std::string &name, std::initializer_list< FieldOption > options)
 

Detailed Description

Definition at line 104 of file ComponentRegistry.h.

Member Function Documentation

◆ AddClassToStack()

void Elevate::ComponentRegistry::AddClassToStack ( std::string  newClass)
static

Definition at line 57 of file ComponentRegistry.cpp.

58{
59#ifdef EE_REGISTRY_LOG
60 EE_CORE_INFO("{}", GetCleanedName(newClass));
61#endif
62 CompilationClassStack().push_back(newClass);
63}
static std::vector< std::string > & CompilationClassStack()
static std::string GetCleanedName(std::string rawName)

◆ AddProperty()

template<typename Class , typename FieldType >
void Elevate::ComponentRegistry::AddProperty ( FieldType Class::*  member,
const std::string &  name,
std::initializer_list< FieldOption options 
)
static

Definition at line 75 of file ComponentRegistry.inl.

76 {
77 constexpr EngineDataType type = DeduceEngineDataType<FieldType>();
78 FieldMeta meta;
79 for (auto&& opt : options) {
80 if (std::holds_alternative<FlattenTag>(opt)) {
81 meta.flatten = true;
82 }
83 else if (std::holds_alternative<DisplayNameTag>(opt)) {
84 meta.displayName = std::get<DisplayNameTag>(opt).value;
85 }
86 else if (std::holds_alternative<TooltipTag>(opt)) {
87 meta.tooltip = std::get<TooltipTag>(opt).text;
88 }
89 else if (std::holds_alternative<ReadOnlyTag>(opt)) {
90 meta.readOnly = true;
91 }
92 else if (std::holds_alternative<ColorTag>(opt)) {
93 meta.isColor = true;
94 }
95 }
96
97 std::string cleanedName = GetCleanedName(name);
98
99#ifdef EE_REGISTRY_LOG
100 EE_TRACE(" --> Exposed field : %s flatten=%d displayName=%s", cleanedName.c_str(), meta.flatten, meta.displayName.c_str());
101#endif
102
103 auto& customFields = GetCustomComponentFields();
104 std::string typeName = typeid(FieldType).name();
105 size_t offset = reinterpret_cast<size_t>(&(reinterpret_cast<Class const volatile*>(0)->*member));
106
107 ComponentField field;
108 if (customFields.find(typeName) != customFields.end())
109 {
110 field = ComponentField(cleanedName, EngineDataType::Custom, offset, meta.displayName, customFields[typeName]);
111 field.flatten = meta.flatten;
112 }
113 else
114 {
115 field = ComponentField(cleanedName, type, offset, meta.displayName);
116 }
117 field.isColor = meta.isColor;
118 field.tooltip = meta.tooltip;
119 field.readOnly = meta.readOnly;
120 CompilationClassFieldStack().push_back(field);
121 }
#define EE_TRACE(...)
Definition Log.h:66
static std::map< std::string, std::vector< ComponentField > > & GetCustomComponentFields()
static std::vector< ComponentField > & CompilationClassFieldStack()
EngineDataType
Definition Data.h:106

◆ ClassPaths()

static std::vector< std::string > & Elevate::ComponentRegistry::ClassPaths ( )
inlinestatic

Definition at line 133 of file ComponentRegistry.h.

133 {
134 static std::vector<std::string> paths;
135 return paths;
136 }

◆ CompilationClassFieldStack()

static std::vector< ComponentField > & Elevate::ComponentRegistry::CompilationClassFieldStack ( )
inlinestatic

Definition at line 143 of file ComponentRegistry.h.

144 {
145 static std::vector<ComponentField> stack;
146 return stack;
147 }

◆ CompilationClassStack()

static std::vector< std::string > & Elevate::ComponentRegistry::CompilationClassStack ( )
inlinestatic

Definition at line 138 of file ComponentRegistry.h.

138 {
139 static std::vector<std::string> stack;
140 return stack;
141 }

◆ DeduceEngineDataType()

template<typename T >
static constexpr EngineDataType Elevate::ComponentRegistry::DeduceEngineDataType ( )
inlinestaticconstexpr

Definition at line 150 of file ComponentRegistry.h.

151 {
152 return EngineDataTypeTrait<std::decay_t<T>>::value;
153 }

◆ GetCleanedName()

std::string Elevate::ComponentRegistry::GetCleanedName ( std::string  rawName)
static

Definition at line 16 of file ComponentRegistry.cpp.

17{
18 std::string cleanedName = "";
19
20 for (int i = 0; i < rawName.length(); i++)
21 {
22 char c = rawName.at(i); // Current char
23 if (i == 0)
24 {
25 if ((c == 'm' || c == 's' || c == 'g') && rawName.at(i + 1) == '_') // If we have a notation of Hungarian style : m_myVariable
26 {
27 i = 1;
28 continue;
29 }
30 else
31 {
32 cleanedName += std::toupper(c);
33 }
34 }
35 else if (rawName.length() > i + 1 && std::isupper(c) && std::islower(rawName.at(i + 1)))
36 {
37 cleanedName += ' ';
38 cleanedName += c;
39 }
40 else if (std::isalpha(c) && rawName.at(i - 1) == '_')
41 {
42 cleanedName += std::toupper(c);
43 }
44 else if (c == '_')
45 {
46 cleanedName += ' ';
47 }
48 else
49 {
50 cleanedName += c;
51 }
52 }
53
54 return cleanedName;
55}

◆ GetCustomComponentFields()

static std::map< std::string, std::vector< ComponentField > > & Elevate::ComponentRegistry::GetCustomComponentFields ( )
inlinestatic

Definition at line 161 of file ComponentRegistry.h.

162 {
163 static std::map<std::string, std::vector<ComponentField>> m_customComponentFields;
164 return m_customComponentFields;
165 }

◆ GetEntries()

static std::unordered_map< std::type_index, Entry > & Elevate::ComponentRegistry::GetEntries ( )
inlinestatic

Definition at line 126 of file ComponentRegistry.h.

126 {
127 static std::unordered_map<std::type_index, Entry> entries;
128 return entries;
129 }

◆ GetName()

std::string Elevate::ComponentRegistry::GetName ( const std::type_info &  type)
static

Definition at line 4 of file ComponentRegistry.cpp.

5{
6 auto& entries = GetEntries();
7 auto it = entries.find(std::type_index(type));
8 if (it != entries.end()) {
9 return it->second.name;
10 }
11 else {
12 return type.name();
13 }
14}
static std::unordered_map< std::type_index, Entry > & GetEntries()

◆ GetParentFieldsIfPossible()

template<typename T >
static auto Elevate::ComponentRegistry::GetParentFieldsIfPossible ( const T *  obj) -> std::vector<ComponentField>
inlinestatic

Definition at line 107 of file ComponentRegistry.h.

107 {
108 return ParentFieldsHelper<T>::Get(obj);
109 }
static std::vector< ComponentField > Get()

◆ PopClassStack()

void Elevate::ComponentRegistry::PopClassStack ( )
static

Definition at line 65 of file ComponentRegistry.cpp.

66{
67 if (CompilationClassStack().empty())
68 {
69 EE_CORE_ERROR("ERROR: Tried to PopClassStack but stack is empty!");
70 return;
71 }
72
73 std::string fullName;
74 for (int i = 0; i < CompilationClassStack().size(); i++)
75 {
76 fullName.append(CompilationClassStack()[i]);
77 if (i != CompilationClassStack().size() - 1)
78 {
79 fullName.append("/");
80 }
81 }
82 CompilationClassStack().pop_back();
83 ClassPaths().push_back(fullName);
84}
static std::vector< std::string > & ClassPaths()

◆ Register()

template<typename T >
void Elevate::ComponentRegistry::Register ( const std::string &  name,
EECategory  category,
std::vector< FieldOption > &  options 
)
static

Definition at line 33 of file ComponentRegistry.inl.

33 {
34 std::type_index ti(typeid(T));
35
36 bool visible = true;
37 std::string iconPath;
38 for (FieldOption& option : options)
39 {
40 if (std::holds_alternative<HideInInspectorTag>(option)) {
41 visible = false;
42 }
43 else if (std::holds_alternative<EditorIconTag>(option)) {
44 iconPath = std::get<EditorIconTag>(option).Path;
45 }
46 }
47
48 GetEntries().emplace(ti, Entry{
49 name,
50 ti,
51 category,
52 [](std::weak_ptr<GameObject> go) -> Component* {
53 if (std::shared_ptr<GameObject> obj = go.lock()) {
54 return obj->GetComponent<T>();
55 }
56 return nullptr;
57 },
58 [](std::weak_ptr<GameObject> go) -> Component* {
59 if (std::shared_ptr<GameObject> obj = go.lock()) {
60 return &obj->AddComponent<T>();
61 }
62 return nullptr;
63 },
64 [](std::weak_ptr<GameObject> go) -> void {
65 if (std::shared_ptr<GameObject> obj = go.lock()) {
66 obj->RemoveComponent<T>();
67 }
68 },
69 visible,
70 iconPath
71 });
72 }
std::variant< HideInInspectorTag, EditorIconTag, FlattenTag, DisplayNameTag, TooltipTag, ReadOnlyTag, ColorTag > FieldOption

The documentation for this class was generated from the following files: