From 5d2aae35615dfd9d296e722c263da119599c3f84 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 11 Nov 2022 13:41:25 +0800 Subject: [PATCH] Made enum editor template specializations instead --- SHADE_Managed/src/Editor/Editor.cxx | 46 ++++++++++++--------- SHADE_Managed/src/Editor/Editor.h++ | 64 ++++++++++++++++++++--------- SHADE_Managed/src/Editor/Editor.hxx | 47 ++++++++++----------- 3 files changed, 94 insertions(+), 63 deletions(-) diff --git a/SHADE_Managed/src/Editor/Editor.cxx b/SHADE_Managed/src/Editor/Editor.cxx index c82cc0a1..e02a6acd 100644 --- a/SHADE_Managed/src/Editor/Editor.cxx +++ b/SHADE_Managed/src/Editor/Editor.cxx @@ -30,6 +30,7 @@ of DigiPen Institute of Technology is prohibited. #include "RangeAttribute.hxx" #include "Math/Vector2.hxx" #include "Math/Vector3.hxx" +#include // Using Directives using namespace System; @@ -146,6 +147,7 @@ namespace SHADE } SHEditorUI::PopID(); } + void Editor::renderFieldInInspector(Reflection::FieldInfo^ field, System::Object^ object) { bool isHovered = false; @@ -164,30 +166,13 @@ namespace SHADE renderFieldInInspector(field, object, SHEditorUI::InputVec2 , &isHovered) || renderFieldInInspector(field, object, SHEditorUI::InputVec3 , &isHovered) || renderFieldInInspector(field, object, nullptr , &isHovered) || - renderFieldInInspector(field, object, nullptr , &isHovered); + renderFieldInInspector(field, object, nullptr , &isHovered) || + renderFieldInInspector (field, object, nullptr , &isHovered); if (!MODIFIED_PRIMITIVE) { - if (field->FieldType->IsSubclassOf(Enum::typeid)) - { - // Get all the names of the enums - const array^ ENUM_NAMES = field->FieldType->GetEnumNames(); - std::vector nativeEnumNames; - for each (String ^ str in ENUM_NAMES) - { - nativeEnumNames.emplace_back(Convert::ToNative(str)); - } - - int val = safe_cast(field->GetValue(object)); - int oldVal = val; - if (SHEditorUI::InputEnumCombo(Convert::ToNative(field->Name), val, nativeEnumNames, &isHovered)) - { - field->SetValue(object, val); - registerUndoAction(object, field, val, oldVal); - } - } // Any List - else if (field->FieldType->IsGenericType && field->FieldType->GetGenericTypeDefinition() == System::Collections::Generic::List::typeid->GetGenericTypeDefinition()) + if (field->FieldType->IsGenericType && field->FieldType->GetGenericTypeDefinition() == System::Collections::Generic::List::typeid->GetGenericTypeDefinition()) { System::Type^ listType = field->FieldType->GenericTypeArguments[0]; System::Collections::IEnumerable^ listEnummerable = safe_cast(field->GetValue(object)); @@ -291,6 +276,27 @@ namespace SHADE } } + bool Editor::renderEnumFieldInInspector(const std::string& fieldName, System::Object^% object, bool* isHovered) + { + // Get all the names of the enums + const array^ ENUM_NAMES = object->GetType()->GetEnumNames(); + std::vector nativeEnumNames; + for each (String ^ str in ENUM_NAMES) + { + nativeEnumNames.emplace_back(Convert::ToNative(str)); + } + + int val = safe_cast(object); + int oldVal = val; + if (SHEditorUI::InputEnumCombo(fieldName, val, nativeEnumNames, isHovered)) + { + object = val; + return true; + } + + return false; + } + void Editor::renderScriptContextMenu(Entity entity, Script^ script) { // Right Click Menu diff --git a/SHADE_Managed/src/Editor/Editor.h++ b/SHADE_Managed/src/Editor/Editor.h++ index 46993f88..501a75ae 100644 --- a/SHADE_Managed/src/Editor/Editor.h++ +++ b/SHADE_Managed/src/Editor/Editor.h++ @@ -26,30 +26,54 @@ namespace SHADE template bool Editor::renderFieldInInspector(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, EditorFieldFunc fieldEditor, bool* isHovered) { - if (fieldInfo->FieldType == ManagedType::typeid) + if constexpr (std::is_same_v) { - RangeAttribute^ rangeAttrib; - if constexpr (std::is_arithmetic_v && !std::is_same_v) + if (fieldInfo->FieldType->IsSubclassOf(Enum::typeid)) { - rangeAttrib = hasAttribute(fieldInfo); - } + System::Object^ enumObj = fieldInfo->GetValue(object); + int oldVal = safe_cast(enumObj); + int val = oldVal; + if (renderEnumFieldInInspector + ( + Convert::ToNative(fieldInfo->Name), + enumObj, + isHovered + )) + { + fieldInfo->SetValue(object, safe_cast(enumObj)); + registerUndoAction(object, fieldInfo, fieldInfo->GetValue(object), oldVal); + } - ManagedType oldVal = safe_cast(fieldInfo->GetValue(object)); - ManagedType val = oldVal; - if (renderFieldInInspector - ( - Convert::ToNative(fieldInfo->Name), - val, - fieldEditor, - isHovered, - rangeAttrib - )) - { - fieldInfo->SetValue(object, val); - registerUndoAction(object, fieldInfo, fieldInfo->GetValue(object), oldVal); + return true; + } + } + else + { + if (fieldInfo->FieldType == ManagedType::typeid) + { + RangeAttribute^ rangeAttrib; + if constexpr (std::is_arithmetic_v && !std::is_same_v) + { + rangeAttrib = hasAttribute(fieldInfo); + } + + ManagedType oldVal = safe_cast(fieldInfo->GetValue(object)); + ManagedType val = oldVal; + if (renderFieldInInspector + ( + Convert::ToNative(fieldInfo->Name), + val, + fieldEditor, + isHovered, + rangeAttrib + )) + { + fieldInfo->SetValue(object, val); + registerUndoAction(object, fieldInfo, fieldInfo->GetValue(object), oldVal); + } + + return true; } - - return true; } return false; diff --git a/SHADE_Managed/src/Editor/Editor.hxx b/SHADE_Managed/src/Editor/Editor.hxx index 1f2c1be7..c9c915f8 100644 --- a/SHADE_Managed/src/Editor/Editor.hxx +++ b/SHADE_Managed/src/Editor/Editor.hxx @@ -90,29 +90,7 @@ namespace SHADE /// The object that contains the data of the field to render. /// static void renderFieldInInspector(System::Reflection::FieldInfo^ field, System::Object^ object); - /// - /// Renders a context menu when right clicked for the scripts - /// - /// The Entity to render the Scripts of. - /// The Script to render the inspector for. - static void renderScriptContextMenu(Entity entity, Script^ script); - /// - /// Adds changes to a variable as an undo-able/redo-able action on the Undo-Redo - /// stack. - /// - /// The object that changes are applied to. - /// The field that was changed. - /// New data to set. - /// Data that was overriden. - static void registerUndoAction(System::Object^ object, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData); - /// - /// Checks if a specific field has the specified attribute - /// - /// Type of Attribute to check for. - /// The field to check. - /// The attribute to check for if it exists. Null otherwise. - generic where Attribute : System::Attribute - static Attribute hasAttribute(System::Reflection::FieldInfo^ field); + static bool renderEnumFieldInInspector(const std::string& fieldName, System::Object^% object, bool* isHovered); /// /// Checks if the specified field is of the specified native and managed type /// equivalent and renders a ImGui field editor based on the specified field @@ -149,6 +127,29 @@ namespace SHADE /// True if the field is modified. template static bool renderFieldInInspector(const std::string& fieldName, ManagedType% managedVal, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib); + /// + /// Renders a context menu when right clicked for the scripts + /// + /// The Entity to render the Scripts of. + /// The Script to render the inspector for. + static void renderScriptContextMenu(Entity entity, Script^ script); + /// + /// Adds changes to a variable as an undo-able/redo-able action on the Undo-Redo + /// stack. + /// + /// The object that changes are applied to. + /// The field that was changed. + /// New data to set. + /// Data that was overriden. + static void registerUndoAction(System::Object^ object, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData); + /// + /// Checks if a specific field has the specified attribute + /// + /// Type of Attribute to check for. + /// The field to check. + /// The attribute to check for if it exists. Null otherwise. + generic where Attribute : System::Attribute + static Attribute hasAttribute(System::Reflection::FieldInfo^ field); }; } #include "Editor.h++"