A game about forced loneliness, made by TACStudios
1using System;
2using System.Reflection;
3using System.Collections;
4using System.Collections.Generic;
5using UnityEngine;
6using UnityEditor.UIElements;
7using UnityEngine.UIElements;
8
9namespace UnityEditor.ShaderGraph.Drawing.Controls
10{
11 [AttributeUsage(AttributeTargets.Property)]
12 class PopupControlAttribute : Attribute, IControlAttribute
13 {
14 string m_Label;
15 //string[] m_Entries;
16
17 public PopupControlAttribute(string label = null)
18 {
19 m_Label = label;
20 //m_Entries = entries;
21 }
22
23 public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
24 {
25 return new PopupControlView(m_Label, node, propertyInfo);
26 }
27 }
28
29 [Serializable]
30 struct PopupList
31 {
32 public int selectedEntry;
33 public string[] popupEntries;
34
35 public PopupList(string[] entries, int defaultEntry)
36 {
37 popupEntries = entries;
38 selectedEntry = defaultEntry;
39 }
40 }
41
42 class PopupControlView : VisualElement
43 {
44 AbstractMaterialNode m_Node;
45 PropertyInfo m_PropertyInfo;
46 PopupField<string> m_PopupField;
47
48 public PopupControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
49 {
50 styleSheets.Add(Resources.Load<StyleSheet>("Styles/Controls/PopupControlView"));
51 m_Node = node;
52 m_PropertyInfo = propertyInfo;
53
54 Type type = propertyInfo.PropertyType;
55 if (type != typeof(PopupList))
56 {
57 throw new ArgumentException("Property must be a PopupList.", "propertyInfo");
58 }
59
60 Add(new Label(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name)));
61 var value = (PopupList)propertyInfo.GetValue(m_Node, null);
62 m_PopupField = new PopupField<string>(new List<string>(value.popupEntries), value.selectedEntry);
63 m_PopupField.RegisterValueChangedCallback(OnValueChanged);
64 Add(m_PopupField);
65 }
66
67 void OnValueChanged(ChangeEvent<string> evt)
68 {
69 var value = (PopupList)m_PropertyInfo.GetValue(m_Node, null);
70 value.selectedEntry = m_PopupField.index;
71 m_PropertyInfo.SetValue(m_Node, value, null);
72 m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
73 }
74 }
75}