#region Copyright & License Information /* * Copyright (c) The OpenRA Developers and Contributors * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. For more * information, see COPYING. */ #endregion using System; using System.Collections; using System.Collections.Generic; namespace OpenRA.Primitives { public class ObservableSortedDictionary : ObservableDictionary { public ObservableSortedDictionary(IComparer comparer) { innerDict = new SortedDictionary(comparer); } public override void Add(TKey key, TValue value) { innerDict.Add(key, value); FireOnRefresh(); } } public class ObservableDictionary : IDictionary, IObservableCollection { protected IDictionary innerDict; public event Action OnAdd = (x, k) => { }; public event Action OnRemove = (x, k) => { }; public event Action OnRemoveAt = (x, i) => { }; public event Action OnSet = (x, o, n) => { }; public event Action OnRefresh = x => { }; protected void FireOnRefresh() { OnRefresh(this); } protected ObservableDictionary() { } public ObservableDictionary(IEqualityComparer comparer) { innerDict = new Dictionary(comparer); } public virtual void Add(TKey key, TValue value) { innerDict.Add(key, value); OnAdd(this, key); } public bool Remove(TKey key) { var found = innerDict.Remove(key); if (found) OnRemove(this, key); return found; } public bool ContainsKey(TKey key) { return innerDict.ContainsKey(key); } public ICollection Keys => innerDict.Keys; public ICollection Values => innerDict.Values; public bool TryGetValue(TKey key, out TValue value) { return innerDict.TryGetValue(key, out value); } public TValue this[TKey key] { get => innerDict[key]; set => innerDict[key] = value; } public void Clear() { innerDict.Clear(); OnRefresh(this); } public int Count => innerDict.Count; public void Add(KeyValuePair item) { Add(item.Key, item.Value); } public bool Contains(KeyValuePair item) { return innerDict.Contains(item); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { innerDict.CopyTo(array, arrayIndex); } public bool IsReadOnly => innerDict.IsReadOnly; public bool Remove(KeyValuePair item) { return Remove(item.Key); } public IEnumerator> GetEnumerator() { return innerDict.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return innerDict.GetEnumerator(); } public IEnumerable ObservedItems => innerDict.Keys; } }