03a0e34eb68195889fbae0f8bea689634713f2d9
[Mograsim.git] / plugins / 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.mograsim.logic.core.timeline.Timeline;
10 import net.mograsim.logic.model.LogicExecuter;
11 import net.mograsim.logic.model.LogicUICanvas;
12 import net.mograsim.logic.model.model.LogicModelModifiable;
13 import net.mograsim.logic.model.model.components.atomic.ModelManualSwitch;
14 import net.mograsim.logic.model.model.components.atomic.ModelNotGate;
15 import net.mograsim.logic.model.model.components.atomic.ModelOrGate;
16 import net.mograsim.logic.model.model.wires.ModelWire;
17 import net.mograsim.logic.model.model.wires.ModelWireCrossPoint;
18 import net.mograsim.logic.model.modeladapter.CoreModelParameters;
19 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
20 import net.mograsim.logic.model.preferences.RenderPreferences;
21 import net.mograsim.plugin.preferences.EclipseRenderPreferences;
22
23 public class SimulationPreview implements IThemePreview
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                 RenderPreferences currentThemeRenderPrefs = new EclipseRenderPreferences(currentTheme,
33                                 MograsimActivator.instance().getPreferenceStore());
34
35                 LogicModelModifiable model = new LogicModelModifiable();
36                 CoreModelParameters params = new CoreModelParameters();
37                 params.gateProcessTime = 50;
38                 params.hardcodedComponentProcessTime = params.gateProcessTime * 5;
39                 params.wireTravelTime = 10;
40
41                 ModelManualSwitch rIn = new ModelManualSwitch(model, 1);
42                 rIn.moveTo(10, 10);
43                 ModelManualSwitch sIn = new ModelManualSwitch(model, 1);
44                 sIn.moveTo(10, 70);
45
46                 ModelOrGate or1 = new ModelOrGate(model, 1);
47                 or1.moveTo(70, 12.5);
48                 new ModelWire(model, rIn.getOutputPin(), or1.getPin("A"));
49
50                 ModelOrGate or2 = new ModelOrGate(model, 1);
51                 or2.moveTo(70, 62.5);
52                 new ModelWire(model, sIn.getOutputPin(), or2.getPin("B"));
53
54                 ModelNotGate not1 = new ModelNotGate(model, 1);
55                 not1.moveTo(110, 17.5);
56                 new ModelWire(model, or1.getPin("Y"), not1.getPin("A"));
57
58                 ModelNotGate not2 = new ModelNotGate(model, 1);
59                 not2.moveTo(110, 67.5);
60                 new ModelWire(model, or2.getPin("Y"), not2.getPin("A"));
61
62                 ModelWireCrossPoint p1 = new ModelWireCrossPoint(model, 1);
63                 p1.moveCenterTo(140, 22.5);
64                 new ModelWire(model, not1.getPin("Y"), p1);
65                 new ModelWire(model, p1, or2.getPin("A"), new Point(140, 35), new Point(50, 60), new Point(50, 67.5));
66
67                 ModelWireCrossPoint p2 = new ModelWireCrossPoint(model, 1);
68                 p2.moveCenterTo(140, 72.5);
69                 new ModelWire(model, not2.getPin("Y"), p2);
70                 new ModelWire(model, p2, or1.getPin("B"), new Point(140, 60), new Point(50, 35), new Point(50, 27.5));
71
72                 ModelWireCrossPoint o1 = new ModelWireCrossPoint(model, 1);
73                 o1.moveCenterTo(150, 22.5);
74                 new ModelWire(model, p1, o1);
75
76                 ModelWireCrossPoint o2 = new ModelWireCrossPoint(model, 1);
77                 o2.moveCenterTo(150, 72.5);
78                 new ModelWire(model, p2, o2);
79
80                 Timeline t = LogicCoreAdapter.convert(model, params);
81                 exec = new LogicExecuter(t);
82
83                 rIn.clicked(0, 0);
84
85                 ui = new LogicUICanvas(parent, SWT.NONE, model, currentThemeRenderPrefs);
86
87                 ui.zoom(3.5, 10, 10);
88                 exec.startLiveExecution();
89
90                 currentTheme.addPropertyChangeListener(e -> ui.redraw());
91         }
92
93         @Override
94         public void dispose()
95         {
96                 exec.stopLiveExecution();
97         }
98 }