1 import java.util.HashMap;
\r
2 import java.util.HashSet;
\r
3 import java.util.Map;
\r
4 import java.util.Set;
\r
6 import org.eclipse.swt.SWT;
\r
7 import org.eclipse.swt.layout.FillLayout;
\r
8 import org.eclipse.swt.widgets.Display;
\r
9 import org.eclipse.swt.widgets.Event;
\r
10 import org.eclipse.swt.widgets.Shell;
\r
12 import era.mi.components.gui.BasicGUIComponent;
\r
13 import era.mi.components.gui.GUIAndGate;
\r
14 import era.mi.components.gui.GUIManualSwitch;
\r
15 import era.mi.components.gui.GUIMerger;
\r
16 import era.mi.components.gui.GUIMux;
\r
17 import era.mi.components.gui.GUINotGate;
\r
18 import era.mi.components.gui.GUISplitter;
\r
19 import era.mi.logic.Simulation;
\r
20 import era.mi.logic.wires.WireArray;
\r
21 import net.haspamelodica.swt.helper.gcs.GeneralGC;
\r
22 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
\r
23 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
\r
24 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
\r
25 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
\r
26 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
\r
28 public class LogicUI
\r
30 private final Display display;
\r
31 private final Shell shell;
\r
32 private final ZoomableCanvas canvas;
\r
33 private final Set<BasicGUIComponent> components;
\r
34 private final Map<BasicGUIComponent, Point> componentPositions;
\r
38 display = new Display();
\r
39 shell = new Shell(display);
\r
40 shell.setLayout(new FillLayout());
\r
41 canvas = new ZoomableCanvas(shell, SWT.NONE);
\r
43 components = new HashSet<>();
\r
44 componentPositions = new HashMap<>();
\r
47 canvas.addZoomedRenderer(gc -> components.forEach(component -> drawComponent(gc, component)));
\r
48 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
\r
49 userInput.buttonDrag = 3;
\r
50 userInput.buttonZoom = 2;
\r
51 userInput.enableUserInput();
\r
52 new ZoomableCanvasOverlay(canvas, null).enableScale();
\r
53 canvas.addListener(SWT.MouseDown, this::mouseDown);
\r
55 private void initComponents()
\r
57 Simulation.TIMELINE.reset();
\r
58 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
59 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
60 addComponent(new GUIManualSwitch(a), 160, 10);
\r
61 addComponent(new GUIAndGate(1, f, a, b), 130, 10);
\r
62 addComponent(new GUINotGate(1, f, g), 100, 10);
\r
63 addComponent(new GUIMerger(h, c, g), 70, 10);
\r
64 addComponent(new GUIMux(1, i, e, h, d), 10, 10);
\r
65 addComponent(new GUISplitter(i, k, j), 40, 10);
\r
67 private void addComponent(BasicGUIComponent component, double x, double y)
\r
69 components.add(component);
\r
70 componentPositions.put(component, new Point(x, y));
\r
72 private void drawComponent(GeneralGC gc, BasicGUIComponent component)
\r
74 TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));
\r
75 component.render(tgc);
\r
76 tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
\r
77 for(int i = 0; i < component.getConnectedWireArraysCount(); i ++)
\r
79 Point connectionPoint = component.getWireArrayConnectionPoint(i);
\r
80 if(connectionPoint != null)
\r
81 tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);
\r
84 private void mouseDown(Event e)
\r
88 Point click = canvas.displayToWorldCoords(e.x, e.y);
\r
89 for(BasicGUIComponent component : components)
\r
90 if(component.getBounds().translate(componentPositions.get(component)).contains(click))
\r
92 if(component.clicked(click.x, click.y))
\r
102 while(!shell.isDisposed())
\r
103 if(!display.readAndDispatch())
\r
107 public static void main(String[] args)
\r
109 new LogicUI().run();
\r