Ejemplo n.º 1
0
        void OnGUI()
        {
            if (list == null || list.Equals(null))
            {
                Close();
                return;
            }

            XGUI.ResetToStyle(null);
            XGUI.BeginVertical();

            XGUI.ResetToStyle(GUI.skin.textField);

            XGUI.LabelWidth = 40;
            varName         = XGUI.TextField("Name", varName);
            typeStr         = XGUI.TextField("Type", typeStr);

            XGUI.ResetToStyle(null);
            XGUI.BeginHorizontal();

            XGUI.ResetToStyle(GUI.skin.button);
            if (XGUI.Button("Cancel"))
            {
                Close();
                return;
            }

            XGUI.Enabled = IsValid();
            if (XGUI.Button("Create"))
            {
                var value = new DynamicValue();
                value.TypeString = typeStr;
                list.list.Add(new Variable(varName, value));
                editor.SaveList();
                Close();
                return;
            }

            XGUI.EndHorizontal();

            if (!IsNameValid())
            {
                EditorGUILayout.HelpBox(
                    "Variable name is empty!",
                    MessageType.Warning);
            }
            else if (!IsNameUnique())
            {
                EditorGUILayout.HelpBox(
                    "Variable name is the same as an existing variable!",
                    MessageType.Warning);
            }
            else if (!IsTypeValid())
            {
                EditorGUILayout.HelpBox(
                    "Variable type does not exist!",
                    MessageType.Warning);
            }

            XGUI.EndVertical();
        }
Ejemplo n.º 2
0
        private bool DrawNodeResults
            (GraphEditor editor, string newSearchStr, bool keysUsed)
        {
            if (searchStr != newSearchStr || justOpened)
            {
                justOpened = false;

                if (searchJob != null)
                {
                    searchJob.IsRunning = false;
                }

                // Define variables for capture
                var contextType    = this.contextWantsType;
                var contextIsInput = this.contextWantsInput;
                var newResults     = new SyncList <SearchResult>();
                results = newResults;

                // Perform search
                searchJob = new Job((job) =>
                {
                    var scores = new List <int>();

                    int timeout = 0;
                    int n       = 0;
                    while (job.IsRunning)
                    {
                        // Check if the search items have changed (and restart
                        // the search if so)
                        if (n > policy.SearchItems.Count)
                        {
                            scores.Clear();
                            newResults.Clear();
                            n = 0;
                        }

                        // Get the next item, if available
                        if (n == policy.SearchItems.Count)
                        {
                            if (timeout > 0)
                            {
                                break;
                            }

                            timeout++;
                            Thread.Sleep(1000);
                            continue;
                        }

                        timeout  = 0;
                        var item = policy.SearchItems[n++];

                        if (contextType != null)
                        {
                            if (!item.MatchesContext(contextIsInput, contextType))
                            {
                                continue;
                            }
                        }

                        // Score the item and insert it
                        var score = FuzzySearch(newSearchStr, item.Label);
                        if (score == int.MinValue)
                        {
                            continue;
                        }

                        int i = 0;
                        for (; i < scores.Count; ++i)
                        {
                            if (score > scores[i] ||
                                (score == scores[i] && item.Label.Length < newResults[i].Label.Length)
                                )
                            {
                                scores.Insert(i, score);
                                newResults.Insert(i, item);
                                break;
                            }
                        }

                        if (i == scores.Count)
                        {
                            scores.Add(score);
                            newResults.Add(item);
                        }
                    }
                }).Start();
            }
            searchStr = newSearchStr;

            // Update the count
            if (Event.current.type != EventType.Repaint && Event.current.type != EventType.ExecuteCommand)
            {
                resultCount = results == null ? 0 : results.Count;
            }

            selected = Mathf.Clamp(selected, 0, resultCount - 1);
            if (keysUsed)
            {
                if (selected > Mathf.FloorToInt(scrollPos) + 11)
                {
                    scrollPos = selected - 11;
                }
                else if (selected < scrollPos)
                {
                    scrollPos = selected;
                }
            }
            scrollPos = Mathf.Clamp(scrollPos, 0, Mathf.Max(0, resultCount - 12));

            XGUI.ResetToStyle(null);
            XGUI.BeginHorizontal();
            XGUI.BeginVertical();
            // Show results
            int  index            = Mathf.Clamp((int)scrollPos, 0, resultCount);
            bool hoveringOnResult = false;

            for (int i = index; i < Mathf.Min(index + 12, resultCount); ++i)
            {
                var result = results[i];

                XGUI.ResetToStyle(GUI.skin.label);
                if (i == selected)
                {
                    XGUI.Normal.background = GetHighlightTex();
                }
                XGUI.BeginHorizontal();

                XGUI.ResetToStyle(GUI.skin.label);
                if (i == selected)
                {
                    XGUI.Normal.textColor = Color.white;
                }
                XGUI.RichText  = true;
                XGUI.Alignment = TextAnchor.MiddleLeft;
                var score     = "" + FuzzySearch(searchStr, result.Label);
                var highlight = FuzzyHighlight(searchStr, result.Label);
                XGUI.Label(score, XGUI.Width(30));
                XGUI.Label(highlight, XGUI.ExpandWidth(true));

                XGUI.EndHorizontal();

                var lastRect = GUILayoutUtility.GetLastRect();
                if (lastRect.Contains(Event.current.mousePosition))
                {
                    if (Event.current.type == EventType.MouseMove)
                    {
                        selected = i;
                        Event.current.Use();
                    }
                    hoveringOnResult = selected == i;
                }
            }
            // No results
            if (resultCount == 0)
            {
                XGUI.ResetToStyle(GUI.skin.label);
                XGUI.Alignment = TextAnchor.LowerCenter;
                XGUI.Enabled   = false;
                XGUI.FontStyle = FontStyle.Italic;
                XGUI.Label("No results", XGUI.MaxHeight(30));
            }
            XGUI.EndVertical();
            scrollPos = GUILayout.VerticalScrollbar
                            (scrollPos, Mathf.Min(resultCount, 12),
                            0, Mathf.Max(resultCount, 12), GUILayout.ExpandHeight(true));
            XGUI.EndHorizontal();

            switch (Event.current.type)
            {
            case EventType.ScrollWheel:
                scrollPos += Event.current.delta.y;
                Event.current.Use();
                break;

            // Detect key events in the search field
            case EventType.Used:
                switch (Event.current.keyCode)
                {
                case KeyCode.KeypadEnter:
                case KeyCode.Return:
                    SelectResult(editor, selected);
                    Close();
                    break;
                }
                break;
            }

            return(hoveringOnResult);
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
0
        private void DrawVariableResults(GraphEditor editor)
        {
            var variables = editor.Graph.Variables;

            if (variables.AsList().Count == 0)
            {
                XGUI.ResetToStyle(GUI.skin.label);
                XGUI.Alignment = TextAnchor.LowerCenter;
                XGUI.Enabled   = false;
                XGUI.FontStyle = FontStyle.Italic;
                XGUI.Label("No variables", XGUI.MaxHeight(30));
            }
            else
            {
                varScrollPos = GUILayout.BeginScrollView(varScrollPos);
                foreach (var variable in variables)
                {
                    XGUI.ResetToStyle(null);
                    XGUI.BeginHorizontal();

                    XGUI.ResetToStyle(GUI.skin.button);
                    if (XGUI.Button("Get", XGUI.Width(40)))
                    {
                        var node = ScriptableObject.CreateInstance <DynamicNode>();
                        node.name = "Get";

                        node.AddOutputSocket(new DynamicSocket(
                                                 variable.Name, variable.Value.Type, variable.Name));

                        node.AddEvalInvoke(new EvalInvoke(
                                               variable.Name, variable.Name, variable.Name, InvokeType.GetVar));
                        editor.AddNode(node, spawnPosition);
                    }
                    if (XGUI.Button("Set", XGUI.Width(40)))
                    {
                        var node = ScriptableObject.CreateInstance <DynamicNode>();
                        node.name = "Set";
                        node.SetInputWidth(60);

                        node.AddInputSocket(new DynamicSocket(
                                                "Exec", typeof(ExecType), "execIn"));
                        node.AddInputSocket(new DynamicSocket(
                                                "Value", variable.Value.Type, "value", SocketFlags.Editable));

                        node.AddOutputSocket(new DynamicSocket(
                                                 "Exec", typeof(ExecType), "execOut"));
                        node.AddOutputSocket(new DynamicSocket(
                                                 variable.Name, variable.Value.Type, variable.Name));

                        node.AddExecInvoke(new ExecInvoke(
                                               "execIn", "execOut", "newValue", variable.Name, variable.Name, InvokeType.SetVar));
                        editor.AddNode(node, spawnPosition);
                    }

                    XGUI.ResetToStyle(GUI.skin.label);
                    XGUI.Label(variable.Name);

                    XGUI.EndHorizontal();
                }

                GUILayout.EndScrollView();
            }
        }