From 53f85a5d227763aa5c8374dd177ff7c2bd86adb1 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Sat, 8 Jun 2019 14:25:07 +0200 Subject: [PATCH] Improved zoom with the help of Jeroenimoo0's PR --- Scripts/Editor/NodeEditorGUI.cs | 56 ++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index c9c460f..2bf3945 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -10,10 +10,13 @@ namespace XNodeEditor { public NodeGraphEditor graphEditor; private List selectionCache; private List culledNodes; + /// 19 if docked, 22 if not private int topPadding { get { return isDocked() ? 19 : 22; } } /// Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run. public event Action onLateGUI; + private Matrix4x4 prevGuiMatrix; + private void OnGUI() { Event e = Event.current; Matrix4x4 m = GUI.matrix; @@ -38,24 +41,40 @@ namespace XNodeEditor { GUI.matrix = m; } - public static void BeginZoomed(Rect rect, float zoom, float topPadding) { - GUI.EndClip(); + public void BeginZoomed() { + GUI.EndGroup(); - GUIUtility.ScaleAroundPivot(Vector2.one / zoom, rect.size * 0.5f); - Vector4 padding = new Vector4(0, topPadding, 0, 0); - padding *= zoom; - GUI.BeginClip(new Rect(-((rect.width * zoom) - rect.width) * 0.5f, -(((rect.height * zoom) - rect.height) * 0.5f) + (topPadding * zoom), - rect.width * zoom, - rect.height * zoom)); + Rect position = new Rect(this.position); + position.x = 0; + position.y = topPadding; + + Vector2 topLeft = new Vector2(position.xMin, position.yMin - topPadding); + Rect clippedArea = ScaleSizeBy(position, zoom, topLeft); + GUI.BeginGroup(clippedArea); + + prevGuiMatrix = GUI.matrix; + Matrix4x4 translation = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one); + Matrix4x4 scale = Matrix4x4.Scale(new Vector3(1.0f / zoom, 1.0f / zoom, 1.0f)); + GUI.matrix = translation * scale * translation.inverse * GUI.matrix; } - public static void EndZoomed(Rect rect, float zoom, float topPadding) { - GUIUtility.ScaleAroundPivot(Vector2.one * zoom, rect.size * 0.5f); - Vector3 offset = new Vector3( - (((rect.width * zoom) - rect.width) * 0.5f), - (((rect.height * zoom) - rect.height) * 0.5f) + (-topPadding * zoom) + topPadding, - 0); - GUI.matrix = Matrix4x4.TRS(offset, Quaternion.identity, Vector3.one); + public void EndZoomed() { + GUI.matrix = prevGuiMatrix; + GUI.EndGroup(); + GUI.BeginGroup(new Rect(0.0f, topPadding - (topPadding * zoom), Screen.width, Screen.height)); + } + + public static Rect ScaleSizeBy(Rect rect, float scale, Vector2 pivotPoint) { + Rect result = rect; + result.x -= pivotPoint.x; + result.y -= pivotPoint.y; + result.xMin *= scale; + result.xMax *= scale; + result.yMin *= scale; + result.yMax *= scale; + result.x += pivotPoint.x; + result.y += pivotPoint.y; + return result; } public void DrawGrid(Rect rect, float zoom, Vector2 panOffset) { @@ -136,8 +155,7 @@ namespace XNodeEditor { p = new Vector2(-p.y, p.x) * Mathf.Sign(side) * tangentLength; inputTangent = p; - } - else { + } else { inputTangent = Vector2.left * Vector2.Distance(windowPoints[i], windowPoints[i + 1]) * 0.01f * zoom; } @@ -258,7 +276,7 @@ namespace XNodeEditor { if (onValidate != null) EditorGUI.BeginChangeCheck(); } - BeginZoomed(position, zoom, topPadding); + BeginZoomed(); Vector2 mousePos = Event.current.mousePosition; @@ -389,7 +407,7 @@ namespace XNodeEditor { } if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) Selection.objects = preSelection.ToArray(); - EndZoomed(position, zoom, topPadding); + EndZoomed(); //If a change in is detected in the selected node, call OnValidate method. //This is done through reflection because OnValidate is only relevant in editor,