Exemple #1
0
        private void InputGUI()
        {
            Vector2 delta = current.delta;

            switch (current.type)
            {
            case EventType.MouseDown:
                GraphEditor.mouse_down_position = GraphEditor.mouse_position;
                switch (current.button)
                {
                case 0:
                    if (current.alt)
                    {
                        break;
                    }
                    if (GraphEditor.hover_node)
                    {
                        if (current.clickCount >= 2)
                        {
                            if (GraphEditor.hover_node is MacroNode)
                            {
                                SetGraphAsset(((MacroNode)GraphEditor.hover_node).reference);
                                current.Use();
                                return;
                            }
                            else
                            {
                                EditorUtils.OpenScriptByType(GraphEditor.hover_node.type);
                                current.Use();
                            }
                        }
                        if (GraphEditor.hover_port)
                        {
                            if (current.control)
                            {
                                if (GraphEditor.hover_port is OutputAction)
                                {
                                    UndoManager.RecordObject(GraphEditor.asset, string.Format("{0} Add Branch", GraphEditor.asset.name));
                                    set_dirty = true;
                                    QuickPlugPort(GraphEditor.hover_port, AddNode <Branch>(GraphEditor.mouse_position / GraphEditor.zoom - GraphEditor.scroll + new Vector2(60.0f, 0.0f)));
                                }
                            }
                            else
                            {
                                if (current.shift)
                                {
                                    GraphEditor.can_drag_node = false;
                                    GraphEditor.can_drag_port = false;
                                    if (GraphEditor.hover_port is IPlug)
                                    {
                                        UndoManager.RecordObject(GraphEditor.asset, string.Format("{0} > {1}: {2} Unplug", GraphEditor.asset.name, GraphEditor.hover_node.name, GraphEditor.hover_port.name));
                                        set_dirty = true;
                                        ((IPlug)GraphEditor.hover_port).Unplug();
                                    }
                                    current.Use();
                                }
                                else
                                {
                                    GraphEditor.can_drag_node = false;
                                    GraphEditor.can_drag_port = true;
                                    SelectOnlyNode(GraphEditor.hover_node);
                                    current.Use();
                                }
                            }
                        }
                        else
                        {
                            if (current.shift)
                            {
                                if (GraphEditor.hover_node.is_selected)
                                {
                                    GraphEditor.can_drag_node = false;
                                    DeselectNode(GraphEditor.hover_node);
                                    current.Use();
                                }
                                else
                                {
                                    GraphEditor.can_drag_node = true;
                                    SelectNode(GraphEditor.hover_node);
                                    current.Use();
                                }
                            }
                            else
                            {
                                if (GraphEditor.hover_node.is_selected)
                                {
                                    GraphEditor.can_drag_node = true;
                                }
                                else
                                {
                                    GraphEditor.can_drag_node = true;
                                    SelectOnlyNode(GraphEditor.hover_node);
                                    current.Use();
                                }
                            }
                        }
                    }
                    else if (editable_area.Contains(GraphEditor.mouse_position))
                    {
                        GraphEditor.can_select = true;
                    }
                    break;

                case 1:
                    break;

                case 2:
                    break;
                }
                break;

            case EventType.MouseUp:
                switch (current.button)
                {
                case 0:
                    if (GraphEditor.can_select && GraphEditor.is_select)
                    {
                        Rect        select_box_in_scroll = new Rect(select_box.position / target_zoom, select_box.size / target_zoom);
                        List <Node> nodes = GraphEditor.graph.nodes.Where((n) => select_box_in_scroll.Overlaps(n.nodeRect)).ToList();
                        if (current.shift)
                        {
                            foreach (Node node in nodes)
                            {
                                if (node.is_selected)
                                {
                                    DeselectNode(node);
                                }
                                else
                                {
                                    SelectNode(node);
                                }
                            }
                            current.Use();
                        }
                        else
                        {
                            ClearSelection();
                            SelectNodes(nodes);
                            current.Use();
                        }
                    }
                    else
                    {
                        if (GraphEditor.hover_node)
                        {
                            if (GraphEditor.is_drag)
                            {
                                if (GraphEditor.drag_port)
                                {
                                    if (GraphEditor.hover_port)
                                    {
                                        PlugPort(GraphEditor.drag_port, GraphEditor.hover_port);
                                    }
                                    else
                                    {
                                        QuickPlugPort(GraphEditor.drag_port, GraphEditor.hover_node);
                                    }
                                    current.Use();
                                }
                            }
                            else
                            {
                                if (!current.shift)
                                {
                                    if (GraphEditor.selection.Count > 1)
                                    {
                                        SelectOnlyNode(GraphEditor.hover_node);
                                        current.Use();
                                    }
                                }
                            }
                        }
                        else if (!GraphEditor.is_scrolling)
                        {
                            ClearSelection();
                        }
                        if (GraphEditor.drag_port && !GraphEditor.hover_node)
                        {
                            InputAction  in_act    = GraphEditor.drag_port as InputAction;
                            OutputAction out_act   = GraphEditor.drag_port as OutputAction;
                            IInputValue  in_value  = GraphEditor.drag_port as IInputValue;
                            IOutputValue out_value = GraphEditor.drag_port as IOutputValue;
                            if (in_act != null)
                            {
                                GraphEditor.waiting_for_new_node = true;
                                AdvancedSearchWindow.Init(GraphEditor.mouse_position, "#tag:in void");
                            }
                            else if (out_act != null)
                            {
                                GraphEditor.waiting_for_new_node = true;
                                AdvancedSearchWindow.Init(GraphEditor.mouse_position, "#tag:out void");
                            }
                            else if (in_value != null)
                            {
                                GraphEditor.waiting_for_new_node = true;
                                AdvancedSearchWindow.Init(GraphEditor.mouse_position, "#tag:out " + in_value.valueType.GetTypeName(false, true));
                            }
                            else if (out_value != null)
                            {
                                GraphEditor.waiting_for_new_node = true;
                                AdvancedSearchWindow.Init(GraphEditor.mouse_position, "#tag:in " + out_value.valueType.GetTypeName(false, true));
                            }
                        }
                    }
                    GraphEditor.can_select   = false;
                    GraphEditor.is_select    = false;
                    GraphEditor.is_scrolling = false;
                    GraphEditor.hover_node   = null;
                    GraphEditor.drag_node    = null;
                    if (!GraphEditor.waiting_for_new_node)
                    {
                        GraphEditor.is_drag       = false;
                        GraphEditor.drag_port     = null;
                        GraphEditor.can_drag_port = false;
                    }
                    GraphEditor.can_drag_node = false;
                    break;

                case 1:
                    if (!GraphEditor.is_drag)
                    {
                        AdvancedSearchWindow.Init(GraphEditor.mouse_position);
                        current.Use();
                    }
                    GraphEditor.is_drag = false;
                    break;

                case 2:
                    break;
                }
                break;

            case EventType.MouseDrag:
                switch (current.button)
                {
                case 0:
                    if (current.alt)
                    {
                        GraphEditor.is_drag      = true;
                        GraphEditor.is_scrolling = true;
                        GraphEditor.scroll      += delta;
                        break;
                    }
                    if (!GraphEditor.is_drag)
                    {
                        if (GraphEditor.can_drag_port && GraphEditor.hover_port)
                        {
                            GraphEditor.is_drag       = true;
                            GraphEditor.can_drag_node = false;
                            GraphEditor.drag_port     = GraphEditor.hover_port;
                        }
                        if (GraphEditor.can_drag_node && GraphEditor.hover_node)
                        {
                            GraphEditor.is_drag       = true;
                            GraphEditor.can_drag_port = false;
                            GraphEditor.drag_node     = GraphEditor.hover_node;
                        }
                    }
                    // ...
                    if (GraphEditor.is_drag)
                    {
                        if (GraphEditor.drag_node)
                        {
                            foreach (Node node in GraphEditor.selection)
                            {
                                node.position += delta;
                            }
                        }
                    }
                    else
                    {
                        if (GraphEditor.can_select)
                        {
                            GraphEditor.is_select = true;
                        }
                    }
                    break;

                case 1:
                    GraphEditor.is_drag      = true;
                    GraphEditor.is_scrolling = true;
                    GraphEditor.scroll      += delta;
                    break;

                case 2:
                    GraphEditor.is_drag      = true;
                    GraphEditor.is_scrolling = true;
                    GraphEditor.scroll      += delta;
                    break;
                }
                break;

            case EventType.ScrollWheel:
                if (!GraphEditor.is_drag)
                {
                    float zoom_delta = 0.1f;
                    zoom_delta   = current.delta.y < 0.0f ? zoom_delta : -zoom_delta;
                    target_zoom += zoom_delta;
                    target_zoom  = Mathf.Clamp(target_zoom, 0.2f, 1.0f);
                }
                break;

            case EventType.ValidateCommand:
                switch (Event.current.commandName)
                {
                case ("Delete"):
                    DeleteSelectedNodes();
                    current.Use();
                    break;

                case ("Duplicate"):
                    DuplicateSelectedNodes();
                    current.Use();
                    break;

                case ("SelectAll"):
                    if (GraphEditor.selection.Count == GraphEditor.graph.nodeCount)
                    {
                        ClearSelection();
                        current.Use();
                    }
                    else
                    {
                        SelectNodes(GraphEditor.graph.nodes);
                        current.Use();
                    }
                    break;
                }
                break;

            case EventType.DragUpdated:
            case EventType.DragPerform:
                if (DragAndDrop.objectReferences.Length > 0)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    if (current.type == EventType.DragPerform)
                    {
                        DragAndDrop.AcceptDrag();
                        GenericMenu generic_menu = new GenericMenu();
                        Type        type;
                        string      type_name;
                        foreach (UnityObject drag_obj in DragAndDrop.objectReferences)
                        {
                            if (drag_obj == null)
                            {
                                continue;
                            }
                            if (drag_obj is GraphAsset)
                            {
                                GraphAsset m_asset = (GraphAsset)drag_obj;
                                if (m_asset)
                                {
                                    generic_menu.AddItem(new GUIContent(string.Format("Add {0} as Macro", m_asset.title.IsNullOrEmpty() ? m_asset.name : m_asset.title)), false, (obj) => {
                                        AddMacro(m_asset, (Vector2)obj);
                                    }, GraphEditor.mouse_position - GraphEditor.scroll);
                                }
                            }
                            else if (drag_obj is MonoScript)
                            {
                                MonoScript script = (MonoScript)drag_obj;
                                if (script)
                                {
                                    type      = script.GetClass();
                                    type_name = type.GetTypeName();

                                    generic_menu.AddItem(new GUIContent(string.Format("{0}/Expose {0} members", type_name)), false, (obj) => {
                                        AddCustomNode <ReflectedObjectNode>((Vector2)obj, true, type);
                                    }, GraphEditor.mouse_position - GraphEditor.scroll);
                                    generic_menu.AddSeparator(string.Format("{0}/", type_name));

                                    AddTypeMethodsToMenu(generic_menu, type, type_name);

                                    if (type.IsSubclassOf(typeof(Node)) && !type.IsAbstract && !type.IsGenericType)
                                    {
                                        generic_menu.AddItem(new GUIContent(string.Format("Add {0} as Node", type_name)), false, (obj) => {
                                            AddNode(type, (Vector2)obj);
                                        }, GraphEditor.mouse_position - GraphEditor.scroll);
                                    }
                                }
                            }

                            if (drag_obj is GraphAsset || drag_obj is MonoScript)
                            {
                                generic_menu.AddSeparator("");
                            }

                            type      = drag_obj.GetType();
                            type_name = type.GetTypeName();

                            generic_menu.AddItem(new GUIContent(string.Format("Expose {0} members", type_name)), false, (obj) => {
                                AddCustomNode <ReflectedObjectNode>((Vector2)obj, true, type);
                            }, GraphEditor.mouse_position - GraphEditor.scroll);

                            AddTypeMethodsToMenu(generic_menu, type, type_name);
                        }
                        DragAndDrop.PrepareStartDrag();
                        generic_menu.ShowAsContext();
                    }
                }
                break;

            case (EventType.KeyDown):
                switch (current.keyCode)
                {
                case KeyCode.Space:
                    if (!current.control)
                    {
                        AdvancedSearchWindow.Init(position.size / 2.0f);
                        current.Use();
                    }
                    break;

                case KeyCode.Delete:
                    DeleteSelectedNodes();
                    current.Use();
                    break;

                case KeyCode.Home:
                    target_zoom        = 1.0f;
                    GraphEditor.scroll = Vector2.zero;
                    current.Use();
                    break;

                case KeyCode.PageDown:
                    GraphEditor.scroll -= new Vector2(0.0f, position.height);
                    current.Use();
                    break;

                case KeyCode.PageUp:
                    GraphEditor.scroll += new Vector2(0.0f, position.height);
                    current.Use();
                    break;
                }
                break;
            }
        }
Exemple #2
0
        public static Texture GetTypeIcon(Type type, bool return_default_icon = true)
        {
            if (type == null)
            {
                if (return_default_icon)
                {
                    return(icons[typeof(Nullable)] = EditorUtils.FindAssetByName <Texture>("http_status_not_found"));
                }
                return(null);
            }
            Texture t;

            if (icons.TryGetValue(type, out t))
            {
                return(t);
            }
            string path;

            if (paths.TryGetValue(type, out path))
            {
                return(icons[type] = EditorUtils.FindAssetByName <Texture>(path));
            }
            IconAttribute icon_flag = type.GetAttribute <IconAttribute>(false);

            if (icon_flag != null)
            {
                return(icons[type] = icon_flag.GetIcon());
            }
            else if (typeof(UnityObject).IsAssignableFrom(type))
            {
                return(icons[type] = EditorGUIUtility.ObjectContent(null, type).image);
            }
            else if (type.IsSubclassOf(typeof(EventNode)))
            {
                return(icons[type] = GetTypeIcon(typeof(EventNode), return_default_icon));
            }
            else
            {
                Type base_type = type;
                while (!base_type.IsGenericType)
                {
                    if (base_type.BaseType == null)
                    {
                        break;
                    }
                    base_type = base_type.BaseType;
                }
                if (base_type.IsGenericType)
                {
                    t = GetTypeIcon(base_type.GetGenericArguments()[0], false);
                    if (t != null)
                    {
                        return(icons[type] = t);
                    }
                }
            }
            if (return_default_icon)
            {
                return(icons[type] = EditorUtils.FindAssetByName <Texture>("http_status_not_found"));
            }
            else
            {
                return(null);
            }
        }