7b4dc0a188b9ee50e3a8b791c0c63a9078cac988
[Mograsim.git] / LogicUI / src / era / mi / examples / gui / LogicUI.java
1 package era.mi.examples.gui;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Map;
6 import java.util.Set;
7
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;
13
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;
29
30 public class LogicUI
31 {
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;
37
38         public LogicUI()
39         {
40                 display = new Display();
41                 shell = new Shell(display);
42                 shell.setLayout(new FillLayout());
43                 canvas = new ZoomableCanvas(shell, SWT.NONE);
44
45                 components = new HashSet<>();
46                 componentPositions = new HashMap<>();
47                 initComponents();
48
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);
56         }
57         private void initComponents()
58         {
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);
68         }
69         private void addComponent(BasicGUIComponent component, double x, double y)
70         {
71                 components.add(component);
72                 componentPositions.put(component, new Point(x, y));
73         }
74         private void drawComponent(GeneralGC gc, BasicGUIComponent component)
75         {
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 ++)
80                 {
81                         Point connectionPoint = component.getWireArrayConnectionPoint(i);
82                         if(connectionPoint != null)
83                                 tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);
84                 }
85         }
86         private void mouseDown(Event e)
87         {
88                 if(e.button == 1)
89                 {
90                         Point click = canvas.displayToWorldCoords(e.x, e.y);
91                         for(BasicGUIComponent component : components)
92                                 if(component.getBounds().translate(componentPositions.get(component)).contains(click))
93                                 {
94                                         if(component.clicked(click.x, click.y))
95                                                 canvas.redraw();
96                                         break;
97                                 }
98                 }
99         }
100
101         public void run()
102         {
103                 shell.open();
104                 while(!shell.isDisposed())
105                         if(!display.readAndDispatch())
106                                 display.sleep();
107         }
108
109         public static void main(String[] args)
110         {
111                 new LogicUI().run();
112         }
113 }