import org.eclipse.swt.widgets.Shell;
import era.mi.components.gui.BasicGUIComponent;
-import era.mi.components.gui.GUIAndGate;
import era.mi.components.gui.GUIManualSwitch;
-import era.mi.components.gui.GUIMerger;
-import era.mi.components.gui.GUIMux;
import era.mi.components.gui.GUINotGate;
-import era.mi.components.gui.GUISplitter;
+import era.mi.components.gui.GUIOrGate;
import era.mi.logic.Simulation;
import era.mi.logic.wires.WireArray;
+import era.mi.wires.gui.GUIWire;
+import era.mi.wires.gui.WireConnectionPoint;
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.gcs.TranslatedGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
public class LogicUI
{
+ private static final int WIRE_DELAY = 40;
+ private static final int OR_DELAY = 100;
+ private static final int NOT_DELAY = 100;
private final Display display;
private final Shell shell;
private final ZoomableCanvas canvas;
private final Set<BasicGUIComponent> components;
private final Map<BasicGUIComponent, Point> componentPositions;
+ private final Set<GUIWire> wires;
public LogicUI()
{
components = new HashSet<>();
componentPositions = new HashMap<>();
+ wires = new HashSet<>();
initComponents();
- canvas.addZoomedRenderer(gc -> components.forEach(component -> drawComponent(gc, component)));
+ canvas.addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));
+ canvas.addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));
ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
userInput.buttonDrag = 3;
userInput.buttonZoom = 2;
private void initComponents()
{
Simulation.TIMELINE.reset();
- 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),
- 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);
- addComponent(new GUIManualSwitch(a), 160, 10);
- addComponent(new GUIAndGate(1, f, a, b), 130, 10);
- addComponent(new GUINotGate(1, f, g), 100, 10);
- addComponent(new GUIMerger(h, c, g), 70, 10);
- addComponent(new GUIMux(1, i, e, h, d), 10, 10);
- addComponent(new GUISplitter(i, k, j), 40, 10);
+ WireArray r = new WireArray(1, WIRE_DELAY);
+ WireArray s = new WireArray(1, WIRE_DELAY);
+ WireArray t2 = new WireArray(1, WIRE_DELAY);
+ WireArray t1 = new WireArray(1, WIRE_DELAY);
+ WireArray q = new WireArray(1, WIRE_DELAY);
+ WireArray nq = new WireArray(1, WIRE_DELAY);
+
+ GUIManualSwitch rIn = addComponent(new GUIManualSwitch(r), 100, 100);
+ GUIManualSwitch sIn = addComponent(new GUIManualSwitch(s), 100, 200);
+ GUIOrGate or1 = addComponent(new GUIOrGate(OR_DELAY, t1, r, nq), 160, 102.5);
+ GUIOrGate or2 = addComponent(new GUIOrGate(OR_DELAY, t2, q, s), 160, 192.5);
+ GUINotGate not1 = addComponent(new GUINotGate(NOT_DELAY, t1, q), 200, 107.5);
+ GUINotGate not2 = addComponent(new GUINotGate(NOT_DELAY, t2, nq), 200, 197.5);
+
+ WireConnectionPoint p1 = addComponent(new WireConnectionPoint(q, 2), 250, 112.5);
+ WireConnectionPoint p2 = addComponent(new WireConnectionPoint(nq, 2), 250, 202.5);
+
+ addWire(rIn, 0, or1, 0);
+ addWire(sIn, 0, or2, 1);
+ addWire(or1, 2, not1, 0);
+ addWire(or2, 2, not2, 0);
+ addWire(not1, 1, p1, 0);
+ addWire(not2, 1, p2, 0);
+ addWire(p1, 1, or2, 0, new Point(250, 130), new Point(140, 185), new Point(140, 197.5));
+ addWire(p2, 1, or1, 1, new Point(250, 185), new Point(140, 130), new Point(140, 117.5));
}
- private void addComponent(BasicGUIComponent component, double x, double y)
+ /**
+ * Returns the given component for convenience.
+ */
+ private <C extends BasicGUIComponent> C addComponent(C component, double x, double y)
{
components.add(component);
componentPositions.put(component, new Point(x, y));
+ return component;
+ }
+ private void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path)
+ {
+ wires.add(new GUIWire(component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path));
}
private void drawComponent(GeneralGC gc, BasicGUIComponent component)
{
--- /dev/null
+package era.mi.wires.gui;
+
+import java.util.Objects;
+
+import era.mi.components.gui.BasicGUIComponent;
+import era.mi.logic.wires.WireArray;
+import net.haspamelodica.swt.helper.gcs.GeneralGC;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+
+public class GUIWire
+{
+ private final WireArray wa;
+ private final double[] path;
+
+ public GUIWire(BasicGUIComponent component1, int component1ConnectionIndex, Point component1Pos, BasicGUIComponent component2, int component2ConnectionIndex, Point component2Pos, Point... path)
+ {
+ this.wa = component1.getConnectedWireArray(component1ConnectionIndex);
+ if(!Objects.equals(wa, component2.getConnectedWireArray(component2ConnectionIndex)))
+ throw new IllegalArgumentException("Given connection points are not connected!");
+ this.path = new double[path.length * 2 + 4];
+ Point component1ConnectionPoint = component1.getWireArrayConnectionPoint(component1ConnectionIndex);
+ this.path[0] = component1Pos.x + component1ConnectionPoint.x;
+ this.path[1] = component1Pos.y + component1ConnectionPoint.y;
+ for(int srcI = 0, dstI = 2; srcI < path.length; srcI ++, dstI += 2)
+ {
+ this.path[dstI + 0] = path[srcI].x;
+ this.path[dstI + 1] = path[srcI].y;
+ }
+ Point component2ConnectionPoint = component2.getWireArrayConnectionPoint(component2ConnectionIndex);
+ this.path[this.path.length - 2] = component2Pos.x + component2ConnectionPoint.x;
+ this.path[this.path.length - 1] = component2Pos.y + component2ConnectionPoint.y;
+ }
+
+ public void render(GeneralGC gc)
+ {
+ gc.drawPolyline(path);
+ }
+}
\ No newline at end of file
--- /dev/null
+package era.mi.wires.gui;
+
+import org.eclipse.swt.graphics.Color;
+
+import era.mi.components.gui.BasicGUIComponent;
+import era.mi.logic.wires.WireArray;
+import net.haspamelodica.swt.helper.gcs.GeneralGC;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
+
+public class WireConnectionPoint implements BasicGUIComponent
+{
+ private final WireArray wa;
+ private final int wiresCrossing;
+
+ public WireConnectionPoint(WireArray wa, int wiresCrossing)
+ {
+ this.wa = wa;
+ this.wiresCrossing = wiresCrossing;
+ }
+
+ @Override
+ public void render(GeneralGC gc)
+ {
+ Color oldBG = gc.getBackground();
+ Color fg = gc.getForeground();
+ gc.setBackground(fg);
+ gc.fillOval(-2, -2, 4, 4);
+ gc.setBackground(oldBG);
+ }
+ @Override
+ public Rectangle getBounds()
+ {
+ return new Rectangle(0, 0, 0, 0);
+ }
+ @Override
+ public int getConnectedWireArraysCount()
+ {
+ return wiresCrossing;
+ }
+ @Override
+ public WireArray getConnectedWireArray(int connectionIndex)
+ {
+ return wa;
+ }
+ @Override
+ public Point getWireArrayConnectionPoint(int connectionIndex)
+ {
+ return new Point(0, 0);
+ }
+}
\ No newline at end of file