From 0e493fef4c1c2793e9df98291bcf2440584b4e0c Mon Sep 17 00:00:00 2001 From: uurha Date: Mon, 29 Jan 2024 01:52:41 +0000 Subject: [PATCH] Update Range and add ReactiveProperty --- Runtime/Properties.meta | 3 ++ Runtime/Properties/ReactiveProperty.cs | 34 ++++++++++++++++ Runtime/Properties/ReactiveProperty.cs.meta | 11 +++++ Runtime/Ranges/Range.cs | 45 +++++++++++++++++---- Runtime/Ranges/RangeExtensions.cs | 30 ++++++++++++++ package.json | 2 +- 6 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 Runtime/Properties.meta create mode 100644 Runtime/Properties/ReactiveProperty.cs create mode 100644 Runtime/Properties/ReactiveProperty.cs.meta diff --git a/Runtime/Properties.meta b/Runtime/Properties.meta new file mode 100644 index 0000000..1dbe593 --- /dev/null +++ b/Runtime/Properties.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a991541ae5c74fd89108a9db99b08eb4 +timeCreated: 1706408583 \ No newline at end of file diff --git a/Runtime/Properties/ReactiveProperty.cs b/Runtime/Properties/ReactiveProperty.cs new file mode 100644 index 0000000..784955c --- /dev/null +++ b/Runtime/Properties/ReactiveProperty.cs @@ -0,0 +1,34 @@ +using System; +using UnityEngine; + +namespace Better.DataStructures.Properties +{ + [Serializable] + public class ReactiveProperty + { + public event Action ValueChangedEvent; + + [SerializeField] + protected T _value; + + public T Value + { + get => _value; + set + { + _value = value; + SetDirty(); + } + } + + public ReactiveProperty(T defaultValue = default) + { + _value = defaultValue; + } + + public virtual void SetDirty() + { + ValueChangedEvent?.Invoke(Value); + } + } +} \ No newline at end of file diff --git a/Runtime/Properties/ReactiveProperty.cs.meta b/Runtime/Properties/ReactiveProperty.cs.meta new file mode 100644 index 0000000..4bd75df --- /dev/null +++ b/Runtime/Properties/ReactiveProperty.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ca59350704629d4f9333a26c2150145 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Ranges/Range.cs b/Runtime/Ranges/Range.cs index 144cd5e..084632f 100644 --- a/Runtime/Ranges/Range.cs +++ b/Runtime/Ranges/Range.cs @@ -1,33 +1,62 @@ using System; +using System.Collections.Generic; using UnityEngine; namespace Better.DataStructures.Ranges { [Serializable] - public struct Range where T : new() + public class Range : IEquatable> { [SerializeField] private T min; [SerializeField] private T max; + public Range() + { + min = default; + max = default; + } + + public Range(T min, T max) + { + this.min = min; + this.max = max; + } + public T Min => min; + public T Max => max; - public Range(T minValue, T maxValue) + public bool Equals(Range other) { - min = minValue; - max = maxValue; + 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); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((Range)obj); } public Range UpdateMax(T maxValue) { - max = maxValue; - return this; + return new Range(min, maxValue); } public Range UpdateMin(T minValue) { - min = minValue; - return this; + return new Range(minValue, max); + } + + public override int GetHashCode() + { + unchecked + { + return (EqualityComparer.Default.GetHashCode(min) * 397) ^ EqualityComparer.Default.GetHashCode(max); + } } } } \ No newline at end of file diff --git a/Runtime/Ranges/RangeExtensions.cs b/Runtime/Ranges/RangeExtensions.cs index 7766663..3183510 100644 --- a/Runtime/Ranges/RangeExtensions.cs +++ b/Runtime/Ranges/RangeExtensions.cs @@ -14,6 +14,36 @@ public static float Clamp(this Range range, float value) { return Mathf.Clamp(value, range.Min, range.Max); } + + public static float Random(this Range range) + { + return UnityEngine.Random.Range(range.Min, range.Max); + } + + public static int Random(this Range range) + { + return UnityEngine.Random.Range(range.Min, range.Max + 1); // +1 to have inclusive random + } + + public static float Lerp(this Range range, float t) + { + return Mathf.Lerp(range.Min, range.Max, t); + } + + public static int Lerp(this Range range, float t) + { + return Mathf.RoundToInt(Mathf.Lerp(range.Min, range.Max, t)); + } + + public static bool Contains(this Range range, float value) + { + return value >= range.Min && value <= range.Max; + } + + public static bool Contains(this Range range, int value) + { + return value >= range.Min && value <= range.Max; + } public static double Clamp(this Range range, double value) { diff --git a/package.json b/package.json index bc82964..9aa755b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.uurha.betterdatastructures", "displayName": "Better Data Structures", "description": "Collection of useful data structures for Unity", - "version": "0.1.1", + "version": "0.1.2", "unity": "2018.3", "author": { "name": "Better Plugins",