Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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