1 package era.mi.examples.gui;
3 import java.util.HashMap;
4 import java.util.HashSet;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.layout.FillLayout;
10 import org.eclipse.swt.widgets.Display;
11 import org.eclipse.swt.widgets.Event;
12 import org.eclipse.swt.widgets.Shell;
14 import era.mi.components.gui.BasicGUIComponent;
15 import era.mi.components.gui.GUIManualSwitch;
16 import era.mi.components.gui.GUINotGate;
17 import era.mi.components.gui.GUIOrGate;
18 import era.mi.logic.Simulation;
19 import era.mi.logic.wires.WireArray;
20 import era.mi.wires.gui.GUIWire;
21 import era.mi.wires.gui.WireConnectionPoint;
22 import net.haspamelodica.swt.helper.gcs.GeneralGC;
23 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
24 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
25 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
26 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
27 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
31 private static final int WIRE_DELAY = 40;
32 private static final int OR_DELAY = 100;
33 private static final int NOT_DELAY = 100;
34 private final Display display;
35 private final Shell shell;
36 private final ZoomableCanvas canvas;
37 private final Set<BasicGUIComponent> components;
38 private final Map<BasicGUIComponent, Point> componentPositions;
39 private final Set<GUIWire> wires;
43 display = new Display();
44 shell = new Shell(display);
45 shell.setLayout(new FillLayout());
46 canvas = new ZoomableCanvas(shell, SWT.NONE);
48 components = new HashSet<>();
49 componentPositions = new HashMap<>();
50 wires = new HashSet<>();
53 canvas.addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));
54 canvas.addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));
55 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
56 userInput.buttonDrag = 3;
57 userInput.buttonZoom = 2;
58 userInput.enableUserInput();
59 new ZoomableCanvasOverlay(canvas, null).enableScale();
60 canvas.addListener(SWT.MouseDown, this::mouseDown);
62 private void initComponents()
64 Simulation.TIMELINE.reset();
65 WireArray r = new WireArray(1, WIRE_DELAY);
66 WireArray s = new WireArray(1, WIRE_DELAY);
67 WireArray t2 = new WireArray(1, WIRE_DELAY);
68 WireArray t1 = new WireArray(1, WIRE_DELAY);
69 WireArray q = new WireArray(1, WIRE_DELAY);
70 WireArray nq = new WireArray(1, WIRE_DELAY);
72 GUIManualSwitch rIn = addComponent(new GUIManualSwitch(r), 100, 100);
73 GUIManualSwitch sIn = addComponent(new GUIManualSwitch(s), 100, 200);
74 GUIOrGate or1 = addComponent(new GUIOrGate(OR_DELAY, t1, r, nq), 160, 102.5);
75 GUIOrGate or2 = addComponent(new GUIOrGate(OR_DELAY, t2, q, s), 160, 192.5);
76 GUINotGate not1 = addComponent(new GUINotGate(NOT_DELAY, t1, q), 200, 107.5);
77 GUINotGate not2 = addComponent(new GUINotGate(NOT_DELAY, t2, nq), 200, 197.5);
79 WireConnectionPoint p1 = addComponent(new WireConnectionPoint(q, 2), 250, 112.5);
80 WireConnectionPoint p2 = addComponent(new WireConnectionPoint(nq, 2), 250, 202.5);
82 addWire(rIn, 0, or1, 0);
83 addWire(sIn, 0, or2, 1);
84 addWire(or1, 2, not1, 0);
85 addWire(or2, 2, not2, 0);
86 addWire(not1, 1, p1, 0);
87 addWire(not2, 1, p2, 0);
88 addWire(p1, 1, or2, 0, new Point(250, 130), new Point(140, 185), new Point(140, 197.5));
89 addWire(p2, 1, or1, 1, new Point(250, 185), new Point(140, 130), new Point(140, 117.5));
92 * Returns the given component for convenience.
94 private <C extends BasicGUIComponent> C addComponent(C component, double x, double y)
96 components.add(component);
97 componentPositions.put(component, new Point(x, y));
100 private void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path)
102 wires.add(new GUIWire(component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path));
104 private void drawComponent(GeneralGC gc, BasicGUIComponent component)
106 TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));
107 component.render(tgc);
108 tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
109 for(int i = 0; i < component.getConnectedWireArraysCount(); i ++)
111 Point connectionPoint = component.getWireArrayConnectionPoint(i);
112 if(connectionPoint != null)
113 tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);
116 private void mouseDown(Event e)
120 Point click = canvas.displayToWorldCoords(e.x, e.y);
121 for(BasicGUIComponent component : components)
122 if(component.getBounds().translate(componentPositions.get(component)).contains(click))
124 if(component.clicked(click.x, click.y))
134 while(!shell.isDisposed())
135 if(!display.readAndDispatch())
139 public static void main(String[] args)