/// <summary>
        /// Adds a new placeholder port on a node.
        /// </summary>
        /// <param name="self">The node to add the new port on.</param>
        /// <param name="direction">The direction of the port the create.</param>
        /// <param name="uniqueId">The ID of the port to create.</param>
        /// <param name="orientation">The orientation of the port to create.</param>
        /// <returns>The newly created placeholder port.</returns>
        public static IPortModel AddPlaceHolderPort(this IInputOutputPortsNodeModel self, PortDirection direction, string uniqueId,
                                                    PortOrientation orientation = PortOrientation.Horizontal)
        {
            if (direction == PortDirection.Input)
            {
                return(self.AddInputPort(uniqueId, PortType.MissingPort, TypeHandle.MissingPort, uniqueId, orientation,
                                         PortModelOptions.NoEmbeddedConstant));
            }

            return(self.AddOutputPort(uniqueId, PortType.MissingPort, TypeHandle.MissingPort, uniqueId, orientation,
                                      PortModelOptions.NoEmbeddedConstant));
        }
        /// <summary>
        /// Adds a new data input port on a node.
        /// </summary>
        /// <param name="self">The node to add the new port on.</param>
        /// <param name="portName">The name of port to create.</param>
        /// <param name="portId">The ID of the port to create.</param>
        /// <param name="orientation">The orientation of the port to create.</param>
        /// <param name="options">The options for the port to create.</param>
        /// <param name="defaultValue">The default value to assign to the constant associated to the port.</param>
        /// <typeparam name="TDataType">The type of data the port to create handles.</typeparam>
        /// <returns>The newly created data input port.</returns>
        public static IPortModel AddDataInputPort <TDataType>(this IInputOutputPortsNodeModel self, string portName,
                                                              string portId            = null, PortOrientation orientation = PortOrientation.Horizontal,
                                                              PortModelOptions options = PortModelOptions.Default, TDataType defaultValue = default)
        {
            Action <IConstant> initializationCallback = null;

            if (defaultValue is Enum || !EqualityComparer <TDataType> .Default.Equals(defaultValue, default))
            {
                initializationCallback = constantModel => constantModel.ObjectValue = defaultValue;
            }

            return(self.AddDataInputPort(portName, typeof(TDataType).GenerateTypeHandle(), portId, orientation, options, initializationCallback));
        }
        public override void SetUp()
        {
            base.SetUp();

            m_Node1 = CreateNode("Node 1", new Vector2(0, 0), 0, 1);
            m_Node2 = CreateNode("Node 2", new Vector2(300, 300), 1);

            // Add the minimap.
            var miniMap = new MiniMap();

            miniMap.SetPosition(new Rect(10, 100, 100, 100));
            miniMap.MaxWidth  = 100;
            miniMap.MaxHeight = 100;
            graphView.Add(miniMap);
        }
Ejemplo n.º 4
0
        public void Test_CreateEdgeCommand_Itemize(TestingMode testingMode, ItemizeTestType itemizeTest, Func <GraphModel, IInputOutputPortsNodeModel> makeNode)
        {
            // create int node
            IInputOutputPortsNodeModel node0 = makeNode(GraphModel);

            var opNode = GraphModel.CreateNode <Type0FakeNodeModel>("Node0", Vector2.zero);

            // connect int to first input
            m_CommandDispatcher.Dispatch(new CreateEdgeCommand(opNode.Input0, node0.OutputsByDisplayOrder.First()));

            var prevItemizeConstants = m_CommandDispatcher.State.Preferences.GetBool(BoolPref.AutoItemizeConstants);
            var prevItemizeVariables = m_CommandDispatcher.State.Preferences.GetBool(BoolPref.AutoItemizeVariables);

            if (itemizeTest == ItemizeTestType.Enabled)
            {
                m_CommandDispatcher.State.Preferences.SetBool(BoolPref.AutoItemizeConstants, true);
                m_CommandDispatcher.State.Preferences.SetBool(BoolPref.AutoItemizeVariables, true);
            }

            // test how the node reacts to getting connected a second time
            TestPrereqCommandPostreq(testingMode,
                                     () =>
            {
                RefreshReference(ref node0);
                RefreshReference(ref opNode);
                var binOp            = GraphModel.NodeModels.OfType <Type0FakeNodeModel>().First();
                IPortModel input0    = binOp.Input0;
                IPortModel input1    = binOp.Input1;
                IPortModel binOutput = binOp.Output0;
                Assert.That(GetNodeCount(), Is.EqualTo(2));
                Assert.That(GetEdgeCount(), Is.EqualTo(1));
                Assert.That(input0, Is.ConnectedTo(node0.OutputsByDisplayOrder.First()));
                Assert.That(input1, Is.Not.ConnectedTo(node0.OutputsByDisplayOrder.First()));
                Assert.That(binOutput.IsConnected, Is.False);
                return(new CreateEdgeCommand(input1, node0.OutputsByDisplayOrder.First()));
            },
                                     () =>
            {
                RefreshReference(ref node0);
                RefreshReference(ref opNode);
                var binOp            = GraphModel.NodeModels.OfType <Type0FakeNodeModel>().First();
                IPortModel input0    = binOp.Input0;
                IPortModel input1    = binOp.Input1;
                IPortModel binOutput = binOp.Output0;
                Assert.That(GetEdgeCount(), Is.EqualTo(2));
                Assert.That(input0, Is.ConnectedTo(node0.OutputsByDisplayOrder.First()));
                Assert.That(binOutput.IsConnected, Is.False);

                if (itemizeTest == ItemizeTestType.Enabled)
                {
                    Assert.That(GetNodeCount(), Is.EqualTo(3));
                    ISingleOutputPortNodeModel newNode = GetNode(2) as ISingleOutputPortNodeModel;
                    Assert.NotNull(newNode);
                    Assert.That(newNode, Is.TypeOf(node0.GetType()));
                    IPortModel output1 = newNode.OutputPort;
                    Assert.That(input1, Is.ConnectedTo(output1));
                }
                else
                {
                    Assert.That(GetNodeCount(), Is.EqualTo(2));
                }
            });

            if (itemizeTest == ItemizeTestType.Enabled)
            {
                m_CommandDispatcher.State.Preferences.SetBool(BoolPref.AutoItemizeConstants, prevItemizeConstants);
                m_CommandDispatcher.State.Preferences.SetBool(BoolPref.AutoItemizeVariables, prevItemizeVariables);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SplitEdgeAndInsertExistingNodeCommand"/> class.
 /// </summary>
 /// <param name="edgeModel">The edge on which to insert a node.</param>
 /// <param name="nodeModel">The node to insert.</param>
 public SplitEdgeAndInsertExistingNodeCommand(IEdgeModel edgeModel, IInputOutputPortsNodeModel nodeModel) : this()
 {
     EdgeModel = edgeModel;
     NodeModel = nodeModel;
 }
 /// <summary>
 /// Adds a new data input port on a node.
 /// </summary>
 /// <param name="self">The node to add the new port on.</param>
 /// <param name="portName">The name of port to create.</param>
 /// <param name="dataType">The type of data the port to create handles.</param>
 /// <param name="portId">The ID of the port to create.</param>
 /// <param name="orientation">The orientation of the port to create.</param>
 /// <param name="options">The options for the port to create.</param>
 /// <param name="initializationCallback">An initialization method for the associated constant (if one is needed
 /// for the port) to be called right after the port is created.</param>
 /// <returns>The newly created data input port.</returns>
 public static IPortModel AddDataInputPort(this IInputOutputPortsNodeModel self, string portName, TypeHandle dataType,
                                           string portId            = null, PortOrientation orientation = PortOrientation.Horizontal,
                                           PortModelOptions options = PortModelOptions.Default, Action <IConstant> initializationCallback = null)
 {
     return(self.AddInputPort(portName, PortType.Data, dataType, portId, orientation, options, initializationCallback));
 }
 /// <summary>
 /// Gets the output ports of a node.
 /// </summary>
 /// <param name="self">The node for which to get the output ports.</param>
 /// <returns>The output ports of the node.</returns>
 public static IEnumerable <IPortModel> GetOutputPorts(this IInputOutputPortsNodeModel self)
 {
     return(self.OutputsById.Values);
 }
 /// <summary>
 /// Adds a new execution output port on a node.
 /// </summary>
 /// <param name="self">The node to add the new port on.</param>
 /// <param name="portName">The name of port to create.</param>
 /// <param name="portId">The ID of the port to create.</param>
 /// <param name="orientation">The orientation of the port to create.</param>
 /// <returns>The newly created execution output port.</returns>
 public static IPortModel AddExecutionOutputPort(this IInputOutputPortsNodeModel self, string portName,
                                                 string portId = null, PortOrientation orientation = PortOrientation.Horizontal)
 {
     return(self.AddOutputPort(portName, PortType.Execution, TypeHandle.ExecutionFlow, portId, orientation));
 }
 /// <summary>
 /// Adds a new data output port on a node.
 /// </summary>
 /// <param name="self">The node to add the new port on.</param>
 /// <param name="portName">The name of port to create.</param>
 /// <param name="portId">The ID of the port to create.</param>
 /// <param name="orientation">The orientation of the port to create.</param>
 /// <typeparam name="TDataType">The type of data the port to create handles.</typeparam>
 /// <returns>The newly created data output port.</returns>
 public static IPortModel AddDataOutputPort <TDataType>(this IInputOutputPortsNodeModel self, string portName,
                                                        string portId = null, PortOrientation orientation = PortOrientation.Horizontal)
 {
     return(self.AddDataOutputPort(portName, typeof(TDataType).GenerateTypeHandle(), portId, orientation));
 }
 /// <summary>
 /// Adds a new data output port on a node.
 /// </summary>
 /// <param name="self">The node to add the new port on.</param>
 /// <param name="portName">The name of port to create.</param>
 /// <param name="dataType">The type of data the port to create handles.</param>
 /// <param name="portId">The ID of the port to create.</param>
 /// <param name="orientation">The orientation of the port to create.</param>
 /// <param name="options">The options for the port to create.</param>
 /// <returns>The newly created data output port.</returns>
 public static IPortModel AddDataOutputPort(this IInputOutputPortsNodeModel self, string portName, TypeHandle dataType,
                                            string portId            = null, PortOrientation orientation = PortOrientation.Horizontal,
                                            PortModelOptions options = PortModelOptions.Default)
 {
     return(self.AddOutputPort(portName, PortType.Data, dataType, portId, orientation, options));
 }
        public IEnumerator NodeUnderMouseShouldSnapWhenMultipleSelectedNodes()
        {
            // Config (ports are connected horizontally)
            //   +-------+
            //   | Node1 o +-------+
            //   +-------+ o Node2 o +-------+
            //             +-------+ o Node3 |
            //                       +-------+

            referenceNode1Model = CreateNode("Node1", k_ReferenceNodePos, 0, 1);

            m_SnappingNodePos = new Vector2(k_ReferenceNodePos.x + k_NodeSize.x, k_ReferenceNodePos.y + k_NodeSize.y * 0.5f);
            snappingNodeModel = CreateNode("Node2", m_SnappingNodePos, 1, 1);

            // Third node
            Vector2 secondSelectedNodePos = new Vector2(m_SnappingNodePos.x + k_NodeSize.x, m_SnappingNodePos.y + k_NodeSize.y * 0.5f);
            IInputOutputPortsNodeModel secondSelectedNodeModel = CreateNode("Node3", secondSelectedNodePos, 1);

            var node1OutputPort = referenceNode1Model.OutputsByDisplayOrder[0];
            var node2InputPort  = snappingNodeModel.InputsByDisplayOrder[0];
            var node2OutputPort = snappingNodeModel.OutputsByDisplayOrder[0];
            var node3InputPort  = secondSelectedNodeModel.InputsByDisplayOrder[0];

            Assert.IsNotNull(node1OutputPort);
            Assert.IsNotNull(node2InputPort);
            Assert.IsNotNull(node2OutputPort);
            Assert.IsNotNull(node3InputPort);

            MarkGraphViewStateDirty();
            yield return(null);

            // Connect the ports together
            var actions = ConnectPorts(node1OutputPort, node2InputPort);

            while (actions.MoveNext())
            {
                yield return(null);
            }
            actions = ConnectPorts(node2OutputPort, node3InputPort);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            Vector2 worldPosNode2 = graphView.ContentViewContainer.LocalToWorld(m_SnappingNodePos);
            Vector2 worldPosNode3 = graphView.ContentViewContainer.LocalToWorld(secondSelectedNodePos);

            Vector2 selectionPosNode2 = worldPosNode2 + m_SelectionOffset;
            Vector2 selectionPosNode3 = worldPosNode3 + m_SelectionOffset;

            // Select Node3 by clicking on it and pressing Ctrl
            helpers.MouseDownEvent(selectionPosNode3, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            helpers.MouseUpEvent(selectionPosNode3, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Move mouse to Node2
            helpers.MouseMoveEvent(selectionPosNode3, selectionPosNode2, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Select Node2 by clicking on it and pressing Ctrl
            helpers.MouseDownEvent(selectionPosNode2, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Move Node2 toward reference Node1 within the snapping range
            float   topToTopDistance = k_NodeSize.y * 0.5f;
            float   offSetY          = k_SnapDistance - topToTopDistance;
            Vector2 moveOffset       = new Vector2(0, offSetY);
            Vector2 end = selectionPosNode2 + moveOffset;

            helpers.MouseDragEvent(selectionPosNode2, end, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            helpers.MouseUpEvent(end, MouseButton.LeftMouse, TestEventHelpers.multiSelectModifier);
            yield return(null);

            // Get the UI ports
            var node1OutputPortUI = node1OutputPort.GetUI <Port>(graphView);
            var node2InputPortUI  = node2InputPort.GetUI <Port>(graphView);
            var node2OutputPortUI = node2OutputPort.GetUI <Port>(graphView);
            var node3InputPortUI  = node3InputPort.GetUI <Port>(graphView);
            var node3             = secondSelectedNodeModel.GetUI <Node>(graphView);

            Assert.IsNotNull(node1OutputPortUI);
            Assert.IsNotNull(node2InputPortUI);
            Assert.IsNotNull(node2OutputPortUI);
            Assert.IsNotNull(node3InputPortUI);
            Assert.IsNotNull(node3);

            // The snapping Node2 should snap to Node1's port
            Assert.AreEqual(node1OutputPortUI.GetGlobalCenter().y, node2InputPortUI.GetGlobalCenter().y);
            // Node3 should not snap to Node1's port
            Assert.AreNotEqual(node1OutputPortUI.GetGlobalCenter().y, node3InputPortUI.GetGlobalCenter().y);
            // Node 3 should have moved by the move offset in x and the same y offset as Node2
            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(secondSelectedNodePos.x + moveOffset.x), node3.layout.x);
            Assert.AreEqual(GraphViewStaticBridge.RoundToPixelGrid(secondSelectedNodePos.y - topToTopDistance), node3.layout.y);

            yield return(null);
        }
        public IEnumerator NodeShouldSnapToNearestConnectedPort()
        {
            // Config (ports are connected horizontally)
            //   +-------+
            //   | Node1 o +-------+
            //   +-------+ o Node2 o +-------+
            //             +-------+ o Node3 |
            //                       +-------+

            referenceNode1Model = CreateNode("Node1", GraphViewStaticBridge.RoundToPixelGrid(k_ReferenceNodePos), 0, 1);

            m_SnappingNodePos = GraphViewStaticBridge.RoundToPixelGrid(new Vector2(k_ReferenceNodePos.x + k_NodeSize.x, k_ReferenceNodePos.y + k_NodeSize.y * 0.5f));
            snappingNodeModel = CreateNode("Node2", m_SnappingNodePos, 1, 1);

            // Third node
            Vector2 secondReferenceNodePos = GraphViewStaticBridge.RoundToPixelGrid(new Vector2(m_SnappingNodePos.x + k_NodeSize.x, m_SnappingNodePos.y + k_NodeSize.y * 0.5f));
            IInputOutputPortsNodeModel secondReferenceNodeModel = CreateNode("Node3", secondReferenceNodePos, 1);

            var node1OutputPort = referenceNode1Model.OutputsByDisplayOrder[0];
            var node2InputPort  = snappingNodeModel.InputsByDisplayOrder[0];
            var node2OutputPort = snappingNodeModel.OutputsByDisplayOrder[0];
            var node3InputPort  = secondReferenceNodeModel.InputsByDisplayOrder[0];

            Assert.IsNotNull(node1OutputPort);
            Assert.IsNotNull(node2InputPort);
            Assert.IsNotNull(node2OutputPort);
            Assert.IsNotNull(node3InputPort);

            MarkGraphViewStateDirty();
            yield return(null);

            // Connect the ports together
            var actions = ConnectPorts(node1OutputPort, node2InputPort);

            while (actions.MoveNext())
            {
                yield return(null);
            }
            actions = ConnectPorts(node2OutputPort, node3InputPort);
            while (actions.MoveNext())
            {
                yield return(null);
            }

            Vector2 worldNodePos = graphView.ContentViewContainer.LocalToWorld(m_SnappingNodePos);
            Vector2 start        = worldNodePos + m_SelectionOffset;

            // We move the snapping Node2 toward reference Node1 within the snapping range
            float   offSetY    = k_SnapDistance - k_NodeSize.y * 0.5f;
            Vector2 moveOffset = GraphViewStaticBridge.RoundToPixelGrid(new Vector2(0, offSetY));
            Vector2 end        = start + moveOffset;

            // Move the snapping node.
            helpers.MouseDownEvent(start);
            yield return(null);

            helpers.MouseDragEvent(start, end);
            yield return(null);

            helpers.MouseUpEvent(end);
            yield return(null);

            // Get the UI ports
            var node1OutputPortUI = node1OutputPort.GetUI <Port>(graphView);
            var node2InputPortUI  = node2InputPort.GetUI <Port>(graphView);
            var node2OutputPortUI = node2OutputPort.GetUI <Port>(graphView);
            var node3InputPortUI  = node3InputPort.GetUI <Port>(graphView);

            Assert.IsNotNull(node1OutputPortUI);
            Assert.IsNotNull(node2InputPortUI);
            Assert.IsNotNull(node2OutputPortUI);
            Assert.IsNotNull(node3InputPortUI);

            // The snapping Node2 should snap to Node1's port
            Assert.AreEqual(node1OutputPortUI.GetGlobalCenter().y, node2InputPortUI.GetGlobalCenter().y);
            // The snapping Node2 should not snap to Node3's port
            Assert.AreNotEqual(node3InputPortUI.GetGlobalCenter().y, node2OutputPortUI.GetGlobalCenter().y);

            worldNodePos = graphView.ContentViewContainer.LocalToWorld(snappingNodeModel.Position);
            start        = worldNodePos + m_SelectionOffset;

            // We move the snapping Node2 toward Node3 within the snapping range
            offSetY    = k_NodeSize.y + k_SnapDistance - 1;
            moveOffset = GraphViewStaticBridge.RoundToPixelGrid(new Vector2(0, offSetY));

            // Move the snapping node.
            helpers.MouseDownEvent(start);
            yield return(null);

            end = start + moveOffset;
            helpers.MouseDragEvent(start, end);
            yield return(null);

            helpers.MouseUpEvent(end);
            yield return(null);

            // The snapping Node2's port should snap to Node3's port
            Assert.AreEqual(node3InputPortUI.GetGlobalCenter().y, node2OutputPortUI.GetGlobalCenter().y);
            // The snapping Node2's port should not snap to Node1's port
            Assert.AreNotEqual(node1OutputPortUI.GetGlobalCenter().y, node2InputPortUI.GetGlobalCenter().y);

            yield return(null);
        }