Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PortInputView.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor.Experimental.GraphView;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.ShaderGraph.Drawing
  6. {
  7. class PortInputView : GraphElement, IDisposable
  8. {
  9. readonly CustomStyleProperty<Color> k_EdgeColorProperty = new CustomStyleProperty<Color>("--edge-color");
  10. Color m_EdgeColor = Color.red;
  11. public Color edgeColor
  12. {
  13. get { return m_EdgeColor; }
  14. }
  15. public MaterialSlot slot
  16. {
  17. get { return m_Slot; }
  18. }
  19. MaterialSlot m_Slot;
  20. ConcreteSlotValueType m_SlotType;
  21. VisualElement m_Control;
  22. VisualElement m_Container;
  23. EdgeControl m_EdgeControl;
  24. public PortInputView(MaterialSlot slot)
  25. {
  26. styleSheets.Add(Resources.Load<StyleSheet>("Styles/PortInputView"));
  27. pickingMode = PickingMode.Ignore;
  28. ClearClassList();
  29. m_Slot = slot;
  30. m_SlotType = slot.concreteValueType;
  31. AddToClassList("type" + m_SlotType);
  32. m_EdgeControl = new EdgeControl
  33. {
  34. @from = new Vector2(232f - 21f, 11.5f),
  35. to = new Vector2(232f, 11.5f),
  36. edgeWidth = 2,
  37. pickingMode = PickingMode.Ignore
  38. };
  39. Add(m_EdgeControl);
  40. m_Container = new VisualElement { name = "container" };
  41. {
  42. CreateControl();
  43. var slotElement = new VisualElement { name = "slot" };
  44. {
  45. slotElement.Add(new VisualElement { name = "dot" });
  46. }
  47. m_Container.Add(slotElement);
  48. }
  49. Add(m_Container);
  50. m_Container.Add(new VisualElement() { name = "disabledOverlay", pickingMode = PickingMode.Ignore });
  51. RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
  52. }
  53. void OnCustomStyleResolved(CustomStyleResolvedEvent e)
  54. {
  55. Color colorValue;
  56. if (e.customStyle.TryGetValue(k_EdgeColorProperty, out colorValue))
  57. m_EdgeColor = colorValue;
  58. m_EdgeControl.UpdateLayout();
  59. m_EdgeControl.inputColor = edgeColor;
  60. m_EdgeControl.outputColor = edgeColor;
  61. }
  62. public void UpdateSlot(MaterialSlot newSlot)
  63. {
  64. m_Slot = newSlot;
  65. Recreate();
  66. }
  67. public void UpdateSlotType()
  68. {
  69. if (slot.concreteValueType != m_SlotType)
  70. Recreate();
  71. }
  72. void Recreate()
  73. {
  74. RemoveFromClassList("type" + m_SlotType);
  75. m_SlotType = slot.concreteValueType;
  76. AddToClassList("type" + m_SlotType);
  77. if (m_Control != null)
  78. {
  79. if (m_Control is IDisposable disposable)
  80. disposable.Dispose();
  81. m_Container.Remove(m_Control);
  82. }
  83. CreateControl();
  84. }
  85. void CreateControl()
  86. {
  87. // Specially designated properties (Use Custom Binding) are shown as a label on the slot when the slot is disconnected, with no ability to set an explicit default.
  88. // If the port for this property is connected to, it will use the regular slot control.
  89. m_Control = (!slot.isConnected && slot.IsConnectionTestable()) ? slot.InstantiateCustomControl() : slot.InstantiateControl();
  90. if (m_Control != null)
  91. {
  92. m_Container.Insert(0, m_Control);
  93. }
  94. else
  95. {
  96. // Some slot types don't support an input control, so hide this
  97. m_Container.visible = m_EdgeControl.visible = false;
  98. }
  99. }
  100. public void Dispose()
  101. {
  102. if (m_Control is IDisposable disposable)
  103. disposable.Dispose();
  104. styleSheets.Clear();
  105. m_Control = null;
  106. m_EdgeControl = null;
  107. UnregisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
  108. }
  109. }
  110. }