示例#1
0
 public void SetWantedOutputContext(GraphEditor editor, Socket socket)
 {
     context           = socket;
     contextWantsInput = false;
     contextWantsType  = socket.GetSocketType(editor.Graph);
 }
示例#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);
        }
示例#3
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();
            }
        }
示例#4
0
        public void OnGUI(GraphEditor editor)
        {
            if (editor.Target != this)
            {
                Close();
                return;
            }
            if (!isOpen)
            {
                return;
            }

            GUI.enabled = true;

            var rect = new Rect();

            rect.size   = size;
            rect.center = position + new Vector2(0, rect.size.y / 2);
            XGUI.ResetToStyle(GUI.skin.box);
            XGUI.Normal.background = GraphEditor.boxTexture;
            XGUI.Box(rect);

            rect.position += Vector2.one * SEARCH_PADDING;
            rect.size     -= Vector2.one * SEARCH_PADDING * 2;
            XGUI.ResetToStyle(null);
            XGUI.BeginArea(rect);
            XGUI.BeginVertical();

            if (XEvent.IsKeyDown(KeyCode.Escape))
            {
                Close();
                XEvent.Use();
            }

            // Search bar
            XGUI.BeginHorizontal();
            XGUI.ResetToStyle(GUI.skin.button);
            var buttonString = searchNodes ? "Nodes" : "Variables";

            if (XGUI.Button(buttonString, XGUI.Width(70)))
            {
                searchNodes = !searchNodes;
            }

            bool keysUsed     = false;
            var  newSearchStr = searchStr;

            if (searchNodes)
            {
                // Detect key events before the text field
                switch (Event.current.type)
                {
                case EventType.KeyDown:
                    switch (Event.current.keyCode)
                    {
                    case KeyCode.UpArrow:
                        selected--;
                        Event.current.Use();
                        break;

                    case KeyCode.DownArrow:
                        selected++;
                        Event.current.Use();
                        break;

                    case KeyCode.Home:
                        selected = 0;
                        Event.current.Use();
                        break;

                    case KeyCode.End:
                        selected = int.MaxValue;
                        Event.current.Use();
                        break;

                    case KeyCode.PageUp:
                        selected -= 11;
                        Event.current.Use();
                        break;

                    case KeyCode.PageDown:
                        selected += 11;
                        Event.current.Use();
                        break;
                    }

                    keysUsed = Event.current.type == EventType.Used;
                    break;
                }

                XGUI.ResetToStyle(GUI.skin.textField);
                GUI.SetNextControlName("search_field");
                newSearchStr = XGUI.TextField(searchStr);
                GUI.FocusControl("search_field");

                var countStr = "" + resultCount;
                if (searchJob != null && searchJob.IsRunning)
                {
                    countStr += "...";
                }
                GUILayout.Label(countStr, GUILayout.ExpandWidth(false));
            }
            GUILayout.EndHorizontal();

            var hoveringOnResult = false;

            if (searchNodes)
            {
                hoveringOnResult = DrawNodeResults(editor, newSearchStr, keysUsed);
            }
            else
            {
                DrawVariableResults(editor);
            }

            GUILayout.EndVertical();
            GUILayout.EndArea();

            switch (Event.current.type)
            {
            case EventType.MouseDown:
                if (hoveringOnResult)
                {
                    SelectResult(editor, selected);
                    Close();
                }
                else if (rect.Contains(Event.current.mousePosition))
                {
                    editor.Target = this;
                    Event.current.Use();
                }
                break;

            case EventType.MouseUp:
                if (rect.Contains(Event.current.mousePosition))
                {
                    editor.Target = this;
                    Event.current.Use();
                }
                break;
            }
        }
示例#5
0
        public static void DrawSocket
            (GraphEditor editor, Node node, Socket socket, Rect rect)
        {
            var graph = editor.Graph;

            GUI.Box(rect, GUIContent.none);

            editor.rectCache[socket] = rect;

            switch (Event.current.type)
            {
            case EventType.MouseDown:
                if (rect.Contains(Event.current.mousePosition))
                {
                    editor.Target = socket;
                    Event.current.Use();
                }
                break;

            case EventType.MouseUp:
                if (!(editor.Target is Socket))
                {
                    break;
                }
                if (!rect.Contains(Event.current.mousePosition))
                {
                    break;
                }

                var otherSocket = (Socket)editor.Target;
                if (socket.Equals(otherSocket))
                {
                    editor.Target = null;
                    break;
                }
                if (socket.NodeID == otherSocket.NodeID)
                {
                    editor.Target = null;
                    break;
                }
                if (socket.IsInput(graph) == otherSocket.IsInput(graph))
                {
                    editor.Target = null;
                    break;
                }
                if (socket.GetSocketType(graph) != otherSocket.GetSocketType(graph))
                {
                    if (!socket.GetSocketType(graph)
                        .IsAssignableFrom(otherSocket.GetSocketType(graph)))
                    {
                        editor.Target = null;
                        break;
                    }
                }

                // Record to graph
                Undo.RegisterCompleteObjectUndo(graph,
                                                string.Format("Link {0} and {1}",
                                                              socket,
                                                              otherSocket
                                                              )
                                                );
                if (!socket.AllowsMultipleLinks(graph))
                {
                    graph.Links.RemoveAllWith(socket);
                }
                if (!otherSocket.AllowsMultipleLinks(graph))
                {
                    graph.Links.RemoveAllWith(otherSocket);
                }
                graph.Links.Add(graph, socket, otherSocket);

                editor.Target = null;

                GUI.changed = true;
                Event.current.Use();
                break;
            }
        }
示例#6
0
        public static void DrawSocket
            (GraphEditor editor, Node node, Socket socket, Rect rect)
        {
            editor.rectCache[socket] = rect;

            var socketType = socket.GetSocketType(editor.Graph);
            var color      = GraphEditor.Skin.objectSocketColor;
            var style      = GraphEditor.Skin.paramSocketStyle;

            if (socketType == typeof(ExecType))
            {
                color = GraphEditor.Skin.execSocketColor;
                style = GraphEditor.Skin.execSocketStyle;
            }
            else if (socketType.IsPrimitive)
            {
                color = GraphEditor.Skin.primitiveSocketColor;
            }

            var on = editor.Graph.Links.IsSocketLinkedTo(socket) ||
                     editor.Graph.Links.IsSocketLinkedFrom(socket);

            XGUI.ResetToStyle(null);
            XGUI.Normal.background = on ? style.onSocketTexture : style.offSocketTexture;
            XGUI.Color             = on ? color : GraphEditor.Skin.TintColor(color, GraphEditor.Skin.offSocketTint);
            XGUI.Color             = !editor.search.IsOpen ? XGUI.Color : GraphEditor.Skin.TintColor(XGUI.Color, GraphEditor.Skin.disabledSocketTint);
            XGUI.Box(rect);

            if (!GUI.enabled)
            {
                return;
            }

            switch (Event.current.type)
            {
            case EventType.MouseDown:
                if (rect.Contains(Event.current.mousePosition))
                {
                    if (Event.current.control)
                    {
                        editor.Graph.Links.RemoveAllWith(socket);
                    }
                    else
                    {
                        editor.Target = socket;
                    }
                    Event.current.Use();
                }
                break;

            case EventType.MouseUp:
                if (!(editor.Target is Socket))
                {
                    break;
                }
                if (!rect.Contains(Event.current.mousePosition))
                {
                    break;
                }

                var otherSocket = (Socket)editor.Target;
                if (socket.Equals(otherSocket))
                {
                    editor.Target = null;
                    break;
                }
                if (socket.NodeID == otherSocket.NodeID)
                {
                    editor.Target = null;
                    break;
                }

                var graph = editor.Graph;

                if (socket.IsInput(graph) == otherSocket.IsInput(graph))
                {
                    editor.Target = null;
                    break;
                }
                if (socket.GetSocketType(graph) != otherSocket.GetSocketType(graph))
                {
                    if (!socket.GetSocketType(graph)
                        .IsAssignableFrom(otherSocket.GetSocketType(graph)))
                    {
                        editor.Target = null;
                        break;
                    }
                }

                // Record to graph
                Undo.RegisterCompleteObjectUndo(graph,
                                                string.Format("Link {0} and {1}",
                                                              socket,
                                                              otherSocket
                                                              )
                                                );
                if (!socket.GetFlags(graph).AllowMultipleLinks())
                {
                    graph.Links.RemoveAllWith(socket);
                }
                if (!otherSocket.GetFlags(graph).AllowMultipleLinks())
                {
                    graph.Links.RemoveAllWith(otherSocket);
                }
                graph.Links.Add(graph, socket, otherSocket);

                editor.Target = null;

                GUI.changed = true;
                Event.current.Use();
                break;
            }
        }