Added the ability to add actions (buttons) to the inspector through reflection

This commit is contained in:
WiggleWizard
2024-09-15 00:10:59 +01:00
parent 2b06fe32de
commit 0dcd383dbb
4 changed files with 58 additions and 0 deletions

View File

@@ -597,6 +597,8 @@ void EditorSelectionPanel::DrawComponent(const Nuake::Entity& entity, entt::meta
void EditorSelectionPanel::DrawComponentContent(entt::meta_any& component)
{
entt::meta_type componentMeta = component.type();
// Draw component bound data
for (auto [fst, dataType] : componentMeta.data())
{
const ComponentFieldTrait fieldTraits = dataType.traits<ComponentFieldTrait>();
@@ -622,6 +624,33 @@ void EditorSelectionPanel::DrawComponentContent(entt::meta_any& component)
ImGui::TableNextRow();
}
// Draw any actions bound to the component
for (auto [fst, funcMeta] : componentMeta.func())
{
ImGui::TableSetColumnIndex(0);
const ComponentFuncTrait funcTraits = funcMeta.traits<ComponentFuncTrait>();
if ((funcTraits & ComponentFuncTrait::Action) == ComponentFuncTrait::Action)
{
ImGui::TableNextColumn();
std::string funcDisplayName = "";
auto prop = funcMeta.prop(HashedName::DisplayName).value();
if (prop)
{
funcDisplayName = std::string(*prop.try_cast<const char*>());
}
std::string buttonName = funcDisplayName;
if (UI::SecondaryButton(buttonName.c_str()))
{
entt::meta_any result = funcMeta.invoke(component);
}
ImGui::TableNextRow();
}
}
}
void EditorSelectionPanel::DrawFieldTypeFloat(entt::meta_data& field, entt::meta_any& component)

View File

@@ -22,6 +22,7 @@ namespace Nuake
{
NK_HASHED_STATIC_STR(GetComponentName)
NK_HASHED_STATIC_STR(AddToEntity)
NK_HASHED_STATIC_STR(ActionName)
};
struct HashedFieldPropName
@@ -45,6 +46,13 @@ namespace Nuake
InspectorExposed = 1 << 0,
};
enum class ComponentFuncTrait : uint16_t
{
None = 0,
// Exposes the component to be added via the inspector
Action = 1 << 0,
};
enum class ComponentFieldTrait : uint16_t
{
None = 0,
@@ -61,6 +69,7 @@ namespace Nuake
}
NK_ENUM_BITWISE_IMPL(ComponentTypeTrait);
NK_ENUM_BITWISE_IMPL(ComponentFuncTrait);
NK_ENUM_BITWISE_IMPL(ComponentFieldTrait);
}
@@ -145,4 +154,13 @@ public:
{ \
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); \
}

View File

@@ -16,3 +16,13 @@ std::string Nuake::ResourceFile::GetRelativePath()
return "";
}
std::string Nuake::ResourceFile::GetAbsolutePath()
{
if (Exist())
{
return file->GetAbsolutePath();
}
return "";
}

View File

@@ -15,6 +15,7 @@ namespace Nuake
bool Exist();
std::string GetRelativePath();
std::string GetAbsolutePath();
Ref<File> file = nullptr;
};