SerializableSelection.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. [Serializable]
  6. public class SerializableSelection : ISelection, ISerializationCallbackReceiver
  7. {
  8. public int Count
  9. {
  10. get { return m_Selection.Count; }
  11. }
  12. public void OnBeforeSerialize()
  13. {
  14. m_Keys = new List<int>(m_Selection).ToArray();
  15. }
  16. public void OnAfterDeserialize()
  17. {
  18. m_Selection.Clear();
  19. m_Selection.UnionWith(m_Keys);
  20. }
  21. public int single
  22. {
  23. get
  24. {
  25. int index = -1;
  26. if (Count == 1)
  27. {
  28. using (IEnumerator<int> enumerator = m_Selection.GetEnumerator())
  29. {
  30. if (enumerator.MoveNext())
  31. index = enumerator.Current;
  32. }
  33. }
  34. return index;
  35. }
  36. }
  37. public int any
  38. {
  39. get
  40. {
  41. int index = -1;
  42. if (Count > 0)
  43. {
  44. using (IEnumerator<int> enumerator = m_Selection.GetEnumerator())
  45. {
  46. if (enumerator.MoveNext())
  47. index = enumerator.Current;
  48. }
  49. }
  50. return index;
  51. }
  52. }
  53. public void Clear()
  54. {
  55. m_Selection.Clear();
  56. }
  57. public void BeginSelection()
  58. {
  59. m_TemporalSelection.Clear();
  60. m_SelectionInProgress = true;
  61. }
  62. public void EndSelection(bool select)
  63. {
  64. m_SelectionInProgress = false;
  65. if (select)
  66. m_Selection.UnionWith(m_TemporalSelection);
  67. else
  68. m_Selection.ExceptWith(m_TemporalSelection);
  69. m_TemporalSelection.Clear();
  70. }
  71. public void Select(int index, bool select)
  72. {
  73. if (select)
  74. selection.Add(index);
  75. else if (IsSelected(index))
  76. selection.Remove(index);
  77. }
  78. public bool IsSelected(int index)
  79. {
  80. return m_Selection.Contains(index) || m_TemporalSelection.Contains(index);
  81. }
  82. HashSet<int> selection
  83. {
  84. get
  85. {
  86. if (m_SelectionInProgress)
  87. return m_TemporalSelection;
  88. return m_Selection;
  89. }
  90. }
  91. IEnumerator<int> IEnumerable<int>.GetEnumerator()
  92. {
  93. return m_Selection.GetEnumerator();
  94. }
  95. IEnumerator IEnumerable.GetEnumerator()
  96. {
  97. return (IEnumerator)m_Selection.GetEnumerator();
  98. }
  99. [SerializeField]
  100. int[] m_Keys = new int[0];
  101. HashSet<int> m_Selection = new HashSet<int>();
  102. HashSet<int> m_TemporalSelection = new HashSet<int>();
  103. bool m_SelectionInProgress = false;
  104. }