AnimancerEvent.Sequence.cs 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #pragma warning disable IDE0016 // Use 'throw' expression.
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Runtime.CompilerServices;
  7. using UnityEngine;
  8. using Object = UnityEngine.Object;
  9. namespace Animancer
  10. {
  11. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerEvent
  12. partial struct AnimancerEvent
  13. {
  14. /// <summary>
  15. /// A variable-size list of <see cref="AnimancerEvent"/>s which keeps itself sorted
  16. /// according to their <see cref="normalizedTime"/>.
  17. /// </summary>
  18. /// <remarks>
  19. /// <em>Animancer Lite doesn't allow events (except for <see cref="OnEnd"/>) in runtime builds.</em>
  20. /// <para></para>
  21. /// <strong>Documentation:</strong>
  22. /// <see href="https://kybernetik.com.au/animancer/docs/manual/events/animancer">
  23. /// Animancer Events</see>
  24. /// </remarks>
  25. /// https://kybernetik.com.au/animancer/api/Animancer/Sequence
  26. ///
  27. public partial class Sequence :
  28. IEnumerable<AnimancerEvent>,
  29. ICloneable<Sequence>
  30. {
  31. /************************************************************************************************************************/
  32. #region Fields and Properties
  33. /************************************************************************************************************************/
  34. internal const string
  35. IndexOutOfRangeError = "index must be within the range of 0 <= index < " + nameof(Count);
  36. #if UNITY_ASSERTIONS
  37. private const string
  38. NullCallbackError =
  39. nameof(AnimancerEvent) + " callbacks can't be null (except for End Events). Use " +
  40. nameof(AnimancerEvent) + "." + nameof(InvokeBoundCallback) + " or " +
  41. nameof(AnimancerEvent) + "." + nameof(DummyCallback) + " instead.";
  42. #endif
  43. /************************************************************************************************************************/
  44. /// <summary>All of the events in this sequence, excluding the <see cref="EndEvent"/>.</summary>
  45. /// <remarks>This field should never be null. It should use <see cref="Array.Empty{T}"/> instead.</remarks>
  46. private AnimancerEvent[] _Events;
  47. /************************************************************************************************************************/
  48. /// <summary>[Pro-Only] The number of events in this sequence, excluding the <see cref="EndEvent"/>.</summary>
  49. public int Count { get; private set; }
  50. /************************************************************************************************************************/
  51. /// <summary>Does this sequence have no events in it, including the <see cref="EndEvent"/>?</summary>
  52. public bool IsEmpty
  53. {
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. get
  56. {
  57. return
  58. _EndEvent.callback == null &&
  59. float.IsNaN(_EndEvent.normalizedTime) &&
  60. Count == 0;
  61. }
  62. }
  63. /************************************************************************************************************************/
  64. /// <summary>The initial <see cref="Capacity"/> which will be used if another value is not specified.</summary>
  65. public const int DefaultCapacity = 4;
  66. /// <summary>[Pro-Only] The size of the internal array used to hold events.</summary>
  67. /// <remarks>
  68. /// When set, the array is re-allocated to the given size.
  69. /// <para></para>
  70. /// If not specified in the constructor, this value starts at 0
  71. /// and increases to the <see cref="DefaultCapacity"/> when the first event is added.
  72. /// </remarks>
  73. public int Capacity
  74. {
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. get => _Events.Length;
  77. set
  78. {
  79. if (value < Count)
  80. throw new ArgumentOutOfRangeException(nameof(value),
  81. $"{nameof(Capacity)} cannot be set lower than {nameof(Count)}");
  82. if (value == _Events.Length)
  83. return;
  84. if (value > 0)
  85. {
  86. var newEvents = new AnimancerEvent[value];
  87. if (Count > 0)
  88. Array.Copy(_Events, 0, newEvents, 0, Count);
  89. _Events = newEvents;
  90. }
  91. else
  92. {
  93. _Events = Array.Empty<AnimancerEvent>();
  94. }
  95. }
  96. }
  97. /************************************************************************************************************************/
  98. /// <summary>
  99. /// The number of times the contents of this sequence have been modified.
  100. /// This applies to general events, but not the <see cref="EndEvent"/>.
  101. /// </summary>
  102. public int Version { get; private set; }
  103. /************************************************************************************************************************/
  104. #region End Event
  105. /************************************************************************************************************************/
  106. private AnimancerEvent _EndEvent = new(float.NaN, null);
  107. /// <summary>
  108. /// A <see cref="callback "/> which will be triggered <strong>every frame</strong>
  109. /// after the <see cref="normalizedTime"/> has passed as long as the animation is playing.
  110. /// </summary>
  111. ///
  112. /// <remarks>
  113. /// Interrupting the animation before it ends doesn't trigger this event.
  114. /// <para></para>
  115. /// By default, the <see cref="normalizedTime"/> will be <see cref="float.NaN"/>
  116. /// so that it chooses the correct value based on the current play direction:
  117. /// playing forwards ends at 1 and playing backwards ends at 0.
  118. /// <para></para>
  119. /// <strong>Documentation:</strong>
  120. /// <see href="https://kybernetik.com.au/animancer/docs/manual/events/end">
  121. /// End Events</see>
  122. /// </remarks>
  123. ///
  124. /// <seealso cref="OnEnd"/>
  125. /// <seealso cref="NormalizedEndTime"/>
  126. public ref AnimancerEvent EndEvent
  127. {
  128. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  129. get => ref _EndEvent;
  130. }
  131. /************************************************************************************************************************/
  132. /// <summary>
  133. /// A callback which will be triggered <strong>every frame</strong> after the
  134. /// <see cref="normalizedTime"/> has passed as long as the animation is playing.
  135. /// </summary>
  136. ///
  137. /// <remarks>
  138. /// Interrupting the animation before it ends doesn't trigger this event.
  139. /// <para></para>
  140. /// By default, the <see cref="normalizedTime"/> will be <see cref="float.NaN"/>
  141. /// so that it chooses the correct value based on the current play direction:
  142. /// playing forwards ends at 1 and playing backwards ends at 0.
  143. /// <para></para>
  144. /// <strong>Documentation:</strong>
  145. /// <see href="https://kybernetik.com.au/animancer/docs/manual/events/end">
  146. /// End Events</see>
  147. /// </remarks>
  148. ///
  149. /// <seealso cref="EndEvent"/>
  150. /// <seealso cref="NormalizedEndTime"/>
  151. public ref Action OnEnd
  152. {
  153. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  154. get => ref _EndEvent.callback;
  155. }
  156. /************************************************************************************************************************/
  157. /// <summary>Shorthand for <c>EndEvent.normalizedTime</c>.</summary>
  158. /// <remarks>
  159. /// This value is <see cref="float.NaN"/> by default so that the actual time
  160. /// can be determined based on the <see cref="AnimancerNodeBase.EffectiveSpeed"/>:
  161. /// positive speed ends at 1 and negative speed ends at 0.
  162. /// <para></para>
  163. /// Use <see cref="AnimancerState.NormalizedEndTime"/> to access that value.
  164. /// </remarks>
  165. /// <seealso cref="EndEvent"/>
  166. /// <seealso cref="OnEnd"/>
  167. public ref float NormalizedEndTime
  168. {
  169. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  170. get => ref _EndEvent.normalizedTime;
  171. }
  172. /************************************************************************************************************************/
  173. /// <summary>
  174. /// Returns the <see cref="NormalizedEndTime"/> but converts <see cref="float.NaN"/>
  175. /// to its corresponding default value: positive speed ends at 1 and negative speed ends at 0.
  176. /// </summary>
  177. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  178. public float GetRealNormalizedEndTime(float speed = 1)
  179. => float.IsNaN(_EndEvent.normalizedTime)
  180. ? GetDefaultNormalizedEndTime(speed)
  181. : _EndEvent.normalizedTime;
  182. /************************************************************************************************************************/
  183. /// <summary>
  184. /// The default <see cref="AnimancerState.NormalizedTime"/> for an animation to start
  185. /// at when playing forwards is 0 (the start of the animation)
  186. /// and when playing backwards is 1 (the end of the animation).
  187. /// <para></para>
  188. /// `speed` 0 or <see cref="float.NaN"/> will also return 0.
  189. /// </summary>
  190. /// <remarks>
  191. /// This method has nothing to do with events, so it is only here because of
  192. /// <see cref="GetDefaultNormalizedEndTime"/>.
  193. /// </remarks>
  194. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  195. public static float GetDefaultNormalizedStartTime(float speed)
  196. => speed < 0 ? 1 : 0;
  197. /// <summary>
  198. /// The default <see cref="normalizedTime"/> for an <see cref="EndEvent"/>
  199. /// when playing forwards is 1 (the end of the animation)
  200. /// and when playing backwards is 0 (the start of the animation).
  201. /// <para></para>
  202. /// `speed` 0 or <see cref="float.NaN"/> will also return 1.
  203. /// </summary>
  204. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  205. public static float GetDefaultNormalizedEndTime(float speed)
  206. => speed < 0 ? 0 : 1;
  207. /************************************************************************************************************************/
  208. #endregion
  209. /************************************************************************************************************************/
  210. #region Names
  211. /************************************************************************************************************************/
  212. private StringReference[] _Names = Array.Empty<StringReference>();
  213. /// <summary>The names of the events, excluding the <see cref="EndEvent"/>.</summary>
  214. /// <remarks>This array is empty by default and can never be <c>null</c>.</remarks>
  215. public StringReference[] Names
  216. {
  217. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  218. get => _Names;
  219. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  220. set => _Names = value ?? Array.Empty<StringReference>();
  221. }
  222. /************************************************************************************************************************/
  223. /// <summary>
  224. /// Returns the name of the event at the specified `index`
  225. /// or <c>null</c> if it's outside of the <see cref="Names"/> array.
  226. /// </summary>
  227. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  228. public StringReference GetName(int index)
  229. => (uint)_Names.Length > (uint)index
  230. ? _Names[index]
  231. : null;
  232. /************************************************************************************************************************/
  233. /// <summary>Sets the name of the event at the specified `index`.</summary>
  234. /// <remarks>
  235. /// If the <see cref="Names"/> did not previously include that `index`
  236. /// it will be resized with a size equal to the <see cref="Count"/>.
  237. /// </remarks>
  238. public void SetName(int index, StringReference name)
  239. {
  240. AnimancerUtilities.Assert((uint)index < (uint)Count, IndexOutOfRangeError);
  241. // Capacity can't be 0 at this point because the above assertion would have failed.
  242. if (_Names.Length <= index)
  243. Array.Resize(ref _Names, Capacity);
  244. _Names[index] = name;
  245. }
  246. /************************************************************************************************************************/
  247. /// <summary>
  248. /// Returns the index of the first event with the specified `name`
  249. /// or <c>-1</c> if there is no such event.
  250. /// </summary>
  251. /// <seealso cref="Names"/>
  252. /// <seealso cref="GetName"/>
  253. /// <seealso cref="SetName"/>
  254. /// <seealso cref="IndexOfRequired(StringReference, int)"/>
  255. public int IndexOf(StringReference name, int startIndex = 0)
  256. {
  257. if (_Names.Length == 0)
  258. return -1;
  259. var count = Mathf.Min(Count, _Names.Length);
  260. for (; startIndex < count; startIndex++)
  261. if (_Names[startIndex] == name)
  262. return startIndex;
  263. return -1;
  264. }
  265. /// <summary>Returns the index of the first event with the specified `name`.</summary>
  266. /// <exception cref="ArgumentException">There is no such event.</exception>
  267. /// <seealso cref="IndexOf(StringReference, int)"/>
  268. public int IndexOfRequired(StringReference name, int startIndex = 0)
  269. {
  270. startIndex = IndexOf(name, startIndex);
  271. if (startIndex >= 0)
  272. return startIndex;
  273. throw new ArgumentException(
  274. $"No event exists with the name '{name}'." +
  275. $" If the specified event isn't required, use IndexOf which will return -1 if not found.");
  276. }
  277. /************************************************************************************************************************/
  278. #endregion
  279. /************************************************************************************************************************/
  280. #endregion
  281. /************************************************************************************************************************/
  282. #region Constructors
  283. /************************************************************************************************************************/
  284. /// <summary>
  285. /// Creates a new <see cref="Sequence"/> which starts at 0 <see cref="Capacity"/>.
  286. /// <para></para>
  287. /// Adding anything to the sequence will set the <see cref="Capacity"/> = <see cref="DefaultCapacity"/>
  288. /// and then double it whenever the <see cref="Count"/> would exceed the <see cref="Capacity"/>.
  289. /// </summary>
  290. public Sequence()
  291. {
  292. _Events = Array.Empty<AnimancerEvent>();
  293. }
  294. /************************************************************************************************************************/
  295. /// <summary>[Pro-Only]
  296. /// Creates a new <see cref="Sequence"/> which starts with the specified
  297. /// <see cref="Capacity"/>. It will be initially empty, but will have room for the
  298. /// given number of elements before any reallocations are required.
  299. /// </summary>
  300. public Sequence(int capacity)
  301. {
  302. _Events = capacity > 0
  303. ? new AnimancerEvent[capacity]
  304. : Array.Empty<AnimancerEvent>();
  305. }
  306. /************************************************************************************************************************/
  307. /// <summary>Creates a new <see cref="Sequence"/> and copies the contents of `copyFrom` into it.</summary>
  308. /// <remarks>To copy into an existing sequence, use <see cref="CopyFrom"/> instead.</remarks>
  309. public Sequence(Sequence copyFrom)
  310. {
  311. _Events = Array.Empty<AnimancerEvent>();
  312. if (copyFrom != null)
  313. CopyFrom(copyFrom);
  314. }
  315. /************************************************************************************************************************/
  316. #endregion
  317. /************************************************************************************************************************/
  318. #region Iteration
  319. /************************************************************************************************************************/
  320. /// <summary>[Pro-Only] Returns the event at the specified `index`.</summary>
  321. public AnimancerEvent this[int index]
  322. {
  323. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  324. get
  325. {
  326. AnimancerUtilities.Assert((uint)index < (uint)Count, IndexOutOfRangeError);
  327. return _Events[index];
  328. }
  329. }
  330. /// <summary>[Pro-Only] Returns the event with the specified `name`.</summary>
  331. /// <exception cref="ArgumentException">There is no event with the specified `name`.</exception>
  332. public AnimancerEvent this[StringReference name]
  333. {
  334. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  335. get => this[IndexOfRequired(name)];
  336. }
  337. /************************************************************************************************************************/
  338. /// <summary>Returns a string containing the details of all events in this sequence.</summary>
  339. public string DeepToString(bool multiLine = true)
  340. {
  341. var text = StringBuilderPool.Instance.Acquire()
  342. .Append(ToString())
  343. .Append('[')
  344. .Append(Count)
  345. .Append(']');
  346. text.Append(multiLine
  347. ? "\n{"
  348. : " {");
  349. for (int i = 0; i < Count; i++)
  350. {
  351. if (multiLine)
  352. text.Append("\n ");
  353. else if (i > 0)
  354. text.Append(',');
  355. text.Append(" [");
  356. text.Append(i)
  357. .Append("] ");
  358. this[i].AppendDetails(text);
  359. var name = GetName(i);
  360. if (name != null)
  361. {
  362. text.Append(", Name: '")
  363. .Append(name)
  364. .Append('\'');
  365. }
  366. }
  367. if (multiLine)
  368. {
  369. text.Append("\n [End] ");
  370. }
  371. else
  372. {
  373. if (Count > 0)
  374. text.Append(',');
  375. text.Append(" [End] ");
  376. }
  377. _EndEvent.AppendDetails(text);
  378. if (multiLine)
  379. text.Append("\n}\n");
  380. else
  381. text.Append(" }");
  382. return text.ReleaseToString();
  383. }
  384. /************************************************************************************************************************/
  385. /// <summary>[Pro-Only]
  386. /// Returns a <see cref="FastEnumerator{T}"/> for the events in this sequence,
  387. /// excluding the <see cref="EndEvent"/>.
  388. /// </summary>
  389. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  390. public FastEnumerator<AnimancerEvent> GetEnumerator()
  391. => new(_Events, Count);
  392. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  393. IEnumerator<AnimancerEvent> IEnumerable<AnimancerEvent>.GetEnumerator()
  394. => GetEnumerator();
  395. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  396. IEnumerator IEnumerable.GetEnumerator()
  397. => GetEnumerator();
  398. /************************************************************************************************************************/
  399. /// <summary>[Pro-Only] Returns the index of the `animancerEvent` or <c>-1</c> if there is no such event.</summary>
  400. /// <seealso cref="IndexOfRequired(int, AnimancerEvent)"/>
  401. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  402. public int IndexOf(AnimancerEvent animancerEvent)
  403. => IndexOf(Count / 2, animancerEvent);
  404. /// <summary>[Pro-Only] Returns the index of the `animancerEvent`.</summary>
  405. /// <exception cref="ArgumentException">There is no such event.</exception>
  406. /// <seealso cref="IndexOf(AnimancerEvent)"/>
  407. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  408. public int IndexOfRequired(AnimancerEvent animancerEvent)
  409. => IndexOfRequired(Count / 2, animancerEvent);
  410. /// <summary>[Pro-Only] Returns the index of the `animancerEvent` or <c>-1</c> if there is no such event.</summary>
  411. /// <seealso cref="IndexOfRequired(int, AnimancerEvent)"/>
  412. public int IndexOf(int indexHint, AnimancerEvent animancerEvent)
  413. {
  414. if (Count == 0)
  415. return -1;
  416. if (indexHint >= Count)
  417. indexHint = Count - 1;
  418. var events = _Events;
  419. var otherEvent = events[indexHint];
  420. if (otherEvent == animancerEvent)
  421. return indexHint;
  422. if (otherEvent.normalizedTime > animancerEvent.normalizedTime)
  423. {
  424. while (--indexHint >= 0)
  425. {
  426. otherEvent = events[indexHint];
  427. if (otherEvent.normalizedTime < animancerEvent.normalizedTime)
  428. return -1;
  429. else if (otherEvent.normalizedTime == animancerEvent.normalizedTime &&
  430. otherEvent.callback == animancerEvent.callback)
  431. return indexHint;
  432. }
  433. }
  434. else
  435. {
  436. while (otherEvent.normalizedTime == animancerEvent.normalizedTime)
  437. {
  438. indexHint--;
  439. if (indexHint < 0)
  440. break;
  441. otherEvent = events[indexHint];
  442. }
  443. while (++indexHint < Count)
  444. {
  445. otherEvent = events[indexHint];
  446. if (otherEvent.normalizedTime > animancerEvent.normalizedTime)
  447. return -1;
  448. else if (otherEvent.normalizedTime == animancerEvent.normalizedTime &&
  449. otherEvent.callback == animancerEvent.callback)
  450. return indexHint;
  451. }
  452. }
  453. return -1;
  454. }
  455. /// <summary>[Pro-Only] Returns the index of the `animancerEvent`.</summary>
  456. /// <exception cref="ArgumentException">There is no such event.</exception>
  457. /// <seealso cref="IndexOf(int, AnimancerEvent)"/>
  458. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  459. public int IndexOfRequired(int indexHint, AnimancerEvent animancerEvent)
  460. {
  461. indexHint = IndexOf(indexHint, animancerEvent);
  462. if (indexHint >= 0)
  463. return indexHint;
  464. throw new ArgumentException($"Event not found in {nameof(Sequence)} '{animancerEvent}'.");
  465. }
  466. /************************************************************************************************************************/
  467. #endregion
  468. /************************************************************************************************************************/
  469. #region Modification
  470. /************************************************************************************************************************/
  471. /// <summary>[Pro-Only]
  472. /// Adds the given event to this sequence. The <see cref="Count"/> is increased by one
  473. /// and if required, the <see cref="Capacity"/> is doubled to fit the new event.
  474. /// </summary>
  475. /// <remarks>
  476. /// This methods returns the index at which the event is added, which is determined by
  477. /// its <see cref="normalizedTime"/> to keep the sequence sorted in ascending order.
  478. /// If there are already any events with the same <see cref="normalizedTime"/>,
  479. /// the new event is added immediately after them.
  480. /// </remarks>
  481. /// <exception cref="ArgumentNullException">
  482. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  483. /// </exception>
  484. public int Add(AnimancerEvent animancerEvent)
  485. {
  486. #if UNITY_ASSERTIONS
  487. if (animancerEvent.callback == null)
  488. throw new ArgumentNullException($"{nameof(AnimancerEvent)}.{nameof(callback)}", NullCallbackError);
  489. #endif
  490. var index = Insert(animancerEvent.normalizedTime);
  491. _Events[index] = animancerEvent;
  492. return index;
  493. }
  494. /// <summary>[Pro-Only]
  495. /// Adds the given event to this sequence. The <see cref="Count"/> is increased by one
  496. /// and if required, the <see cref="Capacity"/> is doubled to fit the new event.
  497. /// </summary>
  498. /// <remarks>
  499. /// This methods returns the index at which the event is added, which is determined by
  500. /// its <see cref="normalizedTime"/> to keep the sequence sorted in ascending order.
  501. /// If there are already any events with the same <see cref="normalizedTime"/>,
  502. /// the new event is added immediately after them.
  503. /// </remarks>
  504. /// <exception cref="ArgumentNullException">
  505. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  506. /// </exception>
  507. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  508. public int Add(float normalizedTime, Action callback)
  509. => Add(new(normalizedTime, callback));
  510. /// <summary>[Pro-Only]
  511. /// Adds the given event to this sequence. The <see cref="Count"/> is increased by one
  512. /// and if required, the <see cref="Capacity"/> is doubled to fit the new event.
  513. /// </summary>
  514. /// <remarks>
  515. /// This methods returns the index at which the event is added, which is determined by
  516. /// its <see cref="normalizedTime"/> to keep the sequence sorted in ascending order.
  517. /// If there are already any events with the same <see cref="normalizedTime"/>,
  518. /// the new event is added immediately after them.
  519. /// </remarks>
  520. public int Add(int indexHint, AnimancerEvent animancerEvent)
  521. {
  522. #if UNITY_ASSERTIONS
  523. if (animancerEvent.callback == null)
  524. throw new ArgumentNullException($"{nameof(AnimancerEvent)}.{nameof(callback)}", NullCallbackError);
  525. #endif
  526. indexHint = Insert(indexHint, animancerEvent.normalizedTime);
  527. _Events[indexHint] = animancerEvent;
  528. return indexHint;
  529. }
  530. /// <summary>[Pro-Only]
  531. /// Adds the given event to this sequence. The <see cref="Count"/> is increased by one
  532. /// and if required, the <see cref="Capacity"/> is doubled to fit the new event.
  533. /// </summary>
  534. /// <remarks>
  535. /// This methods returns the index at which the event is added, which is determined by
  536. /// its <see cref="normalizedTime"/> to keep the sequence sorted in ascending order.
  537. /// If there are already any events with the same <see cref="normalizedTime"/>,
  538. /// the new event is added immediately after them.
  539. /// </remarks>
  540. /// <exception cref="ArgumentNullException">
  541. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  542. /// </exception>
  543. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  544. public int Add(int indexHint, float normalizedTime, Action callback)
  545. => Add(indexHint, new(normalizedTime, callback));
  546. /************************************************************************************************************************/
  547. /// <summary>[Pro-Only]
  548. /// Adds every event in the `enumerable` to this sequence using <see cref="Add(AnimancerEvent)"/>.
  549. /// </summary>
  550. /// <exception cref="ArgumentNullException">
  551. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  552. /// </exception>
  553. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  554. public void AddRange(IEnumerable<AnimancerEvent> enumerable)
  555. {
  556. foreach (var item in enumerable)
  557. Add(item);
  558. }
  559. /************************************************************************************************************************/
  560. /// <summary>[Pro-Only] Adds the specified `callback` to the event at the specified `index`.</summary>
  561. /// <exception cref="ArgumentNullException">
  562. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  563. /// </exception>
  564. public void AddCallback(int index, Action callback)
  565. {
  566. ref var animancerEvent = ref _Events[index];
  567. if (animancerEvent.callback == DummyCallback)
  568. {
  569. #if UNITY_ASSERTIONS
  570. if (callback == null)
  571. throw new ArgumentNullException(nameof(callback), NullCallbackError);
  572. #endif
  573. animancerEvent.callback = callback;
  574. }
  575. else
  576. {
  577. animancerEvent.callback += callback;
  578. }
  579. Version++;
  580. }
  581. /// <summary>[Pro-Only] Adds the specified `callback` to the event with the specified `name`.</summary>
  582. /// <exception cref="ArgumentException">There is no event with the specified `name`.</exception>
  583. /// <exception cref="ArgumentNullException">
  584. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  585. /// </exception>
  586. /// <seealso cref="AddCallbacks(StringReference, Action)"/>
  587. /// <seealso cref="IndexOfRequired(StringReference, int)"/>
  588. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  589. public void AddCallback(StringReference name, Action callback)
  590. => AddCallback(IndexOfRequired(name), callback);
  591. /// <summary>[Pro-Only]
  592. /// Adds the specified `callback` to every event with the specified `name`
  593. /// and returns the number of events that were found.
  594. /// </summary>
  595. /// <exception cref="ArgumentNullException">
  596. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  597. /// </exception>
  598. /// <seealso cref="AddCallback(StringReference, Action)"/>
  599. /// <seealso cref="IndexOf(StringReference, int)"/>
  600. public int AddCallbacks(StringReference name, Action callback)
  601. {
  602. var count = 0;
  603. var index = -1;
  604. while (true)
  605. {
  606. index = IndexOf(name, index + 1);
  607. if (index < 0)
  608. return count;
  609. count++;
  610. AddCallback(index, callback);
  611. }
  612. }
  613. /************************************************************************************************************************/
  614. /// <summary>[Pro-Only]
  615. /// Adds the specified `callback` to the event at the specified `index`.
  616. /// <see cref="GetCurrentParameter{T}"/> will be used to get the callback's parameter.
  617. /// </summary>
  618. /// <exception cref="ArgumentNullException">The `callback` is <c>null</c>.</exception>
  619. /// <seealso cref="AddCallback{T}(StringReference, Action{T})"/>
  620. /// <seealso cref="AddCallbacks{T}(StringReference, Action{T})"/>
  621. public Action AddCallback<T>(int index, Action<T> callback)
  622. {
  623. ref var animancerEvent = ref _Events[index];
  624. AssertContainsParameter<T>(animancerEvent.callback);
  625. var parametized = Parametize(callback);
  626. animancerEvent.callback += parametized;
  627. Version++;
  628. return parametized;
  629. }
  630. /// <summary>[Pro-Only]
  631. /// Adds the specified `callback` to the event with the specified `name`.
  632. /// <see cref="GetCurrentParameter{T}"/> will be used to get the callback's parameter.
  633. /// </summary>
  634. /// <exception cref="ArgumentException">There is no event with the specified `name`.</exception>
  635. /// <exception cref="ArgumentNullException">The `callback` is <c>null</c>.</exception>
  636. /// <seealso cref="AddCallbacks{T}(StringReference, Action{T})"/>
  637. /// <seealso cref="IndexOfRequired(StringReference, int)"/>
  638. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  639. public Action AddCallback<T>(StringReference name, Action<T> callback)
  640. => AddCallback(IndexOfRequired(name), callback);
  641. /// <summary>[Pro-Only]
  642. /// Adds the specified `callback` to every event with the specified `name`
  643. /// and returns the number of events that were found.
  644. /// <see cref="GetCurrentParameter{T}"/> will be used to get the callback's parameter.
  645. /// </summary>
  646. /// <exception cref="ArgumentNullException">The `callback` is <c>null</c>.</exception>
  647. /// <seealso cref="AddCallback{T}(StringReference, Action{T})"/>
  648. /// <seealso cref="IndexOf(StringReference, int)"/>
  649. public int AddCallbacks<T>(StringReference name, Action<T> callback)
  650. {
  651. Action parametized = null;
  652. var count = 0;
  653. var index = -1;
  654. while (true)
  655. {
  656. index = IndexOf(name, index + 1);
  657. if (index < 0)
  658. return count;
  659. AssertContainsParameter<T>(_Events[index].callback);
  660. parametized ??= Parametize(callback);
  661. count++;
  662. AddCallback(index, parametized);
  663. }
  664. }
  665. /************************************************************************************************************************/
  666. /// <summary>[Pro-Only] Removes the specified `callback` from the event at the specified `index`.</summary>
  667. /// <remarks>
  668. /// If the <see cref="callback"/> would become <c>null</c>,
  669. /// it is instead set to the <see cref="DummyCallback"/> since they are not allowed to be <c>null</c>.
  670. /// </remarks>
  671. public void RemoveCallback(int index, Action callback)
  672. {
  673. ref var animancerEvent = ref _Events[index];
  674. animancerEvent.callback -= callback;
  675. animancerEvent.callback ??= DummyCallback;
  676. Version++;
  677. }
  678. /// <summary>[Pro-Only] Removes the specified `callback` from the event with the specified `name`.</summary>
  679. /// <remarks>
  680. /// If the <see cref="callback"/> would become <c>null</c>,
  681. /// it is instead set to the <see cref="DummyCallback"/> since they are not allowed to be <c>null</c>.
  682. /// </remarks>
  683. /// <exception cref="ArgumentException">There is no event with the specified `name`.</exception>
  684. /// <seealso cref="RemoveCallbacks(StringReference, Action)"/>
  685. /// <seealso cref="IndexOfRequired(StringReference, int)"/>
  686. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  687. public void RemoveCallback(StringReference name, Action callback)
  688. => RemoveCallback(IndexOfRequired(name), callback);
  689. /// <summary>[Pro-Only]
  690. /// Removes the specified `callback` from every event with the specified `name`
  691. /// and returns the number of events that were found.
  692. /// </summary>
  693. /// <remarks>
  694. /// If a <see cref="callback"/> would become <c>null</c>,
  695. /// it is instead set to the <see cref="DummyCallback"/> since they are not allowed to be <c>null</c>.
  696. /// </remarks>
  697. /// <seealso cref="RemoveCallback(StringReference, Action)"/>
  698. /// <seealso cref="IndexOfRequired(StringReference, int)"/>
  699. public int RemoveCallbacks(StringReference name, Action callback)
  700. {
  701. var count = 0;
  702. var index = -1;
  703. while (true)
  704. {
  705. index = IndexOf(name, index + 1);
  706. if (index < 0)
  707. return count;
  708. count++;
  709. RemoveCallback(index, callback);
  710. }
  711. }
  712. /************************************************************************************************************************/
  713. /// <summary>[Pro-Only] Replaces the <see cref="callback"/> of the event at the specified `index`.</summary>
  714. /// <exception cref="ArgumentNullException">
  715. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  716. /// </exception>
  717. public void SetCallback(int index, Action callback)
  718. {
  719. #if UNITY_ASSERTIONS
  720. if (callback == null)
  721. throw new ArgumentNullException(nameof(callback), NullCallbackError);
  722. #endif
  723. ref var animancerEvent = ref _Events[index];
  724. animancerEvent.callback = callback;
  725. Version++;
  726. }
  727. /// <summary>[Pro-Only] Replaces the <see cref="callback"/> of the event with the specified `name`.</summary>
  728. /// <exception cref="ArgumentException">There is no event with the specified `name`.</exception>
  729. /// <exception cref="ArgumentNullException">
  730. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  731. /// </exception>
  732. /// <seealso cref="SetCallbacks(StringReference, Action)"/>
  733. /// <seealso cref="IndexOfRequired(StringReference, int)"/>
  734. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  735. public void SetCallback(StringReference name, Action callback)
  736. => SetCallback(IndexOfRequired(name), callback);
  737. /// <summary>[Pro-Only]
  738. /// Replaces the <see cref="callback"/> of every event with the specified `name`
  739. /// and returns the number of events that were found.
  740. /// </summary>
  741. /// <exception cref="ArgumentNullException">
  742. /// Use <see cref="DummyCallback"/> or <see cref="InvokeBoundCallback"/> instead of <c>null</c>.
  743. /// </exception>
  744. /// <seealso cref="SetCallback(StringReference, Action)"/>
  745. /// <seealso cref="IndexOfRequired(StringReference, int)"/>
  746. public int SetCallbacks(StringReference name, Action callback)
  747. {
  748. var count = 0;
  749. var index = -1;
  750. while (true)
  751. {
  752. index = IndexOf(name, index + 1);
  753. if (index < 0)
  754. return count;
  755. count++;
  756. SetCallback(index, callback);
  757. }
  758. }
  759. /************************************************************************************************************************/
  760. /// <summary>[Pro-Only] Sets the <see cref="normalizedTime"/> of the event at the specified `index`.</summary>
  761. /// <remarks>
  762. /// If multiple events have the same <see cref="normalizedTime"/>, this method will
  763. /// avoid re-arranging them where calling <see cref="Remove(int)"/> then
  764. /// <see cref="Add(AnimancerEvent)"/> would always re-add the moved event
  765. /// as the last one with that time.
  766. /// </remarks>
  767. public int SetNormalizedTime(int index, float normalizedTime)
  768. {
  769. #if UNITY_ASSERTIONS
  770. if (!normalizedTime.IsFinite())
  771. throw new ArgumentOutOfRangeException(nameof(normalizedTime), normalizedTime,
  772. $"{nameof(normalizedTime)} {Strings.MustBeFinite}");
  773. #endif
  774. var events = _Events;
  775. var animancerEvent = events[index];
  776. if (animancerEvent.normalizedTime == normalizedTime)
  777. return index;
  778. var moveTo = index;
  779. if (animancerEvent.normalizedTime < normalizedTime)
  780. {
  781. while (moveTo < Count - 1)
  782. {
  783. if (events[moveTo + 1].normalizedTime >= normalizedTime)
  784. break;
  785. else
  786. moveTo++;
  787. }
  788. }
  789. else
  790. {
  791. while (moveTo > 0)
  792. {
  793. if (events[moveTo - 1].normalizedTime <= normalizedTime)
  794. break;
  795. else
  796. moveTo--;
  797. }
  798. }
  799. if (index != moveTo)
  800. {
  801. var name = GetName(index);
  802. Remove(index);
  803. index = moveTo;
  804. Insert(index);
  805. if (!name.IsNullOrEmpty())
  806. SetName(index, name);
  807. }
  808. animancerEvent.normalizedTime = normalizedTime;
  809. events[index] = animancerEvent;
  810. Version++;
  811. return index;
  812. }
  813. /// <summary>[Pro-Only] Sets the <see cref="normalizedTime"/> of the event with the specified `name`.</summary>
  814. /// <remarks>
  815. /// If multiple events have the same <see cref="normalizedTime"/>, this method will
  816. /// avoid re-arranging them where calling <see cref="Remove(int)"/> then
  817. /// <see cref="Add(AnimancerEvent)"/> would always re-add the moved event
  818. /// as the last one with that time.
  819. /// </remarks>
  820. /// <exception cref="ArgumentException">There is no event with the specified `name`.</exception>
  821. /// <seealso cref="IndexOfRequired(StringReference, int)"/>
  822. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  823. public int SetNormalizedTime(StringReference name, float normalizedTime)
  824. => SetNormalizedTime(IndexOfRequired(name), normalizedTime);
  825. /// <summary>[Pro-Only] Sets the <see cref="normalizedTime"/> of the matching `animancerEvent`.</summary>
  826. /// <remarks>
  827. /// If multiple events have the same <see cref="normalizedTime"/>, this method will
  828. /// avoid re-arranging them where calling <see cref="Remove(int)"/> then
  829. /// <see cref="Add(AnimancerEvent)"/> would always re-add the moved event
  830. /// as the last one with that time.
  831. /// </remarks>
  832. /// <exception cref="ArgumentException">There is no event matching the `animancerEvent`.</exception>
  833. /// <seealso cref="IndexOfRequired(AnimancerEvent)"/>
  834. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  835. public int SetNormalizedTime(AnimancerEvent animancerEvent, float normalizedTime)
  836. => SetNormalizedTime(IndexOfRequired(animancerEvent), normalizedTime);
  837. /************************************************************************************************************************/
  838. /// <summary>[Pro-Only]
  839. /// Determines the index where a new event with the specified `normalizedTime` should
  840. /// be added in order to keep this sequence sorted, increases the <see cref="Count"/>
  841. /// by one, doubles the <see cref="Capacity"/> if required, moves any existing events
  842. /// to open up the chosen index, and returns that index.
  843. /// <para></para>
  844. /// </summary>
  845. /// <remarks>
  846. /// This overload starts searching for the desired index from the end of the sequence,
  847. /// based on the assumption that elements will usually be added in order.
  848. /// </remarks>
  849. private int Insert(float normalizedTime)
  850. {
  851. var index = Count;
  852. var events = _Events;
  853. while (index > 0 && events[index - 1].normalizedTime > normalizedTime)
  854. index--;
  855. Insert(index);
  856. return index;
  857. }
  858. /// <summary>[Pro-Only]
  859. /// Determines the index where a new event with the specified `normalizedTime` should
  860. /// be added in order to keep this sequence sorted, increases the <see cref="Count"/>
  861. /// by one, doubles the <see cref="Capacity"/> if required, moves any existing events
  862. /// to open up the chosen index, and returns that index.
  863. /// <para></para>
  864. /// This overload starts searching for the desired index from the `hint`.
  865. /// </summary>
  866. private int Insert(int indexHint, float normalizedTime)
  867. {
  868. if (Count == 0)
  869. {
  870. Count = 0;
  871. }
  872. else
  873. {
  874. if (indexHint >= Count)
  875. indexHint = Count - 1;
  876. var events = _Events;
  877. if (events[indexHint].normalizedTime > normalizedTime)
  878. {
  879. while (indexHint > 0 && events[indexHint - 1].normalizedTime > normalizedTime)
  880. indexHint--;
  881. }
  882. else
  883. {
  884. while (indexHint < Count && events[indexHint].normalizedTime <= normalizedTime)
  885. indexHint++;
  886. }
  887. }
  888. Insert(indexHint);
  889. return indexHint;
  890. }
  891. /************************************************************************************************************************/
  892. /// <summary>[Pro-Only]
  893. /// Increases the <see cref="Count"/> by one, doubles the <see cref="Capacity"/> if required,
  894. /// and moves any existing events to open up the `index`.
  895. /// </summary>
  896. private void Insert(int index)
  897. {
  898. AnimancerUtilities.Assert((uint)index <= (uint)Count, IndexOutOfRangeError);
  899. var capacity = _Events.Length;
  900. if (Count == capacity)
  901. {
  902. if (capacity == 0)
  903. {
  904. capacity = DefaultCapacity;
  905. _Events = new AnimancerEvent[DefaultCapacity];
  906. }
  907. else
  908. {
  909. capacity *= 2;
  910. if (capacity < DefaultCapacity)
  911. capacity = DefaultCapacity;
  912. var events = new AnimancerEvent[capacity];
  913. Array.Copy(_Events, 0, events, 0, index);
  914. if (Count > index)
  915. Array.Copy(_Events, index, events, index + 1, Count - index);
  916. _Events = events;
  917. }
  918. }
  919. else if (Count > index)
  920. {
  921. Array.Copy(_Events, index, _Events, index + 1, Count - index);
  922. }
  923. if (_Names.Length > 0)
  924. {
  925. if (_Names.Length < capacity)
  926. {
  927. var names = new StringReference[capacity];
  928. Array.Copy(_Names, 0, names, 0, Math.Min(_Names.Length, index));
  929. if (index <= Count &&
  930. index < _Names.Length)
  931. Array.Copy(_Names, index, names, index + 1, Count - index);
  932. _Names = names;
  933. }
  934. else
  935. {
  936. if (Count > index)
  937. Array.Copy(_Names, index, _Names, index + 1, Count - index);
  938. _Names[index] = null;
  939. }
  940. }
  941. Count++;
  942. Version++;
  943. }
  944. /************************************************************************************************************************/
  945. /// <summary>[Pro-Only]
  946. /// Removes the event at the specified `index` from this sequence by decrementing the
  947. /// <see cref="Count"/> and copying all events after the removed one down one place.
  948. /// </summary>
  949. public void Remove(int index)
  950. {
  951. AnimancerUtilities.Assert((uint)index < (uint)Count, IndexOutOfRangeError);
  952. Count--;
  953. if (index < Count)
  954. {
  955. Array.Copy(_Events, index + 1, _Events, index, Count - index);
  956. if (_Names.Length > 0)
  957. {
  958. var nameCount = Mathf.Min(Count + 1, _Names.Length);
  959. if (index + 1 < nameCount)
  960. Array.Copy(_Names, index + 1, _Names, index, nameCount - index - 1);
  961. _Names[nameCount - 1] = default;
  962. }
  963. }
  964. else if ((uint)_Names.Length > (uint)index)
  965. {
  966. _Names[index] = default;
  967. }
  968. _Events[Count] = default;
  969. Version++;
  970. }
  971. /// <summary>[Pro-Only]
  972. /// Removes the event with the specified `name` from this sequence by decrementing the
  973. /// <see cref="Count"/> and copying all events after the removed one down one place.
  974. /// Returns true if the event was found and removed.
  975. /// </summary>
  976. public bool Remove(StringReference name)
  977. {
  978. var index = IndexOf(name);
  979. if (index < 0)
  980. return false;
  981. Remove(index);
  982. return true;
  983. }
  984. /// <summary>[Pro-Only]
  985. /// Removes the `animancerEvent` from this sequence by decrementing the
  986. /// <see cref="Count"/> and copying all events after the removed one down one place.
  987. /// Returns true if the event was found and removed.
  988. /// </summary>
  989. public bool Remove(AnimancerEvent animancerEvent)
  990. {
  991. var index = IndexOf(animancerEvent);
  992. if (index < 0)
  993. return false;
  994. Remove(index);
  995. return true;
  996. }
  997. /************************************************************************************************************************/
  998. /// <summary>Removes all events, including the <see cref="EndEvent"/>.</summary>
  999. public void Clear()
  1000. {
  1001. Array.Clear(_Names, 0, _Names.Length);
  1002. Array.Clear(_Events, 0, Count);
  1003. Count = 0;
  1004. Version++;
  1005. _EndEvent = new(float.NaN, null);
  1006. }
  1007. /************************************************************************************************************************/
  1008. #endregion
  1009. /************************************************************************************************************************/
  1010. #region Copying
  1011. /************************************************************************************************************************/
  1012. /// <summary>Creates a new <see cref="Sequence"/> and copies the contents of <c>this</c> into it.</summary>
  1013. /// <remarks>To copy into an existing sequence, use <see cref="CopyFrom"/> instead.</remarks>
  1014. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1015. public Sequence Clone()
  1016. => new(this);
  1017. /// <inheritdoc/>
  1018. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1019. public Sequence Clone(CloneContext context)
  1020. => new(this);
  1021. /************************************************************************************************************************/
  1022. /// <inheritdoc/>
  1023. public void CopyFrom(Sequence copyFrom)
  1024. {
  1025. if (copyFrom == null)
  1026. {
  1027. Array.Clear(_Names, 0, _Names.Length);
  1028. Array.Clear(_Events, 0, Count);
  1029. Count = 0;
  1030. Capacity = 0;
  1031. _EndEvent = default;
  1032. return;
  1033. }
  1034. CopyNamesFrom(copyFrom._Names, copyFrom.Count);
  1035. var sourceCount = copyFrom.Count;
  1036. if (Count > sourceCount)
  1037. Array.Clear(_Events, Count, sourceCount - Count);
  1038. else if (_Events.Length < sourceCount)
  1039. Capacity = sourceCount;
  1040. Count = sourceCount;
  1041. Array.Copy(copyFrom._Events, 0, _Events, 0, sourceCount);
  1042. _EndEvent = copyFrom._EndEvent;
  1043. }
  1044. /************************************************************************************************************************/
  1045. /// <summary>Copies the given array into the <see cref="Names"/>.</summary>
  1046. private void CopyNamesFrom(StringReference[] copyFrom, int maxCount)
  1047. {
  1048. if (_Names.Length == 0)
  1049. {
  1050. // Both empty.
  1051. if (copyFrom.Length == 0)
  1052. return;
  1053. // Copying into empty.
  1054. maxCount = Math.Min(copyFrom.Length, maxCount);
  1055. _Names = new StringReference[copyFrom.Length];
  1056. Array.Copy(copyFrom, _Names, maxCount);
  1057. return;
  1058. }
  1059. // Copying empty into not empty.
  1060. if (copyFrom.Length == 0)
  1061. {
  1062. Array.Clear(_Names, 0, _Names.Length);
  1063. return;
  1064. }
  1065. // Copying into large enough array.
  1066. maxCount = Math.Min(copyFrom.Length, maxCount);
  1067. if (_Names.Length >= maxCount)
  1068. {
  1069. Array.Copy(copyFrom, _Names, maxCount);
  1070. Array.Clear(_Names, maxCount, _Names.Length - maxCount);
  1071. }
  1072. else// Need larger array.
  1073. {
  1074. _Names = new StringReference[copyFrom.Length];
  1075. Array.Copy(copyFrom, _Names, maxCount);
  1076. }
  1077. }
  1078. /************************************************************************************************************************/
  1079. /// <summary>[Pro-Only] Copies the <see cref="AnimationClip.events"/> into this <see cref="Sequence"/>.</summary>
  1080. /// <remarks>
  1081. /// The <see cref="callback"/> of the new events will be empty and can be set by
  1082. /// <see cref="SetCallback(StringReference, Action)"/>.
  1083. /// <para></para>
  1084. /// If you're going to play the `animation`, consider disabling <see cref="Animator.fireEvents"/>
  1085. /// so the events copied by this method are not triggered as <see cref="AnimationEvent"/>s.
  1086. /// Otherwise they would still trigger in addition to the <see cref="AnimancerEvent"/>s copied here.
  1087. /// </remarks>
  1088. public void AddAllEvents(AnimationClip animation)
  1089. {
  1090. if (animation == null)
  1091. return;
  1092. var length = animation.length;
  1093. var animationEvents = animation.events;
  1094. if (animationEvents.Length == 0)
  1095. return;
  1096. var capacity = Count + animationEvents.Length;
  1097. if (Capacity < capacity)
  1098. Capacity = Mathf.Max(Mathf.NextPowerOfTwo(capacity), DefaultCapacity);
  1099. if (_Names.Length < Capacity)
  1100. Array.Resize(ref _Names, Capacity);
  1101. var index = -1;
  1102. for (int i = 0; i < animationEvents.Length; i++)
  1103. {
  1104. var animationEvent = animationEvents[i];
  1105. index = Add(index + 1, new(animationEvent.time / length, InvokeBoundCallback));
  1106. _Names[index] = animationEvent.functionName;
  1107. }
  1108. }
  1109. /************************************************************************************************************************/
  1110. /// <summary>[<see cref="ICollection{T}"/>] [Pro-Only]
  1111. /// Copies all the events from this sequence into the `array`, starting at the `index`.
  1112. /// </summary>
  1113. public void CopyTo(AnimancerEvent[] array, int index)
  1114. {
  1115. Array.Copy(_Events, 0, array, index, Count);
  1116. }
  1117. /************************************************************************************************************************/
  1118. /// <summary>Are all events in this sequence identical to the ones in the `other` sequence?</summary>
  1119. public bool ContentsAreEqual(Sequence other)
  1120. {
  1121. if (other == null ||
  1122. _EndEvent != other._EndEvent)
  1123. return false;
  1124. if (Count != other.Count)
  1125. return false;
  1126. for (int i = Count - 1; i >= 0; i--)
  1127. if (this[i] != other[i])
  1128. return false;
  1129. return true;
  1130. }
  1131. /************************************************************************************************************************/
  1132. #endregion
  1133. /************************************************************************************************************************/
  1134. #region Assertions
  1135. /************************************************************************************************************************/
  1136. /// <summary>[Assert-Conditional]
  1137. /// Throws an <see cref="ArgumentOutOfRangeException"/>
  1138. /// if any event is outside the range of <c>0 &lt;= normalizedTime &lt; 1</c>.
  1139. /// </summary>
  1140. /// <remarks>
  1141. /// This excludes the <see cref="EndEvent"/> since it works differently to other events.
  1142. /// </remarks>
  1143. [System.Diagnostics.Conditional(Strings.Assertions)]
  1144. public void AssertNormalizedTimes(AnimancerState state)
  1145. {
  1146. if (Count == 0 ||
  1147. (_Events[0].normalizedTime >= 0 && _Events[Count - 1].normalizedTime < 1))
  1148. return;
  1149. throw new ArgumentOutOfRangeException(nameof(normalizedTime),
  1150. "Events on looping animations are triggered every loop and must be" +
  1151. $" within the range of 0 <= {nameof(normalizedTime)} < 1.\n{state}\n{DeepToString()}");
  1152. }
  1153. /************************************************************************************************************************/
  1154. /// <summary>[Assert-Conditional]
  1155. /// Calls <see cref="AssertNormalizedTimes(AnimancerState)"/> if `isLooping` is true.
  1156. /// </summary>
  1157. [System.Diagnostics.Conditional(Strings.Assertions)]
  1158. public void AssertNormalizedTimes(AnimancerState state, bool isLooping)
  1159. {
  1160. if (isLooping)
  1161. AssertNormalizedTimes(state);
  1162. }
  1163. /************************************************************************************************************************/
  1164. #endregion
  1165. /************************************************************************************************************************/
  1166. }
  1167. }
  1168. }