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.GUIAndGate;
16 import era.mi.components.gui.GUIManualSwitch;
17 import era.mi.components.gui.GUIMerger;
18 import era.mi.components.gui.GUIMux;
19 import era.mi.components.gui.GUINotGate;
20 import era.mi.components.gui.GUISplitter;
21 import era.mi.logic.Simulation;
22 import era.mi.logic.wires.WireArray;
23 import net.haspamelodica.swt.helper.gcs.GeneralGC;
24 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
25 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
26 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
27 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
28 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
32 private final Display display;
33 private final Shell shell;
34 private final ZoomableCanvas canvas;
35 private final Set<BasicGUIComponent> components;
36 private final Map<BasicGUIComponent, Point> componentPositions;
40 display = new Display();
41 shell = new Shell(display);
42 shell.setLayout(new FillLayout());
43 canvas = new ZoomableCanvas(shell, SWT.NONE);
45 components = new HashSet<>();
46 componentPositions = new HashMap<>();
49 canvas.addZoomedRenderer(gc -> components.forEach(component -> drawComponent(gc, component)));
50 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
51 userInput.buttonDrag = 3;
52 userInput.buttonZoom = 2;
53 userInput.enableUserInput();
54 new ZoomableCanvasOverlay(canvas, null).enableScale();
55 canvas.addListener(SWT.MouseDown, this::mouseDown);
57 private void initComponents()
59 Simulation.TIMELINE.reset();
60 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),
61 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);
62 addComponent(new GUIManualSwitch(a), 160, 10);
63 addComponent(new GUIAndGate(1, f, a, b), 130, 10);
64 addComponent(new GUINotGate(1, f, g), 100, 10);
65 addComponent(new GUIMerger(h, c, g), 70, 10);
66 addComponent(new GUIMux(1, i, e, h, d), 10, 10);
67 addComponent(new GUISplitter(i, k, j), 40, 10);
69 private void addComponent(BasicGUIComponent component, double x, double y)
71 components.add(component);
72 componentPositions.put(component, new Point(x, y));
74 private void drawComponent(GeneralGC gc, BasicGUIComponent component)
76 TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));
77 component.render(tgc);
78 tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
79 for(int i = 0; i < component.getConnectedWireArraysCount(); i ++)
81 Point connectionPoint = component.getWireArrayConnectionPoint(i);
82 if(connectionPoint != null)
83 tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);
86 private void mouseDown(Event e)
90 Point click = canvas.displayToWorldCoords(e.x, e.y);
91 for(BasicGUIComponent component : components)
92 if(component.getBounds().translate(componentPositions.get(component)).contains(click))
94 if(component.clicked(click.x, click.y))
104 while(!shell.isDisposed())
105 if(!display.readAndDispatch())
109 public static void main(String[] args)