using System; using System.Collections; using System.Collections.Generic; namespace UnityEngine.Rendering { /// /// On List Changed Event Args. /// /// List type. public sealed class ListChangedEventArgs : EventArgs { /// /// Index /// public readonly int index; /// /// Item /// public readonly T item; /// /// Constructor. /// /// Index /// Item public ListChangedEventArgs(int index, T item) { this.index = index; this.item = item; } } /// /// List changed event handler. /// /// List type. /// Sender. /// List changed even arguments. public delegate void ListChangedEventHandler(ObservableList sender, ListChangedEventArgs e); /// /// Observable list. /// /// Type of the list. public class ObservableList : IList { IList m_List; /// /// Added item event. /// public event ListChangedEventHandler ItemAdded; /// /// Removed item event. /// public event ListChangedEventHandler ItemRemoved; /// /// Accessor. /// /// Item index. /// The item at the provided index. public T this[int index] { get { return m_List[index]; } set { OnEvent(ItemRemoved, index, m_List[index]); m_List[index] = value; OnEvent(ItemAdded, index, value); } } /// /// Number of elements in the list. /// public int Count { get { return m_List.Count; } } /// /// Is the list read only? /// public bool IsReadOnly { get { return false; } } /// /// Default Constructor. /// public ObservableList() : this(0) { } /// /// Constructor. /// /// Allocation size. public ObservableList(int capacity) { m_List = new List(capacity); } /// /// Constructor. /// /// Input list. public ObservableList(IEnumerable collection) { m_List = new List(collection); } void OnEvent(ListChangedEventHandler e, int index, T item) { if (e != null) e(this, new ListChangedEventArgs(index, item)); } /// /// Check if an element is present in the list. /// /// Item to test against. /// True if the item is in the list. public bool Contains(T item) { return m_List.Contains(item); } /// /// Get the index of an item. /// /// The object to locate in the list. /// The index of the item in the list if it exists, -1 otherwise. public int IndexOf(T item) { return m_List.IndexOf(item); } /// /// Add an item to the list. /// /// Item to add to the list. public void Add(T item) { m_List.Add(item); OnEvent(ItemAdded, m_List.IndexOf(item), item); } /// /// Add multiple objects to the list. /// /// Items to add to the list. public void Add(params T[] items) { foreach (var i in items) Add(i); } /// /// Insert an item in the list. /// /// Index at which to insert the new item. /// Item to insert in the list. public void Insert(int index, T item) { m_List.Insert(index, item); OnEvent(ItemAdded, index, item); } /// /// Remove an item from the list. /// /// Item to remove from the list. /// True if the item was successfuly removed. False otherise. public bool Remove(T item) { int index = m_List.IndexOf(item); bool ret = m_List.Remove(item); if (ret) OnEvent(ItemRemoved, index, item); return ret; } /// /// Remove multiple items from the list. /// /// Items to remove from the list. /// The number of removed items. public int Remove(params T[] items) { if (items == null) return 0; int count = 0; foreach (var i in items) count += Remove(i) ? 1 : 0; return count; } /// /// Remove an item at a specific index. /// /// Index of the item to remove. public void RemoveAt(int index) { var item = m_List[index]; m_List.RemoveAt(index); OnEvent(ItemRemoved, index, item); } /// /// Clear the list. /// public void Clear() { while (Count > 0) RemoveAt(Count - 1); } /// /// Copy items in the list to an array. /// /// Destination array. /// Starting index. public void CopyTo(T[] array, int arrayIndex) { m_List.CopyTo(array, arrayIndex); } /// /// Get enumerator. /// /// The list enumerator. public IEnumerator GetEnumerator() { return m_List.GetEnumerator(); } /// /// Get enumerator. /// /// The list enumerator. IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }