From 4a2d595bd19f82023e87b2f0ae0f4a383b552d7e Mon Sep 17 00:00:00 2001 From: uurha Date: Mon, 14 Oct 2024 01:29:56 +0200 Subject: [PATCH] Add RangeSliders --- .../Editor/CustomEditors/MultiEditor.cs | 3 +- .../Editor/Extensions/BaseSliderExtensions.cs | 20 +++++ .../Extensions/BaseSliderExtensions.cs.meta | 3 + .../SerializedPropertyExtensions.cs | 1 - .../Runtime/DataStructures/Ranges/Range.cs | 86 +++---------------- .../SerializedTypes/SerializedRange.cs | 86 +++++++++++++++++++ .../SerializedTypes/SerializedRange.cs.meta | 3 + .../Runtime/UIElements/RangeSliderFloat.cs | 33 +++++++ .../UIElements/RangeSliderFloat.cs.meta | 3 + .../Runtime/UIElements/RangeSliderInt.cs | 33 +++++++ .../Runtime/UIElements/RangeSliderInt.cs.meta | 3 + 11 files changed, 196 insertions(+), 78 deletions(-) create mode 100644 Assets/BetterCommons/Editor/Extensions/BaseSliderExtensions.cs create mode 100644 Assets/BetterCommons/Editor/Extensions/BaseSliderExtensions.cs.meta create mode 100644 Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedRange.cs create mode 100644 Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedRange.cs.meta create mode 100644 Assets/BetterCommons/Runtime/UIElements/RangeSliderFloat.cs create mode 100644 Assets/BetterCommons/Runtime/UIElements/RangeSliderFloat.cs.meta create mode 100644 Assets/BetterCommons/Runtime/UIElements/RangeSliderInt.cs create mode 100644 Assets/BetterCommons/Runtime/UIElements/RangeSliderInt.cs.meta diff --git a/Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs b/Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs index bf18a88..fbd77aa 100644 --- a/Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs +++ b/Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs @@ -60,7 +60,8 @@ bool WherePredicate((Type type, MultiEditorAttribute attribute) x) return att.EditorFor == targetType; } - return typeof(ExtendedEditor).GetAllInheritedTypesWithoutUnityObject().Select(type => (type, type.GetCustomAttribute())) + return typeof(ExtendedEditor).GetAllInheritedTypesWithoutUnityObject() + .Select(type => (type, type.GetCustomAttribute())) .Where(WherePredicate).OrderBy(x => x.Item2.Order).ToArray(); } diff --git a/Assets/BetterCommons/Editor/Extensions/BaseSliderExtensions.cs b/Assets/BetterCommons/Editor/Extensions/BaseSliderExtensions.cs new file mode 100644 index 0000000..de7ac64 --- /dev/null +++ b/Assets/BetterCommons/Editor/Extensions/BaseSliderExtensions.cs @@ -0,0 +1,20 @@ +using System; +using UnityEditor; +using UnityEditor.UIElements; +using UnityEngine.UIElements; + +namespace Better.Commons.EditorAddons.Extensions +{ + public static class BaseSliderExtensions + { + public static void SetupFromProperty(this BaseSlider self, SerializedProperty property) + where T : IComparable + { + self.label = property.displayName; + self.direction = SliderDirection.Horizontal; + self.showInputField = true; + self.BindProperty(property); + self.AddToClassList("unity-base-field__aligned"); + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Extensions/BaseSliderExtensions.cs.meta b/Assets/BetterCommons/Editor/Extensions/BaseSliderExtensions.cs.meta new file mode 100644 index 0000000..79b8a66 --- /dev/null +++ b/Assets/BetterCommons/Editor/Extensions/BaseSliderExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 78f5f8d269ec44cbb58e76d7dc825f72 +timeCreated: 1728851510 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Extensions/SerializedPropertyExtensions.cs b/Assets/BetterCommons/Editor/Extensions/SerializedPropertyExtensions.cs index 25a6fd0..76ab9df 100644 --- a/Assets/BetterCommons/Editor/Extensions/SerializedPropertyExtensions.cs +++ b/Assets/BetterCommons/Editor/Extensions/SerializedPropertyExtensions.cs @@ -276,7 +276,6 @@ public static bool Verify(this SerializedProperty self) return false; } - try { if (VerifyMethod != null) diff --git a/Assets/BetterCommons/Runtime/DataStructures/Ranges/Range.cs b/Assets/BetterCommons/Runtime/DataStructures/Ranges/Range.cs index 48ab441..2f377bf 100644 --- a/Assets/BetterCommons/Runtime/DataStructures/Ranges/Range.cs +++ b/Assets/BetterCommons/Runtime/DataStructures/Ranges/Range.cs @@ -1,60 +1,21 @@ using System; using System.Collections.Generic; -using UnityEngine; -using UnityEngine.Serialization; +using Better.Commons.Runtime.Interfaces; namespace Better.Commons.Runtime.DataStructures.Ranges { - /// - /// Represents a range with minimum and maximum values of a generic type. - /// - /// The type of the values defining the range. - [Serializable] - public class Range : IEquatable> + public abstract class Range : IEquatable>, ICloneable> { - [FormerlySerializedAs("min")] [SerializeField] private T _min; - [FormerlySerializedAs("max")] [SerializeField] private T _max; - - /// - /// Initializes a new instance of the Range class with default minimum and maximum values. - /// - public Range() - { - _min = default; - _max = default; - } - - /// - /// Initializes a new instance of the Range class by copying another range. - /// - /// The range to copy. - public Range(Range range) - { - _min = range.Min; - _max = range.Max; - } - - /// - /// Initializes a new instance of the Range class with specified minimum and maximum values. - /// - /// The minimum value of the range. - /// The maximum value of the range. - public Range(T min, T max) - { - _min = min; - _max = max; - } - /// /// Gets the minimum value of the range. /// - public T Min => _min; + public abstract T Min { get; } /// /// Gets the maximum value of the range. /// - public T Max => _max; - + public abstract T Max { get; } + /// /// Determines whether the specified Range is equal to the current Range. /// @@ -64,8 +25,10 @@ public bool Equals(Range other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; - return EqualityComparer.Default.Equals(_min, other._min) && EqualityComparer.Default.Equals(_max, other._max); + return EqualityComparer.Default.Equals(Min, other.Min) && EqualityComparer.Default.Equals(Max, other.Max); } + + public abstract Range Clone(); /// /// Determines whether the specified object is equal to the current Range. @@ -79,36 +42,7 @@ public override bool Equals(object obj) if (obj.GetType() != GetType()) return false; return Equals((Range)obj); } - - /// - /// Creates a new instance of the Range that is a copy of the current Range. - /// - /// A new Range instance that is a copy of this Range. - public Range Copy() - { - return new Range(_min, _max); - } - - /// - /// Creates a new instance of the Range with the same minimum value as this instance and a new maximum value. - /// - /// The new maximum value for the range. - /// A new Range instance with the updated maximum value while retaining the original minimum value. - public Range CopyWithMax(T maxValue) - { - return new Range(_min, maxValue); - } - - /// - /// Creates a new instance of the Range with the same maximum value as this instance and a new minimum value. - /// - /// The new minimum value for the range. - /// A new Range instance with the updated minimum value while retaining the original maximum value. - public Range CopyWithMin(T minValue) - { - return new Range(minValue, _max); - } - + /// /// Serves as the default hash function. /// @@ -117,7 +51,7 @@ public override int GetHashCode() { unchecked { - return (EqualityComparer.Default.GetHashCode(_min) * 397) ^ EqualityComparer.Default.GetHashCode(_max); + return (EqualityComparer.Default.GetHashCode(Min) * 397) ^ EqualityComparer.Default.GetHashCode(Max); } } } diff --git a/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedRange.cs b/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedRange.cs new file mode 100644 index 0000000..cbbacfd --- /dev/null +++ b/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedRange.cs @@ -0,0 +1,86 @@ +using System; +using UnityEngine; +using Better.Commons.Runtime.DataStructures.Ranges; + +namespace Better.Commons.Runtime.DataStructures.SerializedTypes +{ + /// + /// Represents a range with minimum and maximum values of a generic type. + /// + /// The type of the values defining the range. + [Serializable] + public class SerializedRange : Range + { + [SerializeField] private T _min; + [SerializeField] private T _max; + + /// + /// Initializes a new instance of the Range class with default minimum and maximum values. + /// + public SerializedRange() + { + _min = default; + _max = default; + } + + /// + /// Initializes a new instance of the Range class by copying another range. + /// + /// The range to copy. + public SerializedRange(Range range) + { + _min = range.Min; + _max = range.Max; + } + + /// + /// Initializes a new instance of the Range class with specified minimum and maximum values. + /// + /// The minimum value of the range. + /// The maximum value of the range. + public SerializedRange(T min, T max) + { + _min = min; + _max = max; + } + + /// + /// Gets the minimum value of the range. + /// + public override T Min => _min; + + /// + /// Gets the maximum value of the range. + /// + public override T Max => _max; + + /// + /// Creates a new instance of the Range that is a copy of the current Range. + /// + /// A new Range instance that is a copy of this Range. + public override Range Clone() + { + return new SerializedRange(_min, _max); + } + + /// + /// Creates a new instance of the Range with the same minimum value as this instance and a new maximum value. + /// + /// The new maximum value for the range. + /// A new Range instance with the updated maximum value while retaining the original minimum value. + public SerializedRange CopyWithMax(T maxValue) + { + return new SerializedRange(_min, maxValue); + } + + /// + /// Creates a new instance of the Range with the same maximum value as this instance and a new minimum value. + /// + /// The new minimum value for the range. + /// A new Range instance with the updated minimum value while retaining the original maximum value. + public SerializedRange CopyWithMin(T minValue) + { + return new SerializedRange(minValue, _max); + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedRange.cs.meta b/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedRange.cs.meta new file mode 100644 index 0000000..e5a0cce --- /dev/null +++ b/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedRange.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b56106881acd4081b4c340650434eda6 +timeCreated: 1728805364 \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/UIElements/RangeSliderFloat.cs b/Assets/BetterCommons/Runtime/UIElements/RangeSliderFloat.cs new file mode 100644 index 0000000..1e1ac00 --- /dev/null +++ b/Assets/BetterCommons/Runtime/UIElements/RangeSliderFloat.cs @@ -0,0 +1,33 @@ +using Better.Commons.Runtime.DataStructures.Ranges; +using UnityEngine.UIElements; + +namespace Better.Commons.Runtime.UIElements +{ + public class RangeSliderFloat : Slider + { + private Range _range; + + public RangeSliderFloat() + { + + } + + public RangeSliderFloat(Range sliderRange) : this() + { + SetRange(sliderRange); + RefreshRange(); + } + + public void SetRange(Range sliderRange) + { + _range = sliderRange; + } + + public void RefreshRange() + { + if(_range == null) return; + lowValue = _range.Min; + highValue = _range.Max; + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/UIElements/RangeSliderFloat.cs.meta b/Assets/BetterCommons/Runtime/UIElements/RangeSliderFloat.cs.meta new file mode 100644 index 0000000..7d9faff --- /dev/null +++ b/Assets/BetterCommons/Runtime/UIElements/RangeSliderFloat.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cc0c0fc549c54aef97ecc592280958d2 +timeCreated: 1728809178 \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/UIElements/RangeSliderInt.cs b/Assets/BetterCommons/Runtime/UIElements/RangeSliderInt.cs new file mode 100644 index 0000000..05cbb66 --- /dev/null +++ b/Assets/BetterCommons/Runtime/UIElements/RangeSliderInt.cs @@ -0,0 +1,33 @@ +using Better.Commons.Runtime.DataStructures.Ranges; +using UnityEngine.UIElements; + +namespace Better.Commons.Runtime.UIElements +{ + public class RangeSliderInt : SliderInt + { + private Range _range; + + public RangeSliderInt() + { + + } + + public RangeSliderInt(Range sliderRange) : this() + { + SetRange(sliderRange); + RefreshRange(); + } + + public void SetRange(Range sliderRange) + { + _range = sliderRange; + } + + public void RefreshRange() + { + if(_range == null) return; + lowValue = _range.Min; + highValue = _range.Max; + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/UIElements/RangeSliderInt.cs.meta b/Assets/BetterCommons/Runtime/UIElements/RangeSliderInt.cs.meta new file mode 100644 index 0000000..0f16911 --- /dev/null +++ b/Assets/BetterCommons/Runtime/UIElements/RangeSliderInt.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 89ecef59ea5f4be3ad133271b73afa0d +timeCreated: 1728860874 \ No newline at end of file