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