示例#1
0
        void Model_OnConnectorAttached(object sender, ConnectorsEventArgs e)
        {
            if (maskNDrives)
            {
                return;
            }

            bool wasMasked = maskMDrives;

            try
            {
                maskMDrives = true;

                IConnection con = _bridge.Model.ConnectorHolders[e.SubjectConnector] as IConnection;

                // get rid of uninteresting situations
                if (con != null)
                {
                    if (con.From.AttachedTo == null || con.To.AttachedTo == null)
                    {
                        return;
                    }
                }
                else
                {
                    con = _bridge.Model.ConnectorHolders[e.ObjectConnector] as IConnection;
                    if (con != null)
                    {
                        if (con.From.AttachedTo == null || con.To.AttachedTo == null)
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }

                // find out who connects to whom
                IConnector portConnector = con.From.AttachedTo;
                IShape     portShape     = (IShape)_bridge.Model.ConnectorHolders[portConnector];
                IShape     valueShape    = (IShape)_bridge.Model.ConnectorHolders[con.To.AttachedTo];

                PortShape port = portShape as PortShape;
                if (port == null)
                {
                    port = valueShape as PortShape;
                    if (port == null)
                    {
                        return;
                    }
                    valueShape    = portShape; //ensure valueShape should now be either signal or bus
                    portConnector = con.To.AttachedTo;
                }
                SignalShape signal = valueShape as SignalShape;
                if (signal != null)
                {
                    // PORT <-> SIGNAL

                    int index = -1;
                    List <YttriumConnector> sl = port.InputConnectors;
                    for (int i = 0; i < sl.Count; i++)
                    {
                        if (sl[i] == portConnector)
                        {
                            index = i;
                            break;
                        }
                    }

                    if (index > -1)
                    {
                        PostCommandSignalDrivesPort(signal.SignalReference, port.PortReference, index);
                        return;
                    }

                    index = -1;
                    sl    = port.OutputConnectors;
                    for (int i = 0; i < sl.Count; i++)
                    {
                        if (sl[i] == portConnector)
                        {
                            index = i;
                            break;
                        }
                    }

                    if (index > -1)
                    {
                        PostCommandPortDrivesSignal(signal.SignalReference, port.PortReference, index);
                    }

                    return;
                }
                BusShape bus = valueShape as BusShape;
                if (bus != null)
                {
                    // PORT <-> BUS

                    int index = -1;
                    List <YttriumConnector> bl = port.BusConnectors;
                    for (int i = 0; i < bl.Count; i++)
                    {
                        if (bl[i] == portConnector)
                        {
                            index = i;
                            break;
                        }
                    }

                    if (index > -1)
                    {
                        PostCommandBusAttachedToPort(bus.BusReference, port.PortReference, index);
                    }

                    return;
                }
            }
            finally
            {
                maskMDrives = wasMasked;
            }
        }
        void Model_OnConnectorDetached(object sender, ConnectorsEventArgs e)
        {
            if(maskNDrives)
                return;

            bool wasMasked = maskMDrives;
            try
            {
                maskMDrives = true;

                IConnection con = _bridge.Model.ConnectorHolders[e.SubjectConnector] as IConnection;
                IConnector cr1 = e.ObjectConnector;

                // get rid of uninteresting situations
                if(con == null)
                {
                    con = _bridge.Model.ConnectorHolders[e.ObjectConnector] as IConnection;
                    cr1 = e.SubjectConnector;
                    if(con == null)
                        return;
                }

                if(con.From.AttachedTo == null && con.To.AttachedTo == null)
                    return;

                IConnector cr2 = con.From.AttachedTo == null ? con.To.AttachedTo : con.From.AttachedTo;

                // find out who connects to whom
                IConnector portConnector = cr1;
                IShape portShape = (IShape)_bridge.Model.ConnectorHolders[portConnector];
                IShape valueShape = (IShape)_bridge.Model.ConnectorHolders[cr2];

                PortShape port = portShape as PortShape;
                if(port == null)
                {
                    port = valueShape as PortShape;
                    if(port == null)
                        return;
                    valueShape = portShape; //ensure valueShape should now be either signal or bus
                    portConnector = cr2;
                }
                SignalShape signal = valueShape as SignalShape;
                if(signal != null)
                {
                    // PORT <-> SIGNAL

                    int index = -1;
                    List<YttriumConnector> sl = port.InputConnectors;
                    for(int i = 0; i < sl.Count; i++)
                    {
                        if(sl[i] == portConnector)
                        {
                            index = i;
                            break;
                        }
                    }

                    if(index > -1)
                    {
                        PostCommandSignalDrivesPortNoLonger(port.PortReference, index);
                        return;
                    }

                    index = -1;
                    sl = port.OutputConnectors;
                    for(int i = 0; i < sl.Count; i++)
                    {
                        if(sl[i] == portConnector)
                        {
                            index = i;
                            break;
                        }
                    }

                    if(index > -1)
                        PostCommandPortDrivesSignalNoLonger(port.PortReference, index);

                    return;
                }
                BusShape bus = valueShape as BusShape;
                if(bus != null)
                {
                    // PORT <-> BUS

                    int index = -1;
                    List<YttriumConnector> bl = port.BusConnectors;
                    for(int i = 0; i < bl.Count; i++)
                    {
                        if(bl[i] == portConnector)
                        {
                            index = i;
                            break;
                        }
                    }

                    if(index > -1)
                        PostCommandBusDetachedFromPort(port.PortReference, index);

                    return;
                }
            }
            finally
            {
                maskMDrives = wasMasked;
            }
        }