public void deleteGateRec(Gates.GateObject node, Gates.GateObject gateToFind, Gates.GateObject previous = null) { if (node == gateToFind) { if (node.hasLeft() && node.input1 != null) { this.connectedGates.Add(node.input1); } if (node.hasRight() && node.input2 != null) { this.connectedGates.Add(node.input2); } if (previous.hasRight() && previous.input2 == node) { previous.input2 = (Gates.GateObject)null; } if (previous.hasLeft() && previous.input1 == node) { previous.input1 = (Gates.GateObject)null; } } if (node == null) { return; } if (node.hasLeft() && node.input1 != null) { this.deleteGateRec(node.input1, gateToFind, node); } if (!node.hasRight() || node.input2 == null) { return; } this.deleteGateRec(node.input2, gateToFind, node); }
public string getEquationTerm(Gates.GateObject gate) { if (gate.GetType() == typeof(Gates.ANDGate)) { return("(#.#)"); } if (gate.GetType() == typeof(Gates.ORGate)) { return("(#+#)"); } if (gate.GetType() == typeof(Gates.XORGate)) { return("(#^#)"); } if (gate.GetType() == typeof(Gates.NANDGate)) { return("`(#.#)"); } if (gate.GetType() == typeof(Gates.NORGate)) { return("`(#+#)"); } if (gate.GetType() == typeof(Gates.XNORGate)) { return("`(#^#)"); } if (gate.GetType() == typeof(Gates.NOTGate)) { return("`(#)"); } return(gate.getName()); }
public Gates.GateObject getGateIndex(int mx, int my) { for (int index = this.connectedGates.Count - 1; index >= 0; --index) { if (this.connectedGates == null) { this.connectedGates.Remove((Gates.GateObject)null); } else { Point pos = this.connectedGates[index].getPos(); if (mx >= pos.X - 30 && mx <= pos.X + 70) { if (my >= pos.Y - 30 && my <= pos.Y + 50) { return(this.connectedGates[index]); } } else { gateArea.ObjectFinder objectFinder = new gateArea.ObjectFinder(); objectFinder.findRecursive(this.connectedGates[index], mx, my); Gates.GateObject gateObject = objectFinder.output; if (gateObject != null) { return(gateObject); } } } } return((Gates.GateObject)null); }
public void findRecursive(Gates.GateObject Node, int mx, int my) { Point pos = Node.getPos(); if (mx >= pos.X - 30 && mx <= pos.X + 70 && (my >= pos.Y - 30 && my <= pos.Y + 50)) { this.output = Node; } else { if (Node == null) { return; } if (Node.hasLeft() && Node.input1 != null) { this.findRecursive(Node.input1, mx, my); } if (!Node.hasRight() || Node.input2 == null) { return; } this.findRecursive(Node.input2, mx, my); } }
public void simulateDiagramRecursive(Gates.GateObject node) { if (node == null) { return; } if (node.hasLeft() && node.input1.getOutput() == -1) { this.simulateDiagramRecursive(node.input1); } if (node.hasLeft() && node.input1.getOutput() != -1) { node.in1val = node.input1.getOutput(); } if (node.hasRight() && node.input2.getOutput() == -1) { this.simulateDiagramRecursive(node.input2); } if (node.hasRight() && node.input2.getOutput() != -1) { node.in2val = node.input2.getOutput(); } if (!(node.GetType() == typeof(Gates.Output)) || node.in1val == -1) { return; } this.result = node.getOutput(); }
protected override void OnMouseUp(MouseEventArgs e) { this.gatePointer = (Gates.GateObject)null; this.mouseDown = false; this.mouseX = -1; this.mouseY = -1; this.mouseXAfter = -1; this.mouseYAfter = -1; }
protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left && MainForm.__pointerTool) { this.gatePointer = this.getGateIndex(e.X, e.Y); } base.OnMouseDown(e); this.mouseDown = true; }
public Gates.GateObject EquationToDiagram(string left, string right, Gates.GateObject node) { Point pos; if (left != "NONE") { Tuple <Gates.GateObject, string, string> lastGate = this.findLastGate(left); node.input1 = lastGate.Item1; if (lastGate.Item1.GetType() == typeof(Gates.Input)) { bool flag = false; for (int index = 0; index < this.outputs.Count; ++index) { if (lastGate.Item1.getName() == this.outputs[index].getName()) { node.input1 = this.outputs[index]; flag = true; break; } } if (!flag) { this.outputs.Add(lastGate.Item1); } } pos = node.getPos(); node.input1.setPos(new Point(pos.X - 70, pos.Y - 40)); this.EquationToDiagram(lastGate.Item2, lastGate.Item3, node.input1); } if (right != "NONE") { Tuple <Gates.GateObject, string, string> lastGate = this.findLastGate(right); node.input2 = lastGate.Item1; if (lastGate.Item1.GetType() == typeof(Gates.Input)) { bool flag = false; for (int index = 0; index < this.outputs.Count; ++index) { if (lastGate.Item1.getName() == this.outputs[index].getName()) { node.input2 = this.outputs[index]; flag = true; break; } } if (!flag) { this.outputs.Add(lastGate.Item1); } } pos = node.getPos(); node.input2.setPos(new Point(pos.X - 70, pos.Y + 40)); this.EquationToDiagram(lastGate.Item2, lastGate.Item3, node.input2); } return(node); }
protected override void OnMouseClick(MouseEventArgs e) { this.drawingWire = false; this.isClicked = !this.isClicked; if (e.Button != MouseButtons.Left) { return; } if (MainForm.__lineTool) { if (this.firstClick == null) { this.gatePointer = (Gates.GateObject)null; this.drawingWire = true; this.firstClick = this.getclickedConnection(e.X, e.Y); if (this.firstClick == null) { this.isClicked = false; this.drawingWire = false; return; } this.wireStart = this.firstClick.Item2; } else { Tuple <Gates.GateObject, Point, string> tuple = this.getclickedConnection(e.X, e.Y); if (tuple != null) { this.connectGates(this.firstClick.Item1, this.firstClick.Item3, tuple.Item1, tuple.Item3); } this.firstClick = (Tuple <Gates.GateObject, Point, string>)null; } } else if (MainForm.__cutTool) { this.wireHighlight = this.getClickedWire(e.X, e.Y); if (this.wireHighlight != -1) { this.gatePointer = (Gates.GateObject)null; Point[] pointArray = this.wireList[this.wireHighlight]; Point point = pointArray[pointArray.Length - 1]; Tuple <Gates.GateObject, Point, string> tuple = this.getclickedConnection(point.X, point.Y); if (tuple.Item3 == "Left") { tuple.Item1.input1 = (Gates.GateObject)null; } else if (tuple.Item3 == "Right") { tuple.Item1.input2 = (Gates.GateObject)null; } } } this.Invalidate(); }
private void bool2DiagramBtn_Click(object sender, EventArgs e) { if (!this.checkEquation(this.bexpText.Text)) { return; } Gates.Output output = new Gates.Output(new Point(400, 200)); ExpressionBox.Equation2DiagramConverter diagramConverter = new ExpressionBox.Equation2DiagramConverter(); this.Close(); this.diagramStore = diagramConverter.EquationToDiagram(this.getEquation(), "NONE", (Gates.GateObject)output); }
public void setProcessingList(Gates.GateObject node) { if (node == null) { return; } this.processingList.Add(node); if (node.hasLeft()) { this.setProcessingList(node.input1); } if (!node.hasRight()) { return; } this.setProcessingList(node.input2); }
private Tuple <Gates.GateObject, Point, string> getclickedConnection(int mx, int my) { Gates.GateObject gateIndex = this.getGateIndex(mx, my); if (gateIndex == null) { return((Tuple <Gates.GateObject, Point, string>)null); } int[][] circlePositions = gateIndex.getCirclePositions(); Point pos = gateIndex.getPos(); if (gateIndex.GetType() == typeof(Gates.Input)) { Point point = new Point(pos.X + circlePositions[0][0], pos.Y + circlePositions[0][1] + 2); string str = "Out"; return(new Tuple <Gates.GateObject, Point, string>(gateIndex, point, str)); } if (gateIndex.GetType() == typeof(Gates.Output)) { Point point = new Point(pos.X + circlePositions[0][0], pos.Y + circlePositions[0][1] + 2); string str = "Left"; return(new Tuple <Gates.GateObject, Point, string>(gateIndex, point, str)); } int index1 = 0; float num = -1f; for (int index2 = 0; index2 < circlePositions.Length; ++index2) { float distancePoints = this.getDistancePoints(new Point(mx, my), new Point(pos.X + circlePositions[index2][0], pos.Y + circlePositions[index2][1] + 2)); if ((double)num == -1.0) { num = distancePoints; } if ((double)distancePoints < (double)num) { num = distancePoints; index1 = index2; } } Point point1 = new Point(pos.X + circlePositions[index1][0], pos.Y + circlePositions[index1][1] + 2); string str1 = index1 != 0 ? (index1 != 1 ? "Right" : "Left") : "Out"; return(new Tuple <Gates.GateObject, Point, string>(gateIndex, point1, str1)); }
public void resetDiagram(Gates.GateObject node) { node.setHighlight(false); if (node.GetType() != typeof(Gates.Input)) { node.in1val = -1; node.in2val = -1; } if (node == null) { return; } if (node.hasLeft() && node.input1 != null) { this.resetDiagram(node.input1); } if (!node.hasRight() || node.input2 == null) { return; } this.resetDiagram(node.input2); }
public void deleteGate(Gates.GateObject gateToFind) { for (int index = 0; index < this.connectedGates.Count; ++index) { if (this.connectedGates[index] == gateToFind) { if (this.connectedGates[index].hasLeft() && this.connectedGates[index].input1 != null) { this.connectedGates.Add(this.connectedGates[index].input1); } if (this.connectedGates[index].hasRight() && this.connectedGates[index].input2 != null) { this.connectedGates.Add(this.connectedGates[index].input2); } this.connectedGates.RemoveAt(index); } else { this.deleteGateRec(this.connectedGates[index], gateToFind, (Gates.GateObject)null); } } this.processingList.Remove(gateToFind); }
private void connectGates(Gates.GateObject gate1, string in1, Gates.GateObject gate2, string in2) { if (in1 == "Out") { if (object.ReferenceEquals((object)gate1.input1, (object)gate2) || object.ReferenceEquals((object)gate1.input2, (object)gate2)) { return; } if (in2 == "Left") { gate2.input1 = gate1; } else { if (!(in2 == "Right")) { return; } gate2.input2 = gate1; } } else { if (!(in2 == "Out") || (object.ReferenceEquals((object)gate2.input1, (object)gate1) || object.ReferenceEquals((object)gate2.input2, (object)gate1))) { return; } if (in1 == "Left") { gate1.input1 = gate2; } else if (in1 == "Right") { gate1.input2 = gate2; } } }
public void doConversion(Gates.GateObject node) { if (node.GetType() != typeof(Gates.Output)) { string equationTerm = this.getEquationTerm(node); if (this.equation == "") { this.equation = equationTerm; } else { for (int startIndex = 0; startIndex < this.equation.Length; ++startIndex) { if ((int)this.equation[startIndex] == 35) { this.equation = this.equation.Remove(startIndex, 1); this.equation = this.equation.Insert(startIndex, equationTerm); break; } } } } if (node == null) { return; } if (node.hasLeft() && node.input1 != null) { this.doConversion(node.input1); } if (!node.hasRight() || node.input2 == null) { return; } this.doConversion(node.input2); }
public int simulateDiagram(Gates.GateObject node) { this.simulateDiagramRecursive(node); return(this.result); }
private void simulateDiagramRec(Gates.GateObject Node) { bool flag = false; if (Node == null) { this.result = this.stack.Pop(); } if (Node.getOutput() != -1) { this.stack.Push(Node.getOutput()); if (Node.getOutput() == 1) { Node.setHighlight(true); } } if (Node.hasLeft()) { if (Node.input1 != null) { if (Node.input1 == Node.input2) { flag = true; } this.simulateDiagramRec(Node.input1); } else { this.error = true; } } if (Node.hasRight() && !flag) { if (Node.input2 != null) { this.simulateDiagramRec(Node.input2); } else { this.error = true; } } if (Node.hasLeft() && Node.hasRight() && this.stack.Count > 0) { if (Node.in2val == -1 && !flag) { Node.in2val = this.stack.Pop(); } else { int num = this.stack.Pop(); Node.in2val = num; this.stack.Push(num); } } if (Node.in1val != -1 || this.stack.Count <= 0) { return; } Node.in1val = this.stack.Pop(); if (Node.in2val != -1 || Node.hasLeft() && !Node.hasRight()) { int output = Node.getOutput(); this.stack.Push(output); if (output == 1) { Node.setHighlight(true); } } }
public ObjectFinder() { this.output = (Gates.GateObject)null; }
public void AddGate(Gates.GateObject gate) { this.connectedGates.Add(gate); this.processingList.Add(gate); this.Invalidate(); }
public void renderGates(Gates.GateObject gate, PaintEventArgs e) { if (gate.getOutput() == 1) { gate.setHighlight(true); } else { gate.setHighlight(false); } Image image = gate.getImage(); Point pos1 = gate.getPos(); e.Graphics.DrawImageUnscaled(image, pos1); if (gate.GetType() == typeof(Gates.Input) || gate.GetType() == typeof(Gates.Output)) { SolidBrush solidBrush = new SolidBrush(Color.Black); Font font = new Font(FontFamily.GenericSansSerif, 12f, FontStyle.Bold); Point point = new Point(pos1.X + 12, pos1.Y - 10); if (gate.GetType() == typeof(Gates.Input)) { e.Graphics.DrawString(gate.getName(), font, (Brush)solidBrush, (PointF)point); } else { e.Graphics.DrawString("Q", font, (Brush)solidBrush, (PointF)point); } } Pen pen1 = new Pen(Color.Black, 1f); int[][] circlePositions1 = gate.getCirclePositions(); for (int index = 0; index < circlePositions1.Length; ++index) { e.Graphics.DrawEllipse(pen1, pos1.X + circlePositions1[index][0], pos1.Y + circlePositions1[index][1], circlePositions1[index][2], circlePositions1[index][3]); } if (gate == null) { return; } Point pos2; Point start; Point end; if (gate.hasLeft() && gate.input1 != null) { Pen pen2 = new Pen(Color.Black, 2f); if (this.wireList.Count == this.wireHighlight && this.wireHighlight != -1) { pen2 = new Pen(Color.Red, 2f); } int[][] circlePositions2 = gate.input1.getCirclePositions(); pos2 = gate.input1.getPos(); start = new Point(circlePositions2[0][0] + pos2.X + 4, circlePositions2[0][1] + pos2.Y + 2); if (gate.input1.getOutput() == 1 && this.simulating) { pen2 = new Pen(Color.FromArgb(38, 126, 241), 2f); } if (circlePositions1.Length > 1) { end = new Point(circlePositions1[1][0] + pos1.X, circlePositions1[1][1] + pos1.Y + 2); Point[] points = this.calculatePoints(start, end); this.wireList.Add(points); e.Graphics.DrawLines(pen2, points); } else { end = new Point(circlePositions1[0][0] + pos1.X, circlePositions1[0][1] + pos1.Y + 2); Point[] points = this.calculatePoints(start, end); this.wireList.Add(points); e.Graphics.DrawLines(pen2, points); } this.renderGates(gate.input1, e); } if (!gate.hasRight() || gate.input2 == null) { return; } Pen pen3 = new Pen(Color.Black, 2f); if (this.wireList.Count == this.wireHighlight && this.wireHighlight != -1) { pen3 = new Pen(Color.Red, 2f); } int[][] circlePositions3 = gate.input2.getCirclePositions(); if (gate.input2.getOutput() == 1 && this.simulating) { pen3 = new Pen(Color.FromArgb(38, 126, 241), 2f); } pos2 = gate.input2.getPos(); start = new Point(circlePositions3[0][0] + pos2.X + 4, circlePositions3[0][1] + pos2.Y + 2); end = new Point(circlePositions1[2][0] + pos1.X, circlePositions1[2][1] + pos1.Y + 2); Point[] points1 = this.calculatePoints(start, end); this.wireList.Add(points1); e.Graphics.DrawLines(pen3, points1); this.renderGates(gate.input2, e); }
public Gates.GateObject getDiagram() { Gates.GateObject gateObject = this.diagramStore; this.diagramStore = (Gates.GateObject)null; return(gateObject); }