feat: add "Add component" button

This commit is contained in:
2026-08-12 01:18:47 +02:00
parent 8c7302de49
commit c3c1d5ae28
5 changed files with 146 additions and 59 deletions
@@ -5,6 +5,7 @@
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <destrum/ObjectModel/Component.h>
#include <destrum/ObjectModel/GameObject.h>
@@ -17,6 +18,13 @@ public:
Registry()[typeName] = std::move(createFn);
}
template <typename TComponent>
static void Register(const char* typeName) {
Register(std::string{typeName}, [](GameObject& owner) -> Component* {
return owner.AddComponent<TComponent>();
});
}
static Component* Create(const std::string& typeName, GameObject& owner) {
const auto it = Registry().find(typeName);
@@ -31,6 +39,15 @@ public:
return Registry().contains(typeName);
}
[[nodiscard]] static std::vector<std::string> GetRegisteredTypeNames() {
std::vector<std::string> typeNames;
typeNames.reserve(Registry().size());
for (const auto& [typeName, _] : Registry()) {
typeNames.push_back(typeName);
}
return typeNames;
}
private:
static std::unordered_map<std::string, CreateFn>& Registry() {
static std::unordered_map<std::string, CreateFn> registry;
@@ -0,0 +1,27 @@
#ifndef DESTRUM_ENGINECOMPONENTLIST_H
#define DESTRUM_ENGINECOMPONENTLIST_H
// Keep the component manifest in one place. Consumers define X before
// expanding one of these lists to generate registration or other metadata.
#include <destrum/Components/Animator.h>
#include <destrum/Components/MeshRendererComponent.h>
#include <destrum/Components/OrbitAndSpin.h>
#include <destrum/Components/Rotator.h>
#include <destrum/Components/Spinner.h>
#include <destrum/Components/Physics/BoxCollider.h>
#include <destrum/Components/Physics/CapsuleCollider.h>
#include <destrum/Components/Physics/Rigidbody.h>
#include <destrum/Components/Physics/SphereCollider.h>
#define DESTRUM_ENGINE_COMPONENTS(X) \
X(MeshRendererComponent) \
X(Rotator) \
X(Spinner) \
X(OrbitAndSpin) \
X(Animator) \
X(Rigidbody) \
X(BoxCollider) \
X(SphereCollider) \
X(CapsuleCollider)
#endif // DESTRUM_ENGINECOMPONENTLIST_H