diff --git a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs index 07b01719..5772461a 100644 --- a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs +++ b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs @@ -35,9 +35,9 @@ namespace Fungus.EditorUtils protected Texture2D addTexture; protected Rect selectionBox; - protected Vector2 startSelectionBoxPosition; - protected List mouseDownSelectionState; - + protected Vector2 startSelectionBoxPosition = new Vector2(-1.0f, -1.0f); + protected List mouseDownSelectionState = new List(); + [MenuItem("Tools/Fungus/Flowchart Window")] static void Init() { @@ -173,7 +173,8 @@ 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.FlexibleSpace(); @@ -581,12 +582,26 @@ namespace Fungus.EditorUtils if (zoom && selectionBox.size == Vector2.zero) { - flowchart.Zoom -= Event.current.delta.y * 0.01f; - flowchart.Zoom = Mathf.Clamp(flowchart.Zoom, minZoomValue, maxZoomValue); - forceRepaintCount = 6; + Vector2 zoomCenter; + 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); } } + 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;