42c2db4a55f7852ce8ce816776a79d960136bb0d
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / views / LogicUIPart.java
1 package net.mograsim.plugin.views;
2
3 import javax.inject.Inject;
4
5 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.layout.GridData;
8 import org.eclipse.swt.layout.GridLayout;
9 import org.eclipse.swt.widgets.Button;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Label;
12 import org.eclipse.swt.widgets.Slider;
13 import org.eclipse.ui.PlatformUI;
14 import org.eclipse.ui.part.ViewPart;
15
16 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
17 import net.mograsim.logic.model.LogicExecuter;
18 import net.mograsim.logic.model.LogicUICanvas;
19 import net.mograsim.machine.Machine;
20 import net.mograsim.machine.mi.AssignableMicroInstructionMemory;
21 import net.mograsim.plugin.EclipsePreferences;
22 import net.mograsim.plugin.MachineContext;
23 import net.mograsim.plugin.MograsimActivator;
24 import net.mograsim.plugin.tables.DisplaySettings;
25 import net.mograsim.plugin.tables.mi.ActiveInstructionPreviewContentProvider;
26 import net.mograsim.plugin.tables.mi.InstructionTable;
27 import net.mograsim.preferences.Preferences;
28
29 public class LogicUIPart extends ViewPart
30 {
31         @Inject
32         private MPart part;
33
34         private LogicExecuter exec;
35         private LogicUICanvas ui;
36
37         @Override
38         public void dispose()
39         {
40                 if (exec != null)
41                         exec.stopLiveExecution();
42         }
43
44         @Override
45         public void createPartControl(Composite parent)
46         {
47                 // set preferences
48                 Preferences.setPreferences(new EclipsePreferences(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(),
49                                 MograsimActivator.instance().getPreferenceStore()));
50
51                 Machine m = MachineContext.getInstance().getMachine();
52
53                 // initialize UI
54                 GridLayout layout = new GridLayout(1, true);
55                 parent.setLayout(layout);
56
57                 addSimulationControlWidgets(parent);
58
59                 ui = new LogicUICanvas(parent, SWT.NONE, m.getModel());
60                 ui.addTransformListener((x, y, z) -> part.setDirty(z < 1));
61                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
62                 userInput.buttonDrag = 3;
63                 userInput.buttonZoom = 2;
64                 userInput.enableUserInput();
65
66                 GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
67                 ui.setLayoutData(uiData);
68
69                 // initialize Instruction preview
70                 InstructionTable instPreview = new InstructionTable(parent, new DisplaySettings());
71                 instPreview.setContentProvider(new ActiveInstructionPreviewContentProvider(instPreview.getTableViewer()));
72                 AssignableMicroInstructionMemory mIMemory = m.getMicroInstructionMemory();
73                 instPreview.bindMicroInstructionMemory(mIMemory);
74                 mIMemory.registerCellModifiedListener(a -> instPreview.refresh());
75                 mIMemory.registerMemoryReassignedListener(n -> instPreview.refresh());
76
77                 GridData previewData = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
78                 instPreview.getTableViewer().getTable().setLayoutData(previewData);
79
80                 // initialize executer
81                 exec = new LogicExecuter(m.getTimeline());
82
83                 // run it
84                 exec.startLiveExecution();
85         }
86
87         private void addSimulationControlWidgets(Composite parent)
88         {
89                 Composite c = new Composite(parent, SWT.NONE);
90                 c.setLayout(new GridLayout(4, false));
91                 Button pauseButton = new Button(c, SWT.TOGGLE);
92                 pauseButton.setText("Running");
93
94                 pauseButton.addListener(SWT.Selection, e ->
95                 {
96                         if (!pauseButton.getSelection())
97                         {
98                                 pauseButton.setText("Running");
99                                 exec.unpauseLiveExecution();
100                         } else
101                         {
102                                 pauseButton.setText("Paused");
103                                 exec.pauseLiveExecution();
104                         }
105                 });
106
107                 Label speedLabel = new Label(c, SWT.NONE);
108                 speedLabel.setText("Simulation Speed: ");
109
110                 Slider slider = new Slider(c, SWT.NONE);
111                 slider.setMinimum(1);
112                 slider.setMaximum(100 + slider.getThumb());
113                 slider.setIncrement(1);
114
115                 Label speedPercentageLabel = new Label(c, SWT.NONE);
116                 speedPercentageLabel.setText("100%");
117
118                 slider.addListener(SWT.Selection, e ->
119                 {
120                         int selection = slider.getSelection();
121                         speedPercentageLabel.setText(selection + "%");
122
123                         exec.setSpeedPercentage(slider.getSelection());
124                 });
125                 slider.setSelection(100);
126
127                 c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
128                 c.pack();
129                 c.setVisible(true);
130         }
131
132         @Override
133         public void setFocus()
134         {
135                 ui.setFocus();
136         }
137 }