| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System.Collections.Specialized; |
| | | 4 | | using System.ComponentModel; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using Hertzole.ScriptableValues.Helpers; |
| | | 7 | | using UnityEngine; |
| | | 8 | | using UnityEngine.Assertions; |
| | | 9 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 10 | | using Unity.Properties; |
| | | 11 | | #endif |
| | | 12 | | |
| | | 13 | | namespace Hertzole.ScriptableValues |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Base class for all <see cref="ScriptableList{T}" />. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks>You should probably inherit from <see cref="ScriptableList{T}" /> instead.</remarks> |
| | | 19 | | #if UNITY_EDITOR |
| | | 20 | | [HelpURL(Documentation.SCRIPTABLE_LIST_URL)] |
| | | 21 | | #endif |
| | | 22 | | public abstract class ScriptableList : RuntimeScriptableObject, ICanBeReadOnly, INotifyCollectionChanged |
| | | 23 | | { |
| | 6 | 24 | | public static readonly PropertyChangingEventArgs clearOnStartChangingArgs = new PropertyChangingEventArgs(nameof |
| | 6 | 25 | | public static readonly PropertyChangedEventArgs clearOnStartChangedArgs = new PropertyChangedEventArgs(nameof(Cl |
| | | 26 | | |
| | 6 | 27 | | public static readonly PropertyChangingEventArgs isReadOnlyChangingArgs = new PropertyChangingEventArgs(nameof(I |
| | 6 | 28 | | public static readonly PropertyChangedEventArgs isReadOnlyChangedArgs = new PropertyChangedEventArgs(nameof(IsRe |
| | | 29 | | |
| | 6 | 30 | | public static readonly PropertyChangingEventArgs setEqualityCheckChangingArgs = new PropertyChangingEventArgs(na |
| | 6 | 31 | | public static readonly PropertyChangedEventArgs setEqualityCheckChangedArgs = new PropertyChangedEventArgs(nameo |
| | | 32 | | |
| | 6 | 33 | | public static readonly PropertyChangingEventArgs countChangingArgs = new PropertyChangingEventArgs(nameof(Count) |
| | 6 | 34 | | public static readonly PropertyChangedEventArgs countChangedArgs = new PropertyChangedEventArgs(nameof(Count)); |
| | | 35 | | |
| | 6 | 36 | | public static readonly PropertyChangingEventArgs capacityChangingArgs = new PropertyChangingEventArgs(nameof(Cap |
| | 6 | 37 | | public static readonly PropertyChangedEventArgs capacityChangedArgs = new PropertyChangedEventArgs(nameof(Capaci |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// If <c>true</c>, an equality check will be run before setting an item through the indexer to make sure th |
| | | 41 | | /// object is |
| | | 42 | | /// not the same as the old one. |
| | | 43 | | /// </summary> |
| | | 44 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 45 | | [CreateProperty] |
| | | 46 | | #endif |
| | | 47 | | public abstract bool SetEqualityCheck { get; set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// If <c>true</c>, the <see cref="ScriptableList{T}" /> will be cleared on play mode start/game boot. |
| | | 51 | | /// </summary> |
| | | 52 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 53 | | [CreateProperty] |
| | | 54 | | #endif |
| | | 55 | | public abstract bool ClearOnStart { get; set; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the number of elements contained in the <see cref="ScriptableList{T}" />. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <returns>The number of elements contained in the <see cref="ScriptableList{T}" />.</returns> |
| | | 61 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 62 | | #if SCRIPTABLE_VALUES_PROPERTIES_SUPPORTS_READONLY |
| | | 63 | | [CreateProperty(ReadOnly = true)] |
| | | 64 | | #else |
| | | 65 | | [CreateProperty] |
| | | 66 | | #endif // SCRIPTABLE_VALUES_PROPERTIES_SUPPORTS_READONLY |
| | | 67 | | #endif // SCRIPTABLE_VALUES_PROPERTIES |
| | | 68 | | public abstract int Count { get; protected set; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the total number of elements the internal data structure can hold without resizing. |
| | | 72 | | /// </summary> |
| | | 73 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 74 | | [CreateProperty] |
| | | 75 | | #endif |
| | | 76 | | public abstract int Capacity { get; set; } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// If read only, the <see cref="ScriptableList{T}" /> cannot be changed at runtime and won't be cleared on |
| | | 80 | | /// </summary> |
| | | 81 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 82 | | [CreateProperty] |
| | | 83 | | #endif |
| | | 84 | | public abstract bool IsReadOnly { get; set; } |
| | | 85 | | |
| | | 86 | | protected event NotifyCollectionChangedEventHandler? OnInternalCollectionChanged; |
| | | 87 | | |
| | | 88 | | event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged |
| | | 89 | | { |
| | 1098 | 90 | | add { OnInternalCollectionChanged += value; } |
| | 1062 | 91 | | remove { OnInternalCollectionChanged -= value; } |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 95 | | internal bool IsCollectionChangedNull() |
| | 8514 | 96 | | { |
| | 8514 | 97 | | return OnInternalCollectionChanged == null; |
| | 8514 | 98 | | } |
| | | 99 | | |
| | | 100 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 101 | | internal void InvokeCollectionChangedBase(NotifyCollectionChangedEventArgs args) |
| | 132 | 102 | | { |
| | 132 | 103 | | Assert.IsNotNull(OnInternalCollectionChanged, "Need to check for null before invoking the event using " + na |
| | | 104 | | |
| | | 105 | | // The event should already be checked for null here. |
| | 132 | 106 | | OnInternalCollectionChanged!.Invoke(this, args); |
| | 132 | 107 | | } |
| | | 108 | | |
| | | 109 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 110 | | internal void ClearBaseSubscribers() |
| | 2236 | 111 | | { |
| | 2236 | 112 | | OnInternalCollectionChanged = null; |
| | 2236 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <inheritdoc /> |
| | | 116 | | protected override void WarnIfLeftOverSubscribers() |
| | 2212 | 117 | | { |
| | 2212 | 118 | | base.WarnIfLeftOverSubscribers(); |
| | 2212 | 119 | | EventHelper.WarnIfLeftOverSubscribers(OnInternalCollectionChanged, "INotifyCollectionChanged.CollectionChang |
| | 2212 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Removes all elements from the <see cref="ScriptableList{T}" />. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <exception cref="System.Data.ReadOnlyException">If the object is read-only and the application is playing.</ |
| | | 126 | | public abstract void Clear(); |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Ensures that the list has at least the specified capacity. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="capacity">The minimum capacity to ensure.</param> |
| | | 132 | | public abstract void EnsureCapacity(int capacity); |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Reverses the order of the elements in the entire <see cref="ScriptableList{T}" />. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <exception cref="System.Data.ReadOnlyException">If the object is read-only and the application is playing.</ |
| | | 138 | | public abstract void Reverse(); |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Reverses the order of the elements in the specified range. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="index">The starting index of the range to reverse.</param> |
| | | 144 | | /// <param name="count">The number of elements in the range to reverse.</param> |
| | | 145 | | /// <exception cref="System.Data.ReadOnlyException">If the object is read-only and the application is playing.</ |
| | | 146 | | public abstract void Reverse(int index, int count); |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Sets the capacity to the actual number of elements in the <see cref="ScriptableList{T}" />, if that numb |
| | | 150 | | /// than a threshold value. |
| | | 151 | | /// </summary> |
| | | 152 | | public abstract void TrimExcess(); |
| | | 153 | | } |
| | | 154 | | } |