| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.ComponentModel; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace SwiftCollections.Observable; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents an array of observable properties, raising events whenever an element is updated. |
| | | 10 | | /// Designed for performance-critical scenarios in Unity game development. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <typeparam name="TValue">The type of elements in the array.</typeparam> |
| | | 13 | | [Serializable] |
| | | 14 | | [JsonConverter(typeof(SwiftStateJsonConverterFactory))] |
| | | 15 | | [MemoryPackable] |
| | | 16 | | public partial class SwiftObservableArray<TValue> : INotifyPropertyChanged |
| | | 17 | | { |
| | | 18 | | #region Fields |
| | | 19 | | |
| | | 20 | | protected SwiftObservableProperty<TValue>[] _items; |
| | | 21 | | |
| | | 22 | | protected PropertyChangedEventHandler _itemChangedHandler; |
| | | 23 | | |
| | | 24 | | #endregion |
| | | 25 | | |
| | | 26 | | #region Events |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Raised when any element in the array changes, providing the index and the new value. |
| | | 30 | | /// </summary> |
| | | 31 | | public event EventHandler<ElementChangedEventArgs<TValue>> ElementChanged; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Raised when the array's state changes. |
| | | 35 | | /// </summary> |
| | | 36 | | public event PropertyChangedEventHandler PropertyChanged; |
| | | 37 | | |
| | | 38 | | #endregion |
| | | 39 | | |
| | | 40 | | #region Nested Types |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Provides details about a changed element, including its index and new value. |
| | | 44 | | /// </summary> |
| | | 45 | | public class ElementChangedEventArgs<T> : EventArgs |
| | | 46 | | { |
| | | 47 | | /// <summary> |
| | | 48 | | /// The index of the changed element. |
| | | 49 | | /// </summary> |
| | 6 | 50 | | public int Index { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// The new value of the changed element. |
| | | 54 | | /// </summary> |
| | 2 | 55 | | public T NewValue { get; } |
| | | 56 | | |
| | 1007 | 57 | | public ElementChangedEventArgs(int index, T newValue) |
| | 1007 | 58 | | { |
| | 1007 | 59 | | Index = index; |
| | 1007 | 60 | | NewValue = newValue; |
| | 1007 | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | #endregion |
| | | 65 | | |
| | | 66 | | #region Constructors |
| | | 67 | | |
| | 20 | 68 | | public SwiftObservableArray(int capacity) |
| | 20 | 69 | | { |
| | 20 | 70 | | SwiftThrowHelper.ThrowIfNegativeOrZero(capacity, nameof(capacity)); |
| | | 71 | | |
| | 20 | 72 | | _items = new SwiftObservableProperty<TValue>[capacity]; |
| | 1045 | 73 | | _itemChangedHandler = (sender, e) => OnItemChanged(sender); |
| | | 74 | | |
| | 202146 | 75 | | for (int i = 0; i < capacity; i++) |
| | 101053 | 76 | | { |
| | 101053 | 77 | | _items[i] = new SwiftObservableProperty<TValue> |
| | 101053 | 78 | | { |
| | 101053 | 79 | | Index = i |
| | 101053 | 80 | | }; |
| | 101053 | 81 | | _items[i].PropertyChanged += _itemChangedHandler; |
| | 101053 | 82 | | } |
| | 20 | 83 | | } |
| | | 84 | | |
| | 1 | 85 | | public SwiftObservableArray(SwiftObservableProperty<TValue>[] observableProperties) |
| | 1 | 86 | | { |
| | 1 | 87 | | SwiftThrowHelper.ThrowIfNull(observableProperties, nameof(observableProperties)); |
| | | 88 | | |
| | 0 | 89 | | _items = observableProperties; |
| | 0 | 90 | | _itemChangedHandler = (sender, e) => OnItemChanged(sender); |
| | | 91 | | |
| | 0 | 92 | | for (int i = 0; i < _items.Length; i++) |
| | 0 | 93 | | { |
| | 0 | 94 | | _items[i].Index = i; |
| | 0 | 95 | | _items[i].PropertyChanged += _itemChangedHandler; |
| | 0 | 96 | | } |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Initializes a new instance of the <see cref="SwiftObservableArray{TValue}"/> class with the specified <see cref |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 103 | | [MemoryPackConstructor] |
| | 7 | 104 | | public SwiftObservableArray(SwiftArrayState<TValue> state) |
| | 7 | 105 | | { |
| | 7 | 106 | | State = state; |
| | 7 | 107 | | } |
| | | 108 | | |
| | | 109 | | #endregion |
| | | 110 | | |
| | | 111 | | #region Properties |
| | | 112 | | |
| | | 113 | | [JsonIgnore] |
| | | 114 | | [MemoryPackIgnore] |
| | 2 | 115 | | public int Capacity => _items.Length; |
| | | 116 | | |
| | | 117 | | [JsonIgnore] |
| | | 118 | | [MemoryPackIgnore] |
| | | 119 | | public TValue this[int index] |
| | | 120 | | { |
| | | 121 | | get |
| | 10 | 122 | | { |
| | 10 | 123 | | ValidateIndex(index); |
| | 8 | 124 | | return _items[index].Value; |
| | 8 | 125 | | } |
| | | 126 | | set |
| | 1029 | 127 | | { |
| | 1029 | 128 | | ValidateIndex(index); |
| | 1029 | 129 | | if (!Equals(_items[index].Value, value)) |
| | 1027 | 130 | | _items[index].Value = value; |
| | 1029 | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | [JsonInclude] |
| | | 135 | | [MemoryPackInclude] |
| | | 136 | | public SwiftArrayState<TValue> State |
| | | 137 | | { |
| | | 138 | | get |
| | 8 | 139 | | { |
| | 8 | 140 | | var values = new TValue[_items.Length]; |
| | | 141 | | |
| | 54 | 142 | | for (int i = 0; i < _items.Length; i++) |
| | 19 | 143 | | values[i] = _items[i].Value; |
| | | 144 | | |
| | 8 | 145 | | return new SwiftArrayState<TValue>(values); |
| | 8 | 146 | | } |
| | | 147 | | internal set |
| | 7 | 148 | | { |
| | 7 | 149 | | var values = value.Items; |
| | | 150 | | |
| | 9 | 151 | | _itemChangedHandler = (sender, e) => OnItemChanged(sender); |
| | | 152 | | |
| | 7 | 153 | | _items = new SwiftObservableProperty<TValue>[values.Length]; |
| | | 154 | | |
| | 48 | 155 | | for (int i = 0; i < values.Length; i++) |
| | 17 | 156 | | { |
| | 17 | 157 | | _items[i] = new SwiftObservableProperty<TValue>(values[i]) |
| | 17 | 158 | | { |
| | 17 | 159 | | Index = i |
| | 17 | 160 | | }; |
| | 17 | 161 | | _items[i].PropertyChanged += _itemChangedHandler; |
| | 17 | 162 | | } |
| | 7 | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | #endregion |
| | | 167 | | |
| | | 168 | | #region Methods |
| | | 169 | | |
| | | 170 | | public TValue[] ToArray() |
| | 6 | 171 | | { |
| | 6 | 172 | | var array = new TValue[_items.Length]; |
| | 202040 | 173 | | for (int i = 0; i < _items.Length; i++) |
| | 101014 | 174 | | array[i] = _items[i].Value; |
| | 6 | 175 | | return array; |
| | 6 | 176 | | } |
| | | 177 | | |
| | | 178 | | private void ValidateIndex(int index) |
| | 1039 | 179 | | { |
| | 1039 | 180 | | if (index < 0 || index >= _items.Length) |
| | 2 | 181 | | throw new IndexOutOfRangeException($"Index {index} is out of bounds for this array."); |
| | 1037 | 182 | | } |
| | | 183 | | |
| | | 184 | | private void OnItemChanged(object sender) |
| | 1027 | 185 | | { |
| | 1027 | 186 | | if (sender is SwiftObservableProperty<TValue> property) |
| | 1027 | 187 | | { |
| | 1027 | 188 | | int index = property.Index; |
| | | 189 | | |
| | 1027 | 190 | | ElementChanged?.Invoke( |
| | 1027 | 191 | | this, |
| | 1027 | 192 | | new ElementChangedEventArgs<TValue>(index, property.Value) |
| | 1027 | 193 | | ); |
| | | 194 | | |
| | 1027 | 195 | | OnPropertyChanged("Items[]"); |
| | 1027 | 196 | | } |
| | 1027 | 197 | | } |
| | | 198 | | |
| | | 199 | | private void OnPropertyChanged(string propertyName) |
| | 1027 | 200 | | { |
| | 1027 | 201 | | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| | 1027 | 202 | | } |
| | | 203 | | |
| | | 204 | | #endregion |
| | | 205 | | } |