summaryrefslogtreecommitdiff
path: root/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs
blob: f1b39b9b344821757cee08d6ce3acccad7629939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
		}
	}
}