A game about forced loneliness, made by TACStudios
1using System;
2using UnityEditor.AnimatedValues;
3using System.Collections.Generic;
4
5namespace UnityEditor.Rendering
6{
7 /// <summary>Used in editor drawer part to store the state of additional properties areas.</summary>
8 /// <typeparam name="TState">An enum to use to describe the state.</typeparam>
9 public abstract class AdditionalPropertiesStateBase<TState>
10 where TState : struct, IConvertible
11 {
12 HashSet<Editor> m_Editors = new ();
13
14 /// <summary>Get or set the state given the mask.</summary>
15 /// <param name="mask">The filtering mask</param>
16 /// <value>True: All flagged area are expended</value>
17 public bool this[TState mask]
18 {
19 get => GetAdditionalPropertiesState(mask);
20 set => SetAdditionalPropertiesState(mask, value);
21 }
22
23 /// <summary>Accessor to the expended state of this specific mask.</summary>
24 /// <param name="mask">The filtering mask</param>
25 /// <returns>True: All flagged area are expended</returns>
26 public abstract bool GetAdditionalPropertiesState(TState mask);
27
28 /// <summary>Setter to the expended state.</summary>
29 /// <param name="mask">The filtering mask.</param>
30 /// <param name="value">True to show the additional properties.</param>
31 public void SetAdditionalPropertiesState(TState mask, bool value)
32 {
33 SetAdditionalPropertiesStateValue(mask, value);
34 }
35
36 /// <summary>Setter to the expended state without resetting animation.</summary>
37 /// <param name="mask">The filtering mask.</param>
38 /// <param name="value">True to show the additional properties.</param>
39 protected abstract void SetAdditionalPropertiesStateValue(TState mask, bool value);
40
41 /// <summary> Utility to set all states to true </summary>
42 public abstract void ShowAll();
43
44 /// <summary> Utility to set all states to false </summary>
45 public abstract void HideAll();
46
47 /// <summary>
48 /// 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.
49 /// </summary>
50 /// <param name="mask">The state mask used to retrieve the associated animation.</param>
51 protected internal void ResetAnimation(TState mask)
52 {
53 }
54 /// <summary>
55 /// Register an editor for this set of additional properties.
56 /// </summary>
57 /// <param name="editor">Editor to register.</param>
58 public void RegisterEditor(Editor editor)
59 {
60 m_Editors.Add(editor);
61 }
62
63 /// <summary>
64 /// Unregister an editor for this set of additional properties.
65 /// </summary>
66 /// <param name="editor">Editor to unregister.</param>
67 public void UnregisterEditor(Editor editor)
68 {
69 m_Editors.Remove(editor);
70 }
71 }
72
73 /// <summary>Used in editor drawer part to store the state of additional properties areas.</summary>
74 /// <typeparam name="TState">An enum to use to describe the state.</typeparam>
75 /// <typeparam name="TTarget">A type given to automatically compute the key.</typeparam>
76 public class AdditionalPropertiesState<TState, TTarget> : AdditionalPropertiesStateBase<TState>
77 where TState : struct, IConvertible
78 {
79 /// <summary>
80 /// Stores the expanded or collapsed state of each section defined by <typeparamref name="TState"/>.
81 /// </summary>
82 protected internal EditorPrefBoolFlags<TState> m_State;
83
84
85 /// <summary>Constructor will create the key to store in the EditorPref the state given generic type passed.</summary>
86 /// <param name="defaultValue">If key did not exist, it will be created with this value for initialization.</param>
87 /// <param name="prefix">[Optional] Prefix scope of the key (Default is CoreRP)</param>
88 /// <param name="stateId">[Optional] Postfix used to differentiate between different keys (Default is UI_AP_State)</param>
89 public AdditionalPropertiesState(TState defaultValue, string prefix = "CoreRP", string stateId = "UI_AP_State")
90 {
91 string key = $"{prefix}:{typeof(TTarget).Name}:{typeof(TState).Name}:{stateId}";
92 m_State = new EditorPrefBoolFlags<TState>(key);
93 AdvancedProperties.UpdateShowAdvancedProperties(key, m_State.rawValue != 0u);
94 }
95
96 /// <inheritdoc/>
97 public override bool GetAdditionalPropertiesState(TState _)
98 {
99 return AdvancedProperties.enabled;
100 }
101
102 /// <inheritdoc/>
103 protected override void SetAdditionalPropertiesStateValue(TState _, bool value)
104 {
105 AdvancedProperties.enabled = value;
106 }
107
108 /// <inheritdoc/>
109 public override void ShowAll()
110 {
111 AdvancedProperties.enabled = true;
112 }
113
114 /// <inheritdoc/>
115 public override void HideAll()
116 {
117 AdvancedProperties.enabled = false;
118 }
119 }
120
121 /// <summary>Used in editor drawer part to store the state of additional properties for a list of elements.</summary>
122 /// <typeparam name="TTarget">A type given to automatically compute the key.</typeparam>
123 public class AdditionalPropertiesStateList<TTarget> : AdditionalPropertiesState<int, TTarget>
124 {
125 /// <summary>Constructor will create the key to store in the EditorPref the state given generic type passed.</summary>
126 /// <param name="prefix">[Optional] Prefix scope of the key (Default is CoreRP)</param>
127 public AdditionalPropertiesStateList(string prefix = "CoreRP")
128 : base(default(int), prefix, "UI_AP_State_List") { }
129
130 /// <summary>
131 /// Swap flag between src index and dst index.
132 /// </summary>
133 /// <param name="srcIndex">src index to swap.</param>
134 /// <param name="dstIndex">dst index to swap.</param>
135 public void SwapFlags(int srcIndex, int dstIndex)
136 {
137 int srcFlag = 1 << srcIndex;
138 int dstFlag = 1 << dstIndex;
139
140 bool srcVal = GetAdditionalPropertiesState(srcFlag);
141 SetAdditionalPropertiesState(srcFlag, GetAdditionalPropertiesState(dstFlag));
142 SetAdditionalPropertiesState(dstFlag, srcVal);
143 }
144
145 /// <summary> Removes a flag at a given index which causes the following flags' index to decrease by one.</summary>
146 /// <param name="index">The index of the flag to be removed.</param>
147 public void RemoveFlagAtIndex(int index)
148 {
149 m_State.rawValue = ExpandedStateList<TTarget>.RightShiftOnceFromIndexToMSB(index, m_State.rawValue);
150 }
151 }
152}