A game about forced loneliness, made by TACStudios
1using System;
2using System.Reflection;
3using UnityEngine;
4using UnityEditor.UIElements;
5using UnityEngine.UIElements;
6
7namespace UnityEditor.ShaderGraph.Drawing.Controls
8{
9 [AttributeUsage(AttributeTargets.Property)]
10 class EnumControlAttribute : Attribute, IControlAttribute
11 {
12 string m_Label;
13
14 public EnumControlAttribute(string label = null)
15 {
16 m_Label = label;
17 }
18
19 public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
20 {
21 return new EnumControlView(m_Label, node, propertyInfo);
22 }
23 }
24
25 class EnumControlView : VisualElement
26 {
27 AbstractMaterialNode m_Node;
28 PropertyInfo m_PropertyInfo;
29
30 public EnumControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
31 {
32 styleSheets.Add(Resources.Load<StyleSheet>("Styles/Controls/EnumControlView"));
33 m_Node = node;
34 m_PropertyInfo = propertyInfo;
35 if (!propertyInfo.PropertyType.IsEnum)
36 throw new ArgumentException("Property must be an enum.", "propertyInfo");
37 Add(new Label(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name)));
38 var enumField = new EnumField((Enum)m_PropertyInfo.GetValue(m_Node, null));
39 enumField.RegisterValueChangedCallback(OnValueChanged);
40 Add(enumField);
41 }
42
43 void OnValueChanged(ChangeEvent<Enum> evt)
44 {
45 var value = (Enum)m_PropertyInfo.GetValue(m_Node, null);
46 if (!evt.newValue.Equals(value))
47 {
48 m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
49 m_PropertyInfo.SetValue(m_Node, evt.newValue, null);
50 }
51 }
52 }
53}