NodeEditorWindow.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEditor.Callbacks;
  4. using UnityEngine;
  5. using System;
  6. using Object = UnityEngine.Object;
  7. namespace XNodeEditor {
  8. [InitializeOnLoad]
  9. public partial class NodeEditorWindow : EditorWindow {
  10. public static NodeEditorWindow current;
  11. /// <summary> Stores node positions for all nodePorts. </summary>
  12. public Dictionary<XNode.NodePort, Rect> portConnectionPoints { get { return _portConnectionPoints; } }
  13. private Dictionary<XNode.NodePort, Rect> _portConnectionPoints = new Dictionary<XNode.NodePort, Rect>();
  14. [SerializeField] private NodePortReference[] _references = new NodePortReference[0];
  15. [SerializeField] private Rect[] _rects = new Rect[0];
  16. private Func<bool> isDocked {
  17. get {
  18. if (_isDocked == null) _isDocked = this.GetIsDockedDelegate();
  19. return _isDocked;
  20. }
  21. }
  22. private Func<bool> _isDocked;
  23. [System.Serializable] private class NodePortReference {
  24. [SerializeField] private XNode.Node _node;
  25. [SerializeField] private string _name;
  26. public NodePortReference(XNode.NodePort nodePort) {
  27. _node = nodePort.node;
  28. _name = nodePort.fieldName;
  29. }
  30. public XNode.NodePort GetNodePort() {
  31. if (_node == null) {
  32. return null;
  33. }
  34. return _node.GetPort(_name);
  35. }
  36. }
  37. private void OnDisable() {
  38. // Cache portConnectionPoints before serialization starts
  39. int count = portConnectionPoints.Count;
  40. _references = new NodePortReference[count];
  41. _rects = new Rect[count];
  42. int index = 0;
  43. foreach (var portConnectionPoint in portConnectionPoints) {
  44. _references[index] = new NodePortReference(portConnectionPoint.Key);
  45. _rects[index] = portConnectionPoint.Value;
  46. index++;
  47. }
  48. }
  49. private void OnEnable() {
  50. // Reload portConnectionPoints if there are any
  51. int length = _references.Length;
  52. if (length == _rects.Length) {
  53. for (int i = 0; i < length; i++) {
  54. XNode.NodePort nodePort = _references[i].GetNodePort();
  55. if (nodePort != null)
  56. _portConnectionPoints.Add(nodePort, _rects[i]);
  57. }
  58. }
  59. }
  60. public Dictionary<XNode.Node, Vector2> nodeSizes { get { return _nodeSizes; } }
  61. private Dictionary<XNode.Node, Vector2> _nodeSizes = new Dictionary<XNode.Node, Vector2>();
  62. public XNode.NodeGraph graph;
  63. public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } }
  64. private Vector2 _panOffset;
  65. public float zoom { get { return _zoom; } set { _zoom = Mathf.Clamp(value, NodeEditorPreferences.GetSettings().minZoom, NodeEditorPreferences.GetSettings().maxZoom); Repaint(); } }
  66. private float _zoom = 1;
  67. void OnFocus() {
  68. current = this;
  69. ValidateGraphEditor();
  70. if (graphEditor != null && NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  71. }
  72. [InitializeOnLoadMethod]
  73. private static void OnLoad() {
  74. Selection.selectionChanged -= OnSelectionChanged;
  75. Selection.selectionChanged += OnSelectionChanged;
  76. }
  77. /// <summary> Handle Selection Change events</summary>
  78. private static void OnSelectionChanged() {
  79. XNode.NodeGraph nodeGraph = Selection.activeObject as XNode.NodeGraph;
  80. if (nodeGraph && !AssetDatabase.Contains(nodeGraph)) {
  81. Open(nodeGraph);
  82. }
  83. }
  84. /// <summary> Make sure the graph editor is assigned and to the right object </summary>
  85. private void ValidateGraphEditor() {
  86. NodeGraphEditor graphEditor = NodeGraphEditor.GetEditor(graph, this);
  87. if (this.graphEditor != graphEditor) {
  88. this.graphEditor = graphEditor;
  89. graphEditor.OnOpen();
  90. }
  91. }
  92. /// <summary> Create editor window </summary>
  93. public static NodeEditorWindow Init() {
  94. NodeEditorWindow w = CreateInstance<NodeEditorWindow>();
  95. w.titleContent = new GUIContent("xNode");
  96. w.wantsMouseMove = true;
  97. w.Show();
  98. return w;
  99. }
  100. public void Save() {
  101. if (AssetDatabase.Contains(graph)) {
  102. EditorUtility.SetDirty(graph);
  103. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  104. } else SaveAs();
  105. }
  106. public void SaveAs() {
  107. string path = EditorUtility.SaveFilePanelInProject("Save NodeGraph", "NewNodeGraph", "asset", "");
  108. if (string.IsNullOrEmpty(path)) return;
  109. else {
  110. XNode.NodeGraph existingGraph = AssetDatabase.LoadAssetAtPath<XNode.NodeGraph>(path);
  111. if (existingGraph != null) AssetDatabase.DeleteAsset(path);
  112. AssetDatabase.CreateAsset(graph, path);
  113. EditorUtility.SetDirty(graph);
  114. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  115. }
  116. }
  117. private void DraggableWindow(int windowID) {
  118. GUI.DragWindow();
  119. }
  120. public Vector2 WindowToGridPosition(Vector2 windowPosition) {
  121. return (windowPosition - (position.size * 0.5f) - (panOffset / zoom)) * zoom;
  122. }
  123. public Vector2 GridToWindowPosition(Vector2 gridPosition) {
  124. return (position.size * 0.5f) + (panOffset / zoom) + (gridPosition / zoom);
  125. }
  126. public Rect GridToWindowRectNoClipped(Rect gridRect) {
  127. gridRect.position = GridToWindowPositionNoClipped(gridRect.position);
  128. return gridRect;
  129. }
  130. public Rect GridToWindowRect(Rect gridRect) {
  131. gridRect.position = GridToWindowPosition(gridRect.position);
  132. gridRect.size /= zoom;
  133. return gridRect;
  134. }
  135. public Vector2 GridToWindowPositionNoClipped(Vector2 gridPosition) {
  136. Vector2 center = position.size * 0.5f;
  137. // UI Sharpness complete fix - Round final offset not panOffset
  138. float xOffset = Mathf.Round(center.x * zoom + (panOffset.x + gridPosition.x));
  139. float yOffset = Mathf.Round(center.y * zoom + (panOffset.y + gridPosition.y));
  140. return new Vector2(xOffset, yOffset);
  141. }
  142. public void SelectNode(XNode.Node node, bool add) {
  143. if (add) {
  144. List<Object> selection = new List<Object>(Selection.objects);
  145. selection.Add(node);
  146. Selection.objects = selection.ToArray();
  147. } else Selection.objects = new Object[] { node };
  148. }
  149. public void DeselectNode(XNode.Node node) {
  150. List<Object> selection = new List<Object>(Selection.objects);
  151. selection.Remove(node);
  152. Selection.objects = selection.ToArray();
  153. }
  154. [OnOpenAsset(0)]
  155. public static bool OnOpen(int instanceID, int line) {
  156. XNode.NodeGraph nodeGraph = EditorUtility.InstanceIDToObject(instanceID) as XNode.NodeGraph;
  157. if (nodeGraph != null) {
  158. Open(nodeGraph);
  159. return true;
  160. }
  161. return false;
  162. }
  163. /// <summary>Open the provided graph in the NodeEditor</summary>
  164. public static void Open(XNode.NodeGraph graph) {
  165. if (!graph) return;
  166. NodeEditorWindow w = GetWindow(typeof(NodeEditorWindow), false, "xNode", true) as NodeEditorWindow;
  167. w.wantsMouseMove = true;
  168. w.graph = graph;
  169. }
  170. /// <summary> Repaint all open NodeEditorWindows. </summary>
  171. public static void RepaintAll() {
  172. NodeEditorWindow[] windows = Resources.FindObjectsOfTypeAll<NodeEditorWindow>();
  173. for (int i = 0; i < windows.Length; i++) {
  174. windows[i].Repaint();
  175. }
  176. }
  177. }
  178. }