NodeEditorGUI.cs 26 KB

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