123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Animancer
- {
- partial struct AnimancerEvent
- {
-
-
-
-
-
-
-
-
-
-
-
- public partial class Sequence : IEnumerable<AnimancerEvent>, ICopyable<Sequence>
- {
-
- #region Fields and Properties
-
- internal const string
- IndexOutOfRangeError = "index must be within the range of 0 <= index < " + nameof(Count);
- #if UNITY_ASSERTIONS
- private const string
- NullCallbackError = nameof(AnimancerEvent) + " callbacks can't be null (except for End Events)." +
- " The " + nameof(AnimancerEvent) + "." + nameof(DummyCallback) + " can be assigned to make an event do nothing.";
- #endif
-
-
-
- private AnimancerEvent[] _Events;
-
-
- public int Count { get; private set; }
-
-
- public bool IsEmpty
- {
- get
- {
- return
- _EndEvent.callback == null &&
- float.IsNaN(_EndEvent.normalizedTime) &&
- Count == 0;
- }
- }
-
-
- public const int DefaultCapacity = 8;
-
-
-
-
-
-
-
- public int Capacity
- {
- get => _Events.Length;
- set
- {
- if (value < Count)
- throw new ArgumentOutOfRangeException(nameof(value),
- $"{nameof(Capacity)} cannot be set lower than {nameof(Count)}");
- if (value == _Events.Length)
- return;
- if (value > 0)
- {
- var newEvents = new AnimancerEvent[value];
- if (Count > 0)
- Array.Copy(_Events, 0, newEvents, 0, Count);
- _Events = newEvents;
- }
- else
- {
- _Events = Array.Empty<AnimancerEvent>();
- }
- }
- }
-
- #region Modification Detection
-
- private int _Version;
-
-
-
-
- public int Version
- {
- get => _Version;
- private set
- {
- _Version = value;
- OnSequenceModified();
- }
- }
-
- #if UNITY_ASSERTIONS
-
-
-
-
-
- public string ShouldNotModifyReason { get; private set; }
- #endif
-
-
-
-
-
-
-
-
-
- [System.Diagnostics.Conditional(Strings.Assertions)]
- public void SetShouldNotModifyReason(string reason)
- {
- #if UNITY_ASSERTIONS
- ShouldNotModifyReason = reason;
- #endif
- }
-
-
- [System.Diagnostics.Conditional(Strings.Assertions)]
- public void OnSequenceModified()
- {
- #if UNITY_ASSERTIONS
- if (string.IsNullOrEmpty(ShouldNotModifyReason) ||
- OptionalWarning.LockedEvents.IsDisabled())
- return;
- OptionalWarning.LockedEvents.Log(
- $"The {nameof(AnimancerEvent)}.{nameof(Sequence)} being modified should not be modified because " +
- ShouldNotModifyReason);
-
- ShouldNotModifyReason = null;
- #endif
- }
-
- #endregion
-
- #region End Event
-
- private AnimancerEvent _EndEvent = new AnimancerEvent(float.NaN, null);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public AnimancerEvent EndEvent
- {
- get => _EndEvent;
- set
- {
- _EndEvent = value;
- OnSequenceModified();
- }
- }
-
-
-
-
- public Action OnEnd
- {
- get => _EndEvent.callback;
- set
- {
- _EndEvent.callback = value;
- OnSequenceModified();
- }
- }
-
-
-
-
-
-
-
-
-
-
- public float NormalizedEndTime
- {
- get => _EndEvent.normalizedTime;
- set
- {
- _EndEvent.normalizedTime = value;
- OnSequenceModified();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- public static float GetDefaultNormalizedStartTime(float speed) => speed < 0 ? 1 : 0;
-
-
-
-
-
-
- public static float GetDefaultNormalizedEndTime(float speed) => speed < 0 ? 0 : 1;
-
- #endregion
-
- #region Names
-
- private string[] _Names;
-
-
- public ref string[] Names => ref _Names;
-
-
-
-
-
- public string GetName(int index)
- {
- if (_Names == null ||
- _Names.Length <= index)
- return null;
- else
- return _Names[index];
- }
-
-
-
-
-
- public void SetName(int index, string name)
- {
- AnimancerUtilities.Assert((uint)index < (uint)Count, IndexOutOfRangeError);
-
- if (_Names == null)
- {
- _Names = new string[Capacity];
- }
- else if (_Names.Length <= index)
- {
- var names = new string[Capacity];
- Array.Copy(_Names, names, _Names.Length);
- _Names = names;
- }
- _Names[index] = name;
- }
-
-
-
-
-
-
-
-
- public int IndexOf(string name, int startIndex = 0)
- {
- if (_Names == null)
- return -1;
- var count = Mathf.Min(Count, _Names.Length);
- for (; startIndex < count; startIndex++)
- if (_Names[startIndex] == name)
- return startIndex;
- return -1;
- }
-
-
-
- public int IndexOfRequired(string name, int startIndex = 0)
- {
- startIndex = IndexOf(name, startIndex);
- if (startIndex >= 0)
- return startIndex;
- throw new ArgumentException($"No event exists with the name '{name}'.");
- }
-
- #endregion
-
- #endregion
-
- #region Constructors
-
-
-
-
-
-
-
- public Sequence()
- {
- _Events = Array.Empty<AnimancerEvent>();
- }
-
-
-
-
-
-
- public Sequence(int capacity)
- {
- _Events = capacity > 0 ? new AnimancerEvent[capacity] : Array.Empty<AnimancerEvent>();
- }
-
-
-
- public Sequence(Sequence copyFrom)
- {
- _Events = Array.Empty<AnimancerEvent>();
- if (copyFrom != null)
- CopyFrom(copyFrom);
- }
-
- #endregion
-
- #region Iteration
-
-
- public AnimancerEvent this[int index]
- {
- get
- {
- AnimancerUtilities.Assert((uint)index < (uint)Count, IndexOutOfRangeError);
- return _Events[index];
- }
- }
-
-
- public AnimancerEvent this[string name] => this[IndexOfRequired(name)];
-
-
-
-
-
-
-
- [System.Diagnostics.Conditional(Strings.Assertions)]
- public void AssertNormalizedTimes(AnimancerState state)
- {
- if (Count == 0 ||
- (_Events[0].normalizedTime >= 0 && _Events[Count - 1].normalizedTime < 1))
- return;
- throw new ArgumentOutOfRangeException(nameof(normalizedTime),
- "Events on looping animations are triggered every loop and must be" +
- $" within the range of 0 <= {nameof(normalizedTime)} < 1.\n{state}\n{DeepToString()}");
- }
-
-
-
- [System.Diagnostics.Conditional(Strings.Assertions)]
- public void AssertNormalizedTimes(AnimancerState state, bool isLooping)
- {
- if (isLooping)
- AssertNormalizedTimes(state);
- }
-
-
- public string DeepToString(bool multiLine = true)
- {
- var text = ObjectPool.AcquireStringBuilder()
- .Append(ToString())
- .Append('[')
- .Append(Count)
- .Append(']');
- text.Append(multiLine ? "\n{" : " {");
- for (int i = 0; i < Count; i++)
- {
- if (multiLine)
- text.Append("\n ");
- else if (i > 0)
- text.Append(',');
- text.Append(" [");
- text.Append(i)
- .Append("] ");
- this[i].AppendDetails(text);
- var name = GetName(i);
- if (name != null)
- {
- text.Append(", Name: '")
- .Append(name)
- .Append('\'');
- }
- }
- if (multiLine)
- {
- text.Append("\n [End] ");
- }
- else
- {
- if (Count > 0)
- text.Append(',');
- text.Append(" [End] ");
- }
- _EndEvent.AppendDetails(text);
- if (multiLine)
- text.Append("\n}\n");
- else
- text.Append(" }");
- return text.ReleaseToString();
- }
-
-
-
-
-
- public FastEnumerator<AnimancerEvent> GetEnumerator()
- => new FastEnumerator<AnimancerEvent>(_Events, Count);
- IEnumerator<AnimancerEvent> IEnumerable<AnimancerEvent>.GetEnumerator()
- => GetEnumerator();
- IEnumerator IEnumerable.GetEnumerator()
- => GetEnumerator();
-
-
-
- public int IndexOf(AnimancerEvent animancerEvent) => IndexOf(Count / 2, animancerEvent);
-
-
-
- public int IndexOfRequired(AnimancerEvent animancerEvent) => IndexOfRequired(Count / 2, animancerEvent);
-
-
- public int IndexOf(int indexHint, AnimancerEvent animancerEvent)
- {
- if (Count == 0)
- return -1;
- if (indexHint >= Count)
- indexHint = Count - 1;
- var otherEvent = _Events[indexHint];
- if (otherEvent == animancerEvent)
- return indexHint;
- if (otherEvent.normalizedTime > animancerEvent.normalizedTime)
- {
- while (--indexHint >= 0)
- {
- otherEvent = _Events[indexHint];
- if (otherEvent.normalizedTime < animancerEvent.normalizedTime)
- return -1;
- else if (otherEvent.normalizedTime == animancerEvent.normalizedTime)
- if (otherEvent.callback == animancerEvent.callback)
- return indexHint;
- }
- }
- else
- {
- while (otherEvent.normalizedTime == animancerEvent.normalizedTime)
- {
- indexHint--;
- if (indexHint < 0)
- break;
- otherEvent = _Events[indexHint];
- }
- while (++indexHint < Count)
- {
- otherEvent = _Events[indexHint];
- if (otherEvent.normalizedTime > animancerEvent.normalizedTime)
- return -1;
- else if (otherEvent.normalizedTime == animancerEvent.normalizedTime)
- if (otherEvent.callback == animancerEvent.callback)
- return indexHint;
- }
- }
- return -1;
- }
-
-
-
- public int IndexOfRequired(int indexHint, AnimancerEvent animancerEvent)
- {
- indexHint = IndexOf(indexHint, animancerEvent);
- if (indexHint >= 0)
- return indexHint;
- throw new ArgumentException($"Event not found in {nameof(Sequence)} '{animancerEvent}'.");
- }
-
- #endregion
-
- #region Modification
-
-
-
-
-
-
-
-
-
-
-
-
- public int Add(AnimancerEvent animancerEvent)
- {
- #if UNITY_ASSERTIONS
- if (animancerEvent.callback == null)
- throw new ArgumentNullException($"{nameof(AnimancerEvent)}.{nameof(callback)}", NullCallbackError);
- #endif
- var index = Insert(animancerEvent.normalizedTime);
- AssertEventUniqueness(index, animancerEvent);
- _Events[index] = animancerEvent;
- return index;
- }
-
-
-
-
-
-
-
-
-
-
- public int Add(float normalizedTime, Action callback)
- => Add(new AnimancerEvent(normalizedTime, callback));
-
-
-
-
-
-
-
-
-
-
-
- public int Add(int indexHint, AnimancerEvent animancerEvent)
- {
- #if UNITY_ASSERTIONS
- if (animancerEvent.callback == null)
- throw new ArgumentNullException($"{nameof(AnimancerEvent)}.{nameof(callback)}", NullCallbackError);
- #endif
- indexHint = Insert(indexHint, animancerEvent.normalizedTime);
- AssertEventUniqueness(indexHint, animancerEvent);
- _Events[indexHint] = animancerEvent;
- return indexHint;
- }
-
-
-
-
-
-
-
-
-
-
- public int Add(int indexHint, float normalizedTime, Action callback)
- => Add(indexHint, new AnimancerEvent(normalizedTime, callback));
-
-
-
-
-
-
- public void AddRange(IEnumerable<AnimancerEvent> enumerable)
- {
- foreach (var item in enumerable)
- Add(item);
- }
-
-
-
- public void AddCallback(int index, Action callback)
- {
- ref var animancerEvent = ref _Events[index];
- AssertCallbackUniqueness(animancerEvent.callback, callback, $"{nameof(callback)} being added");
- animancerEvent.callback += callback;
- Version++;
- }
-
-
-
-
- public void AddCallback(string name, Action callback) => AddCallback(IndexOfRequired(name), callback);
-
-
-
-
-
-
- public void RemoveCallback(int index, Action callback)
- {
- ref var animancerEvent = ref _Events[index];
- animancerEvent.callback -= callback;
- if (animancerEvent.callback == null)
- animancerEvent.callback = DummyCallback;
- Version++;
- }
-
-
-
-
-
-
-
- public void RemoveCallback(string name, Action callback) => RemoveCallback(IndexOfRequired(name), callback);
-
-
-
-
- public void SetCallback(int index, Action callback)
- {
- #if UNITY_ASSERTIONS
- if (callback == null)
- throw new ArgumentNullException(nameof(callback), NullCallbackError);
- #endif
- ref var animancerEvent = ref _Events[index];
- AssertCallbackUniqueness(animancerEvent.callback, callback, $"{nameof(callback)} being assigned");
- animancerEvent.callback = callback;
- Version++;
- }
-
-
-
-
- public void SetCallback(string name, Action callback) => SetCallback(IndexOfRequired(name), callback);
-
-
-
-
-
- [System.Diagnostics.Conditional(Strings.Assertions)]
- private static void AssertCallbackUniqueness(Action oldCallback, Action newCallback, string target)
- {
- #if UNITY_ASSERTIONS
- if (oldCallback == DummyCallback ||
- OptionalWarning.DuplicateEvent.IsDisabled())
- return;
- if (oldCallback == newCallback)
- {
- OptionalWarning.DuplicateEvent.Log($"The {target}" +
- " is identical to an existing event in the sequence" +
- " which may mean that it is being unintentionally added multiple times." +
- $" If the {nameof(AnimancerEvent)}.{nameof(Sequence)} is owned by a Transition then it will not" +
- " be cleared each time it is played so the events should only be initialized once on startup." +
- $" See the documentation for more information: {Strings.DocsURLs.ClearAutomatically}");
- }
- else if (oldCallback?.Method == newCallback?.Method)
- {
- OptionalWarning.DuplicateEvent.Log($"The {target}" +
- " is identical to an existing event in the sequence except for the target object." +
- " This often happens when a Transition is shared by multiple objects," +
- " in which case it can be avoided by giving each object its own" +
- $" {nameof(AnimancerEvent)}.{nameof(Sequence)} as explained in the documentation:" +
- $" {Strings.DocsURLs.SharedEventSequences}");
- }
- #endif
- }
-
-
-
-
- [System.Diagnostics.Conditional(Strings.Assertions)]
- private void AssertEventUniqueness(int index, AnimancerEvent newEvent)
- {
- #if UNITY_ASSERTIONS
- if (OptionalWarning.DuplicateEvent.IsDisabled() || index == 0)
- return;
- var previousEvent = _Events[index - 1];
- if (previousEvent.normalizedTime != newEvent.normalizedTime)
- return;
- AssertCallbackUniqueness(previousEvent.callback, newEvent.callback, $"{nameof(AnimancerEvent)} being added");
- #endif
- }
-
-
-
-
-
-
-
- public int SetNormalizedTime(int index, float normalizedTime)
- {
- #if UNITY_ASSERTIONS
- if (!normalizedTime.IsFinite())
- throw new ArgumentOutOfRangeException(nameof(normalizedTime), normalizedTime,
- $"{nameof(normalizedTime)} {Strings.MustBeFinite}");
- #endif
- var animancerEvent = _Events[index];
- if (animancerEvent.normalizedTime == normalizedTime)
- return index;
- var moveTo = index;
- if (animancerEvent.normalizedTime < normalizedTime)
- {
- while (moveTo < Count - 1)
- {
- if (_Events[moveTo + 1].normalizedTime >= normalizedTime)
- break;
- else
- moveTo++;
- }
- }
- else
- {
- while (moveTo > 0)
- {
- if (_Events[moveTo - 1].normalizedTime <= normalizedTime)
- break;
- else
- moveTo--;
- }
- }
- if (index != moveTo)
- {
- var name = GetName(index);
- Remove(index);
- index = moveTo;
- Insert(index);
- if (!string.IsNullOrEmpty(name))
- SetName(index, name);
- }
- animancerEvent.normalizedTime = normalizedTime;
- _Events[index] = animancerEvent;
- Version++;
- return index;
- }
-
-
-
-
-
-
-
-
- public int SetNormalizedTime(string name, float normalizedTime)
- => SetNormalizedTime(IndexOfRequired(name), normalizedTime);
-
-
-
-
-
-
-
-
- public int SetNormalizedTime(AnimancerEvent animancerEvent, float normalizedTime)
- => SetNormalizedTime(IndexOfRequired(animancerEvent), normalizedTime);
-
-
-
-
-
-
-
-
-
- private int Insert(float normalizedTime)
- {
- var index = Count;
- while (index > 0 && _Events[index - 1].normalizedTime > normalizedTime)
- index--;
- Insert(index);
- return index;
- }
-
-
-
-
-
-
-
- private int Insert(int indexHint, float normalizedTime)
- {
- if (Count == 0)
- {
- Count = 0;
- }
- else
- {
- if (indexHint >= Count)
- indexHint = Count - 1;
- if (_Events[indexHint].normalizedTime > normalizedTime)
- {
- while (indexHint > 0 && _Events[indexHint - 1].normalizedTime > normalizedTime)
- indexHint--;
- }
- else
- {
- while (indexHint < Count && _Events[indexHint].normalizedTime <= normalizedTime)
- indexHint++;
- }
- }
- Insert(indexHint);
- return indexHint;
- }
-
-
-
-
-
- private void Insert(int index)
- {
- AnimancerUtilities.Assert((uint)index <= (uint)Count, IndexOutOfRangeError);
- var capacity = _Events.Length;
- if (Count == capacity)
- {
- if (capacity == 0)
- {
- capacity = DefaultCapacity;
- _Events = new AnimancerEvent[DefaultCapacity];
- }
- else
- {
- capacity *= 2;
- if (capacity < DefaultCapacity)
- capacity = DefaultCapacity;
- var events = new AnimancerEvent[capacity];
- Array.Copy(_Events, 0, events, 0, index);
- if (Count > index)
- Array.Copy(_Events, index, events, index + 1, Count - index);
- _Events = events;
- }
- }
- else if (Count > index)
- {
- Array.Copy(_Events, index, _Events, index + 1, Count - index);
- }
- if (_Names != null)
- {
- if (_Names.Length < capacity)
- {
- var names = new string[capacity];
- Array.Copy(_Names, 0, names, 0, Math.Min(_Names.Length, index));
- if (index <= Count &&
- index < _Names.Length)
- Array.Copy(_Names, index, names, index + 1, Count - index);
- _Names = names;
- }
- else
- {
- if (Count > index)
- Array.Copy(_Names, index, _Names, index + 1, Count - index);
- _Names[index] = null;
- }
- }
- Count++;
- Version++;
- }
-
-
-
-
-
- public void Remove(int index)
- {
- AnimancerUtilities.Assert((uint)index < (uint)Count, IndexOutOfRangeError);
- Count--;
- if (index < Count)
- {
- Array.Copy(_Events, index + 1, _Events, index, Count - index);
- if (_Names != null)
- {
- var nameCount = Mathf.Min(Count + 1, _Names.Length);
- if (index + 1 < nameCount)
- Array.Copy(_Names, index + 1, _Names, index, nameCount - index - 1);
- _Names[nameCount - 1] = default;
- }
- }
- else if (_Names != null && index < _Names.Length)
- {
- _Names[index] = default;
- }
- _Events[Count] = default;
- Version++;
- }
-
-
-
-
-
- public bool Remove(string name)
- {
- var index = IndexOf(name);
- if (index >= 0)
- {
- Remove(index);
- return true;
- }
- else return false;
- }
-
-
-
-
- public bool Remove(AnimancerEvent animancerEvent)
- {
- var index = IndexOf(animancerEvent);
- if (index >= 0)
- {
- Remove(index);
- return true;
- }
- else return false;
- }
-
-
- public void Clear()
- {
- if (_Names != null)
- Array.Clear(_Names, 0, _Names.Length);
- Array.Clear(_Events, 0, Count);
- Count = 0;
- Version++;
- _EndEvent = new AnimancerEvent(float.NaN, null);
- }
-
- #endregion
-
- #region Copying
-
-
- public void CopyFrom(Sequence copyFrom)
- {
- if (copyFrom == null)
- {
- if (_Names != null)
- Array.Clear(_Names, 0, _Names.Length);
- Array.Clear(_Events, 0, Count);
- Count = 0;
- Capacity = 0;
- _EndEvent = default;
- return;
- }
- AnimancerUtilities.CopyExactArray(copyFrom._Names, ref _Names);
- var sourceCount = copyFrom.Count;
- if (Count > sourceCount)
- Array.Clear(_Events, Count, sourceCount - Count);
- else if (_Events.Length < sourceCount)
- Capacity = sourceCount;
- Count = sourceCount;
- Array.Copy(copyFrom._Events, 0, _Events, 0, sourceCount);
- _EndEvent = copyFrom._EndEvent;
- }
-
-
-
-
-
-
-
-
-
-
- public void AddAllEvents(AnimationClip animation)
- {
- if (animation == null)
- return;
- var length = animation.length;
- var animationEvents = animation.events;
- Capacity += animationEvents.Length;
- var index = -1;
- for (int i = 0; i < animationEvents.Length; i++)
- {
- var animationEvent = animationEvents[i];
- index = Add(index + 1, new AnimancerEvent(animationEvent.time / length, DummyCallback));
- SetName(index, animationEvent.functionName);
- }
- }
-
-
-
-
- public void CopyTo(AnimancerEvent[] array, int index)
- {
- Array.Copy(_Events, 0, array, index, Count);
- }
-
- #endregion
-
- }
- }
- }
|