From 7df24e7a66334bad6b473e247c8245736443dd13 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Wed, 19 Oct 2022 10:12:56 +0800 Subject: [PATCH] Fixed RangeAttribute not working correctly --- SHADE_Engine/src/Editor/SHEditorUI.cpp | 46 ++++++++++++++++++++++++++ SHADE_Engine/src/Editor/SHEditorUI.h | 43 +++++++++++++++++++++--- SHADE_Engine/src/Editor/SHEditorUI.hpp | 19 ----------- SHADE_Managed/src/Editor/Editor.cxx | 26 +++++++++++---- 4 files changed, 104 insertions(+), 30 deletions(-) diff --git a/SHADE_Engine/src/Editor/SHEditorUI.cpp b/SHADE_Engine/src/Editor/SHEditorUI.cpp index cbbd6d1d..7107778d 100644 --- a/SHADE_Engine/src/Editor/SHEditorUI.cpp +++ b/SHADE_Engine/src/Editor/SHEditorUI.cpp @@ -214,6 +214,52 @@ namespace SHADE ImGuiInputTextFlags_EnterReturnsTrue); } + bool SHEditorUI::InputSlider(const std::string& label, int min, int max, int& value, bool* isHovered /*= nullptr*/) + { + ImGui::Text(label.c_str()); + if (isHovered) + *isHovered = ImGui::IsItemHovered(); + ImGui::SameLine(); + return ImGui::SliderInt("##", &value, + static_cast(min), static_cast(max), "%d", + ImGuiInputTextFlags_EnterReturnsTrue); + } + + bool SHEditorUI::InputSlider(const std::string& label, unsigned int min, unsigned int max, unsigned int& value, bool* isHovered /*= nullptr*/) + { + int val = static_cast(value); + const bool CHANGED = InputSlider(label, min, max, val, isHovered); + if (CHANGED) + { + value = static_cast(val); + } + + return CHANGED; + } + + bool SHEditorUI::InputSlider(const std::string& label, float min, float max, float& value, bool* isHovered) + { + ImGui::Text(label.c_str()); + if (isHovered) + *isHovered = ImGui::IsItemHovered(); + ImGui::SameLine(); + return ImGui::SliderFloat("##", &value, + static_cast(min), static_cast(max), "%.3f", + ImGuiInputTextFlags_EnterReturnsTrue); + } + + bool SHEditorUI::InputSlider(const std::string& label, double min, double max, double& value, bool* isHovered /*= nullptr*/) + { + float val = static_cast(value); + const bool CHANGED = InputSlider(label, min, max, val, isHovered); + if (CHANGED) + { + value = static_cast(val); + } + + return CHANGED; + } + bool SHEditorUI::InputVec2(const std::string& label, SHVec2& value, bool* isHovered) { static const std::vector COMPONENT_LABELS = { "X", "Y" }; diff --git a/SHADE_Engine/src/Editor/SHEditorUI.h b/SHADE_Engine/src/Editor/SHEditorUI.h index 276bc9f9..f3051aa1 100644 --- a/SHADE_Engine/src/Editor/SHEditorUI.h +++ b/SHADE_Engine/src/Editor/SHEditorUI.h @@ -230,9 +230,9 @@ namespace SHADE /// True if the value was changed. static bool InputAngle(const std::string& label, double& value, bool* isHovered = nullptr); /// - /// Creates a double slider field widget for double input. + /// Creates an int slider field widget for double input. ///
- /// Wraps up ImGui::InputSliderFloat(). + /// Wraps up ImGui::SliderInt(). ///
/// Label used to identify this widget. /// Minimum value of the slider. @@ -240,8 +240,43 @@ namespace SHADE /// Reference to the variable to store the result. /// Label used to identify this widget. + /// Minimum value of the slider. + /// Maximum value of the slider. + /// Reference to the variable to store the result. + /// Label used to identify this widget. + /// Minimum value of the slider. + /// Maximum value of the slider. + /// Reference to the variable to store the result. + /// Label used to identify this widget. + /// Minimum value of the slider. + /// Maximum value of the slider. + /// Reference to the variable to store the result. + /// (min), static_cast(max), "%.3f", - ImGuiInputTextFlags_EnterReturnsTrue); - if (CHANGED) - { - value = static_cast(val); - } - - //return CHANGED; - return false; - } template inline bool SHEditorUI::InputEnumCombo(const std::string& label, Enum& v, int maxVal, std::function toStrFn, bool* isHovered) { diff --git a/SHADE_Managed/src/Editor/Editor.cxx b/SHADE_Managed/src/Editor/Editor.cxx index f0690c06..120f0274 100644 --- a/SHADE_Managed/src/Editor/Editor.cxx +++ b/SHADE_Managed/src/Editor/Editor.cxx @@ -75,12 +75,24 @@ using namespace System::Collections::Generic; NATIVE_TYPE oldVal = val; \ \ RangeAttribute^ rangeAttrib = hasAttribute(field); \ - std::string fieldName = Convert::ToNative(field->Name); \ - if ( \ - (rangeAttrib && SHEditorUI::InputSlider(fieldName, rangeAttrib->Min, rangeAttrib->Max, val, &isHovered)) \ - || \ - SHEditorUI::FUNC(fieldName, val, &isHovered) \ - ) \ + const std::string FIELD_NAME = Convert::ToNative(field->Name); \ + bool changed = false; \ + if (rangeAttrib) \ + { \ + changed = SHEditorUI::InputSlider \ + ( \ + FIELD_NAME, \ + static_cast(rangeAttrib->Min), \ + static_cast(rangeAttrib->Max), \ + val, &isHovered \ + ); \ + } \ + else \ + { \ + changed = SHEditorUI::FUNC(FIELD_NAME, val, &isHovered); \ + } \ + \ + if (changed) \ { \ field->SetValue(object, val); \ registerUndoAction(object, field, val, oldVal); \ @@ -224,7 +236,7 @@ namespace SHADE } void Editor::renderFieldInInspector(Reflection::FieldInfo^ field, Object^ object) { - bool isHovered = false; + bool isHovered = false; if RENDER_FIELD_RANGE (Int16, int, InputInt) else if RENDER_FIELD_RANGE (Int32, int, InputInt)