Fixed RangeAttribute not working correctly

This commit is contained in:
Kah Wei 2022-10-19 10:12:56 +08:00
parent ceb4c6c4ca
commit 7df24e7a66
4 changed files with 104 additions and 30 deletions

View File

@ -214,6 +214,52 @@ namespace SHADE
ImGuiInputTextFlags_EnterReturnsTrue); 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<float>(min), static_cast<float>(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<int>(value);
const bool CHANGED = InputSlider(label, min, max, val, isHovered);
if (CHANGED)
{
value = static_cast<int>(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<float>(min), static_cast<float>(max), "%.3f",
ImGuiInputTextFlags_EnterReturnsTrue);
}
bool SHEditorUI::InputSlider(const std::string& label, double min, double max, double& value, bool* isHovered /*= nullptr*/)
{
float val = static_cast<float>(value);
const bool CHANGED = InputSlider(label, min, max, val, isHovered);
if (CHANGED)
{
value = static_cast<double>(val);
}
return CHANGED;
}
bool SHEditorUI::InputVec2(const std::string& label, SHVec2& value, bool* isHovered) bool SHEditorUI::InputVec2(const std::string& label, SHVec2& value, bool* isHovered)
{ {
static const std::vector<std::string> COMPONENT_LABELS = { "X", "Y" }; static const std::vector<std::string> COMPONENT_LABELS = { "X", "Y" };

View File

@ -230,9 +230,9 @@ namespace SHADE
/// <returns>True if the value was changed.</returns> /// <returns>True if the value was changed.</returns>
static bool InputAngle(const std::string& label, double& value, bool* isHovered = nullptr); static bool InputAngle(const std::string& label, double& value, bool* isHovered = nullptr);
/// <summary> /// <summary>
/// Creates a double slider field widget for double input. /// Creates an int slider field widget for double input.
/// <br/> /// <br/>
/// Wraps up ImGui::InputSliderFloat(). /// Wraps up ImGui::SliderInt().
/// </summary> /// </summary>
/// <param name="label">Label used to identify this widget.</param> /// <param name="label">Label used to identify this widget.</param>
/// <param name="min">Minimum value of the slider.</param> /// <param name="min">Minimum value of the slider.</param>
@ -240,8 +240,43 @@ namespace SHADE
/// <param name="value">Reference to the variable to store the result.</param> /// <param name="value">Reference to the variable to store the result.</param>
/// <param name="isHovered>If set, stores the hover state of this widget.</param> /// <param name="isHovered>If set, stores the hover state of this widget.</param>
/// <returns>True if the value was changed.</returns> /// <returns>True if the value was changed.</returns>
template<typename T> static bool InputSlider(const std::string& label, int min, int max, int& value, bool* isHovered = nullptr);
static bool InputSlider(const std::string& label, T min, T max, T& value, bool* isHovered = nullptr); /// <summary>
/// Creates an unsigned int slider field widget for double input.
/// <br/>
/// Wraps up ImGui::SliderInt().
/// </summary>
/// <param name="label">Label used to identify this widget.</param>
/// <param name="min">Minimum value of the slider.</param>
/// <param name="max">Maximum value of the slider.</param>
/// <param name="value">Reference to the variable to store the result.</param>
/// <param name="isHovered>If set, stores the hover state of this widget.</param>
/// <returns>True if the value was changed.</returns>
static bool InputSlider(const std::string& label, unsigned int min, unsigned int max, unsigned int& value, bool* isHovered = nullptr);
/// <summary>
/// Creates a float slider field widget for double input.
/// <br/>
/// Wraps up ImGui::SliderFloat().
/// </summary>
/// <param name="label">Label used to identify this widget.</param>
/// <param name="min">Minimum value of the slider.</param>
/// <param name="max">Maximum value of the slider.</param>
/// <param name="value">Reference to the variable to store the result.</param>
/// <param name="isHovered>If set, stores the hover state of this widget.</param>
/// <returns>True if the value was changed.</returns>
static bool InputSlider(const std::string& label, float min, float max, float& value, bool* isHovered = nullptr);
/// <summary>
/// Creates a double slider field widget for double input.
/// <br/>
/// Wraps up ImGui::SliderFloat().
/// </summary>
/// <param name="label">Label used to identify this widget.</param>
/// <param name="min">Minimum value of the slider.</param>
/// <param name="max">Maximum value of the slider.</param>
/// <param name="value">Reference to the variable to store the result.</param>
/// <param name="isHovered>If set, stores the hover state of this widget.</param>
/// <returns>True if the value was changed.</returns>
static bool InputSlider(const std::string& label, double min, double max, double& value, bool* isHovered = nullptr);
/// <summary> /// <summary>
/// Creates a 2x double field widget for Vector2 input. /// Creates a 2x double field widget for Vector2 input.
/// <br/> /// <br/>

View File

@ -19,25 +19,6 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* ImGui Wrapper Functions - Widgets */ /* ImGui Wrapper Functions - Widgets */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
template<typename T>
bool SHADE::SHEditorUI::InputSlider(const std::string& label, T min, T max, T& value, bool* isHovered)
{
float val = static_cast<float>(value);
//ImGui::Text(label.c_str());
//if (isHovered)
// *isHovered = ImGui::IsItemHovered();
//ImGui::SameLine();
const bool CHANGED = ImGui::SliderFloat("#", &val,
static_cast<float>(min), static_cast<float>(max), "%.3f",
ImGuiInputTextFlags_EnterReturnsTrue);
if (CHANGED)
{
value = static_cast<T>(val);
}
//return CHANGED;
return false;
}
template<typename Enum> template<typename Enum>
inline bool SHEditorUI::InputEnumCombo(const std::string& label, Enum& v, int maxVal, std::function<const char* (Enum)> toStrFn, bool* isHovered) inline bool SHEditorUI::InputEnumCombo(const std::string& label, Enum& v, int maxVal, std::function<const char* (Enum)> toStrFn, bool* isHovered)
{ {

View File

@ -75,12 +75,24 @@ using namespace System::Collections::Generic;
NATIVE_TYPE oldVal = val; \ NATIVE_TYPE oldVal = val; \
\ \
RangeAttribute^ rangeAttrib = hasAttribute<RangeAttribute^>(field); \ RangeAttribute^ rangeAttrib = hasAttribute<RangeAttribute^>(field); \
std::string fieldName = Convert::ToNative(field->Name); \ const std::string FIELD_NAME = Convert::ToNative(field->Name); \
if ( \ bool changed = false; \
(rangeAttrib && SHEditorUI::InputSlider<NATIVE_TYPE>(fieldName, rangeAttrib->Min, rangeAttrib->Max, val, &isHovered)) \ if (rangeAttrib) \
|| \ { \
SHEditorUI::FUNC(fieldName, val, &isHovered) \ changed = SHEditorUI::InputSlider \
) \ ( \
FIELD_NAME, \
static_cast<NATIVE_TYPE>(rangeAttrib->Min), \
static_cast<NATIVE_TYPE>(rangeAttrib->Max), \
val, &isHovered \
); \
} \
else \
{ \
changed = SHEditorUI::FUNC(FIELD_NAME, val, &isHovered); \
} \
\
if (changed) \
{ \ { \
field->SetValue(object, val); \ field->SetValue(object, val); \
registerUndoAction(object, field, val, oldVal); \ registerUndoAction(object, field, val, oldVal); \
@ -224,7 +236,7 @@ namespace SHADE
} }
void Editor::renderFieldInInspector(Reflection::FieldInfo^ field, Object^ object) void Editor::renderFieldInInspector(Reflection::FieldInfo^ field, Object^ object)
{ {
bool isHovered = false; bool isHovered = false;
if RENDER_FIELD_RANGE (Int16, int, InputInt) if RENDER_FIELD_RANGE (Int16, int, InputInt)
else if RENDER_FIELD_RANGE (Int32, int, InputInt) else if RENDER_FIELD_RANGE (Int32, int, InputInt)