VFXParameterUI.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.Experimental.GraphView;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. using UnityEngine.Profiling;
  8. namespace UnityEditor.VFX.UI
  9. {
  10. class VFXOutputParameterDataAnchor : VFXOutputDataAnchor
  11. {
  12. public static new VFXOutputParameterDataAnchor Create(VFXDataAnchorController controller, VFXNodeUI node)
  13. {
  14. var anchor = new VFXOutputParameterDataAnchor(controller.orientation, controller.direction, controller.portType, node);
  15. anchor.m_EdgeConnector = new VFXEdgeConnector(anchor);
  16. anchor.controller = controller;
  17. anchor.AddManipulator(anchor.m_EdgeConnector);
  18. return anchor;
  19. }
  20. protected VFXOutputParameterDataAnchor(Orientation anchorOrientation, Direction anchorDirection, Type type, VFXNodeUI node) : base(anchorOrientation, anchorDirection, type, node)
  21. {
  22. }
  23. public override bool ContainsPoint(Vector2 localPoint)
  24. {
  25. return base.ContainsPoint(localPoint) && !m_ConnectorText.ContainsPoint(this.ChangeCoordinatesTo(m_ConnectorText, localPoint));
  26. }
  27. }
  28. class VFXInputParameterDataAnchor : VFXDataAnchor
  29. {
  30. public static new VFXInputParameterDataAnchor Create(VFXDataAnchorController controller, VFXNodeUI node)
  31. {
  32. var anchor = new VFXInputParameterDataAnchor(controller.orientation, controller.direction, controller.portType, node);
  33. anchor.m_EdgeConnector = new EdgeConnector<VFXDataEdge>(anchor);
  34. anchor.controller = controller;
  35. anchor.AddManipulator(anchor.m_EdgeConnector);
  36. return anchor;
  37. }
  38. protected VFXInputParameterDataAnchor(Orientation anchorOrientation, Direction anchorDirection, Type type, VFXNodeUI node) : base(anchorOrientation, anchorDirection, type, node)
  39. {
  40. }
  41. public override bool ContainsPoint(Vector2 localPoint)
  42. {
  43. return base.ContainsPoint(localPoint) && !m_ConnectorText.ContainsPoint(this.ChangeCoordinatesTo(m_ConnectorText, localPoint));
  44. }
  45. }
  46. static class UXMLHelper
  47. {
  48. const string folderName = VisualEffectAssetEditorUtility.editorResourcesFolder;
  49. public static string GetUXMLPath(string name)
  50. {
  51. string path = null;
  52. if (s_Cache.TryGetValue(name, out path))
  53. {
  54. return path;
  55. }
  56. return GetUXMLPathRecursive("Assets", name);
  57. }
  58. static Dictionary<string, string> s_Cache = new Dictionary<string, string>();
  59. static string GetUXMLPathRecursive(string path, string name)
  60. {
  61. Profiler.BeginSample("UXMLHelper.GetUXMLPathRecursive");
  62. string localFileName = path + "/" + folderName + "/" + name;
  63. if (System.IO.File.Exists(localFileName))
  64. {
  65. Profiler.EndSample();
  66. s_Cache[name] = localFileName;
  67. return localFileName;
  68. }
  69. foreach (var dir in System.IO.Directory.GetDirectories(path))
  70. {
  71. if (dir.Length <= folderName.Length || !dir.EndsWith(folderName) || !"/\\".Contains(dir[dir.Length - folderName.Length - 1]))
  72. {
  73. string result = GetUXMLPathRecursive(dir, name);
  74. if (result != null)
  75. {
  76. Profiler.EndSample();
  77. return result;
  78. }
  79. }
  80. }
  81. Profiler.EndSample();
  82. return null;
  83. }
  84. }
  85. class VFXParameterUI : VFXNodeUI
  86. {
  87. public VFXParameterUI() : base("uxml/VFXParameter")
  88. {
  89. RemoveFromClassList("VFXNodeUI");
  90. styleSheets.Add(VFXView.LoadStyleSheet("VFXParameter"));
  91. styleSheets.Add(EditorGUIUtility.Load("StyleSheets/GraphView/Node.uss") as StyleSheet);
  92. RegisterCallback<MouseEnterEvent>(OnMouseHover);
  93. RegisterCallback<MouseLeaveEvent>(OnMouseHover);
  94. m_ExposedIcon = this.Q<Image>("exposed-icon");
  95. m_SuperCollapsedButton = this.Q("super-collapse-button");
  96. m_SuperCollapsedButton.AddManipulator(new Clickable(OnToggleSuperCollapse));
  97. this.AddManipulator(new SuperCollapser());
  98. m_Pill = this.Q("pill");
  99. }
  100. VisualElement m_Pill;
  101. void OnToggleSuperCollapse()
  102. {
  103. controller.superCollapsed = !controller.superCollapsed;
  104. }
  105. VisualElement m_SuperCollapsedButton;
  106. public new VFXParameterNodeController controller
  107. {
  108. get { return base.controller as VFXParameterNodeController; }
  109. }
  110. public override VFXDataAnchor InstantiateDataAnchor(VFXDataAnchorController controller, VFXNodeUI node)
  111. {
  112. if (controller.direction == Direction.Input)
  113. return VFXInputParameterDataAnchor.Create(controller, node);
  114. else
  115. return VFXOutputParameterDataAnchor.Create(controller, node);
  116. }
  117. Image m_ExposedIcon;
  118. protected override void SelfChange()
  119. {
  120. base.SelfChange();
  121. if (m_ExposedIcon != null)
  122. m_ExposedIcon.visible = controller.parentController.exposed;
  123. if (controller.parentController.exposed)
  124. {
  125. AddToClassList("exposed");
  126. }
  127. else
  128. {
  129. RemoveFromClassList("exposed");
  130. }
  131. if (m_Pill != null)
  132. m_Pill.tooltip = controller.parentController.model.tooltip;
  133. }
  134. public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
  135. {
  136. if (evt.target == this && controller != null)
  137. {
  138. evt.menu.AppendAction("Convert to Inline", OnConvertToInline, e => DropdownMenuAction.Status.Normal);
  139. evt.menu.AppendSeparator();
  140. }
  141. }
  142. void OnConvertToInline(DropdownMenuAction evt)
  143. {
  144. controller.ConvertToInline();
  145. }
  146. void OnMouseHover(EventBase evt)
  147. {
  148. VFXView view = GetFirstAncestorOfType<VFXView>();
  149. if (view == null)
  150. return;
  151. VFXBlackboard blackboard = view.blackboard;
  152. if (blackboard == null)
  153. return;
  154. VFXBlackboardRow row = blackboard.GetRowFromController(controller.parentController);
  155. if (row == null)
  156. return;
  157. if (evt.eventTypeId == MouseEnterEvent.TypeId())
  158. row.AddToClassList("hovered");
  159. else
  160. row.RemoveFromClassList("hovered");
  161. }
  162. }
  163. }