A game about forced loneliness, made by TACStudios
at master 2.9 kB view raw
1using System; 2using System.Reflection; 3using UnityEngine; 4using UnityEditor.Graphing; 5 6using UnityEngine.UIElements; 7 8namespace UnityEditor.ShaderGraph.Drawing.Controls 9{ 10 [AttributeUsage(AttributeTargets.Property)] 11 class ChannelEnumMaskControlAttribute : Attribute, IControlAttribute 12 { 13 string m_Label; 14 int m_SlotId; 15 16 public ChannelEnumMaskControlAttribute(string label = null, int slotId = 0) 17 { 18 m_Label = label; 19 m_SlotId = slotId; 20 } 21 22 public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo) 23 { 24 return new ChannelEnumMaskControlView(m_Label, m_SlotId, node, propertyInfo); 25 } 26 } 27 28 class ChannelEnumMaskControlView : VisualElement, AbstractMaterialNodeModificationListener 29 { 30 string m_Label; 31 AbstractMaterialNode m_Node; 32 PropertyInfo m_PropertyInfo; 33 IMGUIContainer m_Container; 34 int m_SlotId; 35 36 public ChannelEnumMaskControlView(string label, int slotId, AbstractMaterialNode node, PropertyInfo propertyInfo) 37 { 38 styleSheets.Add(Resources.Load<StyleSheet>("Styles/Controls/ChannelEnumMaskControlView")); 39 m_Node = node; 40 m_PropertyInfo = propertyInfo; 41 m_SlotId = slotId; 42 //if (!propertyInfo.PropertyType.IsEnum) 43 //throw new ArgumentException("Property must be an enum.", "propertyInfo"); 44 m_Label = label; 45 m_Container = new IMGUIContainer(OnGUIHandler); 46 Add(m_Container); 47 } 48 49 void OnGUIHandler() 50 { 51 GUILayout.BeginHorizontal(); 52 GUILayout.Label(m_Label); 53 UpdatePopup(); 54 GUILayout.EndHorizontal(); 55 } 56 57 public void OnNodeModified(ModificationScope scope) 58 { 59 if (scope == ModificationScope.Graph) 60 m_Container.MarkDirtyRepaint(); 61 } 62 63 private void UpdatePopup() 64 { 65 var value = (int)m_PropertyInfo.GetValue(m_Node, null); 66 using (var changeCheckScope = new EditorGUI.ChangeCheckScope()) 67 { 68 int channelCount = SlotValueHelper.GetChannelCount(m_Node.FindSlot<MaterialSlot>(m_SlotId).concreteValueType); 69 string[] enumEntryNames = Enum.GetNames(typeof(TextureChannel)); 70 string[] popupEntries = new string[channelCount]; 71 for (int i = 0; i < popupEntries.Length; i++) 72 popupEntries[i] = enumEntryNames[i]; 73 value = EditorGUILayout.MaskField("", value, popupEntries, GUILayout.Width(80f)); 74 75 if (changeCheckScope.changed) 76 { 77 m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name); 78 m_PropertyInfo.SetValue(m_Node, value, null); 79 } 80 } 81 } 82 } 83}