Components now can be clicked
[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.Event;
10 import org.eclipse.swt.widgets.Shell;
11
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;
27
28 public class LogicUI
29 {
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;
35
36         public LogicUI()
37         {
38                 display = new Display();
39                 shell = new Shell(display);
40                 shell.setLayout(new FillLayout());
41                 canvas = new ZoomableCanvas(shell, SWT.NONE);
42
43                 components = new HashSet<>();
44                 componentPositions = new HashMap<>();
45                 initComponents();
46
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);
54         }
55         private void initComponents()
56         {
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);
66         }
67         private void addComponent(BasicGUIComponent component, double x, double y)
68         {
69                 components.add(component);
70                 componentPositions.put(component, new Point(x, y));
71         }
72         private void drawComponent(GeneralGC gc, BasicGUIComponent component)
73         {
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 ++)
78                 {
79                         Point connectionPoint = component.getWireArrayConnectionPoint(i);
80                         if(connectionPoint != null)
81                                 tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);
82                 }
83         }
84         private void mouseDown(Event e)
85         {
86                 if(e.button == 1)
87                 {
88                         Point click = canvas.displayToWorldCoords(e.x, e.y);
89                         for(BasicGUIComponent component : components)
90                                 if(component.getBounds().translate(componentPositions.get(component)).contains(click))
91                                 {
92                                         if(component.clicked(click.x, click.y))
93                                                 canvas.redraw();
94                                         break;
95                                 }
96                 }
97         }
98
99         public void run()
100         {
101                 shell.open();
102                 while(!shell.isDisposed())
103                         if(!display.readAndDispatch())
104                                 display.sleep();
105         }
106
107         public static void main(String[] args)
108         {
109                 new LogicUI().run();
110         }
111 }