Implemented the RS-Latch-Example
[Mograsim.git] / LogicUI / src / era / mi / examples / gui / LogicUI.java
index cf00924..9d71e00 100644 (file)
@@ -12,14 +12,13 @@ import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Shell;\r
 \r
 import era.mi.components.gui.BasicGUIComponent;\r
-import era.mi.components.gui.GUIAndGate;\r
 import era.mi.components.gui.GUIManualSwitch;\r
-import era.mi.components.gui.GUIMerger;\r
-import era.mi.components.gui.GUIMux;\r
 import era.mi.components.gui.GUINotGate;\r
-import era.mi.components.gui.GUISplitter;\r
+import era.mi.components.gui.GUIOrGate;\r
 import era.mi.logic.Simulation;\r
 import era.mi.logic.wires.WireArray;\r
+import era.mi.wires.gui.GUIWire;\r
+import era.mi.wires.gui.WireConnectionPoint;\r
 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
 import net.haspamelodica.swt.helper.gcs.TranslatedGC;\r
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
@@ -29,11 +28,15 @@ import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInpu
 \r
 public class LogicUI\r
 {\r
+       private static final int                                        WIRE_DELAY      = 40;\r
+       private static final int                                        OR_DELAY        = 100;\r
+       private static final int                                        NOT_DELAY       = 100;\r
        private final Display                                           display;\r
        private final Shell                                                     shell;\r
        private final ZoomableCanvas                            canvas;\r
        private final Set<BasicGUIComponent>            components;\r
        private final Map<BasicGUIComponent, Point>     componentPositions;\r
+       private final Set<GUIWire>                                      wires;\r
 \r
        public LogicUI()\r
        {\r
@@ -44,9 +47,11 @@ public class LogicUI
 \r
                components = new HashSet<>();\r
                componentPositions = new HashMap<>();\r
+               wires = new HashSet<>();\r
                initComponents();\r
 \r
-               canvas.addZoomedRenderer(gc -> components.forEach(component -> drawComponent(gc, component)));\r
+               canvas.addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));\r
+               canvas.addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));\r
                ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);\r
                userInput.buttonDrag = 3;\r
                userInput.buttonZoom = 2;\r
@@ -57,19 +62,44 @@ public class LogicUI
        private void initComponents()\r
        {\r
                Simulation.TIMELINE.reset();\r
-               WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1), e = new WireArray(1, 1),\r
-                               f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1), j = new WireArray(1, 1), k = new WireArray(1, 1);\r
-               addComponent(new GUIManualSwitch(a), 160, 10);\r
-               addComponent(new GUIAndGate(1, f, a, b), 130, 10);\r
-               addComponent(new GUINotGate(1, f, g), 100, 10);\r
-               addComponent(new GUIMerger(h, c, g), 70, 10);\r
-               addComponent(new GUIMux(1, i, e, h, d), 10, 10);\r
-               addComponent(new GUISplitter(i, k, j), 40, 10);\r
+               WireArray r = new WireArray(1, WIRE_DELAY);\r
+               WireArray s = new WireArray(1, WIRE_DELAY);\r
+               WireArray t2 = new WireArray(1, WIRE_DELAY);\r
+               WireArray t1 = new WireArray(1, WIRE_DELAY);\r
+               WireArray q = new WireArray(1, WIRE_DELAY);\r
+               WireArray nq = new WireArray(1, WIRE_DELAY);\r
+\r
+               GUIManualSwitch rIn = addComponent(new GUIManualSwitch(r), 100, 100);\r
+               GUIManualSwitch sIn = addComponent(new GUIManualSwitch(s), 100, 200);\r
+               GUIOrGate or1 = addComponent(new GUIOrGate(OR_DELAY, t1, r, nq), 160, 102.5);\r
+               GUIOrGate or2 = addComponent(new GUIOrGate(OR_DELAY, t2, q, s), 160, 192.5);\r
+               GUINotGate not1 = addComponent(new GUINotGate(NOT_DELAY, t1, q), 200, 107.5);\r
+               GUINotGate not2 = addComponent(new GUINotGate(NOT_DELAY, t2, nq), 200, 197.5);\r
+\r
+               WireConnectionPoint p1 = addComponent(new WireConnectionPoint(q, 2), 250, 112.5);\r
+               WireConnectionPoint p2 = addComponent(new WireConnectionPoint(nq, 2), 250, 202.5);\r
+\r
+               addWire(rIn, 0, or1, 0);\r
+               addWire(sIn, 0, or2, 1);\r
+               addWire(or1, 2, not1, 0);\r
+               addWire(or2, 2, not2, 0);\r
+               addWire(not1, 1, p1, 0);\r
+               addWire(not2, 1, p2, 0);\r
+               addWire(p1, 1, or2, 0, new Point(250, 130), new Point(140, 185), new Point(140, 197.5));\r
+               addWire(p2, 1, or1, 1, new Point(250, 185), new Point(140, 130), new Point(140, 117.5));\r
        }\r
-       private void addComponent(BasicGUIComponent component, double x, double y)\r
+       /**\r
+        * Returns the given component for convenience.\r
+        */\r
+       private <C extends BasicGUIComponent> C addComponent(C component, double x, double y)\r
        {\r
                components.add(component);\r
                componentPositions.put(component, new Point(x, y));\r
+               return component;\r
+       }\r
+       private void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path)\r
+       {\r
+               wires.add(new GUIWire(component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path));\r
        }\r
        private void drawComponent(GeneralGC gc, BasicGUIComponent component)\r
        {\r