Beispiel #1
0
        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 (var a in pointsA)
            {
                foreach (var 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"));
        }
Beispiel #2
0
        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;
            }
        }
Beispiel #3
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();
        }