Skip to content

Commit

Permalink
Add RangeSliders
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Oct 13, 2024
1 parent 7d165a6 commit 4a2d595
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 78 deletions.
3 changes: 2 additions & 1 deletion Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ bool WherePredicate((Type type, MultiEditorAttribute attribute) x)
return att.EditorFor == targetType;
}

return typeof(ExtendedEditor).GetAllInheritedTypesWithoutUnityObject().Select(type => (type, type.GetCustomAttribute<MultiEditorAttribute>()))
return typeof(ExtendedEditor).GetAllInheritedTypesWithoutUnityObject()
.Select(type => (type, type.GetCustomAttribute<MultiEditorAttribute>()))
.Where(WherePredicate).OrderBy(x => x.Item2.Order).ToArray();
}

Expand Down
20 changes: 20 additions & 0 deletions Assets/BetterCommons/Editor/Extensions/BaseSliderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<T>(this BaseSlider<T> self, SerializedProperty property)
where T : IComparable<T>
{
self.label = property.displayName;
self.direction = SliderDirection.Horizontal;
self.showInputField = true;
self.BindProperty(property);
self.AddToClassList("unity-base-field__aligned");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ public static bool Verify(this SerializedProperty self)
return false;
}


try
{
if (VerifyMethod != null)
Expand Down
86 changes: 10 additions & 76 deletions Assets/BetterCommons/Runtime/DataStructures/Ranges/Range.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Represents a range with minimum and maximum values of a generic type.
/// </summary>
/// <typeparam name="T">The type of the values defining the range.</typeparam>
[Serializable]
public class Range<T> : IEquatable<Range<T>>
public abstract class Range<T> : IEquatable<Range<T>>, ICloneable<Range<T>>
{
[FormerlySerializedAs("min")] [SerializeField] private T _min;
[FormerlySerializedAs("max")] [SerializeField] private T _max;

/// <summary>
/// Initializes a new instance of the Range class with default minimum and maximum values.
/// </summary>
public Range()
{
_min = default;
_max = default;
}

/// <summary>
/// Initializes a new instance of the Range class by copying another range.
/// </summary>
/// <param name="range">The range to copy.</param>
public Range(Range<T> range)
{
_min = range.Min;
_max = range.Max;
}

/// <summary>
/// Initializes a new instance of the Range class with specified minimum and maximum values.
/// </summary>
/// <param name="min">The minimum value of the range.</param>
/// <param name="max">The maximum value of the range.</param>
public Range(T min, T max)
{
_min = min;
_max = max;
}

/// <summary>
/// Gets the minimum value of the range.
/// </summary>
public T Min => _min;
public abstract T Min { get; }

/// <summary>
/// Gets the maximum value of the range.
/// </summary>
public T Max => _max;

public abstract T Max { get; }
/// <summary>
/// Determines whether the specified Range is equal to the current Range.
/// </summary>
Expand All @@ -64,8 +25,10 @@ public bool Equals(Range<T> other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return EqualityComparer<T>.Default.Equals(_min, other._min) && EqualityComparer<T>.Default.Equals(_max, other._max);
return EqualityComparer<T>.Default.Equals(Min, other.Min) && EqualityComparer<T>.Default.Equals(Max, other.Max);
}

public abstract Range<T> Clone();

/// <summary>
/// Determines whether the specified object is equal to the current Range.
Expand All @@ -79,36 +42,7 @@ public override bool Equals(object obj)
if (obj.GetType() != GetType()) return false;
return Equals((Range<T>)obj);
}

/// <summary>
/// Creates a new instance of the Range that is a copy of the current Range.
/// </summary>
/// <returns>A new Range instance that is a copy of this Range.</returns>
public Range<T> Copy()
{
return new Range<T>(_min, _max);
}

/// <summary>
/// Creates a new instance of the Range with the same minimum value as this instance and a new maximum value.
/// </summary>
/// <param name="maxValue">The new maximum value for the range.</param>
/// <returns>A new Range instance with the updated maximum value while retaining the original minimum value.</returns>
public Range<T> CopyWithMax(T maxValue)
{
return new Range<T>(_min, maxValue);
}

/// <summary>
/// Creates a new instance of the Range with the same maximum value as this instance and a new minimum value.
/// </summary>
/// <param name="minValue">The new minimum value for the range.</param>
/// <returns>A new Range instance with the updated minimum value while retaining the original maximum value.</returns>
public Range<T> CopyWithMin(T minValue)
{
return new Range<T>(minValue, _max);
}


/// <summary>
/// Serves as the default hash function.
/// </summary>
Expand All @@ -117,7 +51,7 @@ public override int GetHashCode()
{
unchecked
{
return (EqualityComparer<T>.Default.GetHashCode(_min) * 397) ^ EqualityComparer<T>.Default.GetHashCode(_max);
return (EqualityComparer<T>.Default.GetHashCode(Min) * 397) ^ EqualityComparer<T>.Default.GetHashCode(Max);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using UnityEngine;
using Better.Commons.Runtime.DataStructures.Ranges;

namespace Better.Commons.Runtime.DataStructures.SerializedTypes
{
/// <summary>
/// Represents a range with minimum and maximum values of a generic type.
/// </summary>
/// <typeparam name="T">The type of the values defining the range.</typeparam>
[Serializable]
public class SerializedRange<T> : Range<T>
{
[SerializeField] private T _min;
[SerializeField] private T _max;

/// <summary>
/// Initializes a new instance of the Range class with default minimum and maximum values.
/// </summary>
public SerializedRange()
{
_min = default;
_max = default;
}

/// <summary>
/// Initializes a new instance of the Range class by copying another range.
/// </summary>
/// <param name="range">The range to copy.</param>
public SerializedRange(Range<T> range)
{
_min = range.Min;
_max = range.Max;
}

/// <summary>
/// Initializes a new instance of the Range class with specified minimum and maximum values.
/// </summary>
/// <param name="min">The minimum value of the range.</param>
/// <param name="max">The maximum value of the range.</param>
public SerializedRange(T min, T max)
{
_min = min;
_max = max;
}

/// <summary>
/// Gets the minimum value of the range.
/// </summary>
public override T Min => _min;

/// <summary>
/// Gets the maximum value of the range.
/// </summary>
public override T Max => _max;

/// <summary>
/// Creates a new instance of the Range that is a copy of the current Range.
/// </summary>
/// <returns>A new Range instance that is a copy of this Range.</returns>
public override Range<T> Clone()
{
return new SerializedRange<T>(_min, _max);
}

/// <summary>
/// Creates a new instance of the Range with the same minimum value as this instance and a new maximum value.
/// </summary>
/// <param name="maxValue">The new maximum value for the range.</param>
/// <returns>A new Range instance with the updated maximum value while retaining the original minimum value.</returns>
public SerializedRange<T> CopyWithMax(T maxValue)
{
return new SerializedRange<T>(_min, maxValue);
}

/// <summary>
/// Creates a new instance of the Range with the same maximum value as this instance and a new minimum value.
/// </summary>
/// <param name="minValue">The new minimum value for the range.</param>
/// <returns>A new Range instance with the updated minimum value while retaining the original maximum value.</returns>
public SerializedRange<T> CopyWithMin(T minValue)
{
return new SerializedRange<T>(minValue, _max);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions Assets/BetterCommons/Runtime/UIElements/RangeSliderFloat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Better.Commons.Runtime.DataStructures.Ranges;
using UnityEngine.UIElements;

namespace Better.Commons.Runtime.UIElements
{
public class RangeSliderFloat : Slider
{
private Range<float> _range;

public RangeSliderFloat()
{

}

public RangeSliderFloat(Range<float> sliderRange) : this()
{
SetRange(sliderRange);
RefreshRange();
}

public void SetRange(Range<float> sliderRange)
{
_range = sliderRange;
}

public void RefreshRange()
{
if(_range == null) return;
lowValue = _range.Min;
highValue = _range.Max;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions Assets/BetterCommons/Runtime/UIElements/RangeSliderInt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Better.Commons.Runtime.DataStructures.Ranges;
using UnityEngine.UIElements;

namespace Better.Commons.Runtime.UIElements
{
public class RangeSliderInt : SliderInt
{
private Range<int> _range;

public RangeSliderInt()
{

}

public RangeSliderInt(Range<int> sliderRange) : this()
{
SetRange(sliderRange);
RefreshRange();
}

public void SetRange(Range<int> sliderRange)
{
_range = sliderRange;
}

public void RefreshRange()
{
if(_range == null) return;
lowValue = _range.Min;
highValue = _range.Max;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4a2d595

Please sign in to comment.