A game about forced loneliness, made by TACStudios
1using System; 2using System.Linq; 3using System.Reflection; 4using UnityEngine; 5using UnityEngine.UIElements; 6using UnityEngine.Assertions; 7using UnityEditor.Graphing; 8 9namespace UnityEditor.ShaderGraph.Drawing.Controls 10{ 11 [AttributeUsage(AttributeTargets.Property)] 12 class TextControlAttribute : Attribute, IControlAttribute 13 { 14 string m_Label; 15 public TextControlAttribute(string label = null) 16 { 17 m_Label = label; 18 } 19 20 public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo) 21 { 22 if (!TextControlView.validTypes.Contains(propertyInfo.PropertyType)) 23 return null; 24 return new TextControlView(m_Label, node, propertyInfo); 25 } 26 } 27 28 class TextControlView : VisualElement 29 { 30 public static Type[] validTypes = { typeof(string) }; 31 AbstractMaterialNode m_Node; 32 PropertyInfo m_PropertyInfo; 33 string m_Value; 34 int m_UndoGroup = -1; 35 public TextControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo) 36 { 37 styleSheets.Add(Resources.Load<StyleSheet>("Styles/Controls/TextControlView")); 38 m_Node = node; 39 m_PropertyInfo = propertyInfo; 40 label = label ?? ObjectNames.NicifyVariableName(propertyInfo.Name); 41 var container = new VisualElement { name = "container" }; 42 var thisLabel = new Label(label); 43 container.Add(thisLabel); 44 m_Value = GetValue(); 45 string value = null; 46 var field = new TextField { value = m_Value }; 47 field.RegisterCallback<MouseDownEvent>(Repaint); 48 field.RegisterCallback<MouseMoveEvent>(Repaint); 49 field.RegisterValueChangedCallback(evt => 50 { 51 value = GetValue(); 52 value = evt.newValue; 53 if (m_Node.GetType() != typeof(SwizzleNode)) 54 { 55 m_Node.owner.owner.RegisterCompleteObjectUndo("Change" + m_Node.name); 56 m_PropertyInfo.SetValue(m_Node, value, null); 57 m_UndoGroup = -1; 58 } 59 }); 60 61 // Pressing escape while we are editing causes it to revert to the original value when we gained focus 62 field.Q("unity-text-input").RegisterCallback<KeyDownEvent>(evt => 63 { 64 if (evt.keyCode == KeyCode.Escape && m_UndoGroup > -1) 65 { 66 Undo.RevertAllDownToGroup(m_UndoGroup); 67 m_UndoGroup = -1; 68 evt.StopPropagation(); 69 } 70 this.MarkDirtyRepaint(); 71 }, TrickleDown.TrickleDown); 72 field.Q("unity-text-input").RegisterCallback<FocusOutEvent>(evt => 73 { 74 if (m_Node.GetType() == typeof(SwizzleNode)) 75 { 76 //Only set node value when mouse clicked away 77 m_Node.owner.owner.RegisterCompleteObjectUndo("Change" + m_Node.name); 78 m_PropertyInfo.SetValue(m_Node, value, null); 79 m_UndoGroup = -1; 80 //Validate graph to update downstream input slot 81 m_Node.owner.ValidateGraph(); 82 m_Node.Dirty(ModificationScope.Topological); 83 } 84 this.MarkDirtyRepaint(); 85 }, TrickleDown.TrickleDown); 86 container.Add(field); 87 Add(container); 88 } 89 90 string GetValue() 91 { 92 var value = m_PropertyInfo.GetValue(m_Node, null); 93 Assert.IsNotNull(value); 94 return (string)value; 95 } 96 97 void Repaint<T>(MouseEventBase<T> evt) where T : MouseEventBase<T>, new() 98 { 99 evt.StopPropagation(); 100 this.MarkDirtyRepaint(); 101 } 102 } 103}