A game about forced loneliness, made by TACStudios
1using System; 2using System.Reflection; 3using UnityEditor.Graphing; 4using UnityEngine; 5using UnityEngine.UIElements; 6 7namespace UnityEditor.ShaderGraph.Drawing.Controls 8{ 9 [Serializable] 10 struct ToggleData 11 { 12 public bool isOn; 13 public bool isEnabled; 14 15 public ToggleData(bool on, bool enabled) 16 { 17 isOn = on; 18 isEnabled = enabled; 19 } 20 21 public ToggleData(bool on) 22 { 23 isOn = on; 24 isEnabled = true; 25 } 26 } 27 28 [AttributeUsage(AttributeTargets.Property)] 29 class ToggleControlAttribute : Attribute, IControlAttribute 30 { 31 string m_Label; 32 33 public ToggleControlAttribute(string label = null) 34 { 35 m_Label = label; 36 } 37 38 public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo) 39 { 40 return new ToggleControlView(m_Label, node, propertyInfo); 41 } 42 } 43 44 class ToggleControlView : VisualElement, AbstractMaterialNodeModificationListener 45 { 46 AbstractMaterialNode m_Node; 47 PropertyInfo m_PropertyInfo; 48 49 Label m_Label; 50 Toggle m_Toggle; 51 52 public ToggleControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo) 53 { 54 m_Node = node; 55 m_PropertyInfo = propertyInfo; 56 styleSheets.Add(Resources.Load<StyleSheet>("Styles/Controls/ToggleControlView")); 57 58 if (propertyInfo.PropertyType != typeof(ToggleData)) 59 throw new ArgumentException("Property must be a Toggle.", "propertyInfo"); 60 61 label = label ?? ObjectNames.NicifyVariableName(propertyInfo.Name); 62 63 var value = (ToggleData)m_PropertyInfo.GetValue(m_Node, null); 64 var panel = new VisualElement { name = "togglePanel" }; 65 if (!string.IsNullOrEmpty(label)) 66 { 67 m_Label = new Label(label); 68 m_Label.SetEnabled(value.isEnabled); 69 panel.Add(m_Label); 70 } 71 72 m_Toggle = new Toggle(); 73 m_Toggle.OnToggleChanged(OnChangeToggle); 74 m_Toggle.SetEnabled(value.isEnabled); 75 m_Toggle.value = value.isOn; 76 panel.Add(m_Toggle); 77 Add(panel); 78 } 79 80 public void OnNodeModified(ModificationScope scope) 81 { 82 var value = (ToggleData)m_PropertyInfo.GetValue(m_Node, null); 83 m_Toggle.SetEnabled(value.isEnabled); 84 if (m_Label != null) 85 m_Label.SetEnabled(value.isEnabled); 86 87 if (scope == ModificationScope.Graph) 88 { 89 this.MarkDirtyRepaint(); 90 } 91 } 92 93 void OnChangeToggle(ChangeEvent<bool> evt) 94 { 95 m_Node.owner.owner.RegisterCompleteObjectUndo("Toggle Change"); 96 var value = (ToggleData)m_PropertyInfo.GetValue(m_Node, null); 97 value.isOn = evt.newValue; 98 m_PropertyInfo.SetValue(m_Node, value, null); 99 this.MarkDirtyRepaint(); 100 } 101 } 102}