// // Copyright (c) 2017 Anthony Marmont. All rights reserved. // Licensed for use under the Unity Asset Store EULA. See https://unity3d.com/legal/as_terms for full license information. // #if UNITY_EDITOR #pragma warning disable using System; using System.Collections.Generic; using UnityEngine; namespace AssetIcons.Editors.Preferences { /// /// Provides a serializable dictionary for mapping an extension type to an . /// /// /// [Serializable] public sealed class IconMapping : ISerializationCallbackReceiver { [Serializable] private struct TableEntry { public string key; public PreferencesAssetReference value; public TableEntry(string key, PreferencesAssetReference value) { this.key = key; this.value = value; } } #pragma warning disable RECS0092 // Convert field to readonly [SerializeField] private List keyValuePairs = new List(); #pragma warning restore RECS0092 // Convert field to readonly private readonly Dictionary dictionary = new Dictionary(); /// /// An event that fires whenever the content of the table changes. /// public event Action OnChanged; /// /// Returns the value in the table at the specified key. /// /// The key of the value to retrieve. /// /// The value with the specified key. /// public PreferencesAssetReference this[string key] { get { PreferencesAssetReference value; dictionary.TryGetValue(key, out value); return value; } set { int modifyIndex = IndexOfKey(key); if (modifyIndex == -1) { Add(key, value); } dictionary[key] = value; keyValuePairs[modifyIndex] = new TableEntry(key, value); InvokeChanged(); } } /// /// Add a key and a value to the . /// /// The key of the new value. /// The value to add to the table. public void Add(string key, PreferencesAssetReference value) { if (IndexOfKey(key) == -1) { throw new ArgumentException("An item with the same key has already been added."); } dictionary.Add(key, value); keyValuePairs.Add(new TableEntry(key, value)); InvokeChanged(); } /// /// Clear all values from the . /// public void Clear() { dictionary.Clear(); keyValuePairs.Clear(); InvokeChanged(); } /// /// Removes an element from the table using a key. /// /// The key of the value to remove. /// /// true if the element was successfully removed from the table; otherwise, false. /// public bool Remove(string key) { int removeIndex = IndexOfKey(key); if (removeIndex == -1) { return false; } dictionary.Remove(key); keyValuePairs.RemoveAt(removeIndex); InvokeChanged(); return true; } /// /// Implements the Unity interface. /// void ISerializationCallbackReceiver.OnAfterDeserialize() { dictionary.Clear(); for (int i = 0; i < keyValuePairs.Count; i++) { dictionary[keyValuePairs[i].key] = keyValuePairs[i].value; } } /// /// Implements the Unity interface. /// void ISerializationCallbackReceiver.OnBeforeSerialize() { } private void InvokeChanged() { if (OnChanged != null) { OnChanged(); } } private int IndexOfKey(string key) { for (int i = 0; i < keyValuePairs.Count; i++) { string tableKey = keyValuePairs[i].key; if (tableKey == key) { return i; } } return -1; } } } #pragma warning restore #endif