NodeEditorGUI.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using XNode;
  7. using XNodeEditor.Internal;
  8. using Object = UnityEngine.Object;
  9. namespace XNodeEditor
  10. {
  11. /// <summary> Contains GUI methods </summary>
  12. public partial class NodeEditorWindow
  13. {
  14. public NodeGraphEditor graphEditor;
  15. /// <summary>
  16. /// 这是打开XNode编辑器的对象
  17. /// </summary>
  18. public Object selectionActiveObject;
  19. private List<Node> culledNodes;
  20. /// <summary> 19 if docked, 22 if not </summary>
  21. private int topPadding
  22. {
  23. get { return isDocked() ? 19 : 22; }
  24. }
  25. /// <summary> Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run.</summary>
  26. public event Action onLateGUI;
  27. private static readonly Vector3[] polyLineTempArray = new Vector3[2];
  28. protected virtual void OnGUI()
  29. {
  30. Matrix4x4 m = GUI.matrix;
  31. if (graph == null) return;
  32. ValidateGraphEditor();
  33. Controls();
  34. DrawGrid(position, zoom, panOffset);
  35. DrawConnections();
  36. DrawDraggedConnection();
  37. DrawNodes();
  38. DrawSelectionBox();
  39. DrawTooltip();
  40. graphEditor.OnGUI(selectionActiveObject);
  41. // Run and reset onLateGUI
  42. if (onLateGUI != null)
  43. {
  44. onLateGUI();
  45. onLateGUI = null;
  46. }
  47. GUI.matrix = m;
  48. }
  49. public static void BeginZoomed(Rect rect, float zoom, float topPadding)
  50. {
  51. GUI.EndClip();
  52. GUIUtility.ScaleAroundPivot(Vector2.one / zoom, rect.size * 0.5f);
  53. Vector4 padding = new Vector4(0, topPadding, 0, 0);
  54. padding *= zoom;
  55. GUI.BeginClip(new Rect(-((rect.width * zoom) - rect.width) * 0.5f, -(((rect.height * zoom) - rect.height) * 0.5f) + (topPadding * zoom),
  56. rect.width * zoom,
  57. rect.height * zoom));
  58. }
  59. public static void EndZoomed(Rect rect, float zoom, float topPadding)
  60. {
  61. GUIUtility.ScaleAroundPivot(Vector2.one * zoom, rect.size * 0.5f);
  62. Vector3 offset = new Vector3(
  63. (((rect.width * zoom) - rect.width) * 0.5f),
  64. (((rect.height * zoom) - rect.height) * 0.5f) + (-topPadding * zoom) + topPadding,
  65. 0);
  66. GUI.matrix = Matrix4x4.TRS(offset, Quaternion.identity, Vector3.one);
  67. }
  68. public void DrawGrid(Rect rect, float zoom, Vector2 panOffset)
  69. {
  70. rect.position = Vector2.zero;
  71. Vector2 center = rect.size / 2f;
  72. Texture2D gridTex = graphEditor.GetGridTexture();
  73. Texture2D crossTex = graphEditor.GetSecondaryGridTexture();
  74. // Offset from origin in tile units
  75. float xOffset = -(center.x * zoom + panOffset.x) / gridTex.width;
  76. float yOffset = ((center.y - rect.size.y) * zoom + panOffset.y) / gridTex.height;
  77. Vector2 tileOffset = new Vector2(xOffset, yOffset);
  78. // Amount of tiles
  79. float tileAmountX = Mathf.Round(rect.size.x * zoom) / gridTex.width;
  80. float tileAmountY = Mathf.Round(rect.size.y * zoom) / gridTex.height;
  81. Vector2 tileAmount = new Vector2(tileAmountX, tileAmountY);
  82. // Draw tiled background
  83. GUI.DrawTextureWithTexCoords(rect, gridTex, new Rect(tileOffset, tileAmount));
  84. GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f, 0.5f), tileAmount));
  85. }
  86. public void DrawSelectionBox()
  87. {
  88. if (currentActivity == NodeActivity.DragGrid)
  89. {
  90. Vector2 curPos = WindowToGridPosition(Event.current.mousePosition);
  91. Vector2 size = curPos - dragBoxStart;
  92. Rect r = new Rect(dragBoxStart, size);
  93. r.position = GridToWindowPosition(r.position);
  94. r.size /= zoom;
  95. Handles.DrawSolidRectangleWithOutline(r, new Color(0, 0, 0, 0.1f), new Color(1, 1, 1, 0.6f));
  96. }
  97. }
  98. public static bool DropdownButton(string name, float width)
  99. {
  100. return GUILayout.Button(name, EditorStyles.toolbarDropDown, GUILayout.Width(width));
  101. }
  102. /// <summary> Show right-click context menu for hovered reroute </summary>
  103. void ShowRerouteContextMenu(RerouteReference reroute)
  104. {
  105. GenericMenu contextMenu = new GenericMenu();
  106. contextMenu.AddItem(new GUIContent("Remove"), false, () => reroute.RemovePoint());
  107. contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
  108. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  109. }
  110. /// <summary> Show right-click context menu for hovered port </summary>
  111. void ShowPortContextMenu(NodePort hoveredPort)
  112. {
  113. GenericMenu contextMenu = new GenericMenu();
  114. contextMenu.AddItem(new GUIContent("Clear Connections"), false, () => hoveredPort.ClearConnections());
  115. contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
  116. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  117. }
  118. static Vector2 CalculateBezierPoint(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
  119. {
  120. float u = 1 - t;
  121. float tt = t * t, uu = u * u;
  122. float uuu = uu * u, ttt = tt * t;
  123. return new Vector2(
  124. (uuu * p0.x) + (3 * uu * t * p1.x) + (3 * u * tt * p2.x) + (ttt * p3.x),
  125. (uuu * p0.y) + (3 * uu * t * p1.y) + (3 * u * tt * p2.y) + (ttt * p3.y)
  126. );
  127. }
  128. /// <summary> Draws a line segment without allocating temporary arrays </summary>
  129. static void DrawAAPolyLineNonAlloc(float thickness, Vector2 p0, Vector2 p1)
  130. {
  131. polyLineTempArray[0].x = p0.x;
  132. polyLineTempArray[0].y = p0.y;
  133. polyLineTempArray[1].x = p1.x;
  134. polyLineTempArray[1].y = p1.y;
  135. Handles.DrawAAPolyLine(thickness, polyLineTempArray);
  136. }
  137. /// <summary> Draw a bezier from output to input in grid coordinates </summary>
  138. public void DrawNoodle(Gradient gradient, NoodlePath path, NoodleStroke stroke, float thickness, List<Vector2> gridPoints)
  139. {
  140. // convert grid points to window points
  141. for (int i = 0; i < gridPoints.Count; ++i)
  142. gridPoints[i] = GridToWindowPosition(gridPoints[i]);
  143. Color originalHandlesColor = Handles.color;
  144. Handles.color = gradient.Evaluate(0f);
  145. int length = gridPoints.Count;
  146. switch (path)
  147. {
  148. case NoodlePath.Curvy:
  149. Vector2 outputTangent = Vector2.right;
  150. for (int i = 0; i < length - 1; i++)
  151. {
  152. Vector2 inputTangent;
  153. // Cached most variables that repeat themselves here to avoid so many indexer calls :p
  154. Vector2 point_a = gridPoints[i];
  155. Vector2 point_b = gridPoints[i + 1];
  156. float dist_ab = Vector2.Distance(point_a, point_b);
  157. if (i == 0) outputTangent = zoom * dist_ab * 0.01f * Vector2.right;
  158. if (i < length - 2)
  159. {
  160. Vector2 point_c = gridPoints[i + 2];
  161. Vector2 ab = (point_b - point_a).normalized;
  162. Vector2 cb = (point_b - point_c).normalized;
  163. Vector2 ac = (point_c - point_a).normalized;
  164. Vector2 p = (ab + cb) * 0.5f;
  165. float tangentLength = (dist_ab + Vector2.Distance(point_b, point_c)) * 0.005f * zoom;
  166. float side = ((ac.x * (point_b.y - point_a.y)) - (ac.y * (point_b.x - point_a.x)));
  167. p = tangentLength * Mathf.Sign(side) * new Vector2(-p.y, p.x);
  168. inputTangent = p;
  169. }
  170. else
  171. {
  172. inputTangent = zoom * dist_ab * 0.01f * Vector2.left;
  173. }
  174. // Calculates the tangents for the bezier's curves.
  175. float zoomCoef = 50 / zoom;
  176. Vector2 tangent_a = point_a + outputTangent * zoomCoef;
  177. Vector2 tangent_b = point_b + inputTangent * zoomCoef;
  178. // Hover effect.
  179. int division = Mathf.RoundToInt(.2f * dist_ab) + 3;
  180. // Coloring and bezier drawing.
  181. int draw = 0;
  182. Vector2 bezierPrevious = point_a;
  183. for (int j = 1; j <= division; ++j)
  184. {
  185. if (stroke == NoodleStroke.Dashed)
  186. {
  187. draw++;
  188. if (draw >= 2) draw = -2;
  189. if (draw < 0) continue;
  190. if (draw == 0) bezierPrevious = CalculateBezierPoint(point_a, tangent_a, tangent_b, point_b, (j - 1f) / (float) division);
  191. }
  192. if (i == length - 2)
  193. Handles.color = gradient.Evaluate((j + 1f) / division);
  194. Vector2 bezierNext = CalculateBezierPoint(point_a, tangent_a, tangent_b, point_b, j / (float) division);
  195. DrawAAPolyLineNonAlloc(thickness, bezierPrevious, bezierNext);
  196. bezierPrevious = bezierNext;
  197. }
  198. outputTangent = -inputTangent;
  199. }
  200. break;
  201. case NoodlePath.Straight:
  202. for (int i = 0; i < length - 1; i++)
  203. {
  204. Vector2 point_a = gridPoints[i];
  205. Vector2 point_b = gridPoints[i + 1];
  206. // Draws the line with the coloring.
  207. Vector2 prev_point = point_a;
  208. // Approximately one segment per 5 pixels
  209. int segments = (int) Vector2.Distance(point_a, point_b) / 5;
  210. segments = Math.Max(segments, 1);
  211. int draw = 0;
  212. for (int j = 0; j <= segments; j++)
  213. {
  214. draw++;
  215. float t = j / (float) segments;
  216. Vector2 lerp = Vector2.Lerp(point_a, point_b, t);
  217. if (draw > 0)
  218. {
  219. if (i == length - 2) Handles.color = gradient.Evaluate(t);
  220. DrawAAPolyLineNonAlloc(thickness, prev_point, lerp);
  221. }
  222. prev_point = lerp;
  223. if (stroke == NoodleStroke.Dashed && draw >= 2) draw = -2;
  224. }
  225. }
  226. break;
  227. case NoodlePath.Angled:
  228. for (int i = 0; i < length - 1; i++)
  229. {
  230. if (i == length - 1) continue; // Skip last index
  231. if (gridPoints[i].x <= gridPoints[i + 1].x - (50 / zoom))
  232. {
  233. float midpoint = (gridPoints[i].x + gridPoints[i + 1].x) * 0.5f;
  234. Vector2 start_1 = gridPoints[i];
  235. Vector2 end_1 = gridPoints[i + 1];
  236. start_1.x = midpoint;
  237. end_1.x = midpoint;
  238. if (i == length - 2)
  239. {
  240. DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1);
  241. Handles.color = gradient.Evaluate(0.5f);
  242. DrawAAPolyLineNonAlloc(thickness, start_1, end_1);
  243. Handles.color = gradient.Evaluate(1f);
  244. DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]);
  245. }
  246. else
  247. {
  248. DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1);
  249. DrawAAPolyLineNonAlloc(thickness, start_1, end_1);
  250. DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]);
  251. }
  252. }
  253. else
  254. {
  255. float midpoint = (gridPoints[i].y + gridPoints[i + 1].y) * 0.5f;
  256. Vector2 start_1 = gridPoints[i];
  257. Vector2 end_1 = gridPoints[i + 1];
  258. start_1.x += 25 / zoom;
  259. end_1.x -= 25 / zoom;
  260. Vector2 start_2 = start_1;
  261. Vector2 end_2 = end_1;
  262. start_2.y = midpoint;
  263. end_2.y = midpoint;
  264. if (i == length - 2)
  265. {
  266. DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1);
  267. Handles.color = gradient.Evaluate(0.25f);
  268. DrawAAPolyLineNonAlloc(thickness, start_1, start_2);
  269. Handles.color = gradient.Evaluate(0.5f);
  270. DrawAAPolyLineNonAlloc(thickness, start_2, end_2);
  271. Handles.color = gradient.Evaluate(0.75f);
  272. DrawAAPolyLineNonAlloc(thickness, end_2, end_1);
  273. Handles.color = gradient.Evaluate(1f);
  274. DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]);
  275. }
  276. else
  277. {
  278. DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1);
  279. DrawAAPolyLineNonAlloc(thickness, start_1, start_2);
  280. DrawAAPolyLineNonAlloc(thickness, start_2, end_2);
  281. DrawAAPolyLineNonAlloc(thickness, end_2, end_1);
  282. DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]);
  283. }
  284. }
  285. }
  286. break;
  287. case NoodlePath.ShaderLab:
  288. Vector2 start = gridPoints[0];
  289. Vector2 end = gridPoints[length - 1];
  290. //Modify first and last point in array so we can loop trough them nicely.
  291. gridPoints[0] = gridPoints[0] + Vector2.right * (20 / zoom);
  292. gridPoints[length - 1] = gridPoints[length - 1] + Vector2.left * (20 / zoom);
  293. //Draw first vertical lines going out from nodes
  294. Handles.color = gradient.Evaluate(0f);
  295. DrawAAPolyLineNonAlloc(thickness, start, gridPoints[0]);
  296. Handles.color = gradient.Evaluate(1f);
  297. DrawAAPolyLineNonAlloc(thickness, end, gridPoints[length - 1]);
  298. for (int i = 0; i < length - 1; i++)
  299. {
  300. Vector2 point_a = gridPoints[i];
  301. Vector2 point_b = gridPoints[i + 1];
  302. // Draws the line with the coloring.
  303. Vector2 prev_point = point_a;
  304. // Approximately one segment per 5 pixels
  305. int segments = (int) Vector2.Distance(point_a, point_b) / 5;
  306. segments = Math.Max(segments, 1);
  307. int draw = 0;
  308. for (int j = 0; j <= segments; j++)
  309. {
  310. draw++;
  311. float t = j / (float) segments;
  312. Vector2 lerp = Vector2.Lerp(point_a, point_b, t);
  313. if (draw > 0)
  314. {
  315. if (i == length - 2) Handles.color = gradient.Evaluate(t);
  316. DrawAAPolyLineNonAlloc(thickness, prev_point, lerp);
  317. }
  318. prev_point = lerp;
  319. if (stroke == NoodleStroke.Dashed && draw >= 2) draw = -2;
  320. }
  321. }
  322. gridPoints[0] = start;
  323. gridPoints[length - 1] = end;
  324. break;
  325. }
  326. Handles.color = originalHandlesColor;
  327. }
  328. /// <summary> Draws all connections </summary>
  329. public void DrawConnections()
  330. {
  331. Vector2 mousePos = Event.current.mousePosition;
  332. List<RerouteReference> selection = preBoxSelectionReroute != null ? new List<RerouteReference>(preBoxSelectionReroute) : new List<RerouteReference>();
  333. hoveredReroute = new RerouteReference();
  334. List<Vector2> gridPoints = new List<Vector2>(2);
  335. Color col = GUI.color;
  336. foreach (Node node in graph.nodes)
  337. {
  338. //If a null node is found, return. This can happen if the nodes associated script is deleted. It is currently not possible in Unity to delete a null asset.
  339. if (node == null) continue;
  340. // Draw full connections and output > reroute
  341. foreach (NodePort output in node.Outputs)
  342. {
  343. //Needs cleanup. Null checks are ugly
  344. Rect fromRect;
  345. if (!_portConnectionPoints.TryGetValue(output, out fromRect)) continue;
  346. Color portColor = graphEditor.GetPortColor(output);
  347. for (int k = 0; k < output.ConnectionCount; k++)
  348. {
  349. NodePort input = output.GetConnection(k);
  350. Gradient noodleGradient = graphEditor.GetNoodleGradient(output, input);
  351. float noodleThickness = graphEditor.GetNoodleThickness(output, input);
  352. NoodlePath noodlePath = graphEditor.GetNoodlePath(output, input);
  353. NoodleStroke noodleStroke = graphEditor.GetNoodleStroke(output, input);
  354. // Error handling
  355. if (input == null) continue; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return.
  356. if (!input.IsConnectedTo(output)) input.Connect(output);
  357. Rect toRect;
  358. if (!_portConnectionPoints.TryGetValue(input, out toRect)) continue;
  359. List<Vector2> reroutePoints = output.GetReroutePoints(k);
  360. gridPoints.Clear();
  361. gridPoints.Add(fromRect.center);
  362. gridPoints.AddRange(reroutePoints);
  363. gridPoints.Add(toRect.center);
  364. DrawNoodle(noodleGradient, noodlePath, noodleStroke, noodleThickness, gridPoints);
  365. // Loop through reroute points again and draw the points
  366. for (int i = 0; i < reroutePoints.Count; i++)
  367. {
  368. RerouteReference rerouteRef = new RerouteReference(output, k, i);
  369. // Draw reroute point at position
  370. Rect rect = new Rect(reroutePoints[i], new Vector2(12, 12));
  371. rect.position = new Vector2(rect.position.x - 6, rect.position.y - 6);
  372. rect = GridToWindowRect(rect);
  373. // Draw selected reroute points with an outline
  374. if (selectedReroutes.Contains(rerouteRef))
  375. {
  376. GUI.color = NodeEditorPreferences.GetSettings().highlightColor;
  377. GUI.DrawTexture(rect, NodeEditorResources.dotOuter);
  378. }
  379. GUI.color = portColor;
  380. GUI.DrawTexture(rect, NodeEditorResources.dot);
  381. if (rect.Overlaps(selectionBox)) selection.Add(rerouteRef);
  382. if (rect.Contains(mousePos)) hoveredReroute = rerouteRef;
  383. }
  384. }
  385. }
  386. }
  387. GUI.color = col;
  388. if (Event.current.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) selectedReroutes = selection;
  389. }
  390. private void DrawNodes()
  391. {
  392. Event e = Event.current;
  393. BeginZoomed(position, zoom, topPadding);
  394. Vector2 mousePos = Event.current.mousePosition;
  395. if (e.type != EventType.Layout)
  396. {
  397. hoveredNode = null;
  398. hoveredPort = null;
  399. }
  400. List<Node> preSelection = preBoxSelection != null ? new List<Node>(preBoxSelection) : new List<Node>();
  401. // Selection box stuff
  402. Vector2 boxStartPos = GridToWindowPositionNoClipped(dragBoxStart);
  403. Vector2 boxSize = mousePos - boxStartPos;
  404. if (boxSize.x < 0)
  405. {
  406. boxStartPos.x += boxSize.x;
  407. boxSize.x = Mathf.Abs(boxSize.x);
  408. }
  409. if (boxSize.y < 0)
  410. {
  411. boxStartPos.y += boxSize.y;
  412. boxSize.y = Mathf.Abs(boxSize.y);
  413. }
  414. Rect selectionBox = new Rect(boxStartPos, boxSize);
  415. //Save guiColor so we can revert it
  416. Color guiColor = GUI.color;
  417. List<NodePort> removeEntries = new List<NodePort>();
  418. if (e.type == EventType.Layout) culledNodes = new List<Node>();
  419. for (int n = 0; n < graph.nodes.Count; n++)
  420. {
  421. // Skip null nodes. The user could be in the process of renaming scripts, so removing them at this point is not advisable.
  422. if (graph.nodes[n] == null) continue;
  423. if (n >= graph.nodes.Count) return;
  424. Node node = graph.nodes[n];
  425. // Culling
  426. if (e.type == EventType.Layout)
  427. {
  428. // Cull unselected nodes outside view
  429. if (!Selection.Contains(node) && ShouldBeCulled(node))
  430. {
  431. culledNodes.Add(node);
  432. continue;
  433. }
  434. }
  435. else if (culledNodes.Contains(node)) continue;
  436. if (e.type == EventType.Repaint)
  437. {
  438. removeEntries.Clear();
  439. foreach (var kvp in _portConnectionPoints)
  440. if (kvp.Key.node == node)
  441. removeEntries.Add(kvp.Key);
  442. foreach (var k in removeEntries) _portConnectionPoints.Remove(k);
  443. }
  444. NodeEditor nodeEditor = NodeEditor.GetEditor(node, this);
  445. NodeEditor.portPositions.Clear();
  446. // Set default label width. This is potentially overridden in OnBodyGUI
  447. EditorGUIUtility.labelWidth = 84;
  448. //Get node position
  449. Vector2 nodePos = GridToWindowPositionNoClipped(node.position);
  450. GUILayout.BeginArea(new Rect(nodePos, new Vector2(nodeEditor.GetWidth(), 4000)));
  451. bool selected = _selectedNodes.Contains(graph.nodes[n]);
  452. if (selected)
  453. {
  454. GUIStyle style = new GUIStyle(nodeEditor.GetBodyStyle());
  455. GUIStyle highlightStyle = new GUIStyle(nodeEditor.GetBodyHighlightStyle());
  456. highlightStyle.padding = style.padding;
  457. style.padding = new RectOffset();
  458. GUI.color = nodeEditor.GetTint();
  459. GUILayout.BeginVertical(style);
  460. GUI.color = NodeEditorPreferences.GetSettings().highlightColor;
  461. GUILayout.BeginVertical(new GUIStyle(highlightStyle));
  462. }
  463. else
  464. {
  465. GUIStyle style = new GUIStyle(nodeEditor.GetBodyStyle());
  466. GUI.color = nodeEditor.GetTint();
  467. GUILayout.BeginVertical(style);
  468. }
  469. GUI.color = guiColor;
  470. EditorGUI.BeginChangeCheck();
  471. //Draw node contents
  472. nodeEditor.OnHeaderGUI();
  473. nodeEditor.OnBodyGUI();
  474. //If user changed a value, notify other scripts through onUpdateNode
  475. if (EditorGUI.EndChangeCheck())
  476. {
  477. if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node);
  478. EditorUtility.SetDirty(node);
  479. nodeEditor.serializedObject.ApplyModifiedProperties();
  480. }
  481. GUILayout.EndVertical();
  482. //Cache data about the node for next frame
  483. if (e.type == EventType.Repaint)
  484. {
  485. Vector2 size = GUILayoutUtility.GetLastRect().size;
  486. if (nodeSizes.ContainsKey(node)) nodeSizes[node] = size;
  487. else nodeSizes.Add(node, size);
  488. foreach (var kvp in NodeEditor.portPositions)
  489. {
  490. Vector2 portHandlePos = kvp.Value;
  491. portHandlePos += node.position;
  492. Rect rect = new Rect(portHandlePos.x - 8, portHandlePos.y - 8, 16, 16);
  493. portConnectionPoints[kvp.Key] = rect;
  494. }
  495. }
  496. if (selected) GUILayout.EndVertical();
  497. if (e.type != EventType.Layout)
  498. {
  499. //Check if we are hovering this node
  500. Vector2 nodeSize = GUILayoutUtility.GetLastRect().size;
  501. Rect windowRect = new Rect(nodePos, nodeSize);
  502. if (windowRect.Contains(mousePos)) hoveredNode = node;
  503. //If dragging a selection box, add nodes inside to selection
  504. if (currentActivity == NodeActivity.DragGrid)
  505. {
  506. if (windowRect.Overlaps(selectionBox))
  507. preSelection.Add(node);
  508. }
  509. //Check if we are hovering any of this nodes ports
  510. //Check input ports
  511. foreach (NodePort input in node.Inputs)
  512. {
  513. //Check if port rect is available
  514. if (!portConnectionPoints.ContainsKey(input)) continue;
  515. Rect r = GridToWindowRectNoClipped(portConnectionPoints[input]);
  516. if (r.Contains(mousePos)) hoveredPort = input;
  517. }
  518. //Check all output ports
  519. foreach (NodePort output in node.Outputs)
  520. {
  521. //Check if port rect is available
  522. if (!portConnectionPoints.ContainsKey(output)) continue;
  523. Rect r = GridToWindowRectNoClipped(portConnectionPoints[output]);
  524. if (r.Contains(mousePos)) hoveredPort = output;
  525. }
  526. }
  527. GUILayout.EndArea();
  528. }
  529. if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid)
  530. _selectedNodes = preSelection;
  531. EndZoomed(position, zoom, topPadding);
  532. //If a change in is detected in the selected node, call OnValidate method.
  533. //This is done through reflection because OnValidate is only relevant in editor,
  534. //and thus, the code should not be included in build.
  535. // if (onValidate != null && EditorGUI.EndChangeCheck()) onValidate.Invoke(Selection.activeObject, null);
  536. }
  537. private bool ShouldBeCulled(Node node)
  538. {
  539. Vector2 nodePos = GridToWindowPositionNoClipped(node.position);
  540. if (nodePos.x / _zoom > position.width) return true; // Right
  541. else if (nodePos.y / _zoom > position.height) return true; // Bottom
  542. else if (nodeSizes.ContainsKey(node))
  543. {
  544. Vector2 size = nodeSizes[node];
  545. if (nodePos.x + size.x < 0) return true; // Left
  546. else if (nodePos.y + size.y < 0) return true; // Top
  547. }
  548. return false;
  549. }
  550. private void DrawTooltip()
  551. {
  552. if (hoveredPort != null && NodeEditorPreferences.GetSettings().portTooltips && graphEditor != null)
  553. {
  554. string tooltip = graphEditor.GetPortTooltip(hoveredPort);
  555. if (string.IsNullOrEmpty(tooltip)) return;
  556. GUIContent content = new GUIContent(tooltip);
  557. Vector2 size = NodeEditorResources.styles.tooltip.CalcSize(content);
  558. size.x += 8;
  559. Rect rect = new Rect(Event.current.mousePosition - (size), size);
  560. EditorGUI.LabelField(rect, content, NodeEditorResources.styles.tooltip);
  561. Repaint();
  562. }
  563. }
  564. }
  565. }