Now using preference system in plugin.core
[Mograsim.git] / net.mograsim.plugin.core / src / net / mograsim / plugin / SimulationPreview.java
1 package net.mograsim.plugin;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.widgets.Composite;
5 import org.eclipse.ui.themes.ITheme;
6 import org.eclipse.ui.themes.IThemePreview;
7
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
9 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
10 import net.mograsim.logic.core.timeline.Timeline;
11 import net.mograsim.logic.ui.LogicExecuter;
12 import net.mograsim.logic.ui.LogicUICanvas;
13 import net.mograsim.logic.ui.model.ViewModelModifiable;
14 import net.mograsim.logic.ui.model.components.GUIManualSwitch;
15 import net.mograsim.logic.ui.model.components.GUINotGate;
16 import net.mograsim.logic.ui.model.components.GUIOrGate;
17 import net.mograsim.logic.ui.model.wires.GUIWire;
18 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
19 import net.mograsim.logic.ui.modeladapter.LogicModelParameters;
20 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
21
22 public class SimulationPreview implements IThemePreview
23 {
24
25         private LogicUICanvas ui;
26         private LogicExecuter exec;
27
28         @Override
29         @SuppressWarnings("unused")
30         public void createControl(Composite parent, ITheme currentTheme)
31         {
32                 ViewModelModifiable model = new ViewModelModifiable();
33                 LogicModelParameters params = new LogicModelParameters();
34                 params.gateProcessTime = 50;
35                 params.wireTravelTime = 10;
36
37                 GUIManualSwitch rIn = new GUIManualSwitch(model);
38                 rIn.moveTo(10, 10);
39                 GUIManualSwitch sIn = new GUIManualSwitch(model);
40                 sIn.moveTo(10, 70);
41
42                 GUIOrGate or1 = new GUIOrGate(model, 1);
43                 or1.moveTo(70, 12.5);
44                 new GUIWire(model, rIn.getOutputPin(), or1.getPin("A"));
45
46                 GUIOrGate or2 = new GUIOrGate(model, 1);
47                 or2.moveTo(70, 62.5);
48                 new GUIWire(model, sIn.getOutputPin(), or2.getPin("B"));
49
50                 GUINotGate not1 = new GUINotGate(model, 1);
51                 not1.moveTo(110, 17.5);
52                 new GUIWire(model, or1.getPin("Y"), not1.getPin("A"));
53
54                 GUINotGate not2 = new GUINotGate(model, 1);
55                 not2.moveTo(110, 67.5);
56                 new GUIWire(model, or2.getPin("Y"), not2.getPin("A"));
57
58                 WireCrossPoint p1 = new WireCrossPoint(model, 1);
59                 p1.moveCenterTo(140, 22.5);
60                 new GUIWire(model, not1.getPin("Y"), p1);
61                 new GUIWire(model, p1, or2.getPin("A"), new Point(140, 35), new Point(50, 60), new Point(50, 67.5));
62
63                 WireCrossPoint p2 = new WireCrossPoint(model, 1);
64                 p2.moveCenterTo(140, 72.5);
65                 new GUIWire(model, not2.getPin("Y"), p2);
66                 new GUIWire(model, p2, or1.getPin("B"), new Point(140, 60), new Point(50, 35), new Point(50, 27.5));
67
68                 WireCrossPoint o1 = new WireCrossPoint(model, 1);
69                 o1.moveCenterTo(150, 22.5);
70                 new GUIWire(model, p1, o1);
71
72                 WireCrossPoint o2 = new WireCrossPoint(model, 1);
73                 o2.moveCenterTo(150, 72.5);
74                 new GUIWire(model, p2, o2);
75
76                 Timeline t = ViewLogicModelAdapter.convert(model, params);
77                 exec = new LogicExecuter(t);
78
79                 rIn.clicked(0, 0);
80
81                 ui = new LogicUICanvas(parent, SWT.NONE, model);
82                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
83                 userInput.buttonDrag = 3;
84                 userInput.buttonZoom = 2;
85                 userInput.enableUserInput();
86                 update(currentTheme);
87                 currentTheme.getColorRegistry().addListener(e -> update(currentTheme));
88
89                 ui.zoomSteps(12, 10, 10);
90                 exec.startLiveExecution();
91         }
92
93         private void update(ITheme currentTheme)
94         {
95                 ui.setBackground(currentTheme.getColorRegistry().get("net.mograsim.plugin.sim_backgound"));
96                 ui.setForeground(currentTheme.getColorRegistry().get("net.mograsim.plugin.sim_text_color"));
97         }
98
99         @Override
100         public void dispose()
101         {
102                 exec.stopLiveExecution();
103         }
104 }