using System; using UnityEditor.AnimatedValues; using System.Collections.Generic; namespace UnityEditor.Rendering { /// Used in editor drawer part to store the state of additional properties areas. /// An enum to use to describe the state. /// A type given to automatically compute the key. public class AdditionalPropertiesState where TState : struct, IConvertible { EditorPrefBoolFlags m_State; HashSet m_Editors = new HashSet(); Dictionary m_AnimFloats = new Dictionary(); void RepaintAll() { foreach (var editor in m_Editors) { editor.Repaint(); } } /// Constructor will create the key to store in the EditorPref the state given generic type passed. /// If key did not exist, it will be created with this value for initialization. /// [Optional] Prefix scope of the key (Default is CoreRP) public AdditionalPropertiesState(TState defaultValue, string prefix = "CoreRP") { string key = $"{prefix}:{typeof(TTarget).Name}:{typeof(TState).Name}:UI_AP_State"; m_State = new EditorPrefBoolFlags(key); //register key if not already there if (!EditorPrefs.HasKey(key)) { EditorPrefs.SetInt(key, (int)(object)defaultValue); } } /// Get or set the state given the mask. /// The filtering mask /// True: All flagged area are expended public bool this[TState mask] { get => GetAdditionalPropertiesState(mask); set => SetAdditionalPropertiesState(mask, value); } /// Accessor to the expended state of this specific mask. /// The filtering mask /// True: All flagged area are expended public bool GetAdditionalPropertiesState(TState mask) { return m_State.HasFlag(mask); } /// Setter to the expended state. /// The filtering mask /// True to show the additional properties. public void SetAdditionalPropertiesState(TState mask, bool value) { m_State.SetFlag(mask, value); if (value) ResetAnimation(mask); } /// Utility to set all states to true public void ShowAll() { m_State.rawValue = 0xFFFFFFFF; } /// Utility to set all states to false public void HideAll() { m_State.rawValue = 0; } internal AnimFloat GetAnimation(TState mask) { AnimFloat anim = null; if (!m_AnimFloats.TryGetValue(mask, out anim)) { anim = new AnimFloat(0, RepaintAll); anim.speed = CoreEditorConstants.additionalPropertiesHightLightSpeed; m_AnimFloats.Add(mask, anim); } return anim; } void ResetAnimation(TState mask) { AnimFloat anim = GetAnimation(mask); anim.value = 1.0f; anim.target = 0.0f; } /// /// Register an editor for this set of additional properties. /// /// Editor to register. public void RegisterEditor(Editor editor) { m_Editors.Add(editor); } /// /// Unregister an editor for this set of additional properties. /// /// Editor to unregister. public void UnregisterEditor(Editor editor) { m_Editors.Remove(editor); } } }