Beispiel #1
0
        private void Connect(PortModel p)
        {
            //test if the port that you are connecting too is not the start port or the end port
            //of the current connector
            if (p.Equals(Start) || p.Equals(End))
            {
                return;
            }

            //if the selected connector is also an output connector, return false
            //output ports can't be connected to eachother
            if (p.PortType == PortType.Output)
            {
                return;
            }

            //test if the port that you are connecting to is an input and 
            //already has other connectors
            if (p.PortType == PortType.Input && p.Connectors.Count > 0)
            {
                p.Disconnect(p.Connectors[0]);
            }

            //turn the line solid
            End = p;

            if (End != null)
            {
                p.Connect(this);
            }

            return;
        }
        public PortViewModel(PortModel port, NodeModel node)
        {
            _node = node;
            _port = port;

            _port.PropertyChanged += _port_PropertyChanged;
            _node.PropertyChanged += _node_PropertyChanged;
        }
Beispiel #3
0
        private ConnectorModel(WorkspaceModel workspaceModel, NodeModel start, NodeModel end, int startIndex, int endIndex, PortType portType)
        {
            this.workspaceModel = workspaceModel;

            pStart = start.OutPorts[startIndex];

            PortModel endPort = null;

            if (portType == PortType.INPUT)
                endPort = end.InPorts[endIndex];

            pStart.Connect(this);
            this.Connect(endPort);
        }
Beispiel #4
0
        private ConnectorModel(NodeModel start, NodeModel end, int startIndex, int endIndex, PortType portType)
        {
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            pStart = start.OutPorts[startIndex];

            PortModel endPort = null;

            if (portType == PortType.INPUT)
                endPort = end.InPorts[endIndex];

            pStart.Connect(this);
            this.Connect(endPort);
            //sw.Stop();
            //Debug.WriteLine(string.Format("{0} elapsed for constructing connector.", sw.Elapsed));
        }
Beispiel #5
0
        /// <summary>
        ///     Since the ports can have a margin (offset) so that they can moved vertically from its
        ///     initial position, the center of the port needs to be calculted differently and not only
        ///     based on the index. The function adds the height of other nodes as well as their margins
        /// </summary>
        /// <param name="portModel"> The portModel whose height is to be found</param>
        /// <returns> Returns the offset of the given port from the top of the ports </returns>
        internal double GetPortVerticalOffset(PortModel portModel)
        {
            double verticalOffset = 2.9;
            PortType portType;
            int index = GetPortIndexAndType(portModel, out portType);

            //If the port was not found, then it should have just been deleted. Return from function
            if (index == -1)
                return verticalOffset;

            double portHeight = portModel.Height;

            if (portType == PortType.INPUT)
            {
                for (int i = 0; i < index; i++)
                    verticalOffset += inPorts[i].MarginThickness.Top + portHeight;
                verticalOffset += inPorts[index].MarginThickness.Top;
            }
            else if (portType == PortType.OUTPUT)
            {
                for (int i = 0; i < index; i++)
                    verticalOffset += outPorts[i].MarginThickness.Top + portHeight;
                verticalOffset += outPorts[index].MarginThickness.Top;
            }

            return verticalOffset;
        }
 protected override void PortDisconnectedHandler(PortModel obj)
 {
     OnClear();
 }
 protected override void PortConnectedHandler(PortModel arg1, ConnectorModel arg2)
 {
     UpdateUpstream();
 }
        /// <summary>
        /// Construct a view and start drawing.
        /// </summary>
        /// <param name="port"></param>
        public ConnectorViewModel(PortModel port)
        {
            IsConnecting = true;
            _activeStartPort = port;

            Redraw(port.Center);
        }
Beispiel #9
0
        /// <summary>
        /// Construct a view and start drawing.
        /// </summary>
        /// <param name="port"></param>
        public ConnectorViewModel(WorkspaceViewModel workspace, PortModel port)
        {
            this.workspaceViewModel = workspace;
            IsConnecting = true;
            _activeStartPort = port;

            Redraw(port.Center);
        }
Beispiel #10
0
        /// <summary>
        /// Add a port to this node. If the port already exists, return that port.
        /// </summary>
        /// <param name="portType"></param>
        /// <param name="data"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public PortModel AddPort(PortType portType, PortData data, int index)
        {
            PortModel p;
            switch (portType)
            {
                case PortType.INPUT:
                    if (inPorts.Count > index)
                    {
                        p = inPorts[index];

                        //update the name on the node
                        //e.x. when the node is being re-registered during a custom
                        //node save
                        p.PortName = data.NickName;
                        if (data.HasDefaultValue)
                        {
                            p.UsingDefaultValue = true;
                            p.DefaultValueEnabled = true;
                        }

                        return p;
                    }

                    p = new PortModel(index, portType, this, data.NickName)
                    {
                        UsingDefaultValue = data.HasDefaultValue,
                        DefaultValueEnabled = data.HasDefaultValue
                    };

                    p.PropertyChanged += delegate(object sender, PropertyChangedEventArgs args)
                    {
                        if (args.PropertyName == "UsingDefaultValue")
                            RequiresRecalc = true;
                    };

                    InPorts.Add(p);

                    //register listeners on the port
                    p.PortConnected += p_PortConnected;
                    p.PortDisconnected += p_PortDisconnected;

                    return p;

                case PortType.OUTPUT:
                    if (outPorts.Count > index)
                    {
                        p = outPorts[index];
                        p.PortName = data.NickName;
                        return p;
                    }

                    p = new PortModel(index, portType, this, data.NickName)
                    {
                        UsingDefaultValue = false
                    };

                    OutPorts.Add(p);

                    //register listeners on the port
                    p.PortConnected += p_PortConnected;
                    p.PortDisconnected += p_PortDisconnected;

                    return p;
            }

            return null;
        }
Beispiel #11
0
        private void PortDisconnected(PortModel port)
        {
            ValidateConnections();

            if (port.PortType != PortType.Input) return;

            var data = InPorts.IndexOf(port);
            var startPort = port.Connectors[0].Start;
            DisconnectInput(data);
            startPort.Owner.DisconnectOutput(startPort.Owner.OutPorts.IndexOf(startPort), data, this);

            OnNodeModified();
        }
Beispiel #12
0
        private void PortConnected(PortModel port, ConnectorModel connector)
        {
            ValidateConnections();

            if (port.PortType != PortType.Input) return;

            var data = InPorts.IndexOf(port);
            var startPort = connector.Start;
            var outData = startPort.Owner.OutPorts.IndexOf(startPort);
            ConnectInput(data, outData, startPort.Owner);
            startPort.Owner.ConnectOutput(outData, data, this);
            OnConnectorAdded(connector);

            OnNodeModified();
        }
Beispiel #13
0
        /// <summary>
        ///     Add a port to this node. If the port already exists, return that port.
        /// </summary>
        /// <param name="portType"></param>
        /// <param name="data"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public PortModel AddPort(PortType portType, PortData data, int index)
        {
            PortModel p;
            switch (portType)
            {
                case PortType.Input:
                    if (inPorts.Count > index)
                    {
                        p = inPorts[index];
                        p.SetPortData(data);
                    }
                    else
                    {
                        p = new PortModel(portType, this, data);

                        p.PropertyChanged += delegate(object sender, PropertyChangedEventArgs args)
                        {
                            if (args.PropertyName == "UsingDefaultValue")
                            {
                                OnNodeModified();
                            }
                        };

                        //register listeners on the port
                        p.PortConnected += PortConnected;
                        p.PortDisconnected += PortDisconnected;

                        InPorts.Add(p);
                    }
                    
                    return p;

                case PortType.Output:
                    if (outPorts.Count > index)
                    {
                        p = outPorts[index];
                        p.SetPortData(data);
                    }
                    else
                    {
                        p = new PortModel(portType, this, data);
                        OutPorts.Add(p);

                        //register listeners on the port
                        p.PortConnected += PortConnected;
                        p.PortDisconnected += PortDisconnected;
                    }

                    return p;
            }

            return null;
        }
Beispiel #14
0
        /// <summary>
        ///     Since the ports can have a margin (offset) so that they can moved vertically from its
        ///     initial position, the center of the port needs to be calculted differently and not only
        ///     based on the index. The function adds the height of other nodes as well as their margins
        /// </summary>
        /// <param name="portModel"> The portModel whose height is to be found</param>
        /// <returns> Returns the offset of the given port from the top of the ports </returns>
        //TODO(Steve): This kind of UI calculation should probably live on the VM. -- MAGN-5711
        internal double GetPortVerticalOffset(PortModel portModel)
        {
            double verticalOffset = 2.9;
            int index = portModel.Index;

            //If the port was not found, then it should have just been deleted. Return from function
            if (index == -1)
                return verticalOffset;

            double portHeight = portModel.Height;

            switch (portModel.PortType)
            {
                case PortType.Input:
                    for (int i = 0; i < index; i++)
                        verticalOffset += inPorts[i].MarginThickness.Top + portHeight;
                    verticalOffset += inPorts[index].MarginThickness.Top;
                    break;
                case PortType.Output:
                    for (int i = 0; i < index; i++)
                        verticalOffset += outPorts[i].MarginThickness.Top + portHeight;
                    verticalOffset += outPorts[index].MarginThickness.Top;
                    break;
            }

            return verticalOffset;
        }
Beispiel #15
0
 internal int GetPortModelIndex(PortModel portModel)
 {
     if (portModel.PortType == PortType.Input)
         return InPorts.IndexOf(portModel);
     else
         return OutPorts.IndexOf(portModel);
 }
Beispiel #16
0
        internal int GetPortIndex(PortModel portModel, out PortType portType)
        {
            int index = this.inPorts.IndexOf(portModel);
            if (-1 != index)
            {
                portType = PortType.INPUT;
                return index;
            }

            index = this.outPorts.IndexOf(portModel);
            if (-1 != index)
            {
                portType = PortType.OUTPUT;
                return index;
            }

            portType = PortType.INPUT;
            return -1; // No port found.
        }
Beispiel #17
0
        void BeginConnection(Guid nodeId, int portIndex, PortType portType)
        {
            bool isInPort = portType == PortType.Input;
            activeStartPort = null;

            var node = CurrentWorkspace.GetModelInternal(nodeId) as NodeModel;
            if (node == null)
                return;
            PortModel portModel = isInPort ? node.InPorts[portIndex] : node.OutPorts[portIndex];

            // Test if port already has a connection, if so grab it and begin connecting 
            // to somewhere else (we don't allow the grabbing of the start connector).
            if (portModel.Connectors.Count > 0 && portModel.Connectors[0].Start != portModel)
            {
                activeStartPort = portModel.Connectors[0].Start;
                // Disconnect the connector model from its start and end ports
                // and remove it from the connectors collection. This will also
                // remove the view model.
                ConnectorModel connector = portModel.Connectors[0];
                if (CurrentWorkspace.Connectors.Contains(connector))
                {
                    var models = new List<ModelBase> { connector };
                    CurrentWorkspace.RecordAndDeleteModels(models);
                    connector.Delete();
                }
            }
            else
            {
                activeStartPort = portModel;
            }
        }
Beispiel #18
0
 private void DestroyConnectors(PortModel port)
 {
     while (port.Connectors.Any())
     {
         var connector = port.Connectors[0];
         WorkSpace.Connectors.Remove(connector);
         connector.NotifyConnectedPortsOfDeletion();
     }
 }
Beispiel #19
0
 protected virtual void PortConnectedHandler(PortModel arg1, ConnectorModel arg2)
 {
     // Do nothing for a standard node.
 }
Beispiel #20
0
        void MakeConnectionImpl(MakeConnectionCommand command)
        {
            Guid nodeId = command.ModelGuid;

            switch (command.ConnectionMode)
            {
                case MakeConnectionCommand.Mode.Begin:
                    BeginConnection(nodeId, command.PortIndex, command.Type);
                    break;

                case MakeConnectionCommand.Mode.End:
                    EndConnection(nodeId, command.PortIndex, command.Type);
                    break;

                case MakeConnectionCommand.Mode.Cancel:
                    activeStartPort = null;
                    break;
            }
        }
Beispiel #21
0
 /// <summary>
 /// Registers the port events.
 /// </summary>
 /// <param name="item">PortModel.</param>
 /// <returns></returns>
 private PortViewModel SubscribePortEvents(PortModel item)
 {
     PortViewModel portViewModel = new PortViewModel(this, item);            
     portViewModel.MouseEnter += OnRectangleMouseEnter;
     portViewModel.MouseLeave += OnRectangleMouseLeave;
     portViewModel.MouseLeftButtonDown += OnMouseLeftButtonDown;
     return portViewModel;
 }
Beispiel #22
0
        void EndConnection(Guid nodeId, int portIndex, PortType portType)
        {
            bool isInPort = portType == PortType.Input;

            var node = CurrentWorkspace.GetModelInternal(nodeId) as NodeModel;
            if (node == null)
                return;
            
            PortModel portModel = isInPort ? node.InPorts[portIndex] : node.OutPorts[portIndex];
            ConnectorModel connectorToRemove = null;

            // Remove connector if one already exists
            if (portModel.Connectors.Count > 0 && portModel.PortType == PortType.Input)
            {
                connectorToRemove = portModel.Connectors[0];
                connectorToRemove.Delete();
            }

            // We could either connect from an input port to an output port, or 
            // another way around (in which case we swap first and second ports).
            PortModel firstPort, second;
            if (portModel.PortType != PortType.Input)
            {
                firstPort = portModel;
                second = activeStartPort;
            }
            else
            {
                // Create the new connector model
                firstPort = activeStartPort;
                second = portModel;
            }

            ConnectorModel newConnectorModel = ConnectorModel.Make(
                firstPort.Owner,
                second.Owner,
                firstPort.Index,
                second.Index);

            // Record the creation of connector in the undo recorder.
            var models = new Dictionary<ModelBase, UndoRedoRecorder.UserAction>();
            if (connectorToRemove != null)
                models.Add(connectorToRemove, UndoRedoRecorder.UserAction.Deletion);
            models.Add(newConnectorModel, UndoRedoRecorder.UserAction.Creation);
            WorkspaceModel.RecordModelsForUndo(models, CurrentWorkspace.UndoRecorder);
            activeStartPort = null;
        }
Beispiel #23
0
        public void Disconnect(PortModel p)
        {
            if (p.Equals(pStart))
            {
                pStart = null;
            }

            if (p.Equals(pEnd))
            {
                pEnd = null;
            }

            p.Disconnect(this);

        }
Beispiel #24
0
 protected virtual void PortDisconnectedHandler(PortModel port)
 {
     DeleteGeometryForIdentifier(port.Owner.AstIdentifierBase);
 }
Beispiel #25
0
        protected override void DeserializeCore(XmlElement element, SaveContext context)
        {
            XmlElementHelper helper = new XmlElementHelper(element);

            // Restore some information from the node attributes.
            this.GUID = helper.ReadGuid("guid", this.GUID);
            Guid startNodeId = helper.ReadGuid("start");
            int startIndex = helper.ReadInteger("start_index");
            Guid endNodeId = helper.ReadGuid("end");
            int endIndex = helper.ReadInteger("end_index");
            PortType portType = ((PortType)helper.ReadInteger("portType"));

            // Get to the start and end nodes that this connector connects to.
            WorkspaceModel workspace = dynSettings.Controller.DynamoModel.CurrentWorkspace;
            NodeModel startNode = workspace.GetModelInternal(startNodeId) as NodeModel;
            NodeModel endNode = workspace.GetModelInternal(endNodeId) as NodeModel;

            pStart = startNode.OutPorts[startIndex];
            PortModel endPort = null;
            if (portType == PortType.INPUT)
                endPort = endNode.InPorts[endIndex];

            pStart.Connect(this);
            this.Connect(endPort);
        }