Skip to content

Commit

Permalink
wip #48 need work for the step
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerTemp committed Oct 20, 2024
1 parent 612b001 commit 83636c9
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 37 deletions.
126 changes: 100 additions & 26 deletions Editor/Drawers/MinMaxSliderAttributeDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,57 @@ protected override void DrawField(Rect position, SerializedProperty property, GU
}, rightFieldWidth);

// Draw the slider
EditorGUI.BeginChangeCheck();

if (property.propertyType == SerializedPropertyType.Vector2)
{
Vector2 sliderValue = property.vector2Value;
EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minValue, maxValue);

// GUI.SetNextControlName(FieldControlName);
sliderValue.x = EditorGUI.FloatField(labelWithMinFieldRect, label, sliderValue.x);
sliderValue.x = Mathf.Clamp(sliderValue.x, minValue, Mathf.Min(maxValue, sliderValue.y));
if (minMaxSliderAttribute.FreeInput)
{
minValue = Mathf.Min(minValue, sliderValue.x);
maxValue = Mathf.Max(maxValue, sliderValue.y);
}

sliderValue.y = EditorGUI.FloatField(maxFloatFieldRect, sliderValue.y);
sliderValue.y = Mathf.Clamp(sliderValue.y, Mathf.Max(minValue, sliderValue.x), maxValue);
bool hasChange = false;
using(EditorGUI.ChangeCheckScope changed = new EditorGUI.ChangeCheckScope())
{
EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minValue, maxValue);
if(changed.changed)
{
hasChange = true;
}
}

if (EditorGUI.EndChangeCheck())
using(EditorGUI.ChangeCheckScope changed = new EditorGUI.ChangeCheckScope())
{
property.vector2Value = minMaxSliderAttribute.Step < 0
float sliderX = EditorGUI.FloatField(labelWithMinFieldRect, label, sliderValue.x);
if(changed.changed)
{
sliderValue.x = minMaxSliderAttribute.FreeInput? sliderX: Mathf.Clamp(sliderX, minValue, Mathf.Min(maxValue, sliderValue.y));
hasChange = true;
}
}

using(EditorGUI.ChangeCheckScope changed = new EditorGUI.ChangeCheckScope())
{
float sliderY = EditorGUI.FloatField(maxFloatFieldRect, sliderValue.y);
if(changed.changed)
{
sliderValue.y = minMaxSliderAttribute.FreeInput? sliderY: Mathf.Clamp(sliderY, Mathf.Max(minValue, sliderValue.x), maxValue);
hasChange = true;
}
}

if (hasChange)
{
property.vector2Value = minMaxSliderAttribute.Step < 0 || minMaxSliderAttribute.FreeInput
? sliderValue
: BoundV2Step(sliderValue, minValue, maxValue, minMaxSliderAttribute.Step);
}
}
else if (property.propertyType == SerializedPropertyType.Vector2Int)
{
EditorGUI.BeginChangeCheck();

Vector2Int sliderValue = property.vector2IntValue;
float xValue = sliderValue.x;
float yValue = sliderValue.y;
Expand Down Expand Up @@ -238,6 +266,22 @@ private static MetaInfo GetMetaInfo(SerializedProperty property, ISaintsAttribut
};
}

if (minMaxSliderAttribute.FreeInput)
{
if(property.propertyType == SerializedPropertyType.Vector2)
{
Vector2 curValue = property.vector2Value;
minValue = Mathf.Min(minValue, curValue.x);
maxValue = Mathf.Max(maxValue, curValue.y);
}
else
{
Vector2Int curValue = property.vector2IntValue;
minValue = Mathf.Min(minValue, curValue.x);
maxValue = Mathf.Max(maxValue, curValue.y);
}
}

return new MetaInfo
{
Error = "",
Expand Down Expand Up @@ -365,7 +409,7 @@ protected override VisualElement CreateFieldUIToolKit(SerializedProperty propert

MinMaxSliderField minMaxSliderField = new MinMaxSliderField(property.displayName, root);
minMaxSliderField.labelElement.style.overflow = Overflow.Hidden;
minMaxSliderField.AddToClassList("unity-base-field__aligned");
minMaxSliderField.AddToClassList(BaseField<UnityEngine.Object>.alignedFieldUssClassName);

minMaxSliderField.AddToClassList(ClassAllowDisable);

Expand Down Expand Up @@ -413,26 +457,26 @@ protected override void OnAwakeUIToolkit(SerializedProperty property, ISaintsAtt
{
MetaInfo metaInfo = (MetaInfo)minMaxSlider.userData;
ApplyIntValue(property, minMaxSliderAttribute.Step, changed.newValue, (int)metaInfo.MinValue,
(int)metaInfo.MaxValue, minMaxSlider, minIntField, maxIntField, onValueChangedCallback);
(int)metaInfo.MaxValue, minMaxSlider, minIntField, maxIntField, onValueChangedCallback, minMaxSliderAttribute.FreeInput);
});
// minIntField.RegisterCallback<BlurEvent>(evt =>
// {
// Debug.Log("Blur!");
// });
minIntField.RegisterValueChangedCallback(changed =>
{
// Debug.Log(minIntField.isDelayed);
Debug.Log(changed.newValue);
MetaInfo metaInfo = (MetaInfo)minMaxSlider.userData;
ApplyIntValue(property, minMaxSliderAttribute.Step,
ToVector2IntRange(changed.newValue, maxIntField.value), (int)metaInfo.MinValue,
(int)metaInfo.MaxValue, minMaxSlider, minIntField, maxIntField, onValueChangedCallback);
(int)metaInfo.MaxValue, minMaxSlider, minIntField, maxIntField, onValueChangedCallback, minMaxSliderAttribute.FreeInput);
});
maxIntField.RegisterValueChangedCallback(changed =>
{
MetaInfo metaInfo = (MetaInfo)minMaxSlider.userData;
ApplyIntValue(property, minMaxSliderAttribute.Step,
ToVector2IntRange(minIntField.value, changed.newValue), (int)metaInfo.MinValue,
(int)metaInfo.MaxValue, minMaxSlider, minIntField, maxIntField, onValueChangedCallback);
(int)metaInfo.MaxValue, minMaxSlider, minIntField, maxIntField, onValueChangedCallback, minMaxSliderAttribute.FreeInput);
});
}
else
Expand All @@ -444,7 +488,7 @@ protected override void OnAwakeUIToolkit(SerializedProperty property, ISaintsAtt
{
MetaInfo metaInfo = (MetaInfo)minMaxSlider.userData;
ApplyFloatValue(property, minMaxSliderAttribute.Step, changed.newValue, metaInfo.MinValue,
metaInfo.MaxValue, minMaxSlider, minFloatField, maxFloatField, onValueChangedCallback);
metaInfo.MaxValue, minMaxSlider, minFloatField, maxFloatField, onValueChangedCallback, minMaxSliderAttribute.FreeInput);
});
minFloatField.RegisterValueChangedCallback(changed =>
{
Expand All @@ -454,7 +498,7 @@ protected override void OnAwakeUIToolkit(SerializedProperty property, ISaintsAtt
#endif
ApplyFloatValue(property, minMaxSliderAttribute.Step,
ToVector2Range(changed.newValue, maxFloatField.value), metaInfo.MinValue,
metaInfo.MaxValue, minMaxSlider, minFloatField, maxFloatField, onValueChangedCallback);
metaInfo.MaxValue, minMaxSlider, minFloatField, maxFloatField, onValueChangedCallback, minMaxSliderAttribute.FreeInput);
});
maxFloatField.RegisterValueChangedCallback(changed =>
{
Expand All @@ -464,7 +508,7 @@ protected override void OnAwakeUIToolkit(SerializedProperty property, ISaintsAtt
#endif
ApplyFloatValue(property, minMaxSliderAttribute.Step,
ToVector2Range(minFloatField.value, changed.newValue), metaInfo.MinValue,
metaInfo.MaxValue, minMaxSlider, minFloatField, maxFloatField, onValueChangedCallback);
metaInfo.MaxValue, minMaxSlider, minFloatField, maxFloatField, onValueChangedCallback, minMaxSliderAttribute.FreeInput);
});
}
}
Expand Down Expand Up @@ -527,32 +571,50 @@ protected override void OnUpdateUIToolkit(SerializedProperty property, ISaintsAt
}
}

private static void ApplyIntValue(SerializedProperty property, float step, Vector2 sliderValue, int minValue, int maxValue, MinMaxSlider slider, IntegerField minField, IntegerField maxField, Action<object> onValueChangedCallback)
private static void ApplyIntValue(SerializedProperty property, float step, Vector2 sliderValue, int minValue, int maxValue, MinMaxSlider slider, IntegerField minField, IntegerField maxField, Action<object> onValueChangedCallback, bool freeInput)
{
int actualStep = Mathf.Max(1, Mathf.RoundToInt(step));
Vector2Int vector2IntValue = new Vector2Int(
Util.BoundIntStep(sliderValue.x, minValue, maxValue, actualStep),
Util.BoundIntStep(sliderValue.y, minValue, maxValue, actualStep)
);
Vector2Int vector2IntValue = freeInput
? new Vector2Int((int) sliderValue.x, (int) sliderValue.y)
: new Vector2Int(
Util.BoundIntStep(sliderValue.x, minValue, maxValue, actualStep),
Util.BoundIntStep(sliderValue.y, minValue, maxValue, actualStep)
);

property.vector2IntValue = vector2IntValue;
property.serializedObject.ApplyModifiedProperties();
// Debug.Log($"update value to {vector2IntValue}");
onValueChangedCallback.Invoke(vector2IntValue);

if (freeInput)
{
if(slider.lowLimit > vector2IntValue.x)
{
slider.lowLimit = vector2IntValue.x;
}
if(slider.highLimit < vector2IntValue.y)
{
slider.highLimit = vector2IntValue.y;
}
}

slider.SetValueWithoutNotify(vector2IntValue);
minField.SetValueWithoutNotify(vector2IntValue.x);
maxField.SetValueWithoutNotify(vector2IntValue.y);
}

private static void ApplyFloatValue(SerializedProperty property, float step, Vector2 sliderValue, float minValue, float maxValue, MinMaxSlider slider, FloatField minField, FloatField maxField, Action<object> onValueChangedCallback)
private static void ApplyFloatValue(SerializedProperty property, float step, Vector2 sliderValue, float minValue, float maxValue, MinMaxSlider slider, FloatField minField, FloatField maxField, Action<object> onValueChangedCallback, bool freeInput)
{
#if SAINTSFIELD_DEBUG && SAINTSFIELD_DEBUG_MIN_MAX_SLIDER
Debug.Log($"try apply float {minValue}~{maxValue}<={sliderValue}");
#endif

float useMin = freeInput ? float.MinValue : minValue;
float useMax = freeInput ? float.MaxValue : maxValue;

Vector2 vector2Value = step <= 0f
? new Vector2(Mathf.Max(sliderValue.x, minValue), Mathf.Min(sliderValue.y, maxValue))
: BoundV2Step(sliderValue, minValue, maxValue, step);
? new Vector2(Mathf.Max(sliderValue.x, useMin), Mathf.Min(sliderValue.y, useMax))
: BoundV2Step(sliderValue, useMin, useMax, step);

#if SAINTSFIELD_DEBUG && SAINTSFIELD_DEBUG_MIN_MAX_SLIDER
Debug.Log($"apply step={step}: {sliderValue} => {vector2Value}");
Expand All @@ -562,6 +624,18 @@ private static void ApplyFloatValue(SerializedProperty property, float step, Vec
property.serializedObject.ApplyModifiedProperties();
onValueChangedCallback.Invoke(vector2Value);

if (freeInput)
{
if(slider.minValue > vector2Value.x)
{
slider.minValue = vector2Value.x;
}
if(slider.maxValue < vector2Value.y)
{
slider.maxValue = vector2Value.y;
}
}

slider.SetValueWithoutNotify(vector2Value);
minField.SetValueWithoutNotify(vector2Value.x);
maxField.SetValueWithoutNotify(vector2Value.y);
Expand Down
17 changes: 12 additions & 5 deletions Editor/Utils/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,21 @@ public static (string error, T result) GetOf<T>(string by, T defaultValue, Seria
// Debug.Log($"{genResult}/{genResult.GetType()} -> {typeof(T)}");
// Debug.LogException(e);
// return (e.Message, defaultValue);
try
if (typeof(T) == typeof(string))
{
finalResult = (T)genResult;
finalResult = (T)Convert.ChangeType(genResult == null? "": genResult.ToString(), typeof(T));
}
catch (InvalidCastException e)
else
{
Debug.LogException(e);
return (e.Message, defaultValue);
try
{
finalResult = (T)genResult;
}
catch (InvalidCastException e)
{
Debug.LogException(e);
return (e.Message, defaultValue);
}
}
}

Expand Down
17 changes: 12 additions & 5 deletions Runtime/MinMaxSliderAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public class MinMaxSliderAttribute : PropertyAttribute, ISaintsAttribute

public readonly float MinWidth;
public readonly float MaxWidth;

public readonly bool FreeInput;
// ReSharper enable InconsistentNaming

private const float DefaultWidth = 50f;

public MinMaxSliderAttribute(float min, float max, float step=-1f, float minWidth=DefaultWidth, float maxWidth=DefaultWidth)
public MinMaxSliderAttribute(float min, float max, float step=-1f, float minWidth=DefaultWidth, float maxWidth=DefaultWidth, bool free=false)
{
Min = min;
Max = max;
Expand All @@ -34,9 +36,10 @@ public MinMaxSliderAttribute(float min, float max, float step=-1f, float minWidt

MinWidth = minWidth;
MaxWidth = maxWidth;
FreeInput = free;
}

public MinMaxSliderAttribute(int min, int max, int step=1, float minWidth=DefaultWidth, float maxWidth=DefaultWidth)
public MinMaxSliderAttribute(int min, int max, int step=1, float minWidth=DefaultWidth, float maxWidth=DefaultWidth, bool free=false)
{
Min = min;
Max = max;
Expand All @@ -46,9 +49,10 @@ public MinMaxSliderAttribute(int min, int max, int step=1, float minWidth=Defaul

MinWidth = minWidth;
MaxWidth = maxWidth;
FreeInput = free;
}

public MinMaxSliderAttribute(string minCallback, string maxCallback, int step=-1, float minWidth=DefaultWidth, float maxWidth=DefaultWidth)
public MinMaxSliderAttribute(string minCallback, string maxCallback, int step=-1, float minWidth=DefaultWidth, float maxWidth=DefaultWidth, bool free=false)
{
Min = -1;
Max = -1;
Expand All @@ -58,9 +62,10 @@ public MinMaxSliderAttribute(string minCallback, string maxCallback, int step=-1

MinWidth = minWidth;
MaxWidth = maxWidth;
FreeInput = free;
}

public MinMaxSliderAttribute(float min, string maxCallback, float step=-1f, float minWidth=DefaultWidth, float maxWidth=DefaultWidth)
public MinMaxSliderAttribute(float min, string maxCallback, float step=-1f, float minWidth=DefaultWidth, float maxWidth=DefaultWidth, bool free=false)
{
Min = min;
Max = -1;
Expand All @@ -70,9 +75,10 @@ public MinMaxSliderAttribute(float min, string maxCallback, float step=-1f, floa

MinWidth = minWidth;
MaxWidth = maxWidth;
FreeInput = free;
}

public MinMaxSliderAttribute(string minCallback, float max, float step=-1f, float minWidth=DefaultWidth, float maxWidth=DefaultWidth)
public MinMaxSliderAttribute(string minCallback, float max, float step=-1f, float minWidth=DefaultWidth, float maxWidth=DefaultWidth, bool free=false)
{
Min = -1;
Max = max;
Expand All @@ -82,6 +88,7 @@ public MinMaxSliderAttribute(string minCallback, float max, float step=-1f, floa

MinWidth = minWidth;
MaxWidth = maxWidth;
FreeInput = free;
}
}
}
2 changes: 2 additions & 0 deletions Samples~/Example.unity
Original file line number Diff line number Diff line change
Expand Up @@ -11064,6 +11064,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
myToggle: 0
richToggleImGui: 1
richToggleUIToolkit: 0
richToggle2: 0
normalToggle: 0
myStruct:
Expand Down Expand Up @@ -12973,6 +12974,7 @@ MonoBehaviour:
_propLeftRange: {x: 0, y: 0}
_propRightRange: {x: 0, y: 0}
vector2Step03Disabled: {x: 0, y: 0}
freeInt: {x: -5, y: 4}
--- !u!1 &1271404088
GameObject:
m_ObjectHideFlags: 0
Expand Down
2 changes: 1 addition & 1 deletion Samples~/Scripts/MinMaxExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MinMaxExample: MonoBehaviour
[field: SerializeField, MaxValue(nameof(_maxValue))]
public int _minValue { get; private set; }

[field: SerializeField, MinMaxSlider(nameof(_minValue), nameof(_maxValue)), AboveRichLabel(nameof(_range), true), RichLabel("<icon=star.png /><label />")]
[field: SerializeField, MinMaxSlider(nameof(_minValue), nameof(_maxValue)), AboveRichLabel("$" + nameof(_range)), RichLabel("<icon=star.png /><label />")]
public Vector2 _range { get; private set; }

[field: SerializeField, MinMaxSlider(nameof(_minValue), nameof(_maxValue)), AboveRichLabel(nameof(intRange), true)]
Expand Down
5 changes: 5 additions & 0 deletions Samples~/Scripts/MinMaxSliderExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@ public class MinMaxSliderExample: MonoBehaviour
[ReadOnly]
[MinMaxSlider(-1f, 3f, 0.3f)]
public Vector2 vector2Step03Disabled;

[Separator("Free")]

[MinMaxSlider(-5, 5, free: true)] public Vector2Int freeInt;
[MinMaxSlider(-5, 5, free: true)] public Vector2 freeFloat;
}
}

0 comments on commit 83636c9

Please sign in to comment.