VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
ComponentFactory.cpp
2#include "components/ErrorUtils.hpp"
3
4namespace vex {
5
6 ComponentRegistry& ComponentRegistry::getInstance() {
7 static ComponentRegistry instance;
8 return instance;
9 }
10
11#if DEBUG
12 void ComponentRegistry::SetEditorIcon(const std::string &name, ImTextureID icon)
13 {
14 m_editorIcons[name] = icon;
15 }
16
17 ImTextureID ComponentRegistry::GetEditorIcon(const std::string &name)
18 {
19 return m_editorIcons[name];
20 }
21
22 ImTextureID GetEditorIcon_Internal(const std::string &name)
23 {
24 return ComponentRegistry::getInstance().GetEditorIcon(name);
25 }
26#endif
27
28 void ComponentRegistry::unregisterComponent(const std::string& name) {
29 loaders.erase(name);
30 savers.erase(name);
31 inspectors.erase(name);
32 checkers.erase(name);
33 creators.erase(name);
34
35 auto it = std::remove(registeredNames.begin(), registeredNames.end(), name);
36 if (it != registeredNames.end()) {
37 registeredNames.erase(it, registeredNames.end());
38 }
39
40 log("Component '%s' unregistered", name.c_str());
41 }
42
44 if (dynamicComponents.empty()) return;
45
46 log("Hot Reload: Clearing %zu game components...", dynamicComponents.size());
47
48 for (const auto& name : dynamicComponents) {
49 // Manual unregister logic (copy of unregisterComponent but internal)
50 loaders.erase(name);
51 savers.erase(name);
52 inspectors.erase(name);
53 checkers.erase(name);
54 creators.erase(name);
55
56 auto it = std::remove(registeredNames.begin(), registeredNames.end(), name);
57 if (it != registeredNames.end()) {
58 registeredNames.erase(it, registeredNames.end());
59 }
60 }
61
62 dynamicComponents.clear();
63 }
64
65 bool ComponentRegistry::loadComponent(GameObject& obj, const std::string& type, const nlohmann::json& json) {
66 auto it = loaders.find(type);
67 if (it != loaders.end()) {
68 it->second(obj, json);
69 return true;
70 }
71 log("Error: Component type '%s' not registered", type.c_str());
72 return false;
73 }
74
75 nlohmann::json ComponentRegistry::saveComponent(GameObject& obj, const std::string& type) {
76 if (savers.find(type) != savers.end()) return savers[type](obj);
77 return nullptr;
78 }
79
81 for (auto& [name, inspector] : inspectors) {
82 inspector(obj);
83 }
84 }
85
86 std::vector<std::string> ComponentRegistry::getMissingComponents(const GameObject& obj) {
87 std::vector<std::string> missing;
88 for (const auto& name : registeredNames) {
89 if (checkers.find(name) != checkers.end() && !checkers[name](obj)) {
90 missing.push_back(name);
91 }
92 }
93 return missing;
94 }
95
96 void ComponentRegistry::createComponent(GameObject& obj, const std::string& name) {
97 if (creators.find(name) != creators.end()) {
98 creators[name](obj);
99 } else {
100 log("Error: Cannot create component '%s', not registered.", name.c_str());
101 }
102 }
103
104 const std::unordered_map<std::string, ComponentRegistry::ComponentInspector>& ComponentRegistry::getInspectors() const {
105 return inspectors;
106 }
107
108 const std::vector<std::string>& ComponentRegistry::getRegisteredNames() const {
109 return registeredNames;
110 }
111
112}
Contains ComponentRegistry class used to have register of posible to create components....
Class registering GameComponents mainly used to load them in SceneManager.
nlohmann::json saveComponent(GameObject &obj, const std::string &type)
Serializes a component on a GameObject to JSON.
const std::unordered_map< std::string, ComponentInspector > & getInspectors() const
Get component inspectors.
void clearDynamicComponents()
Clears all components registered as dynamic.
bool loadComponent(GameObject &obj, const std::string &type, const nlohmann::json &json)
Loads data into a component on a GameObject from JSON.
void createComponent(GameObject &obj, const std::string &name)
Creates and attaches a specific component to a game object.
std::vector< std::string > getMissingComponents(const GameObject &obj)
Get missing components for a game object.
const std::vector< std::string > & getRegisteredNames() const
Get registered component names.
void unregisterComponent(const std::string &name)
Unregisters a component type and removes all associated callbacks.
void drawInspectorForObject(GameObject &obj)
Draws the ImGui inspector for all components on the object.
Its base class for all game objects, eg. Player, Enemy, Weapon. Your Class needs to inherit from it.
void VEX_EXPORT log(const char *fmt,...)
Logs a formatted message.