From 8b2511f995eb0e42c830f2cc8104628bd5fd8cbf Mon Sep 17 00:00:00 2001 From: WiggleWizard <1405402+WiggleWizard@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:57:22 +0100 Subject: [PATCH] Better API for binding inspector drawers and added the ability to add whole component drawers --- Editor/src/Windows/EditorSelectionPanel.cpp | 31 +++++++++++++-------- Editor/src/Windows/EditorSelectionPanel.h | 29 ++++++++++++++++--- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/Editor/src/Windows/EditorSelectionPanel.cpp b/Editor/src/Windows/EditorSelectionPanel.cpp index 9e060ca4..6c150868 100644 --- a/Editor/src/Windows/EditorSelectionPanel.cpp +++ b/Editor/src/Windows/EditorSelectionPanel.cpp @@ -4,7 +4,9 @@ #include "../Misc/ImGuiTextHelper.h" #include #include "src/Scene/Components/FieldTypes.h" + #include "../ComponentsPanel/MaterialEditor.h" +#include "../ComponentsPanel/BoxColliderPanel.h" #include #include @@ -19,10 +21,6 @@ #include "Tracy.hpp" - -#define REGISTER_TYPE_DRAWER(forType, fn) \ - FieldTypeDrawers[entt::type_id().hash()] = std::bind(&fn, this, std::placeholders::_1, std::placeholders::_2); - using namespace Nuake; EditorSelectionPanel::EditorSelectionPanel() @@ -31,12 +29,12 @@ EditorSelectionPanel::EditorSelectionPanel() virtualScene->SetName("Virtual Scene"); virtualScene->CreateEntity("Camera").AddComponent(); - REGISTER_TYPE_DRAWER(bool, EditorSelectionPanel::DrawFieldTypeBool); - REGISTER_TYPE_DRAWER(float, EditorSelectionPanel::DrawFieldTypeFloat); - REGISTER_TYPE_DRAWER(Vector3, EditorSelectionPanel::DrawFieldTypeVector3); - REGISTER_TYPE_DRAWER(std::string, EditorSelectionPanel::DrawFieldTypeString); - REGISTER_TYPE_DRAWER(ResourceFile, EditorSelectionPanel::DrawFieldTypeResourceFile); - REGISTER_TYPE_DRAWER(DynamicItemList, EditorSelectionPanel::DrawFieldTypeDynamicItemList); + RegisterTypeDrawer(this); + RegisterTypeDrawer(this); + RegisterTypeDrawer(this); + RegisterTypeDrawer(this); + RegisterTypeDrawer(this); + RegisterTypeDrawer(this); } void EditorSelectionPanel::ResolveFile(Ref file) @@ -171,7 +169,7 @@ void EditorSelectionPanel::DrawEntity(Nuake::Entity entity) // mQuakeMapPanel.Draw(entity); mCameraPanel.Draw(entity); // mRigidbodyPanel.Draw(entity); - mBoxColliderPanel.Draw(entity); + // mBoxColliderPanel.Draw(entity); // mSphereColliderPanel.Draw(entity); mCapsuleColliderPanel.Draw(entity); mCylinderColliderPanel.Draw(entity); @@ -551,6 +549,17 @@ void EditorSelectionPanel::DrawComponent(Nuake::Entity& entity, entt::meta_any& { ZoneScoped; + // Call into custom component drawer if one is available for this component + + const auto componentIdHash = component.type().info().hash(); + if (ComponentTypeDrawers.contains(componentIdHash)) + { + const auto drawerFn = ComponentTypeDrawers[componentIdHash]; + drawerFn(entity, component); + + return; + } + const entt::meta_type componentMeta = component.type(); const std::string componentName = Component::GetName(componentMeta); diff --git a/Editor/src/Windows/EditorSelectionPanel.h b/Editor/src/Windows/EditorSelectionPanel.h index 448f7f93..387b6603 100644 --- a/Editor/src/Windows/EditorSelectionPanel.h +++ b/Editor/src/Windows/EditorSelectionPanel.h @@ -9,7 +9,6 @@ #include "../ComponentsPanel/LightPanel.h" #include "../ComponentsPanel/MeshPanel.h" #include "../ComponentsPanel/CameraPanel.h" -#include "../ComponentsPanel/BoxColliderPanel.h" #include "../ComponentsPanel/CapsuleColliderPanel.h" #include "../ComponentsPanel/CylinderColliderPanel.h" #include "../ComponentsPanel/MeshColliderPanel.h" @@ -29,7 +28,8 @@ namespace Nuake class EditorSelectionPanel { - using DrawFieldTypeFn = std::function; + using DrawComponentTypeFn = std::function; + using DrawFieldTypeFn = std::function; private: TransformPanel mTransformPanel; @@ -37,7 +37,6 @@ private: NetScriptPanel mNetScriptPanel; MeshPanel mMeshPanel; CameraPanel mCameraPanel; - BoxColliderPanel mBoxColliderPanel; MeshColliderPanel mMeshColliderPanel; CapsuleColliderPanel mCapsuleColliderPanel; CylinderColliderPanel mCylinderColliderPanel; @@ -62,8 +61,30 @@ public: void DrawResource(Nuake::Resource resource); void DrawPrefabPanel(Ref prefab); + template + void RegisterComponentDrawer() + { + const auto t = entt::type_id(); + ComponentTypeDrawers[t.hash()] = std::bind(Func, std::placeholders::_1, std::placeholders::_2); + } + + template + void RegisterComponentDrawer(O* o) + { + ComponentTypeDrawers[entt::type_id().hash()] = std::bind(Func, o, std::placeholders::_1, std::placeholders::_2); + } + + template + void RegisterTypeDrawer(O* o) + { + FieldTypeDrawers[entt::type_id().hash()] = std::bind(Func, o, std::placeholders::_1, std::placeholders::_2); + } + protected: - // List of functions to call for each type that needs to be drawn + // Drawing functions for each component (for writing very specific inspectors for specific components) + std::unordered_map ComponentTypeDrawers; + + // List of functions to call for each component field type that needs to be drawn std::unordered_map FieldTypeDrawers; private: