Beispiel #1
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);
        }
Beispiel #2
0
 //Copy Constructor
 /// <summary>
 /// Copy Constructor of OrGate Class
 /// </summary>
 /// <param name="G">The Original Gate to be copied</param>
 public OrGate(Gate G)
     : base(G)
 {
     Input1 = ((OrGate)G).Input1;
     Input2 = ((OrGate)G).Input2;
 }
Beispiel #3
0
 //Copy Constructor
 /// <summary>
 /// Copy Constructor of And Class
 /// </summary>
 /// <param name="G">The Original Gate to be copied</param>
 public AndGate(Gate G)
     : base(G)
 {
     Input1 = ((AndGate)G).Input1;
     Input2 = ((AndGate)G).Input2;
 }
Beispiel #4
0
 //Copy Constructor
 /// <summary>
 /// Copy Constructor of SourceGate Class
 /// </summary>
 /// <param name="G">The Original Gate to be copied</param>
 public SourceGate(Gate G)
     : base(G)
 {
     Value = ((SourceGate)G).Value;
 }
Beispiel #5
0
 //Copy Constructor
 /// <summary>
 /// Copy Constructor of SinkGate Class
 /// </summary>
 /// <param name="G">The Original Gate to be copied</param>
 public SinkGate(Gate G)
     : base(G)
 {
     Input1 = ((SinkGate)G).Input1;
 }
Beispiel #6
0
 /// <summary>
 /// This is the constructor of the connection object,
 /// which having two parameters: first and second gate object.
 /// </summary>
 /// <param name="firs"></param>
 /// <param name="secon"></param>
 public Connection(Gate firs, Gate secon)
 {
     first  = firs;
     second = secon;
 }