1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00

Improved zoom with the help of Jeroenimoo0's PR

This commit is contained in:
Thor Brigsted 2019-06-08 14:25:07 +02:00
parent 0c1ae4881d
commit 53f85a5d22

View File

@ -10,10 +10,13 @@ namespace XNodeEditor {
public NodeGraphEditor graphEditor; public NodeGraphEditor graphEditor;
private List<UnityEngine.Object> selectionCache; private List<UnityEngine.Object> selectionCache;
private List<XNode.Node> culledNodes; private List<XNode.Node> culledNodes;
/// <summary> 19 if docked, 22 if not </summary>
private int topPadding { get { return isDocked() ? 19 : 22; } } 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> /// <summary> Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run.</summary>
public event Action onLateGUI; public event Action onLateGUI;
private Matrix4x4 prevGuiMatrix;
private void OnGUI() { private void OnGUI() {
Event e = Event.current; Event e = Event.current;
Matrix4x4 m = GUI.matrix; Matrix4x4 m = GUI.matrix;
@ -38,24 +41,40 @@ namespace XNodeEditor {
GUI.matrix = m; GUI.matrix = m;
} }
public static void BeginZoomed(Rect rect, float zoom, float topPadding) { public void BeginZoomed() {
GUI.EndClip(); GUI.EndGroup();
GUIUtility.ScaleAroundPivot(Vector2.one / zoom, rect.size * 0.5f); Rect position = new Rect(this.position);
Vector4 padding = new Vector4(0, topPadding, 0, 0); position.x = 0;
padding *= zoom; position.y = topPadding;
GUI.BeginClip(new Rect(-((rect.width * zoom) - rect.width) * 0.5f, -(((rect.height * zoom) - rect.height) * 0.5f) + (topPadding * zoom),
rect.width * zoom, Vector2 topLeft = new Vector2(position.xMin, position.yMin - topPadding);
rect.height * zoom)); 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) { public void EndZoomed() {
GUIUtility.ScaleAroundPivot(Vector2.one * zoom, rect.size * 0.5f); GUI.matrix = prevGuiMatrix;
Vector3 offset = new Vector3( GUI.EndGroup();
(((rect.width * zoom) - rect.width) * 0.5f), GUI.BeginGroup(new Rect(0.0f, topPadding - (topPadding * zoom), Screen.width, Screen.height));
(((rect.height * zoom) - rect.height) * 0.5f) + (-topPadding * zoom) + topPadding, }
0);
GUI.matrix = Matrix4x4.TRS(offset, Quaternion.identity, Vector3.one); 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) { 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; p = new Vector2(-p.y, p.x) * Mathf.Sign(side) * tangentLength;
inputTangent = p; inputTangent = p;
} } else {
else {
inputTangent = Vector2.left * Vector2.Distance(windowPoints[i], windowPoints[i + 1]) * 0.01f * zoom; inputTangent = Vector2.left * Vector2.Distance(windowPoints[i], windowPoints[i + 1]) * 0.01f * zoom;
} }
@ -258,7 +276,7 @@ namespace XNodeEditor {
if (onValidate != null) EditorGUI.BeginChangeCheck(); if (onValidate != null) EditorGUI.BeginChangeCheck();
} }
BeginZoomed(position, zoom, topPadding); BeginZoomed();
Vector2 mousePos = Event.current.mousePosition; Vector2 mousePos = Event.current.mousePosition;
@ -389,7 +407,7 @@ namespace XNodeEditor {
} }
if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) Selection.objects = preSelection.ToArray(); 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. //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, //This is done through reflection because OnValidate is only relevant in editor,