NodeEditorWindow.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEditor.Callbacks;
  4. using UnityEngine;
  5. using System;
  6. using System.Linq;
  7. using System.Runtime.Serialization;
  8. using XNode;
  9. using Object = UnityEngine.Object;
  10. namespace XNodeEditor
  11. {
  12. [InitializeOnLoad]
  13. public partial class NodeEditorWindow : EditorWindow
  14. {
  15. public static NodeEditorWindow current;
  16. /// <summary> Stores node positions for all nodePorts. </summary>
  17. public Dictionary<XNode.NodePort, Rect> portConnectionPoints
  18. {
  19. get { return _portConnectionPoints; }
  20. }
  21. private Dictionary<XNode.NodePort, Rect> _portConnectionPoints = new Dictionary<XNode.NodePort, Rect>();
  22. [SerializeField] private NodePortReference[] _references = new NodePortReference[0];
  23. [SerializeField] private Rect[] _rects = new Rect[0];
  24. private Func<bool> isDocked
  25. {
  26. get
  27. {
  28. if (_isDocked == null) _isDocked = this.GetIsDockedDelegate();
  29. return _isDocked;
  30. }
  31. }
  32. private Func<bool> _isDocked;
  33. [System.Serializable]
  34. private class NodePortReference
  35. {
  36. [SerializeField] private XNode.Node _node;
  37. [SerializeField] private string _name;
  38. public NodePortReference(XNode.NodePort nodePort)
  39. {
  40. _node = nodePort.node;
  41. _name = nodePort.fieldName;
  42. }
  43. public XNode.NodePort GetNodePort()
  44. {
  45. if (_node == null)
  46. {
  47. return null;
  48. }
  49. return _node.GetPort(_name);
  50. }
  51. }
  52. private void OnDisable()
  53. {
  54. // Cache portConnectionPoints before serialization starts
  55. int count = portConnectionPoints.Count;
  56. _references = new NodePortReference[count];
  57. _rects = new Rect[count];
  58. int index = 0;
  59. foreach (var portConnectionPoint in portConnectionPoints)
  60. {
  61. _references[index] = new NodePortReference(portConnectionPoint.Key);
  62. _rects[index] = portConnectionPoint.Value;
  63. index++;
  64. }
  65. this.graphEditor.OnDisable();
  66. }
  67. private void OnEnable()
  68. {
  69. // Reload portConnectionPoints if there are any
  70. int length = _references.Length;
  71. if (length == _rects.Length)
  72. {
  73. for (int i = 0; i < length; i++)
  74. {
  75. XNode.NodePort nodePort = _references[i].GetNodePort();
  76. if (nodePort != null)
  77. _portConnectionPoints.Add(nodePort, _rects[i]);
  78. }
  79. }
  80. }
  81. public Dictionary<XNode.Node, Vector2> nodeSizes
  82. {
  83. get { return _nodeSizes; }
  84. }
  85. private Dictionary<XNode.Node, Vector2> _nodeSizes = new Dictionary<XNode.Node, Vector2>();
  86. public XNode.NodeGraph graph;
  87. public Vector2 panOffset
  88. {
  89. get { return _panOffset; }
  90. set
  91. {
  92. _panOffset = value;
  93. Repaint();
  94. }
  95. }
  96. private Vector2 _panOffset;
  97. public float zoom
  98. {
  99. get { return _zoom; }
  100. set
  101. {
  102. _zoom = Mathf.Clamp(value, NodeEditorPreferences.GetSettings().minZoom, NodeEditorPreferences.GetSettings().maxZoom);
  103. Repaint();
  104. }
  105. }
  106. private float _zoom = 1;
  107. void OnFocus()
  108. {
  109. current = this;
  110. ValidateGraphEditor();
  111. if (graphEditor != null)
  112. {
  113. graphEditor.OnWindowFocus();
  114. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  115. }
  116. dragThreshold = Math.Max(1f, Screen.width / 1000f);
  117. }
  118. void OnLostFocus()
  119. {
  120. if (graphEditor != null) graphEditor.OnWindowFocusLost();
  121. }
  122. //
  123. // [InitializeOnLoadMethod]
  124. // private static void OnLoad()
  125. // {
  126. // Selection.selectionChanged -= OnSelectionChanged;
  127. // Selection.selectionChanged += OnSelectionChanged;
  128. // }
  129. //
  130. // /// <summary> Handle Selection Change events</summary>
  131. // private static void OnSelectionChanged()
  132. // {
  133. // XNode.NodeGraph nodeGraph = Selection.activeObject as XNode.NodeGraph;
  134. // if (nodeGraph && !AssetDatabase.Contains(nodeGraph))
  135. // {
  136. // Open(nodeGraph);
  137. // }
  138. // }
  139. /// <summary> Make sure the graph editor is assigned and to the right object </summary>
  140. private void ValidateGraphEditor()
  141. {
  142. NodeGraphEditor graphEditor = NodeGraphEditor.GetEditor(graph, this);
  143. if (this.graphEditor != graphEditor && graphEditor != null)
  144. {
  145. this.graphEditor = graphEditor;
  146. graphEditor.OnOpen();
  147. }
  148. }
  149. /// <summary> Create editor window </summary>
  150. public static NodeEditorWindow Init()
  151. {
  152. NodeEditorWindow w = CreateInstance<NodeEditorWindow>();
  153. w.titleContent = new GUIContent("xNode");
  154. w.wantsMouseMove = true;
  155. w.Show();
  156. return w;
  157. }
  158. public void Save()
  159. {
  160. if (AssetDatabase.Contains(graph))
  161. {
  162. EditorUtility.SetDirty(graph);
  163. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  164. }
  165. else SaveAs();
  166. }
  167. public void SaveAs()
  168. {
  169. string path = EditorUtility.SaveFilePanelInProject("Save NodeGraph", "NewNodeGraph", "asset", "");
  170. if (string.IsNullOrEmpty(path)) return;
  171. else
  172. {
  173. XNode.NodeGraph existingGraph = AssetDatabase.LoadAssetAtPath<XNode.NodeGraph>(path);
  174. if (existingGraph != null) AssetDatabase.DeleteAsset(path);
  175. AssetDatabase.CreateAsset(graph, path);
  176. EditorUtility.SetDirty(graph);
  177. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  178. }
  179. }
  180. private void DraggableWindow(int windowID)
  181. {
  182. GUI.DragWindow();
  183. }
  184. public Vector2 WindowToGridPosition(Vector2 windowPosition)
  185. {
  186. return (windowPosition - (position.size * 0.5f) - (panOffset / zoom)) * zoom;
  187. }
  188. public Vector2 GridToWindowPosition(Vector2 gridPosition)
  189. {
  190. return (position.size * 0.5f) + (panOffset / zoom) + (gridPosition / zoom);
  191. }
  192. public Rect GridToWindowRectNoClipped(Rect gridRect)
  193. {
  194. gridRect.position = GridToWindowPositionNoClipped(gridRect.position);
  195. return gridRect;
  196. }
  197. public Rect GridToWindowRect(Rect gridRect)
  198. {
  199. gridRect.position = GridToWindowPosition(gridRect.position);
  200. gridRect.size /= zoom;
  201. return gridRect;
  202. }
  203. public Vector2 GridToWindowPositionNoClipped(Vector2 gridPosition)
  204. {
  205. Vector2 center = position.size * 0.5f;
  206. // UI Sharpness complete fix - Round final offset not panOffset
  207. float xOffset = Mathf.Round(center.x * zoom + (panOffset.x + gridPosition.x));
  208. float yOffset = Mathf.Round(center.y * zoom + (panOffset.y + gridPosition.y));
  209. return new Vector2(xOffset, yOffset);
  210. }
  211. private List<Node> _selectedNodes = new List<Node>();
  212. public void SelectNode(XNode.Node node, bool add)
  213. {
  214. if (!add)
  215. {
  216. _selectedNodes.Clear();
  217. }
  218. if (!_selectedNodes.Contains(node))
  219. {
  220. _selectedNodes.Add(node);
  221. }
  222. }
  223. public void DeselectNode(XNode.Node node)
  224. {
  225. if (_selectedNodes.Contains(node))
  226. {
  227. _selectedNodes.Remove(node);
  228. }
  229. }
  230. // 双击Inspector里面的XNode打开
  231. [OnOpenAsset(1)]
  232. public static bool OnOpen(int instanceID, int line)
  233. {
  234. XNode.NodeGraph nodeGraph = EditorUtility.InstanceIDToObject(instanceID) as XNode.NodeGraph;
  235. if (nodeGraph != null)
  236. {
  237. Open(nodeGraph, Selection.activeObject);
  238. return true;
  239. }
  240. return false;
  241. }
  242. /// <summary>Open the provided graph in the NodeEditor</summary>
  243. public static NodeEditorWindow Open(XNode.NodeGraph graph, Object selectionActiveObject = null)
  244. {
  245. if (!graph) return null;
  246. NodeEditorWindow w = GetWindow(typeof(NodeEditorWindow), false, graph.Title(), true) as NodeEditorWindow;
  247. w.wantsMouseMove = true;
  248. w.graph = graph;
  249. w.selectionActiveObject = selectionActiveObject;
  250. return w;
  251. }
  252. /// <summary> Repaint all open NodeEditorWindows. </summary>
  253. public static void RepaintAll()
  254. {
  255. NodeEditorWindow[] windows = Resources.FindObjectsOfTypeAll<NodeEditorWindow>();
  256. for (int i = 0; i < windows.Length; i++)
  257. {
  258. windows[i].Repaint();
  259. }
  260. }
  261. public static void CloseWindow()
  262. {
  263. if (current != null)
  264. {
  265. current.Close();
  266. }
  267. }
  268. }
  269. }