CompositeMotionHandle.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace LitMotion
  5. {
  6. /// <summary>
  7. /// A class that manages multiple motion handles at once.
  8. /// </summary>
  9. public sealed class CompositeMotionHandle : ICollection<MotionHandle>, IEnumerable<MotionHandle>
  10. {
  11. public CompositeMotionHandle()
  12. {
  13. handleList = new();
  14. }
  15. public CompositeMotionHandle(int capacity)
  16. {
  17. if (capacity < 0)
  18. {
  19. throw new ArgumentOutOfRangeException("capacity");
  20. }
  21. handleList = new(capacity);
  22. }
  23. /// <summary>
  24. /// Cancel all Motion handles and clear list.
  25. /// </summary>
  26. public void Cancel()
  27. {
  28. for (int i = 0; i < handleList.Count; i++)
  29. {
  30. var handle = handleList[i];
  31. if (handle.IsActive()) handle.Cancel();
  32. }
  33. handleList.Clear();
  34. }
  35. /// <summary>
  36. /// Complete all motion handles and clear list.
  37. /// </summary>
  38. public void Complete()
  39. {
  40. for (int i = 0; i < handleList.Count; i++)
  41. {
  42. var handle = handleList[i];
  43. if (handle.IsActive()) handle.Complete();
  44. }
  45. handleList.Clear();
  46. }
  47. /// <summary>
  48. /// Add motion handle.
  49. /// </summary>
  50. /// <param name="handle">Motion handle</param>
  51. public void Add(MotionHandle handle)
  52. {
  53. handleList.Add(handle);
  54. }
  55. public List<MotionHandle>.Enumerator GetEnumerator()
  56. {
  57. return handleList.GetEnumerator();
  58. }
  59. IEnumerator<MotionHandle> IEnumerable<MotionHandle>.GetEnumerator()
  60. {
  61. return handleList.GetEnumerator();
  62. }
  63. IEnumerator IEnumerable.GetEnumerator()
  64. {
  65. return handleList.GetEnumerator();
  66. }
  67. public void Clear()
  68. {
  69. handleList.Clear();
  70. }
  71. public bool Contains(MotionHandle item)
  72. {
  73. return handleList.Contains(item);
  74. }
  75. public void CopyTo(MotionHandle[] array, int arrayIndex)
  76. {
  77. handleList.CopyTo(array, arrayIndex);
  78. }
  79. public bool Remove(MotionHandle item)
  80. {
  81. return handleList.Remove(item);
  82. }
  83. public int Count => handleList.Count;
  84. public bool IsReadOnly => false;
  85. readonly List<MotionHandle> handleList;
  86. }
  87. }