SerializableSelection.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. [Serializable]
  7. internal abstract class SerializableSelection<T> : ISelection<T>, ISerializationCallbackReceiver
  8. {
  9. internal readonly static int kInvalidID = -1;
  10. [SerializeField]
  11. private T[] m_Keys = new T[0];
  12. private HashSet<T> m_Selection = new HashSet<T>();
  13. private HashSet<T> m_TemporalSelection = new HashSet<T>();
  14. private bool m_SelectionInProgress = false;
  15. public int Count
  16. {
  17. get { return m_Selection.Count + m_TemporalSelection.Count; }
  18. }
  19. public T activeElement
  20. {
  21. get { return First(); }
  22. set
  23. {
  24. Clear();
  25. Select(value, true);
  26. }
  27. }
  28. public T[] elements
  29. {
  30. get
  31. {
  32. var set = m_Selection;
  33. if (m_SelectionInProgress)
  34. {
  35. var union = new HashSet<T>(m_Selection);
  36. union.UnionWith(m_TemporalSelection);
  37. set = union;
  38. }
  39. return new List<T>(set).ToArray();
  40. }
  41. set
  42. {
  43. Clear();
  44. foreach(var element in value)
  45. Select(element, true);
  46. }
  47. }
  48. protected abstract T GetInvalidElement();
  49. public void Clear()
  50. {
  51. GetSelection().Clear();
  52. }
  53. public void BeginSelection()
  54. {
  55. m_SelectionInProgress = true;
  56. Clear();
  57. }
  58. public void EndSelection(bool select)
  59. {
  60. m_SelectionInProgress = false;
  61. if (select)
  62. m_Selection.UnionWith(m_TemporalSelection);
  63. else
  64. m_Selection.ExceptWith(m_TemporalSelection);
  65. m_TemporalSelection.Clear();
  66. }
  67. public void Select(T element, bool select)
  68. {
  69. if(EqualityComparer<T>.Default.Equals(element, GetInvalidElement()))
  70. return;
  71. if (select)
  72. GetSelection().Add(element);
  73. else if (Contains(element))
  74. GetSelection().Remove(element);
  75. }
  76. public bool Contains(T element)
  77. {
  78. return m_Selection.Contains(element) || m_TemporalSelection.Contains(element);
  79. }
  80. private HashSet<T> GetSelection()
  81. {
  82. if (m_SelectionInProgress)
  83. return m_TemporalSelection;
  84. return m_Selection;
  85. }
  86. private T First()
  87. {
  88. T element = First(m_Selection);
  89. if(EqualityComparer<T>.Default.Equals(element, GetInvalidElement()))
  90. element = First(m_TemporalSelection);
  91. return element;
  92. }
  93. private T First(HashSet<T> set)
  94. {
  95. if(set.Count == 0)
  96. return GetInvalidElement();
  97. using (var enumerator = set.GetEnumerator())
  98. {
  99. Debug.Assert(enumerator.MoveNext());
  100. return enumerator.Current;
  101. }
  102. }
  103. void ISerializationCallbackReceiver.OnBeforeSerialize()
  104. {
  105. m_Keys = new List<T>(m_Selection).ToArray();
  106. }
  107. void ISerializationCallbackReceiver.OnAfterDeserialize()
  108. {
  109. elements = m_Keys;
  110. }
  111. }
  112. }