post processing
[WindSway-HDRP.git] / Library / PackageCache / com.unity.render-pipelines.high-definition@4.10.0-preview / Runtime / Core / Debugging / DebugUI.Fields.cs
blobeab26168da7d4fe6934900ef6837a6f59e2abc42
1 using System;
2 using System.Linq;
3 using UnityEngine.Assertions;
5 namespace UnityEngine.Experimental.Rendering
7 public partial class DebugUI
9 // Generic field - will be serialized in the editor if it's not read-only
10 public abstract class Field<T> : Widget, IValueField
12 public Func<T> getter { get; set; }
13 public Action<T> setter { get; set; }
15 // This should be an `event` but they don't play nice with object initializers in the
16 // version of C# we use.
17 public Action<Field<T>, T> onValueChanged;
19 object IValueField.ValidateValue(object value)
21 return ValidateValue((T)value);
24 public virtual T ValidateValue(T value)
26 return value;
29 object IValueField.GetValue()
31 return GetValue();
34 public T GetValue()
36 Assert.IsNotNull(getter);
37 return getter();
40 public void SetValue(object value)
42 SetValue((T)value);
45 public void SetValue(T value)
47 Assert.IsNotNull(setter);
48 var v = ValidateValue(value);
50 if (!v.Equals(getter()))
52 setter(v);
54 if (onValueChanged != null)
55 onValueChanged(this, v);
60 public class BoolField : Field<bool> {}
62 public class IntField : Field<int>
64 public Func<int> min;
65 public Func<int> max;
67 // Runtime-only
68 public int incStep = 1;
69 public int intStepMult = 10;
71 public override int ValidateValue(int value)
73 if (min != null) value = Mathf.Max(value, min());
74 if (max != null) value = Mathf.Min(value, max());
75 return value;
79 public class UIntField : Field<uint>
81 public Func<uint> min;
82 public Func<uint> max;
84 // Runtime-only
85 public uint incStep = 1u;
86 public uint intStepMult = 10u;
88 public override uint ValidateValue(uint value)
90 if (min != null) value = (uint)Mathf.Max((int)value, (int)min());
91 if (max != null) value = (uint)Mathf.Min((int)value, (int)max());
92 return value;
96 public class FloatField : Field<float>
98 public Func<float> min;
99 public Func<float> max;
101 // Runtime-only
102 public float incStep = 0.1f;
103 public float incStepMult = 10f;
104 public int decimals = 3;
106 public override float ValidateValue(float value)
108 if (min != null) value = Mathf.Max(value, min());
109 if (max != null) value = Mathf.Min(value, max());
110 return value;
114 public class EnumField : Field<int>
116 public GUIContent[] enumNames;
117 public int[] enumValues;
118 public int[] quickSeparators;
119 public int[] indexes;
121 public Func<int> getIndex { get; set; }
122 public Action<int> setIndex { get; set; }
124 public int currentIndex { get { return getIndex(); } set { setIndex(value); } }
126 public Type autoEnum
130 enumNames = Enum.GetNames(value).Select(x => new GUIContent(x)).ToArray();
132 // Linq.Cast<T> on a typeless Array breaks the JIT on PS4/Mono so we have to do it manually
133 //enumValues = Enum.GetValues(value).Cast<int>().ToArray();
135 var values = Enum.GetValues(value);
136 enumValues = new int[values.Length];
137 for (int i = 0; i < values.Length; i++)
138 enumValues[i] = (int)values.GetValue(i);
140 InitIndexes();
141 InitQuickSeparators();
145 public void InitQuickSeparators()
147 var enumNamesPrefix = enumNames.Select(x =>
149 string[] splitted = x.text.Split('/');
150 if (splitted.Length == 1)
151 return "";
152 else
153 return splitted[0];
155 quickSeparators = new int[enumNamesPrefix.Distinct().Count()];
156 string lastPrefix = null;
157 for (int i = 0, wholeNameIndex = 0; i < quickSeparators.Length; ++i)
159 var currentTestedPrefix = enumNamesPrefix.ElementAt(wholeNameIndex);
160 while (lastPrefix == currentTestedPrefix)
162 currentTestedPrefix = enumNamesPrefix.ElementAt(++wholeNameIndex);
164 lastPrefix = currentTestedPrefix;
165 quickSeparators[i] = wholeNameIndex++;
169 public void InitIndexes()
171 indexes = new int[enumNames.Length];
172 for (int i = 0; i < enumNames.Length; i++)
174 indexes[i] = i;
179 public class ColorField : Field<Color>
181 public bool hdr = false;
182 public bool showAlpha = true;
184 // Editor-only
185 public bool showPicker = true;
187 // Runtime-only
188 public float incStep = 0.025f;
189 public float incStepMult = 5f;
190 public int decimals = 3;
192 public override Color ValidateValue(Color value)
194 if (!hdr)
196 value.r = Mathf.Clamp01(value.r);
197 value.g = Mathf.Clamp01(value.g);
198 value.b = Mathf.Clamp01(value.b);
199 value.a = Mathf.Clamp01(value.a);
202 return value;
206 public class Vector2Field : Field<Vector2>
208 // Runtime-only
209 public float incStep = 0.025f;
210 public float incStepMult = 10f;
211 public int decimals = 3;
214 public class Vector3Field : Field<Vector3>
216 // Runtime-only
217 public float incStep = 0.025f;
218 public float incStepMult = 10f;
219 public int decimals = 3;
222 public class Vector4Field : Field<Vector4>
224 // Runtime-only
225 public float incStep = 0.025f;
226 public float incStepMult = 10f;
227 public int decimals = 3;