diff options
author | chai <chaifix@163.com> | 2022-06-28 09:40:37 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2022-06-28 09:40:37 +0800 |
commit | 49b25e755b70ec412feaaf0b898d6f7e09d2bea6 (patch) | |
tree | 3c5f4260f30d1c2d7196db93153700d7ddec3157 /Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts | |
parent | c92269331692feca2c276649f6c4ee8911f1f859 (diff) |
+node example
Diffstat (limited to 'Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts')
21 files changed, 689 insertions, 0 deletions
diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/Connection.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/Connection.cs new file mode 100644 index 00000000..ac4e889b --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/Connection.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using XNode; + +namespace XNode.Examples.RuntimeMathNodes { + public class Connection : MonoBehaviour { + private RectTransform rectTransform; + public void SetPosition(Vector2 start, Vector2 end) { + if (!rectTransform) rectTransform = (RectTransform) transform; + transform.position = (start + end) * 0.5f; + + float r = Mathf.Atan2(start.y - end.y, start.x - end.x) * Mathf.Rad2Deg; + transform.rotation = Quaternion.Euler(0, 0, r); + rectTransform.sizeDelta = new Vector2(Vector2.Distance(start, end), rectTransform.sizeDelta.y); + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/Connection.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/Connection.cs.meta new file mode 100644 index 00000000..bb46d546 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/Connection.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 656e34b375c2eb64991ee74b5f01bd13 +timeCreated: 1525819160 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/NodeDrag.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/NodeDrag.cs new file mode 100644 index 00000000..96ba753d --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/NodeDrag.cs @@ -0,0 +1,40 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; + +namespace XNode.Examples.RuntimeMathNodes { + public class NodeDrag : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler { + private Vector3 offset; + private UGUIMathBaseNode node; + + private void Awake() { + node = GetComponentInParent<UGUIMathBaseNode>(); + } + + public void OnDrag(PointerEventData eventData) { + node.transform.localPosition = node.graph.scrollRect.content.InverseTransformPoint(eventData.position) - offset; + } + + public void OnBeginDrag(PointerEventData eventData) { + Vector2 pointer = node.graph.scrollRect.content.InverseTransformPoint(eventData.position); + Vector2 pos = node.transform.localPosition; + offset = pointer - pos; + } + + public void OnEndDrag(PointerEventData eventData) { + node.transform.localPosition = node.graph.scrollRect.content.InverseTransformPoint(eventData.position) - offset; + Vector2 pos = node.transform.localPosition; + pos.y = -pos.y; + node.node.position = pos; + } + + public void OnPointerClick(PointerEventData eventData) { + if (eventData.button != PointerEventData.InputButton.Right) + return; + + node.graph.nodeContextMenu.selectedNode = node.node; + node.graph.nodeContextMenu.OpenAt(eventData.position); + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/NodeDrag.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/NodeDrag.cs.meta new file mode 100644 index 00000000..30ca3d52 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/NodeDrag.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: d3f4b82da0d484640839efdaf81118b8 +timeCreated: 1525909194 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs new file mode 100644 index 00000000..f1b39b9b --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; +using XNode.Examples.MathNodes; + +namespace XNode.Examples.RuntimeMathNodes { + public class RuntimeMathGraph : MonoBehaviour, IPointerClickHandler { + [Header("Graph")] + public MathGraph graph; + [Header("Prefabs")] + public XNode.Examples.RuntimeMathNodes.UGUIMathNode runtimeMathNodePrefab; + public XNode.Examples.RuntimeMathNodes.UGUIVector runtimeVectorPrefab; + public XNode.Examples.RuntimeMathNodes.UGUIDisplayValue runtimeDisplayValuePrefab; + public XNode.Examples.RuntimeMathNodes.Connection runtimeConnectionPrefab; + [Header("References")] + public UGUIContextMenu graphContextMenu; + public UGUIContextMenu nodeContextMenu; + public UGUITooltip tooltip; + + public ScrollRect scrollRect { get; private set; } + private List<UGUIMathBaseNode> nodes; + + private void Awake() { + // Create a clone so we don't modify the original asset + graph = graph.Copy() as MathGraph; + scrollRect = GetComponentInChildren<ScrollRect>(); + graphContextMenu.onClickSpawn -= SpawnNode; + graphContextMenu.onClickSpawn += SpawnNode; + } + + private void Start() { + SpawnGraph(); + } + + public void Refresh() { + Clear(); + SpawnGraph(); + } + + public void Clear() { + for (int i = nodes.Count - 1; i >= 0; i--) { + Destroy(nodes[i].gameObject); + } + nodes.Clear(); + } + + public void SpawnGraph() { + if (nodes != null) nodes.Clear(); + else nodes = new List<UGUIMathBaseNode>(); + + for (int i = 0; i < graph.nodes.Count; i++) { + Node node = graph.nodes[i]; + + UGUIMathBaseNode runtimeNode = null; + if (node is XNode.Examples.MathNodes.MathNode) { + runtimeNode = Instantiate(runtimeMathNodePrefab); + } else if (node is XNode.Examples.MathNodes.Vector) { + runtimeNode = Instantiate(runtimeVectorPrefab); + } else if (node is XNode.Examples.MathNodes.DisplayValue) { + runtimeNode = Instantiate(runtimeDisplayValuePrefab); + } + runtimeNode.transform.SetParent(scrollRect.content); + runtimeNode.node = node; + runtimeNode.graph = this; + nodes.Add(runtimeNode); + } + } + + public UGUIMathBaseNode GetRuntimeNode(Node node) { + for (int i = 0; i < nodes.Count; i++) { + if (nodes[i].node == node) { + return nodes[i]; + } else { } + } + return null; + } + + public void SpawnNode(Type type, Vector2 position) { + Node node = graph.AddNode(type); + node.name = type.Name; + node.position = position; + Refresh(); + } + + public void OnPointerClick(PointerEventData eventData) { + if (eventData.button != PointerEventData.InputButton.Right) + return; + + graphContextMenu.OpenAt(eventData.position); + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs.meta new file mode 100644 index 00000000..7aac1ef6 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: f58f5c7a27d48e34fb5180e77697e866 +timeCreated: 1525811971 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes.meta new file mode 100644 index 00000000..d26d8b02 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 4e934323f65742a4ebcabeebb8da2f92 +folderAsset: yes +timeCreated: 1525818451 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIDisplayValue.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIDisplayValue.cs new file mode 100644 index 00000000..da5e4c20 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIDisplayValue.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using XNode.Examples.MathNodes; + +namespace XNode.Examples.RuntimeMathNodes { + public class UGUIDisplayValue : UGUIMathBaseNode { + public Text label; + + void Update() { + DisplayValue displayValue = node as DisplayValue; + object obj = displayValue.GetInputValue<object>("input"); + if (obj != null) label.text = obj.ToString(); + else label.text = "n/a"; + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIDisplayValue.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIDisplayValue.cs.meta new file mode 100644 index 00000000..d8904cf3 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIDisplayValue.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 151bec3209e717646b65a9214bc08295 +timeCreated: 1525812043 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathBaseNode.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathBaseNode.cs new file mode 100644 index 00000000..8b9c6496 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathBaseNode.cs @@ -0,0 +1,49 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; +using XNode.Examples.MathNodes; + +namespace XNode.Examples.RuntimeMathNodes { + public class UGUIMathBaseNode : MonoBehaviour, IDragHandler { + [HideInInspector] public Node node; + [HideInInspector] public RuntimeMathGraph graph; + public Text header; + + private UGUIPort[] ports; + + public virtual void Start() { + ports = GetComponentsInChildren<UGUIPort>(); + foreach (UGUIPort port in ports) port.node = node; + header.text = node.name; + SetPosition(node.position); + } + + public virtual void UpdateGUI() { } + + private void LateUpdate() { + foreach (UGUIPort port in ports) port.UpdateConnectionTransforms(); + } + + public UGUIPort GetPort(string name) { + for (int i = 0; i < ports.Length; i++) { + if (ports[i].name == name) return ports[i]; + } + return null; + } + + public void SetPosition(Vector2 pos) { + pos.y = -pos.y; + transform.localPosition = pos; + } + + public void SetName(string name) { + header.text = name; + } + + public void OnDrag(PointerEventData eventData) { + + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathBaseNode.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathBaseNode.cs.meta new file mode 100644 index 00000000..a874bda3 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathBaseNode.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: f09009dde769a3845a66ded688a8f8dd +timeCreated: 1525812043 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathNode.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathNode.cs new file mode 100644 index 00000000..76c2663e --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathNode.cs @@ -0,0 +1,48 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using XNode.Examples.MathNodes; + +namespace XNode.Examples.RuntimeMathNodes { + public class UGUIMathNode : UGUIMathBaseNode { + public InputField valA; + public InputField valB; + public Dropdown dropDown; + + private MathNode mathNode; + + public override void Start() { + base.Start(); + mathNode = node as MathNode; + + valA.onValueChanged.AddListener(OnChangeValA); + valB.onValueChanged.AddListener(OnChangeValB); + dropDown.onValueChanged.AddListener(OnChangeDropdown); + UpdateGUI(); + } + + public override void UpdateGUI() { + NodePort portA = node.GetInputPort("a"); + NodePort portB = node.GetInputPort("b"); + valA.gameObject.SetActive(!portA.IsConnected); + valB.gameObject.SetActive(!portB.IsConnected); + + valA.text = mathNode.a.ToString(); + valB.text = mathNode.b.ToString(); + dropDown.value = (int) mathNode.mathType; + } + + private void OnChangeValA(string val) { + mathNode.a = float.Parse(valA.text); + } + + private void OnChangeValB(string val) { + mathNode.b = float.Parse(valB.text); + } + + private void OnChangeDropdown(int val) { + mathNode.mathType = (MathNode.MathType) val; + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathNode.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathNode.cs.meta new file mode 100644 index 00000000..2754d28b --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIMathNode.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: f9595458c0d990a458724e20b81b5637 +timeCreated: 1525812043 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIVector.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIVector.cs new file mode 100644 index 00000000..ca517a3e --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIVector.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using XNode.Examples.MathNodes; + +namespace XNode.Examples.RuntimeMathNodes { + public class UGUIVector : UGUIMathBaseNode { + public InputField valX; + public InputField valY; + public InputField valZ; + + private Vector vectorNode; + + public override void Start() { + base.Start(); + vectorNode = node as Vector; + + valX.onValueChanged.AddListener(OnChangeValX); + valY.onValueChanged.AddListener(OnChangeValY); + valZ.onValueChanged.AddListener(OnChangeValZ); + UpdateGUI(); + } + + public override void UpdateGUI() { + NodePort portX = node.GetInputPort("x"); + NodePort portY = node.GetInputPort("y"); + NodePort portZ = node.GetInputPort("z"); + valX.gameObject.SetActive(!portX.IsConnected); + valY.gameObject.SetActive(!portY.IsConnected); + valZ.gameObject.SetActive(!portZ.IsConnected); + + Vector vectorNode = node as Vector; + valX.text = vectorNode.x.ToString(); + valY.text = vectorNode.y.ToString(); + valZ.text = vectorNode.z.ToString(); + } + + private void OnChangeValX(string val) { + vectorNode.x = float.Parse(valX.text); + } + + private void OnChangeValY(string val) { + vectorNode.y = float.Parse(valY.text); + } + + private void OnChangeValZ(string val) { + vectorNode.z = float.Parse(valZ.text); + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIVector.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIVector.cs.meta new file mode 100644 index 00000000..3afde48a --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeNodes/UGUIVector.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 8f91529775ea04f4681f3f03df79e870 +timeCreated: 1525812043 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIContextMenu.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIContextMenu.cs new file mode 100644 index 00000000..0d2e85c1 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIContextMenu.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; + +namespace XNode.Examples.RuntimeMathNodes { + public class UGUIContextMenu : MonoBehaviour, IPointerExitHandler { + + public Action<Type, Vector2> onClickSpawn; + public CanvasGroup group; + [HideInInspector] public Node selectedNode; + private Vector2 pos; + + private void Start() { + Close(); + } + + public void OpenAt(Vector2 pos) { + transform.position = pos; + group.alpha = 1; + group.interactable = true; + group.blocksRaycasts = true; + transform.SetAsLastSibling(); + } + + public void Close() { + group.alpha = 0; + group.interactable = false; + group.blocksRaycasts = false; + } + + public void SpawnMathNode() { + SpawnNode(typeof(XNode.Examples.MathNodes.MathNode)); + } + + public void SpawnDisplayNode() { + SpawnNode(typeof(XNode.Examples.MathNodes.DisplayValue)); + } + + public void SpawnVectorNode() { + SpawnNode(typeof(XNode.Examples.MathNodes.Vector)); + } + + private void SpawnNode(Type nodeType) { + Vector2 pos = new Vector2(transform.localPosition.x, -transform.localPosition.y); + onClickSpawn(nodeType, pos); + } + + public void RemoveNode() { + RuntimeMathGraph runtimeMathGraph = GetComponentInParent<RuntimeMathGraph>(); + runtimeMathGraph.graph.RemoveNode(selectedNode); + runtimeMathGraph.Refresh(); + Close(); + } + + public void OnPointerExit(PointerEventData eventData) { + Close(); + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIContextMenu.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIContextMenu.cs.meta new file mode 100644 index 00000000..a68a31d4 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIContextMenu.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 402b5de5d75c05445994c83bba07d273 +timeCreated: 1526035634 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIPort.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIPort.cs new file mode 100644 index 00000000..1ed72589 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIPort.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; +using XNode; + +namespace XNode.Examples.RuntimeMathNodes { + public class UGUIPort : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler { + + public string fieldName; + [HideInInspector] public XNode.Node node; + + private NodePort port; + private Connection tempConnection; + private NodePort startPort; + private UGUIPort tempHovered; + private RuntimeMathGraph graph; + private Vector2 startPos; + private List<Connection> connections = new List<Connection>(); + + void Start() { + port = node.GetPort(fieldName); + graph = GetComponentInParent<RuntimeMathGraph>(); + if (port.IsOutput && port.IsConnected) { + for (int i = 0; i < port.ConnectionCount; i++) { + AddConnection(); + } + } + } + + void Reset() { + fieldName = name; + } + + private void OnDestroy() { + // Also destroy connections + for (int i = connections.Count - 1; i >= 0; i--) { + Destroy(connections[i].gameObject); + } + connections.Clear(); + } + + public void UpdateConnectionTransforms() { + if (port.IsInput) return; + + while (connections.Count < port.ConnectionCount) AddConnection(); + while (connections.Count > port.ConnectionCount) { + Destroy(connections[0].gameObject); + connections.RemoveAt(0); + } + + // Loop through connections + for (int i = 0; i < port.ConnectionCount; i++) { + NodePort other = port.GetConnection(i); + UGUIMathBaseNode otherNode = graph.GetRuntimeNode(other.node); + if (!otherNode) Debug.LogWarning(other.node.name + " node not found", this); + Transform port2 = otherNode.GetPort(other.fieldName).transform; + if (!port2) Debug.LogWarning(other.fieldName + " not found", this); + connections[i].SetPosition(transform.position, port2.position); + } + } + + private void AddConnection() { + Connection connection = Instantiate(graph.runtimeConnectionPrefab); + connection.transform.SetParent(graph.scrollRect.content); + connections.Add(connection); + } + + public void OnBeginDrag(PointerEventData eventData) { + if (port.IsOutput) { + tempConnection = Instantiate(graph.runtimeConnectionPrefab); + tempConnection.transform.SetParent(graph.scrollRect.content); + tempConnection.SetPosition(transform.position, eventData.position); + startPos = transform.position; + startPort = port; + } else { + if (port.IsConnected) { + NodePort output = port.Connection; + Debug.Log("has " + port.ConnectionCount + " connections"); + Debug.Log(port.GetConnection(0)); + UGUIMathBaseNode otherNode = graph.GetRuntimeNode(output.node); + UGUIPort otherUGUIPort = otherNode.GetPort(output.fieldName); + Debug.Log("Disconnect"); + output.Disconnect(port); + tempConnection = Instantiate(graph.runtimeConnectionPrefab); + tempConnection.transform.SetParent(graph.scrollRect.content); + tempConnection.SetPosition(otherUGUIPort.transform.position, eventData.position); + startPos = otherUGUIPort.transform.position; + startPort = otherUGUIPort.port; + graph.GetRuntimeNode(node).UpdateGUI(); + } + } + } + + public void OnDrag(PointerEventData eventData) { + if (tempConnection == null) return; + UGUIPort otherPort = FindPortInStack(eventData.hovered); + tempHovered = otherPort; + tempConnection.SetPosition(startPos, eventData.position); + } + + public void OnEndDrag(PointerEventData eventData) { + if (tempConnection == null) return; + if (tempHovered) { + startPort.Connect(tempHovered.port); + graph.GetRuntimeNode(tempHovered.node).UpdateGUI(); + } + Destroy(tempConnection.gameObject); + } + + public UGUIPort FindPortInStack(List<GameObject> stack) { + for (int i = 0; i < stack.Count; i++) { + UGUIPort port = stack[i].GetComponent<UGUIPort>(); + if (port) return port; + } + return null; + } + + public void OnPointerEnter(PointerEventData eventData) { + graph.tooltip.Show(); + object obj = node.GetInputValue<object>(port.fieldName, null); + if (obj != null) graph.tooltip.label.text = obj.ToString(); + else graph.tooltip.label.text = "n/a"; + } + + public void OnPointerExit(PointerEventData eventData) { + graph.tooltip.Hide(); + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIPort.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIPort.cs.meta new file mode 100644 index 00000000..15b8f2f6 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUIPort.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 76c005fc401e40f42b1e76d5ffc80292 +timeCreated: 1526084889 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUITooltip.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUITooltip.cs new file mode 100644 index 00000000..fd61a329 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUITooltip.cs @@ -0,0 +1,45 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +namespace XNode.Examples.RuntimeMathNodes { + public class UGUITooltip : MonoBehaviour { + public CanvasGroup group; + public Text label; + private bool show; + private RuntimeMathGraph graph; + + private void Awake() { + graph = GetComponentInParent<RuntimeMathGraph>(); + } + + private void Start() { + Hide(); + } + + private void Update() { + if (show) UpdatePosition(); + } + + public void Show() { + show = true; + group.alpha = 1; + UpdatePosition(); + transform.SetAsLastSibling(); + } + + public void Hide() { + show = false; + group.alpha = 0; + } + + private void UpdatePosition() { + Vector2 pos; + RectTransform rect = graph.scrollRect.content.transform as RectTransform; + Camera cam = graph.gameObject.GetComponentInParent<Canvas>().worldCamera; + RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, Input.mousePosition, cam, out pos); + transform.localPosition = pos; + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUITooltip.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUITooltip.cs.meta new file mode 100644 index 00000000..4490bf78 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/UGUITooltip.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 6ea6060c58096cd4d8c959a427a9b059 +timeCreated: 1526123957 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |