8242666e56cd3d66e1871235ba5be01d32097028
[Mograsim.git] / LogicUI / src / LogicUI.java
1 import java.util.HashMap;
2 import java.util.HashSet;
3 import java.util.Map;
4 import java.util.Set;
5
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.Shell;
10
11 import era.mi.components.gui.BasicGUIComponent;
12 import era.mi.components.gui.GUIAndGate;
13 import era.mi.components.gui.GUIMerger;
14 import era.mi.components.gui.GUIMux;
15 import era.mi.components.gui.GUINotGate;
16 import era.mi.components.gui.GUISplitter;
17 import era.mi.logic.Simulation;
18 import era.mi.logic.wires.WireArray;
19 import net.haspamelodica.swt.helper.gcs.GeneralGC;
20 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
21 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
22 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
23 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
24 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
25
26 public class LogicUI
27 {
28         private final Display                                           display;
29         private final Shell                                                     shell;
30         private final Set<BasicGUIComponent>            components;
31         private final Map<BasicGUIComponent, Point>     componentPositions;
32
33         public LogicUI()
34         {
35                 display = new Display();
36                 shell = new Shell(display);
37                 shell.setLayout(new FillLayout());
38                 ZoomableCanvas canvas = new ZoomableCanvas(shell, SWT.NONE);
39
40                 components = new HashSet<>();
41                 componentPositions = new HashMap<>();
42                 initComponents();
43
44                 canvas.addZoomedRenderer(gc -> components.forEach(component -> drawComponent(gc, component)));
45                 new ZoomableCanvasUserInput(canvas).enableUserInput();
46                 new ZoomableCanvasOverlay(canvas, null).enableScale();
47         }
48         private void initComponents()
49         {
50                 Simulation.TIMELINE.reset();
51                 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),
52                                 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);
53                 addComponent(new GUIAndGate(1, f, a, b), 130, 10);
54                 addComponent(new GUINotGate(1, f, g), 100, 10);
55                 addComponent(new GUIMerger(h, c, g), 70, 10);
56                 addComponent(new GUIMux(1, i, e, h, d), 10, 10);
57                 addComponent(new GUISplitter(i, k, j), 40, 10);
58         }
59         private void addComponent(BasicGUIComponent component, double x, double y)
60         {
61                 components.add(component);
62                 componentPositions.put(component, new Point(x, y));
63         }
64         private void drawComponent(GeneralGC gc, BasicGUIComponent component)
65         {
66                 TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));
67                 component.render(tgc);
68                 tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
69                 for(int i = 0; i < component.getConnectedWireArraysCount(); i ++)
70                 {
71                         Point connectionPoint = component.getWireArrayConnectionPoint(i);
72                         if(connectionPoint != null)
73                                 tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);
74                 }
75         }
76         public void run()
77         {
78                 shell.open();
79                 while(!shell.isDisposed())
80                         if(!display.readAndDispatch())
81                                 display.sleep();
82         }
83
84         public static void main(String[] args)
85         {
86                 new LogicUI().run();
87         }
88 }