Exemplo n.º 1
0
 //Copy Constructor
 public Circuit(List <Gate> gate, List <Connection> conn)
 {
     gateList  = new List <Gate>();
     ConnList  = new List <Connection>();
     this.name = "foundo";
     for (int i = 0; i < gate.Count(); i++)
     {
         Gate G;
         if (gate[i] is SourceGate)
         {
             G = new SourceGate(gate[i]);
         }
         else if (gate[i] is SinkGate)
         {
             G = new SinkGate(gate[i]);
         }
         else if (gate[i] is AndGate)
         {
             G = new AndGate(gate[i]);
         }
         else if (gate[i] is OrGate)
         {
             G = new OrGate(gate[i]);
         }
         else
         {
             G = new NotGate(gate[i]);
         }
         gateList.Add(G);
     }
     for (int i = 0; i < conn.Count(); i++)
     {
         ConnList.Add(new Connection(conn[i], gateList));
     }
 }
Exemplo n.º 2
0
        private void gridBox_MouseClick(object sender, MouseEventArgs e)
        {
            //Hides the dynamic button when clicking on an empty cell.
            if (dynamicButton != null)
            {
                dynamicButton.Visible = false;
            }
            //First, it checks which button is clicked
            //Then depends on the button clicked, in creates and draw the corresponding objects gate or connection
            if (code == drawCode.Source)
            {
                Gate SG1 = new SourceGate(e.Location);
                if (circ.addGate(SG1) == true)
                {
                    SG1.drawGate(ref gridBox);
                    highlight = Rectangle.Empty;
                }
                code = null;
            }
            else if (code == drawCode.Sink)
            {
                Gate SG1 = new SinkGate(e.Location);
                if (circ.addGate(SG1) == true)
                {
                    SG1.drawGate(ref gridBox);
                    highlight = Rectangle.Empty;
                }
                code = null;
            }
            else if (code == drawCode.And)
            {
                Gate AG1 = new AndGate(e.Location);
                if (circ.addGate(AG1) == true)
                {
                    AG1.drawGate(ref gridBox);
                    highlight = Rectangle.Empty;
                }
                code = null;
            }
            else if (code == drawCode.Or)
            {
                Gate OG1 = new OrGate(e.Location);
                if (circ.addGate(OG1) == true)
                {
                    OG1.drawGate(ref gridBox);
                    highlight = Rectangle.Empty;
                }
                code = null;
            }
            else if (code == drawCode.Not)
            {
                Gate NG1 = new NotGate(e.Location);
                if (circ.addGate(NG1) == true)
                {
                    NG1.drawGate(ref gridBox);
                    highlight = Rectangle.Empty;
                }
                code = null;
            }
            else if (code == drawCode.Connect)
            {
                rowNum = e.Location.Y / 50 + 1;
                colNum = e.Location.X / 50 + 1;
                chosen = circ.getGateFromList(rowNum, colNum);

                if (chosen != null)
                {
                    if (counter == 0)
                    {
                        // if the first gate is sink, gives error message
                        if (chosen is SinkGate)
                        {
                            MessageBox.Show("The sink gate cannot be the first gate", "Invalid Connection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            first = second = null;
                            code  = null;
                        }
                        else
                        {
                            first   = chosen;
                            counter = 1;
                        }
                    }
                    else
                    {
                        // if the second gate is source, gives error message
                        if (chosen is SourceGate)
                        {
                            MessageBox.Show("Source gate cannot be the second gate", "Invalid Connection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            first   = second = null;
                            code    = null;
                            counter = 0;
                        }
                        // if the both selected gate are the same, gives error message
                        else if (first == chosen)
                        {
                            MessageBox.Show("You are trying to connect the same gate", "Invalid Connection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            first   = second = null;
                            code    = null;
                            counter = 0;
                        }
                        else
                        {
                            second  = chosen;
                            counter = 0;
                            Connection c = new Connection(first, second);
                            if (circ.addConnection(c))
                            {
                                c.drawConnection(ref gridBox);
                                first = second = null;
                                circ.reDraw(ref gridBox);
                            }
                        }
                    }
                }
                else
                {
                    code  = null;
                    first = second = null;
                }
            }
            else if (code == null)
            {
                rowNum = e.Location.Y / 50 + 1;
                colNum = e.Location.X / 50 + 1;
                chosen = circ.getGateFromList(rowNum, colNum);
                if (chosen != null)
                {
                    chosenConn = null;
                    Point x = new Point(((colNum - 1) * 50 - 25), ((rowNum - 1) * 50) + 50);
                    highlight = new Rectangle(((colNum - 1) * 50) - 5, ((rowNum - 1) * 50) - 5, 60, 60);
                    gridBox.Refresh();
                    circ.reDraw(ref gridBox);

                    if (chosen is SourceGate)
                    {
                        CreateDynamicButton(x);
                    }
                }
                else
                {
                    chosenConn = circ.getConnectionFromList(e.Location);
                    chosen     = null;
                    if (chosenConn != null)
                    {
                        int x1 = (chosenConn.getFirstGate().getColumn()) * 50;
                        int x2 = (chosenConn.getSecondGate().getColumn() - 1) * 50;
                        int y1 = (chosenConn.getFirstGate().getRow() - 1) * 50;
                        int y2 = (chosenConn.getSecondGate().getRow() - 1) * 50;
                        if (y2 > y1)
                        {
                            highlight = new Rectangle(x1 - 5, y1 + 15, (x2 - x1) + 10, (y2 - y1) + 5);
                        }
                        else if (y2 < y1)
                        {
                            highlight = new Rectangle(x1 - 5, y2 + 15, (x2 - x1) + 10, (y1 - y2) + 10);
                        }
                        else
                        {
                            highlight = new Rectangle(x1 - 5, y1, (x2 - x1) + 10, 50);
                        }
                    }
                    else
                    {
                        highlight = new Rectangle(((colNum - 1) * 50) - 5, ((rowNum - 1) * 50) - 5, 60, 60);
                        gridBox.Refresh();
                        circ.reDraw(ref gridBox);
                    }
                }
            }
            gridBox.Refresh();
            circ.reDraw(ref gridBox);
        }