Exemplo n.º 1
0
        void DrawRectConnection(Rect rectA, Rect rectB, bool highlight)
        {
            Vector2 pointA;
            Vector2 pointB;

            Vector2 p1 = rectA.center;
            Vector2 p2 = rectB.center;

            GLDraw.segment_rect_intersection(rectA, ref p1, ref p2);
            pointA = p2;

            p1 = rectB.center;
            p2 = rectA.center;
            GLDraw.segment_rect_intersection(rectB, ref p1, ref p2);
            pointB = p2;

            Color color = Color.grey;

            if (highlight)
            {
                color = Color.green;
            }

            GLDraw.DrawConnectingCurve(pointA, pointB, color, 2);
        }
        protected virtual void DrawRectConnection(Rect rectA, Rect rectB, bool highlight)
        {
            Vector2[] pointsA = new Vector2[] {
                new Vector2(rectA.xMin + 5, rectA.center.y),
                new Vector2(rectA.xMin + rectA.width / 2, rectA.yMin + 2),
                new Vector2(rectA.xMin + rectA.width / 2, rectA.yMax - 2),
                new Vector2(rectA.xMax - 5, rectA.center.y)
            };

            Vector2[] pointsB = new Vector2[] {
                new Vector2(rectB.xMin + 5, rectB.center.y),
                new Vector2(rectB.xMin + rectB.width / 2, rectB.yMin + 2),
                new Vector2(rectB.xMin + rectB.width / 2, rectB.yMax - 2),
                new Vector2(rectB.xMax - 5, rectB.center.y)
            };

            Vector2 pointA  = Vector2.zero;
            Vector2 pointB  = Vector2.zero;
            float   minDist = float.MaxValue;

            foreach (Vector2 a in pointsA)
            {
                foreach (Vector2 b in pointsB)
                {
                    float d = Vector2.Distance(a, b);
                    if (d < minDist)
                    {
                        pointA  = a;
                        pointB  = b;
                        minDist = d;
                    }
                }
            }

            Color color = Color.grey;

            if (highlight)
            {
                color = Color.green;
            }

            GLDraw.DrawConnectingCurve(pointA, pointB, color, 1.025f);

            Rect dotARect = new Rect(pointA.x - 5, pointA.y - 5, 10, 10);

            GUI.Label(dotARect, "", new GUIStyle("U2D.dragDotActive"));

            Rect dotBRect = new Rect(pointB.x - 5, pointB.y - 5, 10, 10);

            GUI.Label(dotBRect, "", new GUIStyle("U2D.dragDotActive"));
        }
        protected virtual void DrawGrid(Flowchart flowchart)
        {
            float width  = this.position.width / flowchart.zoom;
            float height = this.position.height / flowchart.zoom;

            // Match background color of scene view
            if (EditorGUIUtility.isProSkin)
            {
                GUI.color = new Color32(71, 71, 71, 255);
            }
            else
            {
                GUI.color = new Color32(86, 86, 86, 255);
            }
            GUI.DrawTexture(new Rect(0, 0, width, height), EditorGUIUtility.whiteTexture);

            GUI.color = Color.white;
            Color color = new Color32(96, 96, 96, 255);

            float gridSize = 128f;

            float x = flowchart.scrollPos.x % gridSize;

            while (x < width)
            {
                GLDraw.DrawLine(new Vector2(x, 0), new Vector2(x, height), color, 1f);
                x += gridSize;
            }

            float y = (flowchart.scrollPos.y % gridSize);

            while (y < height)
            {
                if (y >= 0)
                {
                    GLDraw.DrawLine(new Vector2(0, y), new Vector2(width, y), color, 1f);
                }
                y += gridSize;
            }
        }
        protected virtual void DrawFlowchartView(Flowchart flowchart)
        {
            Block[] blocks = flowchart.GetComponentsInChildren <Block>(true);

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

            // 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 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();
        }
        protected virtual void DrawScriptView(FungusScript fungusScript)
        {
            Sequence[] sequences = fungusScript.GetComponentsInChildren <Sequence>(true);

            foreach (Sequence s in sequences)
            {
                fungusScript.scrollViewRect.xMin = Mathf.Min(fungusScript.scrollViewRect.xMin, s.nodeRect.xMin - 400);
                fungusScript.scrollViewRect.xMax = Mathf.Max(fungusScript.scrollViewRect.xMax, s.nodeRect.xMax + 400);
                fungusScript.scrollViewRect.yMin = Mathf.Min(fungusScript.scrollViewRect.yMin, s.nodeRect.yMin - 400);
                fungusScript.scrollViewRect.yMax = Mathf.Max(fungusScript.scrollViewRect.yMax, s.nodeRect.yMax + 400);
            }

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

            EditorZoomArea.Begin(fungusScript.zoom, scriptViewRect);

            DrawGrid(fungusScript);

            GLDraw.BeginGroup(scriptViewRect);

            if (Event.current.button == 0 &&
                Event.current.type == EventType.MouseDown &&
                !mouseOverVariables)
            {
                fungusScript.selectedSequence = null;
                if (!EditorGUI.actionKey)
                {
                    fungusScript.ClearSelectedCommands();
                }
                Selection.activeGameObject = fungusScript.gameObject;
            }

            // Draw connections
            foreach (Sequence s in sequences)
            {
                DrawConnections(fungusScript, s, false);
            }
            foreach (Sequence s in sequences)
            {
                DrawConnections(fungusScript, s, true);
            }

            GUIStyle windowStyle = new GUIStyle();

            windowStyle.stretchHeight = true;

            BeginWindows();

            windowSequenceMap.Clear();
            for (int i = 0; i < sequences.Length; ++i)
            {
                Sequence sequence = sequences[i];

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

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

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

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

                        sequence.nodeRect.x = startDragPosition.x;
                        sequence.nodeRect.y = startDragPosition.y;

                        Undo.RecordObject(sequence, "Node Position");

                        sequence.nodeRect.x = newPos.x;
                        sequence.nodeRect.y = newPos.y;

                        dragWindowId      = -1;
                        forceRepaintCount = 6;
                    }
                }

                Rect windowRect = new Rect(sequence.nodeRect);
                windowRect.x += fungusScript.scrollPos.x;
                windowRect.y += fungusScript.scrollPos.y;

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

                GUI.backgroundColor = Color.white;

                windowSequenceMap.Add(sequence);
            }

            EndWindows();

            // Draw play icons beside all executing sequences
            if (Application.isPlaying)
            {
                foreach (Sequence s in sequences)
                {
                    if (s.IsExecuting())
                    {
                        s.executingIconTimer = playIconFadeTime;
                        forceRepaintCount    = 6;
                    }

                    if (s.executingIconTimer > 0f)
                    {
                        s.executingIconTimer = Mathf.Max(s.executingIconTimer - Time.deltaTime, 0f);

                        Rect rect = new Rect(s.nodeRect);

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

                        if (!s.IsExecuting() && s.executingIconTimer < playIconFadeTime)
                        {
                            float alpha = s.executingIconTimer / playIconFadeTime;
                            GUI.color = new Color(1f, 1f, 1f, alpha);
                        }

                        if (GUI.Button(rect, FungusEditorResources.texPlayBig as Texture, new GUIStyle()))
                        {
                            SelectSequence(fungusScript, s);
                        }

                        GUI.color = Color.white;
                    }
                }
            }

            // Right click to drag view
            if (Event.current.button == 1 && Event.current.type == EventType.MouseDrag)
            {
                fungusScript.scrollPos += Event.current.delta;
                forceRepaintCount       = 6;
            }
            else if (Event.current.type == EventType.ScrollWheel)
            {
                fungusScript.zoom -= Event.current.delta.y * 0.01f;
                fungusScript.zoom  = Mathf.Clamp(fungusScript.zoom, minZoomValue, maxZoomValue);
                forceRepaintCount  = 6;
            }

            GLDraw.EndGroup();

            EditorZoomArea.End();
        }
Exemplo n.º 6
0
        void DrawScriptView(FungusScript fungusScript)
        {
            EditorUtility.SetDirty(fungusScript);

            Sequence[] sequences = fungusScript.GetComponentsInChildren <Sequence>();

            Rect scrollViewRect = new Rect();

            foreach (Sequence s in sequences)
            {
                scrollViewRect.xMin = Mathf.Min(scrollViewRect.xMin, s.nodeRect.xMin);
                scrollViewRect.xMax = Mathf.Max(scrollViewRect.xMax, s.nodeRect.xMax);
                scrollViewRect.yMin = Mathf.Min(scrollViewRect.yMin, s.nodeRect.yMin);
                scrollViewRect.yMax = Mathf.Max(scrollViewRect.yMax, s.nodeRect.yMax);
            }

            // Empty buffer area around edges of scroll rect
            float bufferScale = 0.25f;

            scrollViewRect.xMin -= position.width * bufferScale;
            scrollViewRect.yMin -= position.height * bufferScale;
            scrollViewRect.xMax += position.width * bufferScale;
            scrollViewRect.yMax += position.height * bufferScale;

            // Calc rect for left hand script view
            Rect scriptViewRect = new Rect(0, 0, this.position.width - fungusScript.commandViewWidth, this.position.height);

            // Clip GL drawing so not to overlap scrollbars
            Rect clipRect = new Rect(fungusScript.scriptScrollPos.x + scrollViewRect.x,
                                     fungusScript.scriptScrollPos.y + scrollViewRect.y,
                                     scriptViewRect.width - 15,
                                     scriptViewRect.height - 15);

            GUILayoutUtility.GetRect(scriptViewRect.width, scriptViewRect.height);

            fungusScript.scriptScrollPos = GLDraw.BeginScrollView(scriptViewRect, fungusScript.scriptScrollPos, scrollViewRect, clipRect);

            if (Event.current.type == EventType.ContextClick &&
                clipRect.Contains(Event.current.mousePosition))
            {
                GenericMenu menu     = new GenericMenu();
                Vector2     mousePos = Event.current.mousePosition;
                mousePos += fungusScript.scriptScrollPos;
                menu.AddItem(new GUIContent("Create Sequence"), false, CreateSequenceCallback, mousePos);
                menu.ShowAsContext();

                Event.current.Use();
            }

            BeginWindows();

            GUIStyle windowStyle = new GUIStyle(EditorStyles.toolbarButton);

            windowStyle.stretchHeight = true;
            windowStyle.fixedHeight   = 40;

            windowSequenceMap.Clear();
            for (int i = 0; i < sequences.Length; ++i)
            {
                Sequence sequence = sequences[i];

                float titleWidth  = windowStyle.CalcSize(new GUIContent(sequence.name)).x;
                float windowWidth = Mathf.Max(titleWidth + 10, 100);

                if (fungusScript.selectedSequence == sequence ||
                    fungusScript.executingSequence == sequence)
                {
                    GUI.backgroundColor = Color.green;
                }

                sequence.nodeRect = GUILayout.Window(i, sequence.nodeRect, DrawWindow, "", windowStyle, GUILayout.Width(windowWidth), GUILayout.Height(20), GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

                GUI.backgroundColor = Color.white;

                windowSequenceMap.Add(sequence);
            }

            // Draw connections
            foreach (Sequence s in windowSequenceMap)
            {
                DrawConnections(fungusScript, s, false);
            }
            foreach (Sequence s in windowSequenceMap)
            {
                DrawConnections(fungusScript, s, true);
            }

            EndWindows();

            GLDraw.EndScrollView();
        }