Ejemplo n.º 1
0
        public static void Launch(VariablesEditor editor, ReorderableList list)
        {
            if (instance == null)
            {
                instance = CreateInstance <AddVariableWindow>();
            }
            instance.titleContent = new GUIContent("Add Variable");
            instance.list         = list;
            instance.editor       = editor;
            instance.ShowUtility();
            instance.Focus();

            var size = new Vector2(200, 110);

            instance.minSize = size;
            instance.maxSize = size;

            var rect = instance.position;

            rect.size         = size;
            rect.center       = GUIUtility.GUIToScreenPoint(XEvent.MousePos);
            instance.position = rect;
        }
Ejemplo n.º 2
0
        void OnGUI()
        {
            Tooltip = null;

            // Make the box texture opaque
            if (boxTexture == null)
            {
                if (EditorGUIUtility.isProSkin)
                {
                    // Make a copy of the old texture
                    var oldTex = GUI.skin.box.normal.background;
                    var tmp    = RenderTexture.GetTemporary(oldTex.width, oldTex.height);

                    Graphics.Blit(oldTex, tmp);
                    RenderTexture previous = RenderTexture.active;
                    RenderTexture.active = tmp;
                    boxTexture           = new Texture2D(oldTex.width, oldTex.height);
                    boxTexture.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
                    RenderTexture.active = previous;
                    RenderTexture.ReleaseTemporary(tmp);

                    // Remove alpha
                    var colors = boxTexture.GetPixels();
                    for (int i = 0; i < colors.Length; ++i)
                    {
                        // Pro background color is RGB(64, 64, 64)
                        colors[i].r = 0.2196f + (colors[i].r * colors[i].a);
                        colors[i].g = 0.2196f + (colors[i].g * colors[i].a);
                        colors[i].b = 0.2196f + (colors[i].b * colors[i].a);
                        colors[i].a = 1;
                    }

                    boxTexture.SetPixels(colors);
                    boxTexture.Apply();
                }
                else
                {
                    boxTexture = GUI.skin.box.normal.background;
                }
            }
            if (flatTexture == null)
            {
                flatTexture            = new Texture2D(1, 1);
                flatTexture.filterMode = FilterMode.Point;
                flatTexture.SetPixels(new Color[1] {
                    Color.white
                });
                flatTexture.Apply();
            }

            rectCache = rectCache ?? new Dictionary <Socket, Rect>();
            rectCache.Clear();

            autoRepaintOnSceneChange = true;
            wantsMouseMove           = true;

            Offset = new Vector2(
                Mathf.RoundToInt(Offset.x),
                Mathf.RoundToInt(Offset.y)
                );

            var graphRect = GetGraphRect();

            var mousePos = Event.current.mousePosition;

            GraphPosition = new Vector2(
                mousePos.x - graphRect.xMin - Offset.x,
                -mousePos.y + graphRect.yMin + Offset.y
                );

            // Top Toolbar
            var toolbarHeight = Vector2.up * TOOLBAR_HEIGHT;

            var topToolbarRect = new Rect(position);

            topToolbarRect.position = Vector2.zero;
            topToolbarRect.size    -= new Vector2(10, 0);
            topToolbarRect.height   = toolbarHeight.y;

            XGUI.ResetToStyle(null);
            XGUI.BeginArea(new Rect(0, 0, position.width - 10, TOOLBAR_HEIGHT));
            XGUI.BeginHorizontal();

            var oldGraph = Graph;

            XGUI.ResetToStyle(null);
            XGUI.BeginVertical();
            XGUI.FlexibleSpace();
            Graph = XGUI.ObjectField(Graph, false);
            if (Graph != oldGraph)
            {
                CenterView();
            }
            XGUI.FlexibleSpace();
            XGUI.EndVertical();

            XGUI.FlexibleSpace();

            XGUI.ResetToStyle(GUI.skin.button);
            if (XGUI.Button("Variables"))
            {
                VariablesEditor.Launch(this);
            }

            XGUI.EndHorizontal();
            XGUI.EndArea();

            // Bottom Toolbar
            XGUI.ResetToStyle(null);
            XGUI.BeginArea(new Rect(0, position.size.y - TOOLBAR_HEIGHT, position.width - 10, TOOLBAR_HEIGHT));
            XGUI.BeginHorizontal();

            XGUI.ResetToStyle(GUI.skin.button);
            XGUI.Enabled = Graph != null;
            snap         = XGUI.ToggleButton(snap, "Snap");

            XGUI.ResetToStyle(GUI.skin.button);
            XGUI.Enabled = Graph != null;
            if (XGUI.Button("Center View"))
            {
                CenterView();
            }

            XGUI.FlexibleSpace();

            if (graphRect.Contains(mousePos))
            {
                XGUI.ResetToStyle(GUI.skin.label);
                XGUI.Alignment = TextAnchor.MiddleRight;
                XGUI.Label(
                    string.Format("{0}, {1}", GraphPosition.x, GraphPosition.y),
                    XGUI.ExpandHeight(true));
            }

            XGUI.EndHorizontal();
            XGUI.EndArea();

            // Draw the graph
            {
                XGUI.ResetToStyle(null);
                XGUI.BackgroundColor   = Skin.canvasColor;
                XGUI.Normal.background = flatTexture;
                XGUI.Box(graphRect);

                // Make the clipping window for the graph
                graphRect.position += Vector2.one * GRAPH_PADDING;
                graphRect.size     -= Vector2.one * GRAPH_PADDING * 2;
                XGUI.ResetToStyle(null);
                XGUI.BeginClip(graphRect);

                // Draw the graph
                var gridRect = new Rect(Vector2.zero, graphRect.size);
                DrawGrid(gridRect);
                if (Graph != null && Graph.Nodes != null)
                {
                    // Make a copy since nodes may reorder the list when drawn
                    var nodes = new List <Node>(Graph.Nodes);

                    // Draw nodes
                    foreach (var node in nodes)
                    {
                        NodeEditor.DrawNode(this, node);
                    }

                    // Draw links
                    if (Target is Socket)
                    {
                        var socket = (Socket)Target;

                        if (rectCache.ContainsKey(socket))
                        {
                            DrawConnection(rectCache[socket].center,
                                           Event.current.mousePosition,
                                           socket.IsInput(Graph), true,
                                           Skin.tempLinkColor);
                        }
                    }
                    foreach (var link in Graph.Links)
                    {
                        if (!rectCache.ContainsKey(link.FromSocket) ||
                            !rectCache.ContainsKey(link.ToSocket))
                        {
                            continue;
                        }

                        var from = rectCache[link.FromSocket].center;
                        var to   = rectCache[link.ToSocket].center;

                        var socketType = link.FromSocket.GetSocketType(Graph);
                        var color      = Skin.objectSocketColor;
                        if (socketType == typeof(ExecType))
                        {
                            color = Skin.execSocketColor;
                        }
                        if (socketType.IsPrimitive)
                        {
                            color = Skin.primitiveSocketColor;
                        }

                        if (search.IsOpen)
                        {
                            color = Skin.TintColor(color, Skin.disabledSocketTint);
                        }
                        DrawConnection(from, to, false, false, color);
                    }
                }

                // Draw Tooltip
                if (Tooltip != null)
                {
                    Tooltip.OnGUI();
                }

                // Search box
                search.OnGUI(this);

                GUI.EndClip();
            }

            if (Graph == null)
            {
                CenterViewOn(Vector2.zero);

                var menuRect = new Rect();
                menuRect.size   = new Vector2(300, 200);
                menuRect.center = graphRect.center;
                GUI.Box(menuRect, GUIContent.none);
                GUILayout.BeginArea(menuRect);
                GUILayout.FlexibleSpace();
                GUILayout.BeginHorizontal(GUILayout.Height(150));
                GUILayout.FlexibleSpace();
                GUILayout.BeginVertical(GUILayout.Width(250));

                GUILayout.FlexibleSpace();

                GUILayout.BeginHorizontal();
                var size      = GUI.skin.label.fontSize;
                var alignment = GUI.skin.label.alignment;
                GUI.skin.label.fontSize  = 20;
                GUI.skin.label.alignment = TextAnchor.MiddleCenter;
                GUILayout.Label("Leylines");
                GUI.skin.label.fontSize  = size;
                GUI.skin.label.alignment = alignment;
                GUILayout.EndHorizontal();

                GUILayout.FlexibleSpace();

                GUILayout.BeginVertical(GUILayout.Height(130));
                var rich = GUI.skin.label.richText;
                GUI.skin.label.richText  = true;
                GUI.skin.label.alignment = TextAnchor.MiddleCenter;
                GUILayout.Label("<b>No Graph Loaded</b>");
                GUI.skin.label.richText  = rich;
                GUI.skin.label.alignment = alignment;
                GUILayout.EndVertical();

                GUILayout.EndVertical();
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
                GUILayout.FlexibleSpace();
                GUILayout.EndArea();
            }

            switch (Event.current.type)
            {
            case EventType.MouseDown:
                Target = this;
                GUI.FocusControl(null);
                Event.current.Use();
                break;

            case EventType.MouseDrag:
                if (Graph == null)
                {
                    break;
                }
                if (ReferenceEquals(Target, this))
                {
                    Offset += Event.current.delta;
                    Event.current.Use();
                }
                break;

            case EventType.MouseUp:
                if (ReferenceEquals(Target, this))
                {
                    Target = null;
                    GUI.FocusControl(null);
                }
                if (Target != null && Target.GetType() == typeof(Socket))
                {
                    var clipPos = Event.current.mousePosition;
                    clipPos.y -= topToolbarRect.size.y + GRAPH_PADDING;
                    var socket = (Socket)Target;
                    if (socket.IsInput(Graph))
                    {
                        search.SetWantedOutputContext(this, socket);
                    }
                    else
                    {
                        search.SetWantedInputContext(this, socket);
                    }
                    search.Open(clipPos, GraphPosition, Graph.Policy, true);
                    Target = search;
                    GUI.FocusControl("search_field");
                    Event.current.Use();
                }
                if (Event.current.button == 1 && Graph != null)
                {
                    if (graphRect.Contains(Event.current.mousePosition))
                    {
                        var clipPos = Event.current.mousePosition;
                        clipPos.y -= topToolbarRect.size.y + GRAPH_PADDING;
                        search.UnsetContext();
                        search.Open(clipPos, GraphPosition, Graph.Policy);
                        Target = search;
                        GUI.FocusControl("search_field");
                        Event.current.Use();
                    }
                }
                break;

            case EventType.DragUpdated:
                if (DragAndDrop.objectReferences.Length == 1)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                }
                else
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
                }
                Event.current.Use();
                break;

            case EventType.DragPerform:
                if (DragAndDrop.objectReferences.Length == 1)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    DragAndDrop.AcceptDrag();

                    var obj  = DragAndDrop.objectReferences[0];
                    var type = obj.GetType();

                    var node   = CreateInstance <DynamicNode>();
                    var socket = new DynamicSocket(type.Name, type, type.Name,
                                                   SocketFlags.AllowMultipleLinks | SocketFlags.Editable);
                    socket.SocketValue = obj;
                    node.DisplayName   = obj.GetType().Name;
                    node.AddOutputSocket(socket);
                    AddNode(node, GraphPosition);
                }
                else
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
                }
                Event.current.Use();
                break;
            }

            Repaint();

            if (GUI.changed && Graph != null && !Graph.Equals(null))
            {
                EditorUtility.SetDirty(Graph);
            }
        }