UGUIPort.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using XNode;
  8. namespace XNode.Examples.RuntimeMathNodes {
  9. public class UGUIPort : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler {
  10. public string fieldName;
  11. [HideInInspector] public XNode.Node node;
  12. private NodePort port;
  13. private Connection tempConnection;
  14. private NodePort startPort;
  15. private UGUIPort tempHovered;
  16. private RuntimeMathGraph graph;
  17. private Vector2 startPos;
  18. private List<Connection> connections = new List<Connection>();
  19. void Start() {
  20. port = node.GetPort(fieldName);
  21. graph = GetComponentInParent<RuntimeMathGraph>();
  22. if (port.IsOutput && port.IsConnected) {
  23. for (int i = 0; i < port.ConnectionCount; i++) {
  24. AddConnection();
  25. }
  26. }
  27. }
  28. void Reset() {
  29. fieldName = name;
  30. }
  31. private void OnDestroy() {
  32. // Also destroy connections
  33. for (int i = connections.Count - 1; i >= 0; i--) {
  34. Destroy(connections[i].gameObject);
  35. }
  36. connections.Clear();
  37. }
  38. public void UpdateConnectionTransforms() {
  39. if (port.IsInput) return;
  40. while (connections.Count < port.ConnectionCount) AddConnection();
  41. while (connections.Count > port.ConnectionCount) {
  42. Destroy(connections[0].gameObject);
  43. connections.RemoveAt(0);
  44. }
  45. // Loop through connections
  46. for (int i = 0; i < port.ConnectionCount; i++) {
  47. NodePort other = port.GetConnection(i);
  48. UGUIMathBaseNode otherNode = graph.GetRuntimeNode(other.node);
  49. if (!otherNode) Debug.LogWarning(other.node.name + " node not found", this);
  50. Transform port2 = otherNode.GetPort(other.fieldName).transform;
  51. if (!port2) Debug.LogWarning(other.fieldName + " not found", this);
  52. connections[i].SetPosition(transform.position, port2.position);
  53. }
  54. }
  55. private void AddConnection() {
  56. Connection connection = Instantiate(graph.runtimeConnectionPrefab);
  57. connection.transform.SetParent(graph.scrollRect.content);
  58. connections.Add(connection);
  59. }
  60. public void OnBeginDrag(PointerEventData eventData) {
  61. if (port.IsOutput) {
  62. tempConnection = Instantiate(graph.runtimeConnectionPrefab);
  63. tempConnection.transform.SetParent(graph.scrollRect.content);
  64. tempConnection.SetPosition(transform.position, eventData.position);
  65. startPos = transform.position;
  66. startPort = port;
  67. } else {
  68. if (port.IsConnected) {
  69. NodePort output = port.Connection;
  70. Debug.Log("has " + port.ConnectionCount + " connections");
  71. Debug.Log(port.GetConnection(0));
  72. UGUIMathBaseNode otherNode = graph.GetRuntimeNode(output.node);
  73. UGUIPort otherUGUIPort = otherNode.GetPort(output.fieldName);
  74. Debug.Log("Disconnect");
  75. output.Disconnect(port);
  76. tempConnection = Instantiate(graph.runtimeConnectionPrefab);
  77. tempConnection.transform.SetParent(graph.scrollRect.content);
  78. tempConnection.SetPosition(otherUGUIPort.transform.position, eventData.position);
  79. startPos = otherUGUIPort.transform.position;
  80. startPort = otherUGUIPort.port;
  81. graph.GetRuntimeNode(node).UpdateGUI();
  82. }
  83. }
  84. }
  85. public void OnDrag(PointerEventData eventData) {
  86. if (tempConnection == null) return;
  87. UGUIPort otherPort = FindPortInStack(eventData.hovered);
  88. tempHovered = otherPort;
  89. tempConnection.SetPosition(startPos, eventData.position);
  90. }
  91. public void OnEndDrag(PointerEventData eventData) {
  92. if (tempConnection == null) return;
  93. if (tempHovered) {
  94. startPort.Connect(tempHovered.port);
  95. graph.GetRuntimeNode(tempHovered.node).UpdateGUI();
  96. }
  97. Destroy(tempConnection.gameObject);
  98. }
  99. public UGUIPort FindPortInStack(List<GameObject> stack) {
  100. for (int i = 0; i < stack.Count; i++) {
  101. UGUIPort port = stack[i].GetComponent<UGUIPort>();
  102. if (port) return port;
  103. }
  104. return null;
  105. }
  106. public void OnPointerEnter(PointerEventData eventData) {
  107. graph.tooltip.Show();
  108. object obj = node.GetInputValue<object>(port.fieldName, null);
  109. if (obj != null) graph.tooltip.label.text = obj.ToString();
  110. else graph.tooltip.label.text = "n/a";
  111. }
  112. public void OnPointerExit(PointerEventData eventData) {
  113. graph.tooltip.Hide();
  114. }
  115. }
  116. }