Browse Source

Separated early events from DrawOverlay()

master
Zach Vinless 8 years ago
parent
commit
e85745cdda
  1. 237
      Assets/Fungus/Scripts/Editor/FlowchartWindow.cs

237
Assets/Fungus/Scripts/Editor/FlowchartWindow.cs

@ -11,7 +11,7 @@ using Object = UnityEngine.Object;
namespace Fungus.EditorUtils
{
public class FlowchartWindow : EditorWindow
public class FlowchartWindow : EventWindow
{
protected class ClipboardObject
{
@ -247,6 +247,93 @@ namespace Fungus.EditorUtils
return fungusState.SelectedFlowchart;
}
protected virtual void HandleEarlyEvents(Flowchart flowchart, Event e)
{
switch (e.type)
{
case EventType.MouseDown:
// Clear search filter focus
if (!searchRect.Contains(e.mousePosition) && !popupRect.Contains(e.mousePosition))
{
CloseBlockPopup();
}
if (e.button == 0 && searchRect.Contains(e.mousePosition))
{
blockPopupSelection = 0;
popupScroll = Vector2.zero;
}
rightClickDown = -Vector2.one;
break;
case EventType.KeyDown:
if (GUI.GetNameOfFocusedControl() == searchFieldName)
{
var centerBlock = false;
var selectBlock = false;
var closePopup = false;
var useEvent = false;
switch (e.keyCode)
{
case KeyCode.DownArrow:
++blockPopupSelection;
centerBlock = true;
useEvent = true;
break;
case KeyCode.UpArrow:
--blockPopupSelection;
centerBlock = true;
useEvent = true;
break;
case KeyCode.Return:
centerBlock = true;
selectBlock = true;
closePopup = true;
useEvent = true;
break;
case KeyCode.Escape:
closePopup = true;
useEvent = true;
break;
}
blockPopupSelection = Mathf.Clamp(blockPopupSelection, 0, filteredBlocks.Length - 1);
if (centerBlock && filteredBlocks.Length > 0)
{
var block = filteredBlocks[blockPopupSelection];
CenterBlock(flowchart, block);
if (selectBlock)
{
SelectBlock(flowchart, block);
}
}
if (closePopup)
{
CloseBlockPopup();
}
if (useEvent)
{
e.Use();
}
}
else if (e.keyCode == KeyCode.Escape && flowchart.SelectedBlocks.Count > 0)
{
DeselectAll(flowchart);
e.Use();
}
break;
}
}
protected virtual void OnGUI()
{
var flowchart = GetFlowchart();
@ -286,6 +373,8 @@ namespace Fungus.EditorUtils
}
deleteList.Clear();
HandleEarlyEvents(flowchart, Event.current);
// Clear search filter focus
if (Event.current.type == EventType.MouseDown && !searchRect.Contains(Event.current.mousePosition) &&
!popupRect.Contains(Event.current.mousePosition))
@ -311,6 +400,8 @@ namespace Fungus.EditorUtils
ValidateCommands(flowchart);
ExecuteCommands(flowchart);
base.HandleEvents(Event.current);
if (forceRepaintCount > 0)
{
// Redraw on next frame to get crisp refresh rate
@ -320,20 +411,24 @@ namespace Fungus.EditorUtils
protected virtual void DrawOverlay(Flowchart flowchart)
{
// Main toolbar group
GUILayout.BeginHorizontal(EditorStyles.toolbar);
{
GUILayout.Space(2);
// Draw add block button
if (GUILayout.Button(new GUIContent(addTexture, "Add a new block"), EditorStyles.toolbarButton))
{
Vector2 newNodePosition = new Vector2(50 / flowchart.Zoom - flowchart.ScrollPos.x,
50 / flowchart.Zoom - flowchart.ScrollPos.y);
DeselectAll(flowchart);
Vector2 newNodePosition = new Vector2(
50 / flowchart.Zoom - flowchart.ScrollPos.x, 50 / flowchart.Zoom - flowchart.ScrollPos.y
);
CreateBlock(flowchart, newNodePosition);
}
// Separator
GUILayout.Label("", EditorStyles.toolbarButton, GUILayout.Width(8));
GUILayout.Label("", EditorStyles.toolbarButton, GUILayout.Width(8)); // Separator
// Draw scale bar and labels
GUILayout.Label("Scale", EditorStyles.miniLabel);
var newZoom = GUILayout.HorizontalSlider(
flowchart.Zoom, minZoomValue, maxZoomValue, GUILayout.MinWidth(40), GUILayout.MaxWidth(100)
@ -345,6 +440,7 @@ namespace Fungus.EditorUtils
DoZoom(flowchart, newZoom - flowchart.Zoom, Vector2.one * 0.5f);
}
// Draw center button
if (GUILayout.Button("Center", EditorStyles.toolbarButton))
{
flowchart.ScrollPos = flowchart.CenterPosition;
@ -352,85 +448,18 @@ namespace Fungus.EditorUtils
GUILayout.FlexibleSpace();
var blocks = flowchart.GetComponents<Block>();
// Intercept mouse and keyboard events before search field uses them
if (GUI.GetNameOfFocusedControl() == searchFieldName)
{
if (Event.current.type == EventType.KeyDown)
{
var centerBlock = false;
var selectBlock = false;
var closePopup = false;
var useEvent = false;
switch (Event.current.keyCode)
{
case KeyCode.DownArrow:
++blockPopupSelection;
centerBlock = true;
useEvent = true;
break;
case KeyCode.UpArrow:
--blockPopupSelection;
centerBlock = true;
useEvent = true;
break;
case KeyCode.Return:
centerBlock = true;
selectBlock = true;
closePopup = true;
useEvent = true;
break;
case KeyCode.Escape:
closePopup = true;
useEvent = true;
break;
}
blockPopupSelection = Mathf.Clamp(blockPopupSelection, 0, filteredBlocks.Length - 1);
if (centerBlock && filteredBlocks.Length > 0)
{
var block = filteredBlocks[blockPopupSelection];
CenterBlock(flowchart, block);
if (selectBlock)
{
SelectBlock(flowchart, block);
}
}
if (closePopup)
{
CloseBlockPopup();
}
if (useEvent)
{
Event.current.Use();
}
}
}
else if (Event.current.type == EventType.MouseDown && Event.current.button == 0 &&
searchRect.Contains(Event.current.mousePosition))
{
blockPopupSelection = 0;
}
// Draw search bar
GUI.SetNextControlName(searchFieldName);
var newString = EditorGUILayout.TextField(searchString, GUI.skin.FindStyle("ToolbarSeachTextField"), GUILayout.Width(150));
if (newString != searchString)
{
searchString = newString;
}
filteredBlocks = flowchart.GetComponents<Block>().Where(block => {
return block.BlockName.ToLower().Contains(searchString.ToLower());
}).ToArray();
// Update this every frame in case of redo/undo while popup is open
filteredBlocks = blocks.Where(block => block.BlockName.ToLower().Contains(searchString.ToLower())).ToArray();
blockPopupSelection = Mathf.Clamp(blockPopupSelection, 0, filteredBlocks.Length - 1);
}
if (Event.current.type == EventType.Repaint)
{
@ -446,13 +475,24 @@ namespace Fungus.EditorUtils
CloseBlockPopup();
}
// Eat all click events on toolbar
if (Event.current.type == EventType.MouseDown)
{
if (Event.current.mousePosition.y < searchRect.height)
{
Event.current.Use();
}
}
}
GUILayout.EndHorizontal();
// Name and description group
GUILayout.BeginHorizontal();
{
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
{
GUILayout.Label(flowchart.name, EditorStyles.whiteBoldLabel);
GUILayout.Space(2);
@ -461,49 +501,48 @@ namespace Fungus.EditorUtils
{
GUILayout.Label(flowchart.Description, EditorStyles.helpBox);
}
}
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
// Variables group
GUILayout.BeginHorizontal();
{
GUILayout.BeginVertical(GUILayout.Width(440));
GUILayout.FlexibleSpace();
var rawMousePosition = Event.current.mousePosition; // mouse position outside of scrollview to test against toolbar rect
flowchart.VariablesScrollPos = GUILayout.BeginScrollView(flowchart.VariablesScrollPos, GUILayout.MaxHeight(position.height * 0.75f));
{
GUILayout.FlexibleSpace();
flowchart.VariablesScrollPos = GUILayout.BeginScrollView(flowchart.VariablesScrollPos);
{
GUILayout.Space(8);
FlowchartEditor flowchartEditor = Editor.CreateEditor (flowchart) as FlowchartEditor;
flowchartEditor.DrawVariablesGUI();
DestroyImmediate(flowchartEditor);
Rect variableWindowRect = GUILayoutUtility.GetLastRect();
if (flowchart.VariablesExpanded &&
flowchart.Variables.Count > 0)
if (flowchart.VariablesExpanded && flowchart.Variables.Count > 0)
{
variableWindowRect.y -= 20;
variableWindowRect.height += 20;
}
if (Event.current.type == EventType.Repaint)
// Eat mouse events
if (Event.current.type == EventType.MouseDown)
{
if (Event.current.mousePosition.x <= variableWindowRect.width &&
Event.current.mousePosition.y <= variableWindowRect.height)
{
Rect toolbarRect = new Rect(0, 0, position.width, 18);
mouseOverPopup = (GUI.GetNameOfFocusedControl() == searchFieldName && popupRect.Contains(rawMousePosition));
mouseOverVariables = variableWindowRect.Contains(Event.current.mousePosition) ||
toolbarRect.Contains(rawMousePosition) || mouseOverPopup;
Event.current.Use();
}
}
}
GUILayout.EndScrollView();
}
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
}
GUILayout.EndHorizontal();
// Draw block search popup on top of other controls

Loading…
Cancel
Save