diff --git a/Assets/Fungus/Flowchart/Editor/FlowchartEditor.cs b/Assets/Fungus/Flowchart/Editor/FlowchartEditor.cs index c509c15c..24a4eced 100644 --- a/Assets/Fungus/Flowchart/Editor/FlowchartEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/FlowchartEditor.cs @@ -67,11 +67,19 @@ namespace Fungus GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); - if (GUILayout.Button("Flowchart Window")) + if (GUILayout.Button(new GUIContent("Open Flowchart Window", "Opens the Flowchart Window"))) { EditorWindow.GetWindow(typeof(FlowchartWindow), false, "Flowchart"); } + GUILayout.Space(10); + + if (GUILayout.Button(new GUIContent("Center View", "Centers the window view at the center of all blocks in the Flowchart"))) + { + // Reset the zoom so we don't have adjust the center position depending on zoom + flowchart.scrollPos = flowchart.centerPosition; + flowchart.zoom = FlowchartWindow.maxZoomValue; + } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); diff --git a/Assets/Fungus/Flowchart/Editor/FlowchartWindow.cs b/Assets/Fungus/Flowchart/Editor/FlowchartWindow.cs index bc5e2e76..2f5fd60e 100755 --- a/Assets/Fungus/Flowchart/Editor/FlowchartWindow.cs +++ b/Assets/Fungus/Flowchart/Editor/FlowchartWindow.cs @@ -19,8 +19,8 @@ namespace Fungus protected int dragWindowId = -1; protected Vector2 startDragPosition; - protected const float minZoomValue = 0.25f; - protected const float maxZoomValue = 1f; + public const float minZoomValue = 0.25f; + public const float maxZoomValue = 1f; protected GUIStyle nodeStyle = new GUIStyle(); @@ -248,6 +248,10 @@ namespace Fungus Selection.activeGameObject = flowchart.gameObject; } + // The center of the Flowchart depends on the block positions and window dimensions, so we calculate it + // here in the FlowchartWindow class and store it on the Flowchart object for use later. + CalcFlowchartCenter(flowchart, blocks); + // Draw connections foreach (Block block in blocks) { @@ -391,6 +395,33 @@ namespace Fungus EditorZoomArea.End(); } + public virtual void CalcFlowchartCenter(Flowchart flowchart, Block[] blocks) + { + if (flowchart == null || + blocks.Count() == 0) + { + return; + } + + Vector2 min = blocks[0].nodeRect.min; + Vector2 max = blocks[0].nodeRect.max; + + foreach (Block block in blocks) + { + min.x = Mathf.Min(min.x, block.nodeRect.center.x); + min.y = Mathf.Min(min.y, block.nodeRect.center.y); + max.x = Mathf.Max(max.x, block.nodeRect.center.x); + max.y = Mathf.Max(max.y, block.nodeRect.center.y); + } + + Vector2 center = (min + max) * -0.5f; + + center.x += position.width * 0.5f; + center.y += position.height * 0.5f; + + flowchart.centerPosition = center; + } + protected virtual void PanAndZoom(Flowchart flowchart) { // Right click to drag view diff --git a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs index 2deb4925..9691232b 100644 --- a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs +++ b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs @@ -130,6 +130,12 @@ namespace Fungus [Tooltip("List of commands to hide in the Add Command menu. Use this to restrict the set of commands available when editing a Flowchart.")] public List hideCommands = new List(); + /** + * Position in the center of all blocks in the flowchart. + */ + [NonSerialized] + public Vector2 centerPosition = Vector2.zero; + /** * Cached list of flowchart objects in the scene for fast lookup */