VFXDataAnchor.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor.Experimental.GraphView;
  5. using UnityEngine;
  6. using UnityEngine.VFX;
  7. using UnityEngine.UIElements;
  8. using UnityEngine.Profiling;
  9. using Type = System.Type;
  10. using PositionType = UnityEngine.UIElements.Position;
  11. namespace UnityEditor.VFX.UI
  12. {
  13. class VFXDataAnchor : Port, IControlledElement<VFXDataAnchorController>, IEdgeConnectorListener
  14. {
  15. VFXDataAnchorController m_Controller;
  16. Controller IControlledElement.controller
  17. {
  18. get { return m_Controller; }
  19. }
  20. public VFXDataAnchorController controller
  21. {
  22. get { return m_Controller; }
  23. set
  24. {
  25. if (m_Controller != null)
  26. {
  27. m_Controller.UnregisterHandler(this);
  28. }
  29. m_Controller = value;
  30. if (m_Controller != null)
  31. {
  32. if (m_Controller.model != null)
  33. m_Controller.model.RefreshErrors(m_Controller.sourceNode.viewController.graph);
  34. m_Controller.RegisterHandler(this);
  35. }
  36. }
  37. }
  38. VFXNodeUI m_Node;
  39. public new VFXNodeUI node
  40. {
  41. get { return m_Node; }
  42. }
  43. protected VFXDataAnchor(Orientation anchorOrientation, Direction anchorDirection, Type type, VFXNodeUI node) : base(anchorOrientation, anchorDirection, Capacity.Multi, type)
  44. {
  45. Profiler.BeginSample("VFXDataAnchor.VFXDataAnchor");
  46. this.AddStyleSheetPath("VFXDataAnchor");
  47. AddToClassList("VFXDataAnchor");
  48. this.AddStyleSheetPath("VFXTypeColor");
  49. m_Node = node;
  50. RegisterCallback<MouseEnterEvent>(OnMouseEnter);
  51. RegisterCallback<MouseLeaveEvent>(OnMouseLeave);
  52. this.AddManipulator(new ContextualMenuManipulator(BuildContextualMenu));
  53. Profiler.EndSample();
  54. }
  55. public VisualElement connector
  56. {
  57. get { return m_ConnectorBox; }
  58. }
  59. public virtual void BuildContextualMenu(ContextualMenuPopulateEvent evt)
  60. {
  61. var op = controller.sourceNode.model as VFXOperatorNumericCascadedUnified;
  62. if (op != null)
  63. evt.menu.AppendAction("Remove Slot", OnRemove, e => op.operandCount > 2 ? DropdownMenuAction.Status.Normal : DropdownMenuAction.Status.Disabled);
  64. }
  65. void OnRemove(DropdownMenuAction e)
  66. {
  67. var op = controller.sourceNode as VFXCascadedOperatorController;
  68. op.RemoveOperand(controller);
  69. }
  70. public static VFXDataAnchor Create(VFXDataAnchorController controller, VFXNodeUI node)
  71. {
  72. var anchor = new VFXDataAnchor(controller.orientation, controller.direction, controller.portType, node);
  73. anchor.m_EdgeConnector = new VFXEdgeConnector(anchor);
  74. anchor.controller = controller;
  75. anchor.AddManipulator(anchor.m_EdgeConnector);
  76. return anchor;
  77. }
  78. bool m_EdgeDragging;
  79. public override void OnStartEdgeDragging()
  80. {
  81. m_EdgeDragging = true;
  82. highlight = false;
  83. }
  84. public override void OnStopEdgeDragging()
  85. {
  86. m_EdgeDragging = false;
  87. highlight = true;
  88. }
  89. void OnMouseEnter(MouseEnterEvent e)
  90. {
  91. if (m_EdgeDragging && !highlight)
  92. e.PreventDefault();
  93. }
  94. void OnMouseLeave(MouseLeaveEvent e)
  95. {
  96. if (m_EdgeDragging && !highlight)
  97. e.PreventDefault();
  98. }
  99. public override bool collapsed
  100. {
  101. get { return !controller.expandedInHierachy; }
  102. }
  103. IEnumerable<VFXDataEdge> GetAllEdges()
  104. {
  105. VFXView view = GetFirstAncestorOfType<VFXView>();
  106. foreach (var edgeController in controller.connections)
  107. {
  108. VFXDataEdge edge = view.GetDataEdgeByController(edgeController as VFXDataEdgeController);
  109. if (edge != null)
  110. yield return edge;
  111. }
  112. }
  113. void IControlledElement.OnControllerChanged(ref ControllerChangedEvent e)
  114. {
  115. if (e.controller == controller)
  116. {
  117. SelfChange(e.change);
  118. }
  119. }
  120. public virtual void SelfChange(int change)
  121. {
  122. if (change != VFXDataAnchorController.Change.hidden)
  123. {
  124. if (controller.connected)
  125. AddToClassList("connected");
  126. else
  127. RemoveFromClassList("connected");
  128. portType = controller.portType;
  129. string className = VFXTypeDefinition.GetTypeCSSClass(controller.portType);
  130. // update the css type of the class
  131. foreach (var cls in VFXTypeDefinition.GetTypeCSSClasses())
  132. {
  133. if (cls != className)
  134. {
  135. m_ConnectorBox.RemoveFromClassList(cls);
  136. RemoveFromClassList(cls);
  137. }
  138. }
  139. AddToClassList(className);
  140. m_ConnectorBox.AddToClassList(className);
  141. AddToClassList("EdgeConnector");
  142. switch (controller.direction)
  143. {
  144. case Direction.Input:
  145. AddToClassList("Input");
  146. break;
  147. case Direction.Output:
  148. AddToClassList("Output");
  149. break;
  150. }
  151. portName = "";
  152. }
  153. if (controller.expandedInHierachy)
  154. {
  155. style.display = DisplayStyle.Flex;
  156. RemoveFromClassList("hidden");
  157. }
  158. else
  159. {
  160. style.display = DisplayStyle.None;
  161. AddToClassList("hidden");
  162. }
  163. UpdateCapColor();
  164. if (controller.direction == Direction.Output)
  165. m_ConnectorText.text = controller.name;
  166. else
  167. m_ConnectorText.text = "";
  168. }
  169. void IEdgeConnectorListener.OnDrop(GraphView graphView, Edge edge)
  170. {
  171. VFXView view = graphView as VFXView;
  172. VFXDataEdge dataEdge = edge as VFXDataEdge;
  173. VFXDataEdgeController edgeController = new VFXDataEdgeController(dataEdge.input.controller, dataEdge.output.controller);
  174. view.controller.AddElement(edgeController);
  175. }
  176. public override void Disconnect(Edge edge)
  177. {
  178. base.Disconnect(edge);
  179. UpdateCapColor();
  180. }
  181. void IEdgeConnectorListener.OnDropOutsidePort(Edge edge, Vector2 position)
  182. {
  183. VFXSlot startSlot = controller.model;
  184. VFXView view = this.GetFirstAncestorOfType<VFXView>();
  185. VFXViewController viewController = view.controller;
  186. List<VisualElement> picked = new List<VisualElement>();
  187. panel.PickAll(position, picked);
  188. VFXNodeUI endNode = null;
  189. foreach (var element in picked)
  190. {
  191. if (element is VFXNodeUI node)
  192. {
  193. endNode = node;
  194. break;
  195. }
  196. }
  197. VFXDataEdge dataEdge = edge as VFXDataEdge;
  198. bool exists = false;
  199. if (dataEdge.controller != null)
  200. {
  201. exists = true;
  202. view.controller.RemoveElement(dataEdge.controller);
  203. }
  204. if (endNode != null)
  205. {
  206. VFXNodeController nodeController = endNode.controller;
  207. if (nodeController != null)
  208. {
  209. IVFXSlotContainer slotContainer = nodeController.slotContainer;
  210. if (controller.direction == Direction.Input)
  211. {
  212. foreach (var output in nodeController.outputPorts.Where(t => t.model == null || t.model.IsMasterSlot()))
  213. {
  214. if (viewController.CreateLink(controller, output))
  215. break;
  216. }
  217. }
  218. else
  219. {
  220. foreach (var input in nodeController.inputPorts.Where(t => t.model == null || t.model.IsMasterSlot() && !t.model.HasLink(true)))
  221. {
  222. if (viewController.CreateLink(input, controller))
  223. break;
  224. }
  225. }
  226. }
  227. }
  228. else if (controller.direction == Direction.Input && Event.current.modifiers == EventModifiers.Alt)
  229. {
  230. var targetType = controller.portType;
  231. var attribute = VFXLibrary.GetAttributeFromSlotType(controller.portType);
  232. VFXModelDescriptorParameters parameterDesc;
  233. if (attribute != null && attribute.usages.HasFlag(VFXTypeAttribute.Usage.ExcludeFromProperty))
  234. {
  235. parameterDesc = VFXLibrary.GetParameters().FirstOrDefault(t =>
  236. {
  237. if (!t.model.outputSlots[0].CanLink(controller.model))
  238. return false;
  239. var attributeCandidate = VFXLibrary.GetAttributeFromSlotType(t.model.type);
  240. return attributeCandidate == null || !attributeCandidate.usages.HasFlag(VFXTypeAttribute.Usage.ExcludeFromProperty);
  241. });
  242. }
  243. else
  244. {
  245. parameterDesc = VFXLibrary.GetParameters().FirstOrDefault(t =>
  246. {
  247. return t.model.type == targetType;
  248. });
  249. }
  250. if (parameterDesc != null)
  251. {
  252. Vector2 pos = view.contentViewContainer.GlobalToBound(position) - new Vector2(140, 20);
  253. view.UpdateSelectionWithNewNode();
  254. VFXParameter parameter = viewController.AddVFXParameter(pos, parameterDesc, false);
  255. parameter.SetSettingValue("m_Exposed", true);
  256. startSlot.Link(parameter.outputSlots[0]);
  257. CopyValueToParameter(parameter);
  258. viewController.AddVFXModel(pos, parameter);
  259. }
  260. }
  261. else if (!exists)
  262. {
  263. if (direction == Direction.Input || viewController.model.visualEffectObject is VisualEffectSubgraphOperator || viewController.model.visualEffectObject is VisualEffectSubgraphBlock) // no context for subgraph operators.
  264. VFXFilterWindow.Show(VFXViewWindow.currentWindow, Event.current.mousePosition, view.ViewToScreenPosition(Event.current.mousePosition), new VFXNodeProvider(viewController, AddLinkedNode, ProviderFilter, new Type[] { typeof(VFXOperator), typeof(VFXParameter) }));
  265. else
  266. VFXFilterWindow.Show(VFXViewWindow.currentWindow, Event.current.mousePosition, view.ViewToScreenPosition(Event.current.mousePosition), new VFXNodeProvider(viewController, AddLinkedNode, ProviderFilter, new Type[] { typeof(VFXOperator), typeof(VFXParameter), typeof(VFXContext) }));
  267. }
  268. }
  269. bool ProviderFilter(VFXNodeProvider.Descriptor d)
  270. {
  271. var mySlot = controller.model;
  272. var parameterDescriptor = d.modelDescriptor as VFXParameterController;
  273. IVFXSlotContainer container = null;
  274. if (parameterDescriptor != null)
  275. {
  276. container = parameterDescriptor.model;
  277. }
  278. else
  279. {
  280. VFXModelDescriptor desc = d.modelDescriptor as VFXModelDescriptor;
  281. if (desc == null)
  282. {
  283. string path = d.modelDescriptor as string;
  284. if (path != null && !path.StartsWith(VisualEffectAssetEditorUtility.templatePath))
  285. {
  286. if (Path.GetExtension(path) == VisualEffectSubgraphOperator.Extension)
  287. {
  288. var subGraph = AssetDatabase.LoadAssetAtPath<VisualEffectSubgraphOperator>(path);
  289. if (subGraph != null && (!controller.viewController.model.isSubgraph || !subGraph.GetResource().GetOrCreateGraph().subgraphDependencies.Contains(controller.viewController.model.subgraph) && subGraph.GetResource() != controller.viewController.model))
  290. return true;
  291. }
  292. }
  293. return false;
  294. }
  295. container = desc.model as IVFXSlotContainer;
  296. if (container == null)
  297. return false;
  298. if (direction == Direction.Output
  299. && mySlot != null
  300. && container is VFXOperatorDynamicOperand
  301. && (container as VFXOperatorDynamicOperand).validTypes.Contains(mySlot.property.type))
  302. return true;
  303. }
  304. IEnumerable<Type> validTypes = null;
  305. if (mySlot == null)
  306. {
  307. var op = controller.sourceNode.model as VFXOperatorDynamicOperand;
  308. if (op != null)
  309. validTypes = op.validTypes;
  310. }
  311. var getSlots = direction == Direction.Input ? (System.Func<int, VFXSlot>)container.GetOutputSlot : (System.Func<int, VFXSlot>)container.GetInputSlot;
  312. int count = direction == Direction.Input ? container.GetNbOutputSlots() : container.GetNbInputSlots();
  313. for (int i = 0; i < count; ++i)
  314. {
  315. var slot = getSlots(i);
  316. if (mySlot != null && slot.CanLink(mySlot))
  317. return true;
  318. else if (validTypes != null && validTypes.Contains(slot.property.type))
  319. return true;
  320. }
  321. return false;
  322. }
  323. void AddLinkedNode(VFXNodeProvider.Descriptor d, Vector2 mPos)
  324. {
  325. var mySlot = controller.model;
  326. VFXView view = GetFirstAncestorOfType<VFXView>();
  327. VFXViewController viewController = controller.viewController;
  328. if (view == null) return;
  329. var newNodeController = view.AddNode(d, mPos);
  330. if (newNodeController == null)
  331. return;
  332. IEnumerable<Type> validTypes = null;
  333. var op = controller.sourceNode.model as VFXOperatorNumericCascadedUnified;
  334. if (mySlot == null && op != null)
  335. {
  336. validTypes = op.validTypes;
  337. }
  338. // If linking to a new parameter, copy the slot value and space
  339. if (direction == Direction.Input && controller.model != null) //model will be null for upcomming which won't have a value
  340. {
  341. if (newNodeController is VFXOperatorController)
  342. {
  343. var inlineOperator = (newNodeController as VFXOperatorController).model as VFXInlineOperator;
  344. if (inlineOperator != null)
  345. {
  346. var value = controller.model.value;
  347. object convertedValue = null;
  348. if (VFXConverter.TryConvertTo(value, inlineOperator.type, out convertedValue))
  349. {
  350. inlineOperator.inputSlots[0].value = convertedValue;
  351. }
  352. if (inlineOperator.inputSlots[0].spaceable && controller.model.spaceable)
  353. {
  354. inlineOperator.inputSlots[0].space = controller.model.space;
  355. }
  356. }
  357. }
  358. }
  359. var ports = direction == Direction.Input ? newNodeController.outputPorts : newNodeController.inputPorts;
  360. int count = ports.Count();
  361. for (int i = 0; i < count; ++i)
  362. {
  363. var port = ports[i];
  364. if (mySlot != null)
  365. {
  366. if (viewController.CreateLink(direction == Direction.Input ? controller : port, direction == Direction.Input ? port : controller))
  367. {
  368. break;
  369. }
  370. }
  371. else if (validTypes != null)
  372. {
  373. if (validTypes.Contains(port.model.property.type))
  374. {
  375. if (viewController.CreateLink(controller, port))
  376. {
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. }
  383. void CopyValueToParameter(VFXParameter parameter)
  384. {
  385. var value = controller.model.value;
  386. object convertedValue = null;
  387. if (VFXConverter.TryConvertTo(value, parameter.type, out convertedValue))
  388. {
  389. parameter.value = convertedValue;
  390. }
  391. }
  392. }
  393. }