コード例 #1
0
        /// <summary>
        /// Called when the user has started to drag out a connector, thus creating a new connection.
        /// </summary>
        public ExecutionConnectionViewModel ExecutionConnectionDragStarted(ExecutionConnectorViewModel draggedOutConnector, Point curDragPoint)
        {
            //
            // Create a new connection to add to the view-model.
            //
            var connection = new ExecutionConnectionViewModel();

            if (draggedOutConnector.Type == ConnectorType.Output)
            {
                //
                // The user is dragging out a source connector (an output) and will connect it to a destination connector (an input).
                //
                connection.SourceConnector      = draggedOutConnector;
                connection.DestConnectorHotspot = curDragPoint;
            }
            else
            {
                //
                // The user is dragging out a destination connector (an input) and will connect it to a source connector (an output).
                //
                connection.DestConnector          = draggedOutConnector;
                connection.SourceConnectorHotspot = curDragPoint;
            }

            //
            // Add the new connection to the view-model.
            //
            this.Network.ExecutionConnections.Add(connection);

            return(connection);
        }
コード例 #2
0
        /// <summary>
        /// Called when the user has finished dragging out the new connection.
        /// </summary>
        public void ExecutionConnectionDragCompleted(ExecutionConnectionViewModel newConnection, ExecutionConnectorViewModel connectorDraggedOut, ExecutionConnectorViewModel connectorDraggedOver)
        {
            if (connectorDraggedOver == null)
            {
                //
                // The connection was unsuccessful.
                // Maybe the user dragged it out and dropped it in empty space.
                //
                this.Network.ExecutionConnections.Remove(newConnection);
                return;
            }

            //
            // Only allow connections from output connector to input connector (ie each
            // connector must have a different type).
            // Also only allocation from one node to another, never one node back to the same node.
            //
            bool connectionOk = connectorDraggedOut.ParentNode != connectorDraggedOver.ParentNode &&
                                connectorDraggedOut.Type != connectorDraggedOver.Type;

            if (!connectionOk)
            {
                //
                // Connections between connectors that have the same type,
                // eg input -> input or output -> output, are not allowed,
                // Remove the connection.
                //
                this.Network.ExecutionConnections.Remove(newConnection);
                return;
            }

            //
            // The user has dragged the connection on top of another valid connector.
            //

            //
            // Remove any existing connection between the same two connectors.
            //
            var existingConnection = FindExecutionConnection(connectorDraggedOut, connectorDraggedOver);

            if (existingConnection != null)
            {
                this.Network.ExecutionConnections.Remove(existingConnection);
            }

            //
            // Finalize the connection by attaching it to the connector
            // that the user dragged the mouse over.
            //
            if (newConnection.DestConnector == null)
            {
                newConnection.DestConnector = connectorDraggedOver;
            }
            else
            {
                newConnection.SourceConnector = connectorDraggedOver;
            }
        }
コード例 #3
0
 /// <summary>
 /// Called as the user continues to drag the connection.
 /// </summary>
 public void ExecutionConnectionDragging(Point curDragPoint, ExecutionConnectionViewModel connection)
 {
     if (connection.DestConnector == null)
     {
         connection.DestConnectorHotspot = curDragPoint;
     }
     else
     {
         connection.SourceConnectorHotspot = curDragPoint;
     }
 }
コード例 #4
0
        /// <summary>
        /// Create a node and add it to the view-model.
        /// </summary>

        /*public NodeViewModel CreateNode(string name, Point nodeLocation, bool centerNode)
         * {
         *  var node = new NodeViewModel(name);
         *  node.X = nodeLocation.X;
         *  node.Y = nodeLocation.Y;
         *
         *  node.InputConnectors.Add(new ConnectorViewModel("In1"));
         *  node.InputConnectors.Add(new ConnectorViewModel("In2"));
         *  node.OutputConnectors.Add(new ConnectorViewModel("Out1"));
         *  node.OutputConnectors.Add(new ConnectorViewModel("Out2"));
         *
         *  if (centerNode)
         *  {
         *      //
         *      // We want to center the node.
         *      //
         *      // For this to happen we need to wait until the UI has determined the
         *      // size based on the node's data-template.
         *      //
         *      // So we define an anonymous method to handle the SizeChanged event for a node.
         *      //
         *      // Note: If you don't declare sizeChangedEventHandler before initializing it you will get
         *      //       an error when you try and unsubscribe the event from within the event handler.
         *      //
         *      EventHandler<EventArgs> sizeChangedEventHandler = null;
         *      sizeChangedEventHandler =
         *          delegate (object sender, EventArgs e)
         *          {
         *              //
         *              // This event handler will be called after the size of the node has been determined.
         *              // So we can now use the size of the node to modify its position.
         *              //
         *              node.X -= node.Size.Width / 2;
         *              node.Y -= node.Size.Height / 2;
         *
         *              //
         *              // Don't forget to unhook the event, after the initial centering of the node
         *              // we don't need to be notified again of any size changes.
         *              //
         *              node.SizeChanged -= sizeChangedEventHandler;
         *          };
         *
         *      //
         *      // Now we hook the SizeChanged event so the anonymous method is called later
         *      // when the size of the node has actually been determined.
         *      //
         *      node.SizeChanged += sizeChangedEventHandler;
         *  }
         *
         *  //
         *  // Add the node to the view-model.
         *  //
         *  this.Network.Nodes.Add(node);
         *
         *  return node;
         * }*/

        /// <summary>
        /// Utility method to delete a connection from the view-model.
        /// </summary>
        public void DeleteExecutionConnection(ExecutionConnectionViewModel connection)
        {
            this.Network.ExecutionConnections.Remove(connection);
        }