From fdc8965b62a2abec77f3f1e4be95601a5d165e96 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 11 Nov 2022 12:07:26 +0800 Subject: [PATCH] Made String and GameObject editor template specializations instead --- SHADE_Managed/src/Editor/Editor.cxx | 63 +++++++---------------------- SHADE_Managed/src/Editor/Editor.h++ | 39 ++++++++++++++++++ 2 files changed, 53 insertions(+), 49 deletions(-) diff --git a/SHADE_Managed/src/Editor/Editor.cxx b/SHADE_Managed/src/Editor/Editor.cxx index d29f838d..c82cc0a1 100644 --- a/SHADE_Managed/src/Editor/Editor.cxx +++ b/SHADE_Managed/src/Editor/Editor.cxx @@ -18,12 +18,9 @@ of DigiPen Institute of Technology is prohibited. #include "Editor/Editor.hxx" // STL Includes #include -// External Dependencies -#include "Editor/SHEditorUI.h" // Project Headers #include "Components/Component.hxx" #include "Scripts/ScriptStore.hxx" -#include "Utility/Convert.hxx" #include "Utility/Debug.hxx" #include "Serialisation/ReflectionUtilities.hxx" #include "Editor/IconsMaterialDesign.h" @@ -154,18 +151,20 @@ namespace SHADE bool isHovered = false; const bool MODIFIED_PRIMITIVE = - renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputCheckbox, &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputFloat , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputDouble , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputVec2 , &isHovered) || - renderFieldInInspector(field, object, SHEditorUI::InputVec3 , &isHovered); + renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputInt , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputCheckbox, &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputFloat , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputDouble , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputVec2 , &isHovered) || + renderFieldInInspector(field, object, SHEditorUI::InputVec3 , &isHovered) || + renderFieldInInspector(field, object, nullptr , &isHovered) || + renderFieldInInspector(field, object, nullptr , &isHovered); if (!MODIFIED_PRIMITIVE) { @@ -187,40 +186,6 @@ namespace SHADE registerUndoAction(object, field, val, oldVal); } } - else if (field->FieldType == String::typeid) - { - // Prevent issues where String^ is null due to being empty - String^ stringVal = safe_cast(field->GetValue(object)); - if (stringVal == nullptr) - { - stringVal = ""; - } - - // Actual Field - std::string val = Convert::ToNative(stringVal); - std::string oldVal = val; - if (SHEditorUI::InputTextField(Convert::ToNative(field->Name), val, &isHovered)) - { - field->SetValue(object, Convert::ToCLI(val)); - registerUndoAction(object, field, Convert::ToCLI(val), Convert::ToCLI(oldVal)); - } - } - else if (field->FieldType == GameObject::typeid) - { - GameObject gameObj = safe_cast(field->GetValue(object)); - uint32_t entityId = gameObj.GetEntity(); - if (SHEditorUI::InputGameObjectField(Convert::ToNative(field->Name), entityId, &isHovered, !gameObj)) - { - GameObject newVal = GameObject(entityId); - if (entityId != MAX_EID) - { - // Null GameObject set - newVal = GameObject(entityId); - } - field->SetValue(object, newVal); - registerUndoAction(object, field, newVal, gameObj); - } - } // Any List else if (field->FieldType->IsGenericType && field->FieldType->GetGenericTypeDefinition() == System::Collections::Generic::List::typeid->GetGenericTypeDefinition()) { diff --git a/SHADE_Managed/src/Editor/Editor.h++ b/SHADE_Managed/src/Editor/Editor.h++ index b68b5da8..46993f88 100644 --- a/SHADE_Managed/src/Editor/Editor.h++ +++ b/SHADE_Managed/src/Editor/Editor.h++ @@ -16,6 +16,10 @@ of DigiPen Institute of Technology is prohibited. // Primary Include #include "Editor.hxx" +// External Dependencies +#include "Editor/SHEditorUI.h" +// Project Includes +#include "Utility/Convert.hxx" namespace SHADE { @@ -102,6 +106,41 @@ namespace SHADE return true; } + return false; + } + template<> + bool Editor::renderFieldInInspector(const std::string& fieldName, System::String^% managedVal, EditorFieldFunc, bool* isHovered, RangeAttribute^) + { + // Prevent issues where String^ is null due to being empty + if (managedVal == nullptr) + managedVal = ""; + + // Actual Field + std::string val = Convert::ToNative(managedVal); + if (SHEditorUI::InputTextField(fieldName, val, isHovered)) + { + managedVal = Convert::ToCLI(val); + return true; + } + + return false; + } + template<> + bool Editor::renderFieldInInspector(const std::string& fieldName, GameObject% managedVal, EditorFieldFunc, bool* isHovered, RangeAttribute^) + { + uint32_t entityId = managedVal.GetEntity(); + if (SHEditorUI::InputGameObjectField(fieldName, entityId, isHovered, !managedVal)) + { + GameObject newVal = GameObject(entityId); + if (entityId != MAX_EID) + { + // Null GameObject set + newVal = GameObject(entityId); + } + managedVal = newVal; + return true; + } + return false; } }