1 package era.mi.examples.gui;
\r
3 import java.util.HashMap;
\r
4 import java.util.HashSet;
\r
5 import java.util.Map;
\r
6 import java.util.Set;
\r
7 import java.util.concurrent.atomic.AtomicBoolean;
\r
9 import org.eclipse.swt.SWT;
\r
10 import org.eclipse.swt.layout.FillLayout;
\r
11 import org.eclipse.swt.widgets.Display;
\r
12 import org.eclipse.swt.widgets.Event;
\r
13 import org.eclipse.swt.widgets.Shell;
\r
15 import era.mi.components.gui.BasicGUIComponent;
\r
16 import era.mi.components.gui.GUIManualSwitch;
\r
17 import era.mi.components.gui.GUINotGate;
\r
18 import era.mi.components.gui.GUIOrGate;
\r
19 import era.mi.logic.Simulation;
\r
20 import era.mi.logic.wires.WireArray;
\r
21 import era.mi.wires.gui.GUIWire;
\r
22 import era.mi.wires.gui.WireConnectionPoint;
\r
23 import net.haspamelodica.swt.helper.gcs.GeneralGC;
\r
24 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
\r
25 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
\r
26 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
\r
27 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
\r
28 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
\r
30 public class LogicUI
\r
32 private static final int WIRE_DELAY = 40;
\r
33 private static final int OR_DELAY = 100;
\r
34 private static final int NOT_DELAY = 100;
\r
35 private final Display display;
\r
36 private final Shell shell;
\r
37 private final ZoomableCanvas canvas;
\r
38 private final Set<BasicGUIComponent> components;
\r
39 private final Map<BasicGUIComponent, Point> componentPositions;
\r
40 private final Set<GUIWire> wires;
\r
44 display = new Display();
\r
45 shell = new Shell(display);
\r
46 shell.setLayout(new FillLayout());
\r
47 canvas = new ZoomableCanvas(shell, SWT.NONE);
\r
49 components = new HashSet<>();
\r
50 componentPositions = new HashMap<>();
\r
51 wires = new HashSet<>();
\r
54 canvas.addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));
\r
55 canvas.addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));
\r
56 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
\r
57 userInput.buttonDrag = 3;
\r
58 userInput.buttonZoom = 2;
\r
59 userInput.enableUserInput();
\r
60 new ZoomableCanvasOverlay(canvas, null).enableScale();
\r
61 canvas.addListener(SWT.MouseDown, this::mouseDown);
\r
63 private void initComponents()
\r
65 Simulation.TIMELINE.reset();
\r
66 WireArray r = new WireArray(1, WIRE_DELAY);
\r
67 WireArray s = new WireArray(1, WIRE_DELAY);
\r
68 WireArray t2 = new WireArray(1, WIRE_DELAY);
\r
69 WireArray t1 = new WireArray(1, WIRE_DELAY);
\r
70 WireArray q = new WireArray(1, WIRE_DELAY);
\r
71 WireArray nq = new WireArray(1, WIRE_DELAY);
\r
73 GUIManualSwitch rIn = addComponent(new GUIManualSwitch(r), 100, 100);
\r
74 GUIManualSwitch sIn = addComponent(new GUIManualSwitch(s), 100, 200);
\r
75 GUIOrGate or1 = addComponent(new GUIOrGate(OR_DELAY, t1, r, nq), 160, 102.5);
\r
76 GUIOrGate or2 = addComponent(new GUIOrGate(OR_DELAY, t2, q, s), 160, 192.5);
\r
77 GUINotGate not1 = addComponent(new GUINotGate(NOT_DELAY, t1, q), 200, 107.5);
\r
78 GUINotGate not2 = addComponent(new GUINotGate(NOT_DELAY, t2, nq), 200, 197.5);
\r
80 WireConnectionPoint p1 = addComponent(new WireConnectionPoint(q, 2), 250, 112.5);
\r
81 WireConnectionPoint p2 = addComponent(new WireConnectionPoint(nq, 2), 250, 202.5);
\r
83 addWire(rIn, 0, or1, 0);
\r
84 addWire(sIn, 0, or2, 1);
\r
85 addWire(or1, 2, not1, 0);
\r
86 addWire(or2, 2, not2, 0);
\r
87 addWire(not1, 1, p1, 0);
\r
88 addWire(not2, 1, p2, 0);
\r
89 addWire(p1, 1, or2, 0, new Point(250, 130), new Point(140, 185), new Point(140, 197.5));
\r
90 addWire(p2, 1, or1, 1, new Point(250, 185), new Point(140, 130), new Point(140, 117.5));
\r
93 * Returns the given component for convenience.
\r
95 private <C extends BasicGUIComponent> C addComponent(C component, double x, double y)
\r
97 components.add(component);
\r
98 componentPositions.put(component, new Point(x, y));
\r
101 private void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, int component2ConnectionIndex, Point... path)
\r
103 wires.add(new GUIWire(component1, component1ConnectionIndex, componentPositions.get(component1), component2, component2ConnectionIndex, componentPositions.get(component2), path));
\r
105 private void drawComponent(GeneralGC gc, BasicGUIComponent component)
\r
107 TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));
\r
108 component.render(tgc);
\r
109 tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
\r
110 for(int i = 0; i < component.getConnectedWireArraysCount(); i ++)
\r
112 Point connectionPoint = component.getWireArrayConnectionPoint(i);
\r
113 if(connectionPoint != null)
\r
114 tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);
\r
117 private void mouseDown(Event e)
\r
121 Point click = canvas.displayToWorldCoords(e.x, e.y);
\r
122 for(BasicGUIComponent component : components)
\r
123 if(component.getBounds().translate(componentPositions.get(component)).contains(click))
\r
125 if(component.clicked(click.x, click.y))
\r
134 AtomicBoolean running = new AtomicBoolean(true);
\r
135 Thread simulationThread = new Thread(() ->
\r
137 while(running.get())
\r
139 //always execute to keep timeline from "hanging behind" for too long
\r
140 Simulation.TIMELINE.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
\r
142 if(Simulation.TIMELINE.hasNext())
\r
144 sleepTime = Simulation.TIMELINE.nextEventTime() - System.currentTimeMillis();
\r
150 Thread.sleep(sleepTime);
\r
151 } catch(InterruptedException e)
\r
152 {} //it is normal execution flow to be interrupted
\r
155 simulationThread.start();
\r
156 Simulation.TIMELINE.addEventAddedListener(event ->
\r
158 if(event.getTiming() >= System.currentTimeMillis() / (double) 1)
\r
159 simulationThread.interrupt();
\r
163 while(!shell.isDisposed())
\r
164 if(!display.readAndDispatch())
\r
166 running.set(false);
\r
167 simulationThread.interrupt();
\r
170 public static void main(String[] args)
\r
172 new LogicUI().run();
\r