Question by
DaiMangouDev · Aug 25, 2017 at 03:52 AM ·
c#editorwindowzoommatrix4x4
How can I make my EditorWindow Pan and Zoom ;zoom function work ?
I wrote created a Pan and Zoom class, which you can setup in a few seconds to implement a pan and zoom system in an EditorWindow but my zoom function is not working correctly.
Here i the entire script I wrote for generating and pan and zoom area in the EditorWindow. just 90 lines long
Whenever i zoom in it looks perfect but zooming out from a moue position other than where i zoomed in causes unwanted behavior , also the zoom calculations are making gui draw at negative x and y positions get clipped
once you try the script you will notice all the main issues.
#region Pan and Zoom
public class PanAndZoom
{
#region Variables
private Vector2 Offset = Vector2.zero;
public Vector2 Pan = new Vector2(0, 0);
public float zoom = 1;
private Vector2 PanOffset;
private Matrix4x4 matrix;
private Matrix4x4 normalMatrix = Matrix4x4.TRS(new Vector3(), Quaternion.identity, Vector3.one);
private Vector2 mousePos;
#endregion
#region Begin Pan and Zoom Area
public void BeginArea(Rect zoomAndPanArea, float min, float max, bool resetPan)
{
GUI.EndGroup();
#region Pan
if (Offset == Vector2.zero && Event.current.rawType == EventType.MouseDown)
Offset = Event.current.mousePosition;
if (Event.current.rawType == EventType.MouseDrag && (Event.current.button == 2 || Event.current.alt))
{
Pan = Event.current.mousePosition - Offset + PanOffset;
Event.current.Use();
}
if (Event.current.rawType == EventType.MouseUp)
{
Offset = Vector2.zero;
PanOffset = Pan;
}
#endregion
#region zoom
if (Event.current.type == EventType.ScrollWheel)
{
if (mousePos != Event.current.mousePosition)
mousePos = Event.current.mousePosition;
Vector2 delta = Event.current.delta;
float zoomDelta = -delta.y / 150.0f;
zoom += zoomDelta * 4;
zoom = Mathf.Clamp(zoom, min, max);
Event.current.Use();
}
zoomAndPanArea.x += 0;
zoomAndPanArea.y += 0;
zoomAndPanArea.width = Screen.width / zoom;
zoomAndPanArea.height = Screen.height / zoom;
GUI.BeginClip(zoomAndPanArea, (Pan * (1f / zoom)), new Vector2(0,-21), resetPan);
Matrix4x4 matrix = GUI.matrix;
Matrix4x4 lhs = Matrix4x4.TRS(mousePos, Quaternion.identity, new Vector3(zoom, zoom, 1f)) * Matrix4x4.TRS(-mousePos, Quaternion.identity, Vector3.one);
GUI.matrix = lhs * matrix;
GUI.Box(zoomAndPanArea, "");
// GUI.Box(ThisScreen.ScreenRect, "");
#endregion
}
#endregion
#region End Pan and Zoom Area
public void EndArea()
{
GUI.matrix = normalMatrix;
GUI.EndClip();
GUI.BeginGroup(new Rect(0f, 21, Screen.width, Screen.height));
}
#endregion
}
#endregion
Master @Bunny83 are you there :(
Comment