mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-15 20:08:54 +03:00
Added the ability to remove components
This commit is contained in:
@@ -548,7 +548,7 @@ void EditorSelectionPanel::DrawNetScriptPanel(Ref<Nuake::File> 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<component>();
|
||||
auto componentType = component.type();
|
||||
entity.RemoveComponent(componentType);
|
||||
ImGui::PopStyleVar();
|
||||
delete boldFont;
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ private:
|
||||
void DrawWrenScriptPanel(Ref<Nuake::WrenScript> wrenFile);
|
||||
void DrawNetScriptPanel(Ref<Nuake::File> 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);
|
||||
|
||||
@@ -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<klass>(entity); \
|
||||
} \
|
||||
\
|
||||
\
|
||||
static void (*GetInitializeComponentClass())() \
|
||||
{ \
|
||||
return &klass::InitializeComponentClass; \
|
||||
} \
|
||||
\
|
||||
inline static auto ComponentFactory = entt::meta<klass>(); \
|
||||
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<auto Data> \
|
||||
static auto BindComponentField(const char* varName, const char* displayName) \
|
||||
{ \
|
||||
return ComponentFactory \
|
||||
.data<Data>(entt::hashed_string(varName)) \
|
||||
.prop(HashedName::DisplayName, displayName); \
|
||||
} \
|
||||
\
|
||||
template<auto Getter, auto Setter> \
|
||||
static auto BindComponentProperty(const char* varName, const char* displayName) \
|
||||
{ \
|
||||
return ComponentFactory \
|
||||
.data<Getter, Setter>(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 <typename... Enums> \
|
||||
static auto SetFlags(Enums... enums) \
|
||||
{ \
|
||||
static_assert((std::is_enum_v<Enums> && ...), "All arguments must be of enum class type"); \
|
||||
return ComponentFactory.traits((enums | ...)); \
|
||||
} \
|
||||
\
|
||||
template<auto Func> \
|
||||
static void BindAction(const char* funcName, const char* actionName) \
|
||||
{ \
|
||||
ComponentFactory \
|
||||
.func<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<klass>(entity); \
|
||||
} \
|
||||
\
|
||||
static void RemoveFromEntity(entt::entity entity, entt::registry* enttRegistry) \
|
||||
{ \
|
||||
enttRegistry->erase<klass>(entity); \
|
||||
} \
|
||||
\
|
||||
\
|
||||
static void (*GetInitializeComponentClass())() \
|
||||
{ \
|
||||
return &klass::InitializeComponentClass; \
|
||||
} \
|
||||
\
|
||||
inline static auto ComponentFactory = entt::meta<klass>(); \
|
||||
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<auto Data> \
|
||||
static auto BindComponentField(const char* varName, const char* displayName) \
|
||||
{ \
|
||||
return ComponentFactory \
|
||||
.data<Data>(entt::hashed_string(varName)) \
|
||||
.prop(HashedName::DisplayName, displayName); \
|
||||
} \
|
||||
\
|
||||
template<auto Getter, auto Setter> \
|
||||
static auto BindComponentProperty(const char* varName, const char* displayName) \
|
||||
{ \
|
||||
return ComponentFactory \
|
||||
.data<Getter, Setter>(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 <typename... Enums> \
|
||||
static auto SetFlags(Enums... enums) \
|
||||
{ \
|
||||
static_assert((std::is_enum_v<Enums> && ...), "All arguments must be of enum class type"); \
|
||||
return ComponentFactory.traits((enums | ...)); \
|
||||
} \
|
||||
\
|
||||
template<auto Func> \
|
||||
static void BindAction(const char* funcName, const char* actionName) \
|
||||
{ \
|
||||
ComponentFactory \
|
||||
.func<Func>(entt::hashed_string(funcName)) \
|
||||
.prop(HashedName::DisplayName, actionName); \
|
||||
SetFlags(ComponentFuncTrait::Action); \
|
||||
}
|
||||
|
||||
@@ -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<typename T>
|
||||
T& AddComponent()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user