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.
public abstract class AdditionalPropertiesStateBase
where TState : struct, IConvertible
{
HashSet m_Editors = new ();
/// 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 abstract bool GetAdditionalPropertiesState(TState mask);
/// Setter to the expended state.
/// The filtering mask.
/// True to show the additional properties.
public void SetAdditionalPropertiesState(TState mask, bool value)
{
SetAdditionalPropertiesStateValue(mask, value);
}
/// Setter to the expended state without resetting animation.
/// The filtering mask.
/// True to show the additional properties.
protected abstract void SetAdditionalPropertiesStateValue(TState mask, bool value);
/// Utility to set all states to true
public abstract void ShowAll();
/// Utility to set all states to false
public abstract void HideAll();
///
/// Resets the animation associated with the given mask to a default state with the animated value set to 1.0 and the target value set to 0.0.
///
/// The state mask used to retrieve the associated animation.
protected internal void ResetAnimation(TState mask)
{
}
///
/// 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);
}
}
/// 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 : AdditionalPropertiesStateBase
where TState : struct, IConvertible
{
///
/// Stores the expanded or collapsed state of each section defined by .
///
protected internal EditorPrefBoolFlags m_State;
/// 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)
/// [Optional] Postfix used to differentiate between different keys (Default is UI_AP_State)
public AdditionalPropertiesState(TState defaultValue, string prefix = "CoreRP", string stateId = "UI_AP_State")
{
string key = $"{prefix}:{typeof(TTarget).Name}:{typeof(TState).Name}:{stateId}";
m_State = new EditorPrefBoolFlags(key);
AdvancedProperties.UpdateShowAdvancedProperties(key, m_State.rawValue != 0u);
}
///
public override bool GetAdditionalPropertiesState(TState _)
{
return AdvancedProperties.enabled;
}
///
protected override void SetAdditionalPropertiesStateValue(TState _, bool value)
{
AdvancedProperties.enabled = value;
}
///
public override void ShowAll()
{
AdvancedProperties.enabled = true;
}
///
public override void HideAll()
{
AdvancedProperties.enabled = false;
}
}
/// Used in editor drawer part to store the state of additional properties for a list of elements.
/// A type given to automatically compute the key.
public class AdditionalPropertiesStateList : AdditionalPropertiesState
{
/// Constructor will create the key to store in the EditorPref the state given generic type passed.
/// [Optional] Prefix scope of the key (Default is CoreRP)
public AdditionalPropertiesStateList(string prefix = "CoreRP")
: base(default(int), prefix, "UI_AP_State_List") { }
///
/// Swap flag between src index and dst index.
///
/// src index to swap.
/// dst index to swap.
public void SwapFlags(int srcIndex, int dstIndex)
{
int srcFlag = 1 << srcIndex;
int dstFlag = 1 << dstIndex;
bool srcVal = GetAdditionalPropertiesState(srcFlag);
SetAdditionalPropertiesState(srcFlag, GetAdditionalPropertiesState(dstFlag));
SetAdditionalPropertiesState(dstFlag, srcVal);
}
/// Removes a flag at a given index which causes the following flags' index to decrease by one.
/// The index of the flag to be removed.
public void RemoveFlagAtIndex(int index)
{
m_State.rawValue = ExpandedStateList.RightShiftOnceFromIndexToMSB(index, m_State.rawValue);
}
}
}