Added Range Attribute

This commit is contained in:
Kah Wei 2022-10-18 22:34:46 +08:00
parent 5727bf3905
commit ceb4c6c4ca
8 changed files with 166 additions and 34 deletions

View File

@ -214,25 +214,6 @@ namespace SHADE
ImGuiInputTextFlags_EnterReturnsTrue); ImGuiInputTextFlags_EnterReturnsTrue);
} }
bool SHEditorUI::InputSlider(const std::string& label, double min, double max, double& 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 = 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

@ -240,7 +240,8 @@ 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>
static bool InputSlider(const std::string& label, double min, double max, double& value, bool* isHovered = nullptr); template<typename T>
static bool InputSlider(const std::string& label, T min, T max, T& 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

@ -16,9 +16,28 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE 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

@ -30,6 +30,7 @@ of DigiPen Institute of Technology is prohibited.
#include "Editor/Command/SHCommandManager.h" #include "Editor/Command/SHCommandManager.h"
#include "Editor/Command/SHCommand.hpp" #include "Editor/Command/SHCommand.hpp"
#include "TooltipAttribute.hxx" #include "TooltipAttribute.hxx"
#include "RangeAttribute.hxx"
// Using Directives // Using Directives
using namespace System; using namespace System;
@ -61,6 +62,31 @@ using namespace System::Collections::Generic;
} \ } \
} \ } \
/// <summary> /// <summary>
/// Alternative to RENDER_FIELD that checks for RangeAttribute and switches to a slider
/// instead.
/// </summary>
/// <param name="MANAGED_TYPE">The managed type of the object to edit.</param>
/// <param name="NATIVE_TYPE">The native type of the object to edit.</param>
/// <param name="FUNC">The SHEditorUI:: function to use for editing.</param>
#define RENDER_FIELD_RANGE(MANAGED_TYPE, NATIVE_TYPE, FUNC) \
(field->FieldType == MANAGED_TYPE::typeid) \
{ \
NATIVE_TYPE val = safe_cast<NATIVE_TYPE>(field->GetValue(object)); \
NATIVE_TYPE oldVal = val; \
\
RangeAttribute^ rangeAttrib = hasAttribute<RangeAttribute^>(field); \
std::string fieldName = Convert::ToNative(field->Name); \
if ( \
(rangeAttrib && SHEditorUI::InputSlider<NATIVE_TYPE>(fieldName, rangeAttrib->Min, rangeAttrib->Max, val, &isHovered)) \
|| \
SHEditorUI::FUNC(fieldName, val, &isHovered) \
) \
{ \
field->SetValue(object, val); \
registerUndoAction(object, field, val, oldVal); \
} \
} \
/// <summary>
/// Macro expansion that is used in renderFieldInInspector() to check the type of a field /// Macro expansion that is used in renderFieldInInspector() to check the type of a field
/// named "field" against the specified type and if it matches, retrieves the value of /// named "field" against the specified type and if it matches, retrieves the value of
/// that field from an object named "object" and pass it into the specified SHEditorUI:: /// that field from an object named "object" and pass it into the specified SHEditorUI::
@ -77,6 +103,7 @@ using namespace System::Collections::Generic;
{ \ { \
NATIVE_TYPE val = Convert::ToNative(safe_cast<MANAGED_TYPE>(field->GetValue(object))); \ NATIVE_TYPE val = Convert::ToNative(safe_cast<MANAGED_TYPE>(field->GetValue(object))); \
NATIVE_TYPE oldVal = val; \ NATIVE_TYPE oldVal = val; \
\
if (SHEditorUI::FUNC(Convert::ToNative(field->Name), val, &isHovered)) \ if (SHEditorUI::FUNC(Convert::ToNative(field->Name), val, &isHovered)) \
{ \ { \
field->SetValue(object, Convert::ToCLI(val)); \ field->SetValue(object, Convert::ToCLI(val)); \
@ -199,16 +226,16 @@ namespace SHADE
{ {
bool isHovered = false; bool isHovered = false;
if RENDER_FIELD (Int16, int, InputInt) if RENDER_FIELD_RANGE (Int16, int, InputInt)
else if RENDER_FIELD (Int32, int, InputInt) else if RENDER_FIELD_RANGE (Int32, int, InputInt)
else if RENDER_FIELD (Int64, int, InputInt) else if RENDER_FIELD_RANGE (Int64, int, InputInt)
else if RENDER_FIELD (UInt16, unsigned int, InputUnsignedInt) else if RENDER_FIELD_RANGE (UInt16, unsigned int, InputUnsignedInt)
else if RENDER_FIELD (UInt32, unsigned int, InputUnsignedInt) else if RENDER_FIELD_RANGE (UInt32, unsigned int, InputUnsignedInt)
else if RENDER_FIELD (UInt64, unsigned int, InputUnsignedInt) else if RENDER_FIELD_RANGE (UInt64, unsigned int, InputUnsignedInt)
else if RENDER_FIELD (Byte, int, InputInt) else if RENDER_FIELD_RANGE (Byte, int, InputInt)
else if RENDER_FIELD (bool, bool, InputCheckbox) else if RENDER_FIELD (bool, bool, InputCheckbox)
else if RENDER_FIELD (float, float, InputFloat) else if RENDER_FIELD_RANGE (float, float, InputFloat)
else if RENDER_FIELD (double, double, InputDouble) else if RENDER_FIELD_RANGE (double, double, InputDouble)
else if (field->FieldType->IsSubclassOf(Enum::typeid)) else if (field->FieldType->IsSubclassOf(Enum::typeid))
{ {
// Get all the names of the enums // Get all the names of the enums

View File

@ -0,0 +1,39 @@
/************************************************************************************//*!
\file RangeAttribute.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 18, 2022
\brief Contains the definition of the functions of the managed Range Attribute
class.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#include "SHpch.h"
#include "RangeAttribute.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
float RangeAttribute::Min::get()
{
return minVal;
}
float RangeAttribute::Max::get()
{
return maxVal;
}
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
RangeAttribute::RangeAttribute(float min, float max)
: minVal { min }
, maxVal { max }
{}
}

View File

@ -0,0 +1,61 @@
/************************************************************************************//*!
\file RangeAttribute.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 18, 2022
\brief Contains the definition of the managed Range Attribute class with
the declaration of functions for working with it.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
namespace SHADE
{
/// <summary>
/// Simple attribute to constrain the range of values for a field on the editor.
/// </summary>
[System::AttributeUsage(System::AttributeTargets::Field)]
public ref class RangeAttribute : public System::Attribute
{
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Minimum value for the Ranged field.
/// </summary>
property float Min
{
float get();
}
/// <summary>
/// Maximum value for the Ranged field.
/// </summary>
property float Max
{
float get();
}
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for a Tooltip attribute that fills in the description.
/// </summary>
/// <param name="description">Text to be shown when a field is hovered.</param>
RangeAttribute(float min, float max);
private:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
float minVal;
float maxVal;
};
}

View File

@ -17,7 +17,7 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE namespace SHADE
{ {
/// <summary> /// <summary>
/// Simple attribute to mark that a field in a Script should be serialised. /// Simple attribute to provide a field in a script with a tooltip.
/// </summary> /// </summary>
[System::AttributeUsage(System::AttributeTargets::Field)] [System::AttributeUsage(System::AttributeTargets::Field)]
public ref class TooltipAttribute : public System::Attribute public ref class TooltipAttribute : public System::Attribute

View File

@ -5,7 +5,11 @@ public class RaccoonShowcase : Script
{ {
[SerializeField] [SerializeField]
[Tooltip("Speed of the rotation in radians per second.")] [Tooltip("Speed of the rotation in radians per second.")]
[Range(-1.0f, 2.0f)]
private double RotateSpeed = 1.0; private double RotateSpeed = 1.0;
//[SerializeField]
//[Range(-5, 20)]
//private int test = 5;
[SerializeField] [SerializeField]
[Tooltip("Speed of the scaling in radians per second around each axis.")] [Tooltip("Speed of the scaling in radians per second around each axis.")]
private Vector3 ScaleSpeed = new Vector3(1.0, 1.0, 0.0); private Vector3 ScaleSpeed = new Vector3(1.0, 1.0, 0.0);