From 52776186d3f88b3a9716fab754fb35763da7cdde Mon Sep 17 00:00:00 2001 From: WiggleWizard <1405402+WiggleWizard@users.noreply.github.com> Date: Mon, 16 Sep 2024 00:17:28 +0100 Subject: [PATCH] Added the ability to remove components --- Editor/src/Windows/EditorSelectionPanel.cpp | 5 +- Editor/src/Windows/EditorSelectionPanel.h | 2 +- Nuake/src/Core/Object/Object.h | 183 ++++++++++---------- Nuake/src/Scene/Entities/Entity.h | 26 +++ 4 files changed, 125 insertions(+), 91 deletions(-) diff --git a/Editor/src/Windows/EditorSelectionPanel.cpp b/Editor/src/Windows/EditorSelectionPanel.cpp index c4f35310..be5abe21 100644 --- a/Editor/src/Windows/EditorSelectionPanel.cpp +++ b/Editor/src/Windows/EditorSelectionPanel.cpp @@ -548,7 +548,7 @@ void EditorSelectionPanel::DrawNetScriptPanel(Ref file) ImGui::PopTextWrapPos(); } -void EditorSelectionPanel::DrawComponent(const Nuake::Entity& entity, entt::meta_any& component) +void EditorSelectionPanel::DrawComponent(Nuake::Entity& entity, entt::meta_any& component) { ZoneScoped; @@ -571,7 +571,8 @@ void EditorSelectionPanel::DrawComponent(const Nuake::Entity& entity, entt::meta if(removed) { - // entity.RemoveComponent(); + auto componentType = component.type(); + entity.RemoveComponent(componentType); ImGui::PopStyleVar(); delete boldFont; } diff --git a/Editor/src/Windows/EditorSelectionPanel.h b/Editor/src/Windows/EditorSelectionPanel.h index 3594f801..6d8ba30a 100644 --- a/Editor/src/Windows/EditorSelectionPanel.h +++ b/Editor/src/Windows/EditorSelectionPanel.h @@ -75,7 +75,7 @@ private: void DrawWrenScriptPanel(Ref wrenFile); void DrawNetScriptPanel(Ref file); - void DrawComponent(const Nuake::Entity& entity, entt::meta_any& component); + void DrawComponent(Nuake::Entity& entity, entt::meta_any& component); void DrawComponentContent(entt::meta_any& component); void DrawFieldTypeFloat(entt::meta_data& field, entt::meta_any& component); diff --git a/Nuake/src/Core/Object/Object.h b/Nuake/src/Core/Object/Object.h index 2cad7125..a135717a 100644 --- a/Nuake/src/Core/Object/Object.h +++ b/Nuake/src/Core/Object/Object.h @@ -22,6 +22,7 @@ namespace Nuake { NK_HASHED_STATIC_STR(GetComponentName) NK_HASHED_STATIC_STR(AddToEntity) + NK_HASHED_STATIC_STR(RemoveFromEntity) NK_HASHED_STATIC_STR(ActionName) }; @@ -75,92 +76,98 @@ namespace Nuake #define NUAKECOMPONENT(klass, componentName) \ public: \ - static std::string ClassName() \ - { \ - static std::string className = #klass; \ - return className; \ - } \ - \ - static std::string ComponentName() \ - { \ - static std::string name = componentName; \ - return name; \ - } \ - \ - static void AddToEntity(entt::entity entity, entt::registry* enttRegistry) \ - { \ - enttRegistry->emplace_or_replace(entity); \ - } \ - \ - \ - static void (*GetInitializeComponentClass())() \ - { \ - return &klass::InitializeComponentClass; \ - } \ - \ - inline static auto ComponentFactory = entt::meta(); \ - static void InternalInitializeClass() \ - { \ - static bool initialized = false; \ - if (initialized) \ - return; \ - \ - ComponentFactory.type(entt::hashed_string(#klass)) \ - .traits(ComponentTypeTrait::InspectorExposed); \ - ComponentFactory.func<&klass::ComponentName>(HashedFnName::GetComponentName); \ - ComponentFactory.func<&klass::AddToEntity>(HashedFnName::AddToEntity); \ - \ - if (klass::GetInitializeComponentClass() != Component::GetInitializeComponentClass()) \ - { \ - GetInitializeComponentClass()(); \ - } \ - \ - initialized = true; \ - \ - } \ - \ - template \ - static auto BindComponentField(const char* varName, const char* displayName) \ - { \ - return ComponentFactory \ - .data(entt::hashed_string(varName)) \ - .prop(HashedName::DisplayName, displayName); \ - } \ - \ - template \ - static auto BindComponentProperty(const char* varName, const char* displayName) \ - { \ - return ComponentFactory \ - .data(entt::hashed_string(varName)) \ - .prop(HashedName::DisplayName, displayName); \ - } \ - \ - static auto FieldFloatLimits(float stepSize, float min, float max) \ - { \ - return ComponentFactory \ - .prop(HashedFieldPropName::FloatStep, stepSize) \ - .prop(HashedFieldPropName::FloatMin, min) \ - .prop(HashedFieldPropName::FloatMax, max); \ - } \ - \ - static auto ResourceFileRestriction(const char* fileType) \ - { \ - return ComponentFactory \ - .prop(HashedFieldPropName::ResourceFileType, fileType); \ - } \ - \ - template \ - static auto SetFlags(Enums... enums) \ - { \ - static_assert((std::is_enum_v && ...), "All arguments must be of enum class type"); \ - return ComponentFactory.traits((enums | ...)); \ - } \ - \ - template \ - static void BindAction(const char* funcName, const char* actionName) \ - { \ - ComponentFactory \ - .func(entt::hashed_string(funcName)) \ - .prop(HashedName::DisplayName, actionName); \ - SetFlags(ComponentFuncTrait::Action); \ + static std::string ClassName() \ + { \ + static std::string className = #klass; \ + return className; \ + } \ + \ + static std::string ComponentName() \ + { \ + static std::string name = componentName; \ + return name; \ + } \ + \ + static void AddToEntity(entt::entity entity, entt::registry* enttRegistry) \ + { \ + enttRegistry->emplace_or_replace(entity); \ + } \ + \ + static void RemoveFromEntity(entt::entity entity, entt::registry* enttRegistry) \ + { \ + enttRegistry->erase(entity); \ + } \ + \ + \ + static void (*GetInitializeComponentClass())() \ + { \ + return &klass::InitializeComponentClass; \ + } \ + \ + inline static auto ComponentFactory = entt::meta(); \ + static void InternalInitializeClass() \ + { \ + static bool initialized = false; \ + if (initialized) \ + return; \ + \ + ComponentFactory.type(entt::hashed_string(#klass)) \ + .traits(ComponentTypeTrait::InspectorExposed); \ + ComponentFactory.func<&klass::ComponentName>(HashedFnName::GetComponentName); \ + ComponentFactory.func<&klass::AddToEntity>(HashedFnName::AddToEntity); \ + ComponentFactory.func<&klass::RemoveFromEntity>(HashedFnName::RemoveFromEntity); \ + \ + if (klass::GetInitializeComponentClass() != Component::GetInitializeComponentClass()) \ + { \ + GetInitializeComponentClass()(); \ + } \ + \ + initialized = true; \ + \ + } \ + \ + template \ + static auto BindComponentField(const char* varName, const char* displayName) \ + { \ + return ComponentFactory \ + .data(entt::hashed_string(varName)) \ + .prop(HashedName::DisplayName, displayName); \ + } \ + \ + template \ + static auto BindComponentProperty(const char* varName, const char* displayName) \ + { \ + return ComponentFactory \ + .data(entt::hashed_string(varName)) \ + .prop(HashedName::DisplayName, displayName); \ + } \ + \ + static auto FieldFloatLimits(float stepSize, float min, float max) \ + { \ + return ComponentFactory \ + .prop(HashedFieldPropName::FloatStep, stepSize) \ + .prop(HashedFieldPropName::FloatMin, min) \ + .prop(HashedFieldPropName::FloatMax, max); \ + } \ + \ + static auto ResourceFileRestriction(const char* fileType) \ + { \ + return ComponentFactory \ + .prop(HashedFieldPropName::ResourceFileType, fileType); \ + } \ + \ + template \ + static auto SetFlags(Enums... enums) \ + { \ + static_assert((std::is_enum_v && ...), "All arguments must be of enum class type"); \ + return ComponentFactory.traits((enums | ...)); \ + } \ + \ + template \ + static void BindAction(const char* funcName, const char* actionName) \ + { \ + ComponentFactory \ + .func(entt::hashed_string(funcName)) \ + .prop(HashedName::DisplayName, actionName); \ + SetFlags(ComponentFuncTrait::Action); \ } diff --git a/Nuake/src/Scene/Entities/Entity.h b/Nuake/src/Scene/Entities/Entity.h index b64d3137..aacd59df 100644 --- a/Nuake/src/Scene/Entities/Entity.h +++ b/Nuake/src/Scene/Entities/Entity.h @@ -58,6 +58,32 @@ namespace Nuake func.invoke(newComponent, m_EntityHandle, &m_Scene->m_Registry); } + void RemoveComponent(entt::meta_type& enttMetaType) + { + if (!enttMetaType) + { + Logger::Log("Meta data empty/invalid", "Entity", WARNING); + return; + } + + entt::meta_any newComponent = enttMetaType.construct(); + if (!newComponent) + { + Logger::Log("Could not create a component from the meta type", "Entity", CRITICAL); + return; + } + + auto func = enttMetaType.func(HashedFnName::RemoveFromEntity); + // TODO: [WiggleWizard] Needs a lot more validation + if (!func) + { + Logger::Log("No such function exists or is registered on component", "Entity", CRITICAL); + return; + } + + func.invoke(newComponent, m_EntityHandle, &m_Scene->m_Registry); + } + template T& AddComponent() {