Skip to content

Commit

Permalink
Improved zoom with the help of Jeroenimoo0's PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Siccity committed Jun 8, 2019
1 parent 0c1ae48 commit 53f85a5
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions Scripts/Editor/NodeEditorGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ public partial class NodeEditorWindow {
public NodeGraphEditor graphEditor;
private List<UnityEngine.Object> selectionCache;
private List<XNode.Node> culledNodes;
/// <summary> 19 if docked, 22 if not </summary>
private int topPadding { get { return isDocked() ? 19 : 22; } }
/// <summary> Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run.</summary>
public event Action onLateGUI;

private Matrix4x4 prevGuiMatrix;

private void OnGUI() {
Event e = Event.current;
Matrix4x4 m = GUI.matrix;
Expand All @@ -38,24 +41,40 @@ private void OnGUI() {
GUI.matrix = m;
}

public static void BeginZoomed(Rect rect, float zoom, float topPadding) {
GUI.EndClip();
public void BeginZoomed() {
GUI.EndGroup();

Rect position = new Rect(this.position);
position.x = 0;
position.y = topPadding;

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));
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) {
Expand Down Expand Up @@ -136,8 +155,7 @@ public void DrawNoodle(Color col, List<Vector2> gridPoints) {

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;
}

Expand Down Expand Up @@ -258,7 +276,7 @@ private void DrawNodes() {
if (onValidate != null) EditorGUI.BeginChangeCheck();
}

BeginZoomed(position, zoom, topPadding);
BeginZoomed();

Vector2 mousePos = Event.current.mousePosition;

Expand Down Expand Up @@ -389,7 +407,7 @@ private void DrawNodes() {
}

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,
Expand Down

0 comments on commit 53f85a5

Please sign in to comment.