RuntimeMathGraph.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.Examples.MathNodes;
  8. namespace XNode.Examples.RuntimeMathNodes {
  9. public class RuntimeMathGraph : MonoBehaviour, IPointerClickHandler {
  10. [Header("Graph")]
  11. public MathGraph graph;
  12. [Header("Prefabs")]
  13. public XNode.Examples.RuntimeMathNodes.UGUIMathNode runtimeMathNodePrefab;
  14. public XNode.Examples.RuntimeMathNodes.UGUIVector runtimeVectorPrefab;
  15. public XNode.Examples.RuntimeMathNodes.UGUIDisplayValue runtimeDisplayValuePrefab;
  16. public XNode.Examples.RuntimeMathNodes.Connection runtimeConnectionPrefab;
  17. [Header("References")]
  18. public UGUIContextMenu graphContextMenu;
  19. public UGUIContextMenu nodeContextMenu;
  20. public UGUITooltip tooltip;
  21. public ScrollRect scrollRect { get; private set; }
  22. private List<UGUIMathBaseNode> nodes;
  23. private void Awake() {
  24. // Create a clone so we don't modify the original asset
  25. graph = graph.Copy() as MathGraph;
  26. scrollRect = GetComponentInChildren<ScrollRect>();
  27. graphContextMenu.onClickSpawn -= SpawnNode;
  28. graphContextMenu.onClickSpawn += SpawnNode;
  29. }
  30. private void Start() {
  31. SpawnGraph();
  32. }
  33. public void Refresh() {
  34. Clear();
  35. SpawnGraph();
  36. }
  37. public void Clear() {
  38. for (int i = nodes.Count - 1; i >= 0; i--) {
  39. Destroy(nodes[i].gameObject);
  40. }
  41. nodes.Clear();
  42. }
  43. public void SpawnGraph() {
  44. if (nodes != null) nodes.Clear();
  45. else nodes = new List<UGUIMathBaseNode>();
  46. for (int i = 0; i < graph.nodes.Count; i++) {
  47. Node node = graph.nodes[i];
  48. UGUIMathBaseNode runtimeNode = null;
  49. if (node is XNode.Examples.MathNodes.MathNode) {
  50. runtimeNode = Instantiate(runtimeMathNodePrefab);
  51. } else if (node is XNode.Examples.MathNodes.Vector) {
  52. runtimeNode = Instantiate(runtimeVectorPrefab);
  53. } else if (node is XNode.Examples.MathNodes.DisplayValue) {
  54. runtimeNode = Instantiate(runtimeDisplayValuePrefab);
  55. }
  56. runtimeNode.transform.SetParent(scrollRect.content);
  57. runtimeNode.node = node;
  58. runtimeNode.graph = this;
  59. nodes.Add(runtimeNode);
  60. }
  61. }
  62. public UGUIMathBaseNode GetRuntimeNode(Node node) {
  63. for (int i = 0; i < nodes.Count; i++) {
  64. if (nodes[i].node == node) {
  65. return nodes[i];
  66. } else { }
  67. }
  68. return null;
  69. }
  70. public void SpawnNode(Type type, Vector2 position) {
  71. Node node = graph.AddNode(type);
  72. node.name = type.Name;
  73. node.position = position;
  74. Refresh();
  75. }
  76. public void OnPointerClick(PointerEventData eventData) {
  77. if (eventData.button != PointerEventData.InputButton.Right)
  78. return;
  79. graphContextMenu.OpenAt(eventData.position);
  80. }
  81. }
  82. }