VFXEditableDataAnchor.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using UnityEditor.Experimental.GraphView;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. using System.Collections.Generic;
  5. using Type = System.Type;
  6. using UnityEngine.Profiling;
  7. namespace UnityEditor.VFX.UI
  8. {
  9. partial class VFXEditableDataAnchor : VFXDataAnchor
  10. {
  11. PropertyRM m_PropertyRM;
  12. VFXView m_View;
  13. // TODO This is a workaround to avoid having a generic type for the anchor as generic types mess with USS.
  14. public static new VFXEditableDataAnchor Create(VFXDataAnchorController controller, VFXNodeUI node)
  15. {
  16. Profiler.BeginSample("VFXEditableDataAnchor.Create");
  17. var anchor = new VFXEditableDataAnchor(controller.orientation, controller.direction, controller.portType, node);
  18. anchor.m_EdgeConnector = new VFXEdgeConnector(anchor);
  19. anchor.controller = controller;
  20. anchor.AddManipulator(anchor.m_EdgeConnector);
  21. Profiler.EndSample();
  22. return anchor;
  23. }
  24. protected VFXEditableDataAnchor(Orientation anchorOrientation, Direction anchorDirection, Type type, VFXNodeUI node) : base(anchorOrientation, anchorDirection, type, node)
  25. {
  26. Profiler.BeginSample("VFXEditableDataAnchor.VFXEditableDataAnchor");
  27. RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
  28. RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
  29. Profiler.EndSample();
  30. }
  31. public void AssetMoved()
  32. {
  33. m_PropertyRM.UpdateGUI(true);
  34. }
  35. public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
  36. {
  37. base.BuildContextualMenu(evt);
  38. evt.menu.AppendAction("Copy Value", OnCopyValue);
  39. evt.menu.AppendAction("Paste Value", OnPasteValue, OnValidatePasteValue);
  40. }
  41. static object s_Clipboard;
  42. void OnCopyValue(DropdownMenuAction a)
  43. {
  44. s_Clipboard = controller.value;
  45. }
  46. void OnPasteValue(DropdownMenuAction a)
  47. {
  48. controller.value = VFXConverter.ConvertTo(s_Clipboard, portType);
  49. }
  50. DropdownMenuAction.Status OnValidatePasteValue(DropdownMenuAction a)
  51. {
  52. return s_Clipboard != null && controller.editable && VFXConverter.CanConvertTo(s_Clipboard.GetType(), portType) ? DropdownMenuAction.Status.Normal : DropdownMenuAction.Status.Disabled;
  53. }
  54. void OnAttachToPanel(AttachToPanelEvent e)
  55. {
  56. m_View = GetFirstAncestorOfType<VFXView>();
  57. if (m_View == null)
  58. {
  59. //This can happen with asynchnous events.
  60. return;
  61. }
  62. m_View.allDataAnchors.Add(this);
  63. }
  64. void OnDetachFromPanel(DetachFromPanelEvent e)
  65. {
  66. if (m_View != null)
  67. m_View.allDataAnchors.Remove(this);
  68. }
  69. public float GetPreferredLabelWidth()
  70. {
  71. if (m_PropertyRM == null) return 0;
  72. return m_PropertyRM.GetPreferredLabelWidth();
  73. }
  74. public float GetPreferredControlWidth()
  75. {
  76. if (m_PropertyRM == null) return 0;
  77. return m_PropertyRM.GetPreferredControlWidth();
  78. }
  79. public void SetLabelWidth(float label)
  80. {
  81. m_PropertyRM.SetLabelWidth(label);
  82. }
  83. public void ForceUpdate()
  84. {
  85. m_PropertyRM.ForceUpdate();
  86. }
  87. void BuildProperty()
  88. {
  89. Profiler.BeginSample("VFXNodeUI.BuildProperty");
  90. float effectiveWidth = -1;
  91. if (m_PropertyRM != null)
  92. {
  93. Remove(m_PropertyRM);
  94. effectiveWidth = m_PropertyRM.effectiveLabelWidth;
  95. }
  96. m_PropertyRM = PropertyRM.Create(controller, VFXNodeUI.DefaultLabelWidth);
  97. if (m_PropertyRM != null)
  98. {
  99. Add(m_PropertyRM);
  100. if (effectiveWidth >= 0)
  101. m_PropertyRM.SetLabelWidth(effectiveWidth);
  102. }
  103. Profiler.EndSample();
  104. }
  105. public override void SelfChange(int change)
  106. {
  107. Profiler.BeginSample("VFXEditableDataAnchor.SelfChange");
  108. base.SelfChange(change);
  109. if (m_PropertyRM == null || !m_PropertyRM.IsCompatible(controller))
  110. BuildProperty();
  111. OnRecompile(false);
  112. Profiler.EndSample();
  113. }
  114. public void OnRecompile(bool valueOnly)
  115. {
  116. if (m_PropertyRM != null && controller != null)
  117. {
  118. if (!valueOnly)
  119. {
  120. controller.UpdateInfos();
  121. bool editable = controller.editable;
  122. m_PropertyRM.propertyEnabled = editable && controller.expandedInHierachy;
  123. m_PropertyRM.indeterminate = !editable && controller.indeterminate;
  124. m_PropertyRM.Update();
  125. }
  126. else
  127. m_PropertyRM.UpdateValue();
  128. }
  129. }
  130. public Rect internalRect
  131. {
  132. get
  133. {
  134. Rect layout = this.layout;
  135. return new Rect(0.0f, 0.0f, layout.width, layout.height);
  136. }
  137. }
  138. public override bool ContainsPoint(Vector2 localPoint)
  139. {
  140. return internalRect.Contains(localPoint);
  141. }
  142. }
  143. }