Configured Java editor save actions: Format code, don't organize imports
[Mograsim.git] / LogicUI / src / era / mi / gui / LogicUI.java
1 package era.mi.gui;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Map;
6 import java.util.Set;
7 import java.util.concurrent.atomic.AtomicBoolean;
8
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.layout.FillLayout;
11 import org.eclipse.swt.widgets.Display;
12 import org.eclipse.swt.widgets.Event;
13 import org.eclipse.swt.widgets.Shell;
14
15 import era.mi.gui.components.BasicGUIComponent;
16 import era.mi.gui.wires.GUIWire;
17 import era.mi.logic.Simulation;
18 import net.haspamelodica.swt.helper.gcs.GeneralGC;
19 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
20 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
21 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
22 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
23 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
24
25 /**
26  * Standalone simulation visualizer.
27  * 
28  * @author Daniel Kirschten
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         private final Set<GUIWire> wires;
38
39         public LogicUI()
40         {
41                 display = new Display();
42                 shell = new Shell(display);
43                 shell.setLayout(new FillLayout());
44                 canvas = new ZoomableCanvas(shell, SWT.NONE);
45
46                 components = new HashSet<>();
47                 componentPositions = new HashMap<>();
48                 wires = new HashSet<>();
49
50                 canvas.addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));
51                 canvas.addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));
52                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
53                 userInput.buttonDrag = 3;
54                 userInput.buttonZoom = 2;
55                 userInput.enableUserInput();
56                 new ZoomableCanvasOverlay(canvas, null).enableScale();
57                 canvas.addListener(SWT.MouseDown, this::mouseDown);
58         }
59
60         /**
61          * Add a component to be drawn. Returns the given component for convenience.
62          * 
63          * @author Daniel Kirschten
64          */
65         public <C extends BasicGUIComponent> C addComponent(C component, double x, double y)
66         {
67                 components.add(component);
68                 componentPositions.put(component, new Point(x, y));
69                 return component;
70         }
71
72         /**
73          * Add a graphical wire between the given connection points of the given components. The given components have to be added and the given
74          * connection points have to be connected logically first.
75          * 
76          * @author Daniel Kirschten
77          */
78         public void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2,
79                         int component2ConnectionIndex, Point... path)
80         {
81                 wires.add(new GUIWire(canvas::redrawThreadsafe, component1, component1ConnectionIndex, componentPositions.get(component1),
82                                 component2, component2ConnectionIndex, componentPositions.get(component2), path));
83         }
84
85         private void drawComponent(GeneralGC gc, BasicGUIComponent component)
86         {
87                 TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));
88                 component.render(tgc);
89                 tgc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
90         }
91
92         private void mouseDown(Event e)
93         {
94                 if (e.button == 1)
95                 {
96                         Point click = canvas.displayToWorldCoords(e.x, e.y);
97                         for (BasicGUIComponent component : components)
98                                 if (component.getBounds().translate(componentPositions.get(component)).contains(click))
99                                 {
100                                         if (component.clicked(click.x, click.y))
101                                                 canvas.redraw();
102                                         break;
103                                 }
104                 }
105         }
106
107         /**
108          * Start the simulation timeline, and open the UI shell. Returns when the shell is closed.
109          */
110         public void run()
111         {
112                 AtomicBoolean running = new AtomicBoolean(true);
113                 Thread simulationThread = new Thread(() ->
114                 {
115                         while (running.get())
116                         {
117                                 // always execute to keep timeline from "hanging behind" for too long
118                                 Simulation.TIMELINE.executeUpTo(System.currentTimeMillis(), System.currentTimeMillis() + 10);
119                                 long sleepTime;
120                                 if (Simulation.TIMELINE.hasNext())
121                                         sleepTime = Simulation.TIMELINE.nextEventTime() - System.currentTimeMillis();
122                                 else
123                                         sleepTime = 10;
124                                 try
125                                 {
126                                         if (sleepTime > 0)
127                                                 Thread.sleep(sleepTime);
128                                 }
129                                 catch (InterruptedException e)
130                                 {
131                                 } // it is normal execution flow to be interrupted
132                         }
133                 });
134                 simulationThread.start();
135                 Simulation.TIMELINE.addEventAddedListener(event ->
136                 {
137                         if (event.getTiming() <= System.currentTimeMillis())
138                                 simulationThread.interrupt();
139                 });
140
141                 shell.open();
142                 while (!shell.isDisposed())
143                         if (!display.readAndDispatch())
144                                 display.sleep();
145                 running.set(false);
146                 simulationThread.interrupt();
147         }
148 }