Better API for binding inspector drawers and added the ability to add whole component drawers

This commit is contained in:
WiggleWizard
2024-09-16 10:57:22 +01:00
parent d338a98c9b
commit 8b2511f995
2 changed files with 45 additions and 15 deletions

View File

@@ -4,7 +4,9 @@
#include "../Misc/ImGuiTextHelper.h"
#include <src/Scene/Components.h>
#include "src/Scene/Components/FieldTypes.h"
#include "../ComponentsPanel/MaterialEditor.h"
#include "../ComponentsPanel/BoxColliderPanel.h"
#include <src/Rendering/Textures/Material.h>
#include <src/Resource/ResourceLoader.h>
@@ -19,10 +21,6 @@
#include "Tracy.hpp"
#define REGISTER_TYPE_DRAWER(forType, fn) \
FieldTypeDrawers[entt::type_id<forType>().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<CameraComponent>();
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<bool, &EditorSelectionPanel::DrawFieldTypeBool>(this);
RegisterTypeDrawer<float, &EditorSelectionPanel::DrawFieldTypeFloat>(this);
RegisterTypeDrawer<Vector3, &EditorSelectionPanel::DrawFieldTypeVector3>(this);
RegisterTypeDrawer<std::string, &EditorSelectionPanel::DrawFieldTypeString>(this);
RegisterTypeDrawer<ResourceFile, &EditorSelectionPanel::DrawFieldTypeResourceFile>(this);
RegisterTypeDrawer<DynamicItemList, &EditorSelectionPanel::DrawFieldTypeDynamicItemList>(this);
}
void EditorSelectionPanel::ResolveFile(Ref<Nuake::File> 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);

View File

@@ -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<void(entt::meta_data&, entt::meta_any&)>;
using DrawComponentTypeFn = std::function<void(Nuake::Entity& entity, entt::meta_any& componentInstance)>;
using DrawFieldTypeFn = std::function<void(entt::meta_data& fieldMeta, entt::meta_any& componentInstance)>;
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<Nuake::Prefab> prefab);
template<class T, auto Func>
void RegisterComponentDrawer()
{
const auto t = entt::type_id<T>();
ComponentTypeDrawers[t.hash()] = std::bind(Func, std::placeholders::_1, std::placeholders::_2);
}
template<class T, auto Func, class O>
void RegisterComponentDrawer(O* o)
{
ComponentTypeDrawers[entt::type_id<T>().hash()] = std::bind(Func, o, std::placeholders::_1, std::placeholders::_2);
}
template<class T, auto Func, class O>
void RegisterTypeDrawer(O* o)
{
FieldTypeDrawers[entt::type_id<T>().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<entt::id_type, DrawComponentTypeFn> ComponentTypeDrawers;
// List of functions to call for each component field type that needs to be drawn
std::unordered_map<entt::id_type, DrawFieldTypeFn> FieldTypeDrawers;
private: