SerializableSelection.cs 3.6 KB

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