| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [Serializable]
- public class SerializableSelection : ISelection, ISerializationCallbackReceiver
- {
- public int Count
- {
- get { return m_Selection.Count; }
- }
- public void OnBeforeSerialize()
- {
- m_Keys = new List<int>(m_Selection).ToArray();
- }
- public void OnAfterDeserialize()
- {
- m_Selection.Clear();
- m_Selection.UnionWith(m_Keys);
- }
- public int single
- {
- get
- {
- int index = -1;
- if (Count == 1)
- {
- using (IEnumerator<int> enumerator = m_Selection.GetEnumerator())
- {
- if (enumerator.MoveNext())
- index = enumerator.Current;
- }
- }
- return index;
- }
- }
- public int any
- {
- get
- {
- int index = -1;
- if (Count > 0)
- {
- using (IEnumerator<int> enumerator = m_Selection.GetEnumerator())
- {
- if (enumerator.MoveNext())
- index = enumerator.Current;
- }
- }
- return index;
- }
- }
- public void Clear()
- {
- m_Selection.Clear();
- }
- public void BeginSelection()
- {
- m_TemporalSelection.Clear();
- m_SelectionInProgress = true;
- }
- public void EndSelection(bool select)
- {
- m_SelectionInProgress = false;
- if (select)
- m_Selection.UnionWith(m_TemporalSelection);
- else
- m_Selection.ExceptWith(m_TemporalSelection);
- m_TemporalSelection.Clear();
- }
- public void Select(int index, bool select)
- {
- if (select)
- selection.Add(index);
- else if (IsSelected(index))
- selection.Remove(index);
- }
- public bool IsSelected(int index)
- {
- return m_Selection.Contains(index) || m_TemporalSelection.Contains(index);
- }
- HashSet<int> selection
- {
- get
- {
- if (m_SelectionInProgress)
- return m_TemporalSelection;
- return m_Selection;
- }
- }
- IEnumerator<int> IEnumerable<int>.GetEnumerator()
- {
- return m_Selection.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return (IEnumerator)m_Selection.GetEnumerator();
- }
- [SerializeField]
- int[] m_Keys = new int[0];
- HashSet<int> m_Selection = new HashSet<int>();
- HashSet<int> m_TemporalSelection = new HashSet<int>();
- bool m_SelectionInProgress = false;
- }
|