From fb73853bf7d6a5f3f67a2b985f047c27f3ba44c4 Mon Sep 17 00:00:00 2001 From: Zach Vinless Date: Wed, 2 Nov 2016 21:18:32 -0700 Subject: [PATCH 1/2] Added support for variable zoom center in flowchart window Flowchart window now zoom in/out by mouse cursor or center rather than upper left corner --- .../Fungus/Scripts/Editor/FlowchartWindow.cs | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs index 17e7069d..4fe1b354 100644 --- a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs +++ b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs @@ -34,6 +34,8 @@ namespace Fungus.EditorUtils protected Texture2D addTexture; + protected bool cursorZoom = true; + [MenuItem("Tools/Fungus/Flowchart Window")] static void Init() { @@ -166,7 +168,13 @@ namespace Fungus.EditorUtils GUILayout.Space(8); - flowchart.Zoom = GUILayout.HorizontalSlider(flowchart.Zoom, minZoomValue, maxZoomValue, GUILayout.Width(100)); + var newZoom = GUILayout.HorizontalSlider(flowchart.Zoom, minZoomValue, maxZoomValue, GUILayout.Width(100)); + DoZoom(flowchart, newZoom - flowchart.Zoom, Vector2.one * 0.5f); + + GUILayout.Space(8); + var toggleStyle = new GUIStyle(GUI.skin.button); + toggleStyle.fontSize = 8; + cursorZoom = GUILayout.Toggle(cursorZoom, " Cursor\nZoom", toggleStyle, GUILayout.Width(45), GUILayout.Height(22)); GUILayout.FlexibleSpace(); @@ -484,12 +492,33 @@ namespace Fungus.EditorUtils if (zoom) { - flowchart.Zoom -= Event.current.delta.y * 0.01f; - flowchart.Zoom = Mathf.Clamp(flowchart.Zoom, minZoomValue, maxZoomValue); - forceRepaintCount = 6; + Vector2 zoomCenter; + if (cursorZoom) + { + zoomCenter.x = Event.current.mousePosition.x / position.width; + zoomCenter.y = Event.current.mousePosition.y / position.height; + zoomCenter *= flowchart.Zoom; + } + else + { + zoomCenter = Vector2.one * 0.5f; + } + + DoZoom(flowchart, -Event.current.delta.y * 0.01f, zoomCenter); } } + protected virtual void DoZoom(Flowchart flowchart, float delta, Vector2 center) + { + var prevZoom = flowchart.Zoom; + flowchart.Zoom += delta; + flowchart.Zoom = Mathf.Clamp(flowchart.Zoom, minZoomValue, maxZoomValue); + var deltaSize = position.size / prevZoom - position.size / flowchart.Zoom; + var offset = -Vector2.Scale(deltaSize, center); + flowchart.ScrollPos += offset; + forceRepaintCount = 6; + } + protected virtual void DrawGrid(Flowchart flowchart) { float width = this.position.width / flowchart.Zoom; From c4462691f486d53636e8147ceb75ea0520ce7e51 Mon Sep 17 00:00:00 2001 From: Zach Vinless Date: Thu, 3 Nov 2016 08:11:30 -0700 Subject: [PATCH 2/2] Removed option to change scroll wheel zoom modes Now always zooms to cursor Also added a couple of field initializers for safety --- .../Fungus/Scripts/Editor/FlowchartWindow.cs | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs index a3803f0e..5772461a 100644 --- a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs +++ b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs @@ -35,10 +35,8 @@ namespace Fungus.EditorUtils protected Texture2D addTexture; protected Rect selectionBox; - protected Vector2 startSelectionBoxPosition; - protected List mouseDownSelectionState; - - protected bool cursorZoom = true; + protected Vector2 startSelectionBoxPosition = new Vector2(-1.0f, -1.0f); + protected List mouseDownSelectionState = new List(); [MenuItem("Tools/Fungus/Flowchart Window")] static void Init() @@ -178,11 +176,6 @@ namespace Fungus.EditorUtils var newZoom = GUILayout.HorizontalSlider(flowchart.Zoom, minZoomValue, maxZoomValue, GUILayout.Width(100)); DoZoom(flowchart, newZoom - flowchart.Zoom, Vector2.one * 0.5f); - GUILayout.Space(8); - var toggleStyle = new GUIStyle(GUI.skin.button); - toggleStyle.fontSize = 8; - cursorZoom = GUILayout.Toggle(cursorZoom, " Cursor\nZoom", toggleStyle, GUILayout.Width(45), GUILayout.Height(22)); - GUILayout.FlexibleSpace(); GUILayout.BeginVertical(); @@ -590,16 +583,9 @@ namespace Fungus.EditorUtils if (zoom && selectionBox.size == Vector2.zero) { Vector2 zoomCenter; - if (cursorZoom) - { - zoomCenter.x = Event.current.mousePosition.x / position.width; - zoomCenter.y = Event.current.mousePosition.y / position.height; - zoomCenter *= flowchart.Zoom; - } - else - { - zoomCenter = Vector2.one * 0.5f; - } + zoomCenter.x = Event.current.mousePosition.x / position.width; + zoomCenter.y = Event.current.mousePosition.y / position.height; + zoomCenter *= flowchart.Zoom; DoZoom(flowchart, -Event.current.delta.y * 0.01f, zoomCenter); }