From d98d00b916908b95fc8ff938b4d9feb02c93f1ba Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 11 Nov 2022 15:20:14 +0800 Subject: [PATCH] Wonky solution for array editor that doesn't work for elements outside of the first --- SHADE_Managed/src/Editor/Editor.cxx | 39 ++++++++++++++---- SHADE_Managed/src/Editor/Editor.h++ | 64 ++++++++++++++++++++++------- SHADE_Managed/src/Editor/Editor.hxx | 5 ++- 3 files changed, 84 insertions(+), 24 deletions(-) diff --git a/SHADE_Managed/src/Editor/Editor.cxx b/SHADE_Managed/src/Editor/Editor.cxx index e02a6acd..ef5557c6 100644 --- a/SHADE_Managed/src/Editor/Editor.cxx +++ b/SHADE_Managed/src/Editor/Editor.cxx @@ -167,7 +167,7 @@ namespace SHADE 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) { @@ -175,22 +175,23 @@ namespace SHADE 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)); - + RangeAttribute^ rangeAttrib = hasAttribute(field); + System::Collections::IList^ iList = safe_cast(field->GetValue(object)); SHEditorUI::Text(Convert::ToNative(field->Name)); SHEditorUI::SameLine(); SHEditorUI::Button("+"); SHEditorUI::Indent(); - int i = 0; - for each (System::Object ^ obj in listEnummerable) + for (int i = 0; i < iList->Count; ++i) { - int val = safe_cast(obj); - SHEditorUI::InputInt(std::to_string(i), val, &isHovered); + System::Object^ obj = iList[i]; + if (renderFieldInInspector(std::to_string(i), obj, rangeAttrib)) + { + iList[i] = obj; + } SHEditorUI::SameLine(); SHEditorUI::Button("-"); - ++i; } SHEditorUI::Unindent(); } @@ -276,6 +277,28 @@ namespace SHADE } } + bool Editor::renderFieldInInspector(const std::string& fieldName, System::Object^% object, RangeAttribute^ rangeAttrib) + { + const bool MODIFIED_PRIMITIVE = + renderFieldInInspector(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputCheckbox, nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputFloat , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputDouble , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputVec2 , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, SHEditorUI::InputVec3 , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, nullptr , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, nullptr , nullptr, rangeAttrib) || + renderFieldInInspector(fieldName, object, nullptr , nullptr, rangeAttrib); + + return MODIFIED_PRIMITIVE; + } + bool Editor::renderEnumFieldInInspector(const std::string& fieldName, System::Object^% object, bool* isHovered) { // Get all the names of the enums diff --git a/SHADE_Managed/src/Editor/Editor.h++ b/SHADE_Managed/src/Editor/Editor.h++ index 501a75ae..ead03f49 100644 --- a/SHADE_Managed/src/Editor/Editor.h++ +++ b/SHADE_Managed/src/Editor/Editor.h++ @@ -62,7 +62,7 @@ namespace SHADE if (renderFieldInInspector ( Convert::ToNative(fieldInfo->Name), - val, + &val, fieldEditor, isHovered, rangeAttrib @@ -78,19 +78,53 @@ namespace SHADE return false; } + + template + bool Editor::renderFieldInInspector(const std::string& fieldName, System::Object^% object, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib) + { + if constexpr (std::is_same_v) + { + if (object->GetType()->IsSubclassOf(Enum::typeid)) + { + int managedVal = safe_cast(object); + if (renderFieldInInspector(fieldName, &managedVal, fieldEditor, isHovered, rangeAttrib)) + { + object = managedVal; + } + return true; + } + } + else + { + if (object->GetType() == ManagedType::typeid) + { + ManagedType managedVal = safe_cast(object); + cli::interior_ptr managedValPtr = &managedVal; + if (renderFieldInInspector(fieldName, managedValPtr, fieldEditor, isHovered, rangeAttrib)) + { + object = managedVal; + return true; + } + return false; + } + } + + return false; + } + template - bool Editor::renderFieldInInspector(const std::string& fieldName, ManagedType% managedVal, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib) + bool Editor::renderFieldInInspector(const std::string& fieldName, interior_ptr managedValPtr, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib) { // Retrieve the native version of the object NativeType val; if constexpr (IsPrimitiveTypeMatches_V) { - val = safe_cast(managedVal); + val = safe_cast(*managedValPtr); } else { - val = Convert::ToNative(managedVal); + val = Convert::ToNative(*managedValPtr); } // Throw into the SHEditorUI function @@ -119,12 +153,12 @@ namespace SHADE { if constexpr (IsPrimitiveTypeMatches_V) { - managedVal = val; + *managedValPtr = val; } else { - managedVal = Convert::ToCLI(val); + *managedValPtr = Convert::ToCLI(val); } return true; @@ -133,27 +167,27 @@ namespace SHADE return false; } template<> - bool Editor::renderFieldInInspector(const std::string& fieldName, System::String^% managedVal, EditorFieldFunc, bool* isHovered, RangeAttribute^) + bool Editor::renderFieldInInspector(const std::string& fieldName, interior_ptr managedValPtr, EditorFieldFunc, bool* isHovered, RangeAttribute^) { // Prevent issues where String^ is null due to being empty - if (managedVal == nullptr) - managedVal = ""; + if (*managedValPtr == nullptr) + *managedValPtr = ""; // Actual Field - std::string val = Convert::ToNative(managedVal); + std::string val = Convert::ToNative(*managedValPtr); if (SHEditorUI::InputTextField(fieldName, val, isHovered)) { - managedVal = Convert::ToCLI(val); + *managedValPtr = Convert::ToCLI(val); return true; } return false; } template<> - bool Editor::renderFieldInInspector(const std::string& fieldName, GameObject% managedVal, EditorFieldFunc, bool* isHovered, RangeAttribute^) + bool Editor::renderFieldInInspector(const std::string& fieldName, interior_ptr managedValPtr, EditorFieldFunc, bool* isHovered, RangeAttribute^) { - uint32_t entityId = managedVal.GetEntity(); - if (SHEditorUI::InputGameObjectField(fieldName, entityId, isHovered, !managedVal)) + uint32_t entityId = managedValPtr->GetEntity(); + if (SHEditorUI::InputGameObjectField(fieldName, entityId, isHovered, !(*managedValPtr))) { GameObject newVal = GameObject(entityId); if (entityId != MAX_EID) @@ -161,7 +195,7 @@ namespace SHADE // Null GameObject set newVal = GameObject(entityId); } - managedVal = newVal; + *managedValPtr = newVal; return true; } diff --git a/SHADE_Managed/src/Editor/Editor.hxx b/SHADE_Managed/src/Editor/Editor.hxx index c9c915f8..f9bf751f 100644 --- a/SHADE_Managed/src/Editor/Editor.hxx +++ b/SHADE_Managed/src/Editor/Editor.hxx @@ -90,6 +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); + static bool renderFieldInInspector(const std::string& fieldName, System::Object^% object, RangeAttribute^ rangeAttrib); 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 @@ -126,7 +127,9 @@ namespace SHADE /// /// True if the field is modified. template - static bool renderFieldInInspector(const std::string& fieldName, ManagedType% managedVal, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib); + static bool renderFieldInInspector(const std::string& fieldName, interior_ptr managedValPtr, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib); + template + static bool renderFieldInInspector(const std::string& fieldName, System::Object^% object, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib); /// /// Renders a context menu when right clicked for the scripts ///