EdgeView.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using UnityEditor.Experimental.GraphView;
  2. using UnityEngine.UIElements;
  3. using UnityEngine;
  4. namespace GraphProcessor
  5. {
  6. public class EdgeView : Edge
  7. {
  8. public bool isConnected = false;
  9. public SerializableEdge serializedEdge { get { return userData as SerializableEdge; } }
  10. readonly string edgeStyle = "GraphProcessorStyles/EdgeView";
  11. protected BaseGraphView owner => ((input ?? output) as PortView).owner.owner;
  12. public EdgeView() : base()
  13. {
  14. styleSheets.Add(Resources.Load<StyleSheet>(edgeStyle));
  15. RegisterCallback<MouseDownEvent>(OnMouseDown);
  16. }
  17. public override void OnPortChanged(bool isInput)
  18. {
  19. base.OnPortChanged(isInput);
  20. UpdateEdgeSize();
  21. }
  22. public void UpdateEdgeSize()
  23. {
  24. if (input == null && output == null)
  25. return;
  26. PortData inputPortData = (input as PortView)?.portData;
  27. PortData outputPortData = (output as PortView)?.portData;
  28. for (int i = 1; i < 20; i++)
  29. RemoveFromClassList($"edge_{i}");
  30. int maxPortSize = Mathf.Max(inputPortData?.sizeInPixel ?? 0, outputPortData?.sizeInPixel ?? 0);
  31. if (maxPortSize > 0)
  32. AddToClassList($"edge_{Mathf.Max(1, maxPortSize - 6)}");
  33. }
  34. protected override void OnCustomStyleResolved(ICustomStyle styles)
  35. {
  36. base.OnCustomStyleResolved(styles);
  37. UpdateEdgeControl();
  38. }
  39. void OnMouseDown(MouseDownEvent e)
  40. {
  41. if (e.clickCount == 2)
  42. {
  43. // Empirical offset:
  44. var position = e.mousePosition;
  45. position += new Vector2(-10f, -28);
  46. Vector2 mousePos = owner.ChangeCoordinatesTo(owner.contentViewContainer, position);
  47. owner.AddRelayNode(input as PortView, output as PortView, mousePos);
  48. }
  49. }
  50. }
  51. }