示例#1
0
        public static FungusCmdEditorData SaveFungusCmd_Editor(Flowchart fc)
        {
            if (fc == null)
            {
                return(null);
            }

            var commands = fc.GetComponents <Command>();

            if (commands == null || commands.Length < 1)
            {
                return(null);
            }

            FungusCmdEditorData ret = new FungusCmdEditorData();

            ret.flowchart = fc;
            foreach (var cmd in commands)
            {
                if (cmd is PlayAnimState)
                {
                    var data = SaveCmdData_PlayAnimState(cmd as PlayAnimState);
                    if (data != null)
                    {
                        ret.cmdData_PlayAnimStates.Add(data);
                    }
                }
                else
                if (cmd is Say)
                {
                    var data = SaveCmdData_Say(cmd as Say);
                    if (data != null)
                    {
                        ret.cmdData_Say.Add(data);
                    }
                }
                else
                if (cmd is TimelineCtrl)
                {
                    var data = SaveCmdData_TimelineCtrl(cmd as TimelineCtrl);
                    if (data != null)
                    {
                        ret.cmdData_Pd.Add(data);
                    }
                }
                else
                if (cmd is Portrait)
                {
                    var data = SaveCmdData_Portrait(cmd as Portrait);
                    if (data != null)
                    {
                        ret.cmdData_Portrait.Add(data);
                    }
                }
            }

            return(ret);
        }
示例#2
0
    void Start()
    {
        var blocks = MyFlowchart.GetComponents <Block>();

        foreach (Block block in blocks)
        {
            Debug.Log(block.CommandList.Count);
            //block.CommandList[0].BroadcastMessage("Hi");
        }
    }
示例#3
0
        /// <summary>
        /// Keeps blocks like those with a Game Started event from interfering with the load process.
        /// </summary>
        protected virtual void PreventInterruptions(Flowchart flowchart)
        {
            var blocks = flowchart.GetComponents <Block>();

            for (int i = 0; i < blocks.Length; i++)
            {
                var block = blocks[i];

                // Getting rid of the Game Started event
                var hasGameStartedHandler = block._EventHandler as GameStarted != null;

                if (hasGameStartedHandler)
                {
                    block._EventHandler = null;
                }
            }
        }
示例#4
0
        public static void BlockField(SerializedProperty property, GUIContent label, GUIContent nullLabel, Flowchart flowchart)
        {
            if (flowchart == null)
            {
                return;
            }

            var block = property.objectReferenceValue as Block;

            // Build dictionary of child blocks
            List <GUIContent> blockNames = new List <GUIContent>();

            int selectedIndex = 0;

            blockNames.Add(nullLabel);
            var blocks = flowchart.GetComponents <Block>();

            blocks = blocks.OrderBy(x => x.BlockName).ToArray();

            for (int i = 0; i < blocks.Length; ++i)
            {
                blockNames.Add(new GUIContent(blocks[i].BlockName));

                if (block == blocks[i])
                {
                    selectedIndex = i + 1;
                }
            }

            selectedIndex = EditorGUILayout.Popup(label, selectedIndex, blockNames.ToArray());
            if (selectedIndex == 0)
            {
                block = null; // Option 'None'
            }
            else
            {
                block = blocks[selectedIndex - 1];
            }

            property.objectReferenceValue = block;
        }
示例#5
0
        public static Block BlockField(Rect position, GUIContent nullLabel, Flowchart flowchart, Block block)
        {
            if (flowchart == null)
            {
                return(null);
            }

            Block result = block;

            // Build dictionary of child blocks
            List <GUIContent> blockNames = new List <GUIContent>();

            int selectedIndex = 0;

            blockNames.Add(nullLabel);
            Block[] blocks = flowchart.GetComponents <Block>();
            blocks = blocks.OrderBy(x => x.BlockName).ToArray();

            for (int i = 0; i < blocks.Length; ++i)
            {
                blockNames.Add(new GUIContent(blocks[i].BlockName));

                if (block == blocks[i])
                {
                    selectedIndex = i + 1;
                }
            }

            selectedIndex = EditorGUI.Popup(position, selectedIndex, blockNames.ToArray());
            if (selectedIndex == 0)
            {
                result = null; // Option 'None'
            }
            else
            {
                result = blocks[selectedIndex - 1];
            }

            return(result);
        }
示例#6
0
        protected virtual void DrawFlowchartView(Flowchart flowchart)
        {
            Block[] blocks = flowchart.GetComponents<Block>();

            foreach (Block block in blocks)
            {
                flowchart.scrollViewRect.xMin = Mathf.Min(flowchart.scrollViewRect.xMin, block.nodeRect.xMin - 400);
                flowchart.scrollViewRect.xMax = Mathf.Max(flowchart.scrollViewRect.xMax, block.nodeRect.xMax + 400);
                flowchart.scrollViewRect.yMin = Mathf.Min(flowchart.scrollViewRect.yMin, block.nodeRect.yMin - 400);
                flowchart.scrollViewRect.yMax = Mathf.Max(flowchart.scrollViewRect.yMax, block.nodeRect.yMax + 400);
            }

            // Calc rect for script view
            Rect scriptViewRect = new Rect(0, 0, this.position.width / flowchart.zoom, this.position.height / flowchart.zoom);

            EditorZoomArea.Begin(flowchart.zoom, scriptViewRect);

            DrawGrid(flowchart);
            
            GLDraw.BeginGroup(scriptViewRect);

            if (Event.current.button == 0 && 
                Event.current.type == EventType.MouseDown &&
                !mouseOverVariables)
            {
                flowchart.selectedBlock = null;
                if (!EditorGUI.actionKey)
                {
                    flowchart.ClearSelectedCommands();
                }
                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)
            {
                DrawConnections(flowchart, block, false);
            }
            foreach (Block block in blocks)
            {
                DrawConnections(flowchart, block, true);
            }

            GUIStyle windowStyle = new GUIStyle();
            windowStyle.stretchHeight = true;

            BeginWindows();

            windowBlockMap.Clear();
            for (int i = 0; i < blocks.Length; ++i)
            {
                Block block = blocks[i];

                float nodeWidthA = nodeStyle.CalcSize(new GUIContent(block.blockName)).x + 10;
                float nodeWidthB = 0f;
                if (block.eventHandler != null)
                {
                    nodeWidthB = nodeStyle.CalcSize(new GUIContent(block.eventHandler.GetSummary())).x + 10;
                }

                block.nodeRect.width = Mathf.Max(Mathf.Max(nodeWidthA, nodeWidthB), 120);
                block.nodeRect.height = 40;

                if (Event.current.button == 0)
                {
                    if (Event.current.type == EventType.MouseDrag && dragWindowId == i)
                    {
                        block.nodeRect.x += Event.current.delta.x;
                        block.nodeRect.y += Event.current.delta.y;

                        forceRepaintCount = 6;
                    }
                    else if (Event.current.type == EventType.MouseUp &&
                             dragWindowId == i)
                    {
                        Vector2 newPos = new Vector2(block.nodeRect.x, block.nodeRect.y);
                        
                        block.nodeRect.x = startDragPosition.x;
                        block.nodeRect.y = startDragPosition.y;
                        
                        Undo.RecordObject(block, "Node Position");
                        
                        block.nodeRect.x = newPos.x;
                        block.nodeRect.y = newPos.y;

                        dragWindowId = -1;
                        forceRepaintCount = 6;
                    }
                }

                Rect windowRect = new Rect(block.nodeRect);
                windowRect.x += flowchart.scrollPos.x;
                windowRect.y += flowchart.scrollPos.y;

                GUILayout.Window(i, windowRect, DrawWindow, "", windowStyle);

                GUI.backgroundColor = Color.white;

                windowBlockMap.Add(block);
            }

            EndWindows();

            // Draw Event Handler labels
            foreach (Block block in blocks)
            {
                if (block.eventHandler != null)
                {
                    string handlerLabel = "";
                    EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(block.eventHandler.GetType());
                    if (info != null)
                    {
                        handlerLabel = "<" + info.EventHandlerName + "> ";
                    }

                    GUIStyle handlerStyle = new GUIStyle(EditorStyles.whiteLabel);
                    handlerStyle.wordWrap = true;
                    handlerStyle.margin.top = 0;
                    handlerStyle.margin.bottom = 0;
                    handlerStyle.alignment = TextAnchor.MiddleCenter;

                    Rect rect = new Rect(block.nodeRect);
                    rect.height = handlerStyle.CalcHeight(new GUIContent(handlerLabel), block.nodeRect.width);
                    rect.x += flowchart.scrollPos.x;
                    rect.y += flowchart.scrollPos.y - rect.height;

                    GUI.Label(rect, handlerLabel, handlerStyle);
                }
            }


            // Draw play icons beside all executing blocks
            if (Application.isPlaying)
            {
                foreach (Block b in blocks)
                {
                    if (b.IsExecuting())
                    {
                        b.executingIconTimer = Time.realtimeSinceStartup + Block.executingIconFadeTime;
                        b.activeCommand.executingIconTimer = Time.realtimeSinceStartup + Block.executingIconFadeTime;
                        forceRepaintCount = 6;
                    }

                    if (b.executingIconTimer > Time.realtimeSinceStartup)
                    {
                        Rect rect = new Rect(b.nodeRect);

                        rect.x += flowchart.scrollPos.x - 37;
                        rect.y += flowchart.scrollPos.y + 3;
                        rect.width = 34;
                        rect.height = 34;

                        if (!b.IsExecuting())
                        {
                            float alpha = (b.executingIconTimer - Time.realtimeSinceStartup) / Block.executingIconFadeTime;
                            alpha = Mathf.Clamp01(alpha);
                            GUI.color = new Color(1f, 1f, 1f, alpha); 
                        }

                        if (GUI.Button(rect, FungusEditorResources.texPlayBig as Texture, new GUIStyle()))
                        {
                            SelectBlock(flowchart, b);
                        }

                        GUI.color = Color.white;
                    }
                }
            }

            PanAndZoom(flowchart);

            GLDraw.EndGroup();

            EditorZoomArea.End();
        }
 public static Block BlockField(Rect position, GUIContent nullLabel, Flowchart flowchart, Block block)
 {
     if (flowchart == null)
     {
         return null;
     }
     
     Block result = block;
     
     // Build dictionary of child blocks
     List<GUIContent> blockNames = new List<GUIContent>();
     
     int selectedIndex = 0;
     blockNames.Add(nullLabel);
     Block[] blocks = flowchart.GetComponents<Block>();
     for (int i = 0; i < blocks.Length; ++i)
     {
         blockNames.Add(new GUIContent(blocks[i].name));
         
         if (block == blocks[i])
         {
             selectedIndex = i + 1;
         }
     }
     
     selectedIndex = EditorGUI.Popup(position, selectedIndex, blockNames.ToArray());
     if (selectedIndex == 0)
     {
         result = null; // Option 'None'
     }
     else
     {
         result = blocks[selectedIndex - 1];
     }
     
     return result;
 }
        public static void BlockField(SerializedProperty property, GUIContent label, GUIContent nullLabel, Flowchart flowchart)
        {
            if (flowchart == null)
            {
                return;
            }

            var block = property.objectReferenceValue as Block;
        
            // Build dictionary of child blocks
            List<GUIContent> blockNames = new List<GUIContent>();
            
            int selectedIndex = 0;
            blockNames.Add(nullLabel);
            var blocks = flowchart.GetComponents<Block>();
            for (int i = 0; i < blocks.Length; ++i)
            {
                blockNames.Add(new GUIContent(blocks[i].BlockName));
                
                if (block == blocks[i])
                {
                    selectedIndex = i + 1;
                }
            }
            
            selectedIndex = EditorGUILayout.Popup(label, selectedIndex, blockNames.ToArray());
            if (selectedIndex == 0)
            {
                block = null; // Option 'None'
            }
            else
            {
                block = blocks[selectedIndex - 1];
            }
            
            property.objectReferenceValue = block;
        }
        protected virtual void HandleSelectionBox(Flowchart flowchart)
        {
            if (Event.current.button == 0 && Event.current.modifiers != EventModifiers.Alt &&
                !(UnityEditor.Tools.current == Tool.View && UnityEditor.Tools.viewTool == ViewTool.Pan))
            {
                switch (Event.current.type)
                {
                case EventType.MouseDown:
                    if (!mouseOverVariables)
                    {
                        startSelectionBoxPosition = Event.current.mousePosition;
                        mouseDownSelectionState = new List<Block>(flowchart.SelectedBlocks);
                        Event.current.Use();
                    }
                    break;

                case EventType.MouseDrag:
                    if (startSelectionBoxPosition.x >= 0 && startSelectionBoxPosition.y >= 0)
                    {
                        var topLeft = Vector2.Min(startSelectionBoxPosition, Event.current.mousePosition);
                        var bottomRight = Vector2.Max(startSelectionBoxPosition, Event.current.mousePosition);
                        selectionBox = Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);

                        Rect zoomSelectionBox = selectionBox;
                        zoomSelectionBox.position -= flowchart.ScrollPos * flowchart.Zoom;
                        zoomSelectionBox.position /= flowchart.Zoom;
                        zoomSelectionBox.size /= flowchart.Zoom;

                        foreach (var block in flowchart.GetComponents<Block>())
                        {
                            if (zoomSelectionBox.Overlaps(block._NodeRect))
                            {
                                if (mouseDownSelectionState.Contains(block))
                                {
                                    flowchart.SelectedBlocks.Remove(block);
                                }
                                else
                                {
                                    flowchart.AddSelectedBlock(block);
                                }
                            }
                            else if (mouseDownSelectionState.Contains(block))
                            {
                                flowchart.AddSelectedBlock(block);
                            }
                            else
                            {
                                flowchart.SelectedBlocks.Remove(block);
                            }
                        }
                    }
                    Event.current.Use();
                    break;
                }

                if (Event.current.rawType == EventType.MouseUp)
                {
                    selectionBox.size = Vector2.zero;
                    selectionBox.position = -Vector2.one;
                    startSelectionBoxPosition = selectionBox.position;

                    var tempList = new List<Block>(flowchart.SelectedBlocks);
                    flowchart.SelectedBlocks = mouseDownSelectionState;
                    Undo.RecordObject(flowchart, "Select");
                    flowchart.SelectedBlocks = tempList;

                    if (flowchart.SelectedBlock != null)
                    {
                        SetBlockForInspector(flowchart, flowchart.SelectedBlock);
                    }
                }
            }
        }
        protected virtual void DrawFlowchartView(Flowchart flowchart)
        {
            Block[] blocks = flowchart.GetComponents<Block>();

            foreach (var block in blocks)
            {
                var node = block as Node;
                if (node == null)
                {
                    continue;
                }

                var newRect = new Rect();
                newRect.xMin = Mathf.Min(flowchart.ScrollViewRect.xMin, node._NodeRect.xMin - 400);
                newRect.xMax = Mathf.Max(flowchart.ScrollViewRect.xMax, node._NodeRect.xMax + 400);
                newRect.yMin = Mathf.Min(flowchart.ScrollViewRect.yMin, node._NodeRect.yMin - 400);
                newRect.yMax = Mathf.Max(flowchart.ScrollViewRect.yMax, node._NodeRect.yMax + 400);
                flowchart.ScrollViewRect = newRect;
            }

            // Draw background color / drop shadow
            if (Event.current.type == EventType.Repaint)
            {
                UnityEditor.Graphs.Styles.graphBackground.Draw(
                    new Rect(0, 17, position.width, position.height - 17), false, false, false, false
                );            
            }
            // Calc rect for script view
            Rect scriptViewRect = new Rect(0, 0, this.position.width / flowchart.Zoom, this.position.height / flowchart.Zoom);

            // Update right click start outside of EditorZoomArea
            if (Event.current.button == 1)
            {
                if (Event.current.type == EventType.MouseDown)
                {
                    rightClickDown = Event.current.mousePosition;
                }
                else if (Event.current.type == EventType.MouseDrag)
                {
                    if (Vector2.Distance(rightClickDown, Event.current.mousePosition) > rightClickTolerance)
                    {
                        rightClickDown = -Vector2.one;
                    }
                }
            }

            EditorZoomArea.Begin(flowchart.Zoom, scriptViewRect);

            if (Event.current.type == EventType.Repaint)
            {
                DrawGrid(flowchart);
            }
            
            // 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.
            if (flowchart != null && blocks.Length > 0)
            {
                CalcFlowchartCenter(flowchart, blocks);
            }

            // Draw connections
            foreach (var block in blocks)
            {
                DrawConnections(flowchart, block, false);
            }
            foreach (var block in blocks)
            {
                DrawConnections(flowchart, block, true);
            }

            GUIStyle windowStyle = new GUIStyle();
            windowStyle.stretchHeight = true;

            BeginWindows();

            windowBlockMap.Clear();
            bool useEvent = false;
            bool endDrag = false;
            for (int i = 0; i < blocks.Length; ++i)
            {
                var block = blocks[i];

                float nodeWidthA = nodeStyle.CalcSize(new GUIContent(block.BlockName)).x + 10;
                float nodeWidthB = 0f;
                if (block._EventHandler != null)
                {
                    nodeWidthB = nodeStyle.CalcSize(new GUIContent(block._EventHandler.GetSummary())).x + 10;
                }

                if (Event.current.button == 0)
                {
                    Rect tempRect = block._NodeRect;
                    tempRect.width = Mathf.Max(Mathf.Max(nodeWidthA, nodeWidthB), 120);
                    tempRect.height = 40;

                    if (dragWindowId > -1 && flowchart.SelectedBlocks.Contains(block))
                    {
                        if (Event.current.type == EventType.MouseDrag)
                        {
                            tempRect.x += Event.current.delta.x;
                            tempRect.y += Event.current.delta.y;

                            forceRepaintCount = 6;
                            useEvent = true;
                        }
                        else if (Event.current.rawType == EventType.MouseUp)
                        {
                            Vector2 newPos = new Vector2(tempRect.x, tempRect.y);
                            tempRect.x = startDragPosition.x + (newPos.x - blocks[dragWindowId]._NodeRect.position.x);
                            tempRect.y = startDragPosition.y + (newPos.y - blocks[dragWindowId]._NodeRect.position.y);

                            block._NodeRect = tempRect;
                            
                            Undo.RecordObject(block, "Node Position");
                            
                            tempRect.x = newPos.x;
                            tempRect.y = newPos.y;

                            forceRepaintCount = 6;
                            useEvent = true;
                            endDrag = true;
                        }
                    }

                    block._NodeRect = tempRect;
                }

                Rect windowRect = new Rect(block._NodeRect);
                windowRect.x += flowchart.ScrollPos.x;
                windowRect.y += flowchart.ScrollPos.y;

                GUILayout.Window(i, windowRect, DrawWindow, "", windowStyle);

                GUI.backgroundColor = Color.white;

                windowBlockMap.Add(block);
            }

            dragWindowId = endDrag ? -1 : dragWindowId;

            if (useEvent)
            {
                Event.current.Use();
            }

            EndWindows();

            // Draw Event Handler labels
            foreach (var block in blocks)
            {
                if (block._EventHandler != null)
                {
                    string handlerLabel = "";
                    EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(block._EventHandler.GetType());
                    if (info != null)
                    {
                        handlerLabel = "<" + info.EventHandlerName + "> ";
                    }

                    GUIStyle handlerStyle = new GUIStyle(EditorStyles.whiteLabel);
                    handlerStyle.wordWrap = true;
                    handlerStyle.margin.top = 0;
                    handlerStyle.margin.bottom = 0;
                    handlerStyle.alignment = TextAnchor.MiddleCenter;

                    Rect rect = new Rect(block._NodeRect);
                    rect.height = handlerStyle.CalcHeight(new GUIContent(handlerLabel), block._NodeRect.width);
                    rect.x += flowchart.ScrollPos.x;
                    rect.y += flowchart.ScrollPos.y - rect.height;

                    GUI.Label(rect, handlerLabel, handlerStyle);
                }
            }
                
            // Draw play icons beside all executing blocks
            if (Application.isPlaying)
            {
                foreach (var b in blocks)
                {
                    if (b.IsExecuting())
                    {
                        b.ExecutingIconTimer = Time.realtimeSinceStartup + FungusConstants.ExecutingIconFadeTime;
                        b.ActiveCommand.ExecutingIconTimer = Time.realtimeSinceStartup + FungusConstants.ExecutingIconFadeTime;
                        forceRepaintCount = 6;
                    }

                    if (b.ExecutingIconTimer > Time.realtimeSinceStartup)
                    {
                        Rect rect = new Rect(b._NodeRect);

                        rect.x += flowchart.ScrollPos.x - 37;
                        rect.y += flowchart.ScrollPos.y + 3;
                        rect.width = 34;
                        rect.height = 34;

                        if (!b.IsExecuting())
                        {
                            float alpha = (b.ExecutingIconTimer - Time.realtimeSinceStartup) / FungusConstants.ExecutingIconFadeTime;
                            alpha = Mathf.Clamp01(alpha);
                            GUI.color = new Color(1f, 1f, 1f, alpha); 
                        }

                        if (GUI.Button(rect, FungusEditorResources.PlayBig, new GUIStyle()))
                        {
                            SelectBlock(flowchart, b);
                        }

                        GUI.color = Color.white;
                    }
                }
            }

            PanAndZoom(flowchart);

            EditorZoomArea.End();
            
            // Handle right click up outside of EditorZoomArea to avoid strange offsets
            if (Event.current.type == EventType.MouseUp && Event.current.button == 1 &&
                rightClickDown != -Vector2.one && !mouseOverVariables)
            {
                var menu = new GenericMenu();
                var mousePosition = rightClickDown;

                Block hitBlock = null;
                foreach (var block in blocks)
                {
                    if (block._NodeRect.Contains(rightClickDown / flowchart.Zoom - flowchart.ScrollPos))
                    {
                        hitBlock = block;
                        break;
                    }
                }
                // Clicked on a block
                if (hitBlock != null)
                {
                    flowchart.AddSelectedBlock(hitBlock);

                    // Use a copy because flowchart.SelectedBlocks gets modified
                    var blockList = new List<Block>(flowchart.SelectedBlocks);
                    menu.AddItem(new GUIContent ("Copy"), false, () => Copy(flowchart));
                    menu.AddItem(new GUIContent ("Cut"), false, () => Cut(flowchart));
                    menu.AddItem(new GUIContent ("Duplicate"), false, () => Duplicate(flowchart));
                    menu.AddItem(new GUIContent ("Delete"), false, DeleteBlocks, blockList);
                }
                // Clicked on empty space in grid
                else
                {
                    DeselectAll(flowchart);

                    menu.AddItem(new GUIContent("Add Block"), false, () => CreateBlock(flowchart, mousePosition / flowchart.Zoom - flowchart.ScrollPos));

                    if (copyList.Count > 0)
                    {
                        menu.AddItem(new GUIContent("Paste"), false, () => Paste(flowchart, mousePosition));
                    }
                    else
                    {
                        menu.AddDisabledItem(new GUIContent("Paste"));
                    }
                }

                var menuRect = new Rect();
                menuRect.position = new Vector2(mousePosition.x, mousePosition.y - 12f);
                menu.DropDown(menuRect);
                Event.current.Use();               
            }

            // If event has yet to be used and user isn't multiselecting or panning, clear selection
            bool validModifier = Event.current.alt || GetAppendModifierDown();
            if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && !validModifier)
            {
                DeselectAll(flowchart);
            }

            // Draw selection box
            if (startSelectionBoxPosition.x >= 0 && startSelectionBoxPosition.y >= 0)
            {
                GUI.Box(selectionBox, "", (GUIStyle) "SelectionRect");
                forceRepaintCount = 6;
            }
        }
        protected virtual void DrawOverlay(Flowchart flowchart)
        {
            GUILayout.BeginHorizontal(EditorStyles.toolbar);
            
            GUILayout.Space(2);

            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);
                CreateBlock(flowchart, newNodePosition);
            }
            
            // Separator
            GUILayout.Label("", EditorStyles.toolbarButton, GUILayout.Width(8));

            GUILayout.Label("Scale", EditorStyles.miniLabel);
            var newZoom = GUILayout.HorizontalSlider(
                flowchart.Zoom, minZoomValue, maxZoomValue, GUILayout.MinWidth(40), GUILayout.MaxWidth(100)
            );
            GUILayout.Label(flowchart.Zoom.ToString("0.0#x"), EditorStyles.miniLabel, GUILayout.Width(30));

            if (newZoom != flowchart.Zoom)
            {
                DoZoom(flowchart, newZoom - flowchart.Zoom, Vector2.one * 0.5f);
            }

            if (GUILayout.Button("Center", EditorStyles.toolbarButton))
            {
                flowchart.ScrollPos = flowchart.CenterPosition;
            }

            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;
            }

            GUI.SetNextControlName(searchFieldName);
            var newString = EditorGUILayout.TextField(searchString, GUI.skin.FindStyle("ToolbarSeachTextField"), GUILayout.Width(150));
            if (newString != searchString)
            {
                searchString = newString;
            }

            // 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)
            {
                searchRect = GUILayoutUtility.GetLastRect();
                popupRect = searchRect;
                popupRect.width += 12;
                popupRect.y += popupRect.height;
                popupRect.height = Mathf.Min(filteredBlocks.Length * 16, position.height - 22);
            }

            if (GUILayout.Button("", GUI.skin.FindStyle("ToolbarSeachCancelButton")))
            {
                CloseBlockPopup();
            }

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();

            GUILayout.FlexibleSpace();

            GUILayout.BeginVertical();
            GUILayout.Label(flowchart.name, EditorStyles.whiteBoldLabel);
            
            GUILayout.Space(2);
            
            if (flowchart.Description.Length > 0)
            {
                GUILayout.Label(flowchart.Description, EditorStyles.helpBox);
            }
            GUILayout.EndVertical();

            GUILayout.EndHorizontal();

            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();

            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)
            {
                variableWindowRect.y -= 20;
                variableWindowRect.height += 20;
            }
            if (Event.current.type == EventType.Repaint)
            {
                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; 
            }

            GUILayout.EndScrollView();

            GUILayout.EndVertical();

            GUILayout.FlexibleSpace();

            GUILayout.EndHorizontal();

            // Draw block search popup on top of other controls
            if (GUI.GetNameOfFocusedControl() == searchFieldName && filteredBlocks.Length > 0)
            {
                DrawBlockPopup(flowchart);
            }
        }
        protected virtual void ExecuteCommands(Flowchart flowchart)
        {
            if (Event.current.type == EventType.ExecuteCommand)
            {
                switch (Event.current.commandName)
                {
                case "Copy":
                    Copy(flowchart);
                    Event.current.Use();
                    break;
                
                case "Cut":
                    Cut(flowchart);
                    Event.current.Use();
                    break;

                case "Paste":
                    Paste(flowchart, position.center - position.position);
                    Event.current.Use();
                    break;

                case "Delete":
                    DeleteBlocks(flowchart.SelectedBlocks);
                    Event.current.Use();
                    break;

                case "Duplicate":
                    Duplicate(flowchart);
                    Event.current.Use();
                    break;

                case "SelectAll":
                    Undo.RecordObject(flowchart, "Selection");
                    flowchart.ClearSelectedBlocks();
                    foreach (var block in flowchart.GetComponents<Block>())
                    {
                        flowchart.AddSelectedBlock(block);
                    }
                    Event.current.Use();
                    break;

                case "Find":
                    blockPopupSelection = 0;
                    EditorGUI.FocusTextInControl(searchFieldName);
                    Event.current.Use();
                    break;
                }
            }
        }
示例#13
0
        protected virtual void DrawFlowchartView(Flowchart flowchart)
        {
            Block[] blocks = flowchart.GetComponents <Block>();

            foreach (var block in blocks)
            {
                var node = block as Node;
                if (node == null)
                {
                    continue;
                }

                var newRect = new Rect();
                newRect.xMin             = Mathf.Min(flowchart.ScrollViewRect.xMin, node._NodeRect.xMin - 400);
                newRect.xMax             = Mathf.Max(flowchart.ScrollViewRect.xMax, node._NodeRect.xMax + 400);
                newRect.yMin             = Mathf.Min(flowchart.ScrollViewRect.yMin, node._NodeRect.yMin - 400);
                newRect.yMax             = Mathf.Max(flowchart.ScrollViewRect.yMax, node._NodeRect.yMax + 400);
                flowchart.ScrollViewRect = newRect;
            }

            // Calc rect for script view
            Rect scriptViewRect = new Rect(0, 0, this.position.width / flowchart.Zoom, this.position.height / flowchart.Zoom);

            EditorZoomArea.Begin(flowchart.Zoom, scriptViewRect);

            DrawGrid(flowchart);

            GLDraw.BeginGroup(scriptViewRect);

            if (Event.current.button == 0 &&
                Event.current.type == EventType.MouseDown &&
                !mouseOverVariables)
            {
                flowchart.SelectedBlock = null;
                if (!EditorGUI.actionKey)
                {
                    flowchart.ClearSelectedCommands();
                }
                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 (var block in blocks)
            {
                DrawConnections(flowchart, block, false);
            }
            foreach (var block in blocks)
            {
                DrawConnections(flowchart, block, true);
            }

            GUIStyle windowStyle = new GUIStyle();

            windowStyle.stretchHeight = true;

            BeginWindows();

            windowBlockMap.Clear();
            for (int i = 0; i < blocks.Length; ++i)
            {
                var block = blocks[i];

                float nodeWidthA = nodeStyle.CalcSize(new GUIContent(block.BlockName)).x + 10;
                float nodeWidthB = 0f;
                if (block._EventHandler != null)
                {
                    nodeWidthB = nodeStyle.CalcSize(new GUIContent(block._EventHandler.GetSummary())).x + 10;
                }

                if (Event.current.button == 0)
                {
                    Rect tempRect = block._NodeRect;
                    tempRect.width  = Mathf.Max(Mathf.Max(nodeWidthA, nodeWidthB), 120);
                    tempRect.height = 40;

                    if (Event.current.type == EventType.MouseDrag && dragWindowId == i)
                    {
                        tempRect.x += Event.current.delta.x;
                        tempRect.y += Event.current.delta.y;

                        forceRepaintCount = 6;
                    }
                    else if (Event.current.type == EventType.MouseUp &&
                             dragWindowId == i)
                    {
                        Vector2 newPos = new Vector2(tempRect.x, tempRect.y);

                        tempRect.x = startDragPosition.x;
                        tempRect.y = startDragPosition.y;

                        Undo.RecordObject((Block)block, "Node Position");

                        tempRect.x = newPos.x;
                        tempRect.y = newPos.y;

                        dragWindowId      = -1;
                        forceRepaintCount = 6;
                    }

                    block._NodeRect = tempRect;
                }

                Rect windowRect = new Rect(block._NodeRect);
                windowRect.x += flowchart.ScrollPos.x;
                windowRect.y += flowchart.ScrollPos.y;

                GUILayout.Window(i, windowRect, DrawWindow, "", windowStyle);

                GUI.backgroundColor = Color.white;

                windowBlockMap.Add(block);
            }

            EndWindows();

            // Draw Event Handler labels
            foreach (var block in blocks)
            {
                if (block._EventHandler != null)
                {
                    string handlerLabel            = "";
                    EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(block._EventHandler.GetType());
                    if (info != null)
                    {
                        handlerLabel = "<" + info.EventHandlerName + "> ";
                    }

                    GUIStyle handlerStyle = new GUIStyle(EditorStyles.whiteLabel);
                    handlerStyle.wordWrap      = true;
                    handlerStyle.margin.top    = 0;
                    handlerStyle.margin.bottom = 0;
                    handlerStyle.alignment     = TextAnchor.MiddleCenter;

                    Rect rect = new Rect(block._NodeRect);
                    rect.height = handlerStyle.CalcHeight(new GUIContent(handlerLabel), block._NodeRect.width);
                    rect.x     += flowchart.ScrollPos.x;
                    rect.y     += flowchart.ScrollPos.y - rect.height;

                    GUI.Label(rect, handlerLabel, handlerStyle);
                }
            }

            // Draw play icons beside all executing blocks
            if (Application.isPlaying)
            {
                foreach (var b in blocks)
                {
                    if (b.IsExecuting())
                    {
                        b.ExecutingIconTimer = Time.realtimeSinceStartup + FungusConstants.ExecutingIconFadeTime;
                        b.ActiveCommand.ExecutingIconTimer = Time.realtimeSinceStartup + FungusConstants.ExecutingIconFadeTime;
                        forceRepaintCount = 6;
                    }

                    if (b.ExecutingIconTimer > Time.realtimeSinceStartup)
                    {
                        Rect rect = new Rect(b._NodeRect);

                        rect.x     += flowchart.ScrollPos.x - 37;
                        rect.y     += flowchart.ScrollPos.y + 3;
                        rect.width  = 34;
                        rect.height = 34;

                        if (!b.IsExecuting())
                        {
                            float alpha = (b.ExecutingIconTimer - Time.realtimeSinceStartup) / FungusConstants.ExecutingIconFadeTime;
                            alpha     = Mathf.Clamp01(alpha);
                            GUI.color = new Color(1f, 1f, 1f, alpha);
                        }

                        if (GUI.Button(rect, FungusEditorResources.texPlayBig as Texture, new GUIStyle()))
                        {
                            SelectBlock(flowchart, b);
                        }

                        GUI.color = Color.white;
                    }
                }
            }

            PanAndZoom(flowchart);

            GLDraw.EndGroup();

            EditorZoomArea.End();
        }