1 package net.mograsim.plugin;
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;
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.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.atomic.GUIManualSwitch;
14 import net.mograsim.logic.model.model.components.atomic.GUINotGate;
15 import net.mograsim.logic.model.model.components.atomic.GUIOrGate;
16 import net.mograsim.logic.model.model.wires.GUIWire;
17 import net.mograsim.logic.model.model.wires.WireCrossPoint;
18 import net.mograsim.logic.model.modeladapter.LogicModelParameters;
19 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
20 import net.mograsim.preferences.Preferences;
22 public class SimulationPreview implements IThemePreview
24 private LogicUICanvas ui;
25 private LogicExecuter exec;
26 private Preferences oldPreferences;
27 private Preferences currentThemePreferences;
30 @SuppressWarnings("unused")
31 public void createControl(Composite parent, ITheme currentTheme)
33 oldPreferences = Preferences.current();
35 currentThemePreferences = new ThemePreferences(currentTheme);
36 // TODO this will change the global preferences; so if another LogicUICanvas redraws, it will use the "new" colors too.
37 Preferences.setPreferences(currentThemePreferences);
39 ViewModelModifiable model = new ViewModelModifiable();
40 LogicModelParameters params = new LogicModelParameters();
41 params.gateProcessTime = 50;
42 params.wireTravelTime = 10;
44 GUIManualSwitch rIn = new GUIManualSwitch(model, 1);
46 GUIManualSwitch sIn = new GUIManualSwitch(model, 1);
49 GUIOrGate or1 = new GUIOrGate(model, 1);
51 new GUIWire(model, rIn.getOutputPin(), or1.getPin("A"));
53 GUIOrGate or2 = new GUIOrGate(model, 1);
55 new GUIWire(model, sIn.getOutputPin(), or2.getPin("B"));
57 GUINotGate not1 = new GUINotGate(model, 1);
58 not1.moveTo(110, 17.5);
59 new GUIWire(model, or1.getPin("Y"), not1.getPin("A"));
61 GUINotGate not2 = new GUINotGate(model, 1);
62 not2.moveTo(110, 67.5);
63 new GUIWire(model, or2.getPin("Y"), not2.getPin("A"));
65 WireCrossPoint p1 = new WireCrossPoint(model, 1);
66 p1.moveCenterTo(140, 22.5);
67 new GUIWire(model, not1.getPin("Y"), p1);
68 new GUIWire(model, p1, or2.getPin("A"), new Point(140, 35), new Point(50, 60), new Point(50, 67.5));
70 WireCrossPoint p2 = new WireCrossPoint(model, 1);
71 p2.moveCenterTo(140, 72.5);
72 new GUIWire(model, not2.getPin("Y"), p2);
73 new GUIWire(model, p2, or1.getPin("B"), new Point(140, 60), new Point(50, 35), new Point(50, 27.5));
75 WireCrossPoint o1 = new WireCrossPoint(model, 1);
76 o1.moveCenterTo(150, 22.5);
77 new GUIWire(model, p1, o1);
79 WireCrossPoint o2 = new WireCrossPoint(model, 1);
80 o2.moveCenterTo(150, 72.5);
81 new GUIWire(model, p2, o2);
83 Timeline t = ViewLogicModelAdapter.convert(model, params);
84 exec = new LogicExecuter(t);
88 ui = new LogicUICanvas(parent, SWT.NONE, model);
91 exec.startLiveExecution();
93 currentTheme.addPropertyChangeListener(e -> ui.redraw());
99 exec.stopLiveExecution();
100 if (Preferences.current() == currentThemePreferences)
101 Preferences.setPreferences(oldPreferences);