Example #1
0
        public void SlotDragging(Graphs.Slot slot, bool allowEndDrag, bool allowMultiple)
        {
            Debug.Assert(allowMultiple);

            if (!allowEndDrag)
            {
                return;
            }
            if (_dragSourceSlot == null || _dragSourceSlot == slot)
            {
                return;
            }

            // Is this slot can be a drop target?
            if (_dropTarget != slot &&
                slot.node.graph.CanConnect(_dragSourceSlot, slot) &&
                !slot.node.graph.Connected(_dragSourceSlot, slot))
            {
                _dropTarget = slot;
            }

            Event.current.Use();
        }
        // Disconnect given two slots.
        public static void DisconnectSlots(Graphs.Slot fromSlot, Graphs.Slot toSlot)
        {
            var nodeTo       = ((Block)toSlot.node).runtimeInstance;
            var triggerEvent = GetEventOfOutputSlot(fromSlot);
            var targetMethod = GetMethodOfInputSlot(toSlot);

            if (targetMethod == null)
            {
                return;
            }
            var methodName = targetMethod.Name;

            var eventCount = triggerEvent.GetPersistentEventCount();

            for (var i = 0; i < eventCount; i++)
            {
                if (nodeTo == triggerEvent.GetPersistentTarget(i) &&
                    methodName == triggerEvent.GetPersistentMethodName(i))
                {
                    UnityEventTools.RemovePersistentListener(triggerEvent, i);
                    break;
                }
            }
        }
Example #3
0
        // Create a connection between two slots.
        public static bool ConnectSlots(Graphs.Slot fromSlot, Graphs.Slot toSlot)
        {
            EnumTypes();
            var nodeTo       = ((Node)toSlot.node).runtimeInstance;
            var triggerEvent = GetEventOfOutputSlot(fromSlot);
            var targetMethod = GetMethodOfInputSlot(toSlot);

            // Determine the type of the target action.
            var actionType = GetUnityActionToInvokeMethod(targetMethod);

            if (actionType == null)
            {
                return(false);                    // invalid target method type
            }
            // Create an action that is bound to the target method.
            var targetAction = Delegate.CreateDelegate(
                actionType, nodeTo, targetMethod
                );

            if (triggerEvent is UnityEvent)
            {
                // The trigger event has no parameter.
                // Add the action to the event with a default parameter.
                if (actionType == typeof(UnityAction))
                {
                    UnityEventTools.AddVoidPersistentListener(
                        triggerEvent, (UnityAction)targetAction
                        );
                    return(true);
                }
                if (actionType == typeof(UnityAction <float>))
                {
                    UnityEventTools.AddFloatPersistentListener(
                        triggerEvent, (UnityAction <float>)targetAction, 1.0f
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <float> )
            {
                // The trigger event has a float parameter.
                // Then the target method should have a float parameter too.
                if (actionType == typeof(UnityAction <float>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <float>)triggerEvent,
                        (UnityAction <float>)targetAction
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <int> )
            {
                // The trigger event has a int parameter.
                // Then the target method should have a int parameter too.
                if (actionType == typeof(UnityAction <int>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <int>)triggerEvent,
                        (UnityAction <int>)targetAction
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <string> )
            {
                // The trigger event has a string parameter.
                // Then the target method should have a string parameter too.
                if (actionType == typeof(UnityAction <string>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <string>)triggerEvent,
                        (UnityAction <string>)targetAction
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <Vector3> )
            {
                // The trigger event has a Vector3 parameter.
                // Then the target method should have a Vector3 parameter too.
                if (actionType == typeof(UnityAction <Vector3>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <Vector3>)triggerEvent,
                        (UnityAction <Vector3>)targetAction
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <Quaternion> )
            {
                // The trigger event has a Quaternion parameter.
                // Then the target method should have a Quaternion parameter too.
                if (actionType == typeof(UnityAction <Quaternion>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <Quaternion>)triggerEvent,
                        (UnityAction <Quaternion>)targetAction
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <Color> )
            {
                // The trigger event has a color parameter.
                // Then the target method should have a color parameter too.
                if (actionType == typeof(UnityAction <Color>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <Color>)triggerEvent,
                        (UnityAction <Color>)targetAction
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <Texture> )
            {
                // The trigger event has a color parameter.
                // Then the target method should have a color parameter too.
                if (actionType == typeof(UnityAction <Texture>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <Texture>)triggerEvent,
                        (UnityAction <Texture>)targetAction
                        );
                    return(true);
                }
            }

            return(false); // trigger-target mismatch
        }
Example #4
0
        // Returns a method that is bound to a given input slot.
        static MethodInfo GetMethodOfInputSlot(Graphs.Slot slot)
        {
            var node = ((Node)slot.node).runtimeInstance;

            return(node.GetType().GetMethod(slot.name));
        }
Example #5
0
 public void EndSlotDragging(Graphs.Slot slot, bool allowMultiple)
 {
 }
Example #6
0
 public void BeginSlotDragging(Graphs.Slot slot, bool allowStartDrag, bool allowEndDrag)
 {
 }
        // Create a connection between two slots.
        public static bool ConnectSlots(Graphs.Slot fromSlot, Graphs.Slot toSlot)
        {
            EnumTypes();
            var nodeTo       = ((Block)toSlot.node).runtimeInstance;
            var triggerEvent = GetEventOfOutputSlot(fromSlot);
            var targetMethod = GetMethodOfInputSlot(toSlot);

            // Determine the type of the target action.
            var actionType = GetUnityActionToInvokeMethod(targetMethod);

            if (actionType == null)
            {
                return(false); // invalid target method type
            }
            // Create an action that is bound to the target method.
            var targetAction = Delegate.CreateDelegate(
                actionType, nodeTo, targetMethod
                );

            if (triggerEvent is UnityEvent)
            {
                // The trigger event has no parameter.
                // Add the action to the event with a default parameter.
                if (actionType == typeof(UnityAction))
                {
                    UnityEventTools.AddVoidPersistentListener(
                        triggerEvent, (UnityAction)targetAction
                        );
                    return(true);
                }
                if (actionType == typeof(UnityAction <float>))
                {
                    UnityEventTools.AddFloatPersistentListener(
                        triggerEvent, (UnityAction <float>)targetAction, 1.0f
                        );
                    return(true);
                }
            } /*else if (triggerEvent is UnityEvent<float>) {
               *                // The trigger event has a float parameter.
               *                // Then the target method should have a float parameter too.
               *                if (actionType == typeof(UnityAction<float>)) {
               *                        // Add the action to the event.
               *                        UnityEventTools.AddPersistentListener (
               *                                (UnityEvent<float>)triggerEvent,
               *                                (UnityAction<float>)targetAction
               *                        );
               *                        return true;
               *                }
               *        }*/
            else if (triggerEvent is UnityEvent <int> )
            {
                // The trigger event has a int parameter.
                // Then the target method should have a int parameter too.
                if (actionType == typeof(UnityAction <int>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <int>)triggerEvent,
                        (UnityAction <int>)targetAction
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <string> )
            {
                // The trigger event has a string parameter.
                // Then the target method should have a string parameter too.
                if (actionType == typeof(UnityAction <string>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <string>)triggerEvent,
                        (UnityAction <string>)targetAction
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <Vector3> )
            {
                // The trigger event has a Vector3 parameter.
                // Then the target method should have a Vector3 parameter too.
                if (actionType == typeof(UnityAction <Vector3>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <Vector3>)triggerEvent,
                        (UnityAction <Vector3>)targetAction
                        );
                    return(true);
                }
            }/* else if (triggerEvent is UnityEvent<Quaternion>) {
              *                 // The trigger event has a Quaternion parameter.
              *                 // Then the target method should have a Quaternion parameter too.
              *                 if (actionType == typeof(UnityAction<Quaternion>)) {
              *                         // Add the action to the event.
              *                         UnityEventTools.AddPersistentListener (
              *                                 (UnityEvent<Quaternion>)triggerEvent,
              *                                 (UnityAction<Quaternion>)targetAction
              *                         );
              *                         return true;
              *                 }
              *         }*/
            else if (triggerEvent is UnityEvent <Color> )
            {
                // The trigger event has a color parameter.
                // Then the target method should have a color parameter too.
                if (actionType == typeof(UnityAction <Color>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <Color>)triggerEvent,
                        (UnityAction <Color>)targetAction
                        );
                    return(true);
                }
            }
            else if (triggerEvent is UnityEvent <Texture> )
            {
                // The trigger event has a color parameter.
                // Then the target method should have a color parameter too.
                if (actionType == typeof(UnityAction <Texture>))
                {
                    // Add the action to the event.
                    UnityEventTools.AddPersistentListener(
                        (UnityEvent <Texture>)triggerEvent,
                        (UnityAction <Texture>)targetAction
                        );
                    return(true);
                }
            }
            else
            {
                var act = GetActionDataType(actionType);
                var evt = GetEventDataType(triggerEvent.GetType());

                if (act == evt)  //same type
                                 //		UnityEventTools.AddPersistentListener(triggerEvent as UnityEvent,targetAction as UnityAction);
                {
                    if (_AddPersistenceListener == null)
                    {
                        var mi = typeof(UnityEventTools)
                                 .GetMethods();
                        foreach (var m in mi)
                        {
                            if (m.Name == "AddPersistentListener" && m.IsGenericMethod && m.GetGenericArguments().Length == 1)
                            {
                                _AddPersistenceListener = m;
                                break;
                            }
                        }
                    }

                    var actType = GetActionBaseType(targetAction.GetType());
                    var evtType = GetEventBaseType(triggerEvent.GetType());

                    /*	object action,trigger;
                     *
                     *  MethodInfo castMethod = typeof(ConnectionTools).GetMethod("Cast",BindingFlags.Static | BindingFlags.Public );
                     *  action=castMethod.MakeGenericMethod(actType).Invoke (null, new object[]{ targetAction });
                     *
                     *  trigger=castMethod.MakeGenericMethod(evtType).Invoke (null, new object[]{ triggerEvent });*/

                    var mv = _AddPersistenceListener.MakeGenericMethod(new[] { evt });
                    mv.Invoke(null, new object[] { triggerEvent, targetAction });
                    return(true);
                }
            }

            return(false); // trigger-target mismatch
        }
Example #8
0
        static Vector2 GetPositionAsToSlot(Graphs.Slot slot)
        {
            var rect = GetSlotPosition(slot);

            return(GUIClip(new Vector2(rect.x, rect.y + kEdgeSlotYOffset)));
        }
Example #9
0
        public static GroupNode FromNodes(string name, List <Node> nodes, System.Type graphType)
        {
            if (nodes.Count == 0)
            {
                throw new ArgumentException("No nodes to group");
            }
            Graph parentGraph = nodes[0].graph;

            if (parentGraph == null)
            {
                throw new ArgumentException("Nodes needs to be attached to a graph");
            }
            GroupNode node = new GroupNode(name, parentGraph, graphType);

            parentGraph.AddNode(node);
            node.m_ProxyInNode = ProxyNode.Instance(true);
            node.subGraph.AddNode(node.m_ProxyInNode);
            node.m_ProxyOutNode = ProxyNode.Instance(false);
            node.subGraph.AddNode(node.m_ProxyOutNode);
            List <UnityEditor.Graphs.Edge> list = new List <UnityEditor.Graphs.Edge>();

            foreach (Node node2 in nodes)
            {
                list.AddRange(node2.outputEdges);
                list.AddRange(node2.inputEdges);
                node.AddChildNode(node2);
                parentGraph.nodes.Remove(node2);
            }
            foreach (UnityEditor.Graphs.Edge edge in list)
            {
                if ((edge.fromSlot.node.graph == node.subGraph) && (edge.toSlot.node.graph == node.subGraph))
                {
                    if (!node.subGraph.Connected(edge.fromSlot, edge.toSlot))
                    {
                        node.subGraph.Connect(edge.fromSlot, edge.toSlot);
                    }
                }
                else if ((edge.fromSlot.node.graph == node.subGraph) && (edge.toSlot.node.graph != node.subGraph))
                {
                    string str = edge.fromSlot.name;
                    int    num = 0;
                    while (node.m_ProxyInNode[str] != null)
                    {
                        str = edge.fromSlot.name + "_" + num++;
                    }
                    Slot s = new Slot(SlotType.InputSlot, str);
                    node.m_ProxyInNode.AddSlot(s);
                    node.subGraph.Connect(edge.fromSlot, s);
                    Slot slot2 = new Slot(SlotType.OutputSlot, str);
                    node.AddSlot(slot2);
                    node.graph.Connect(slot2, edge.toSlot);
                }
                else if ((edge.fromSlot.node.graph != node.subGraph) && (edge.toSlot.node.graph == node.subGraph))
                {
                    string str2 = edge.toSlot.name;
                    int    num2 = 0;
                    while (node.m_ProxyOutNode[str2] != null)
                    {
                        str2 = edge.toSlot.name + "_" + num2++;
                    }
                    Slot slot3 = new Slot(SlotType.OutputSlot, str2);
                    node.m_ProxyOutNode.AddSlot(slot3);
                    node.subGraph.Connect(slot3, edge.toSlot);
                    Slot slot4 = new Slot(SlotType.InputSlot, str2);
                    node.AddSlot(slot4);
                    node.graph.Connect(edge.fromSlot, slot4);
                }
                node.graph.RemoveEdge(edge);
            }
            return(node);
        }
Example #10
0
 internal bool HasConnectionTo(Slot toSlot)
 {
     return(this.edges.Any((Edge e) => e.toSlot == toSlot));
 }
Example #11
0
        public static GroupNode FromNodes(string name, List <Node> nodes, Type graphType)
        {
            if (nodes.Count == 0)
            {
                throw new ArgumentException("No nodes to group");
            }
            Graph graph = nodes[0].graph;

            if (graph == null)
            {
                throw new ArgumentException("Nodes needs to be attached to a graph");
            }
            GroupNode groupNode = new GroupNode(name, graph, graphType);

            graph.AddNode(groupNode);
            groupNode.m_ProxyInNode = ProxyNode.Instance(true);
            groupNode.subGraph.AddNode(groupNode.m_ProxyInNode);
            groupNode.m_ProxyOutNode = ProxyNode.Instance(false);
            groupNode.subGraph.AddNode(groupNode.m_ProxyOutNode);
            List <Edge> list = new List <Edge>();

            foreach (Node current in nodes)
            {
                list.AddRange(current.outputEdges);
                list.AddRange(current.inputEdges);
                groupNode.AddChildNode(current);
                graph.nodes.Remove(current);
            }
            foreach (Edge current2 in list)
            {
                if (current2.fromSlot.node.graph == groupNode.subGraph && current2.toSlot.node.graph == groupNode.subGraph)
                {
                    if (!groupNode.subGraph.Connected(current2.fromSlot, current2.toSlot))
                    {
                        groupNode.subGraph.Connect(current2.fromSlot, current2.toSlot);
                    }
                }
                else if (current2.fromSlot.node.graph == groupNode.subGraph && current2.toSlot.node.graph != groupNode.subGraph)
                {
                    string name2 = current2.fromSlot.name;
                    int    num   = 0;
                    while (groupNode.m_ProxyInNode[name2] != null)
                    {
                        name2 = current2.fromSlot.name + "_" + num++;
                    }
                    Slot slot = new Slot(SlotType.InputSlot, name2);
                    groupNode.m_ProxyInNode.AddSlot(slot);
                    groupNode.subGraph.Connect(current2.fromSlot, slot);
                    Slot slot2 = new Slot(SlotType.OutputSlot, name2);
                    groupNode.AddSlot(slot2);
                    groupNode.graph.Connect(slot2, current2.toSlot);
                }
                else if (current2.fromSlot.node.graph != groupNode.subGraph && current2.toSlot.node.graph == groupNode.subGraph)
                {
                    string name3 = current2.toSlot.name;
                    int    num2  = 0;
                    while (groupNode.m_ProxyOutNode[name3] != null)
                    {
                        name3 = current2.toSlot.name + "_" + num2++;
                    }
                    Slot slot3 = new Slot(SlotType.OutputSlot, name3);
                    groupNode.m_ProxyOutNode.AddSlot(slot3);
                    groupNode.subGraph.Connect(slot3, current2.toSlot);
                    Slot slot4 = new Slot(SlotType.InputSlot, name3);
                    groupNode.AddSlot(slot4);
                    groupNode.graph.Connect(current2.fromSlot, slot4);
                }
                groupNode.graph.RemoveEdge(current2);
            }
            return(groupNode);
        }