Added the two Combos mentioned in the prev. commit to test them.
[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.events.MouseEvent;
8 import org.eclipse.swt.events.MouseTrackListener;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Slider;
15 import org.eclipse.ui.PlatformUI;
16 import org.eclipse.ui.part.ViewPart;
17
18 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
19 import net.mograsim.logic.model.LogicExecuter;
20 import net.mograsim.logic.model.LogicUICanvas;
21 import net.mograsim.machine.Machine;
22 import net.mograsim.machine.mi.AssignableMicroInstructionMemory;
23 import net.mograsim.plugin.EclipsePreferences;
24 import net.mograsim.plugin.MachineContext;
25 import net.mograsim.plugin.MograsimActivator;
26 import net.mograsim.plugin.nature.MachineContextSwtTools;
27 import net.mograsim.plugin.nature.MachineContextSwtTools.MachineCombo;
28 import net.mograsim.plugin.nature.MachineContextSwtTools.MograsimProjectCombo;
29 import net.mograsim.plugin.tables.DisplaySettings;
30 import net.mograsim.plugin.tables.mi.ActiveInstructionPreviewContentProvider;
31 import net.mograsim.plugin.tables.mi.InstructionTable;
32 import net.mograsim.preferences.Preferences;
33
34 public class LogicUIPart extends ViewPart
35 {
36         @Inject
37         private MPart part;
38
39         private LogicExecuter exec;
40         private LogicUICanvas ui;
41
42         @Override
43         public void dispose()
44         {
45                 if (exec != null)
46                         exec.stopLiveExecution();
47                 super.dispose();
48         }
49
50         @Override
51         public void createPartControl(Composite parent)
52         {
53                 // set preferences
54                 Preferences.setPreferences(new EclipsePreferences(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(),
55                                 MograsimActivator.instance().getPreferenceStore()));
56
57                 Machine m = MachineContext.getInstance().getMachine();
58
59                 // initialize UI
60                 GridLayout layout = new GridLayout(1, true);
61                 parent.setLayout(layout);
62
63                 addSimulationControlWidgets(parent);
64
65                 ui = new LogicUICanvas(parent, SWT.NONE, m.getModel());
66                 ui.addTransformListener((x, y, z) -> part.setDirty(z < 1));
67                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
68                 userInput.buttonDrag = 3;
69                 userInput.buttonZoom = 2;
70                 userInput.enableUserInput();
71
72                 GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
73                 ui.setLayoutData(uiData);
74
75                 // initialize Instruction preview
76                 InstructionTable instPreview = new InstructionTable(parent, new DisplaySettings());
77                 instPreview.setContentProvider(new ActiveInstructionPreviewContentProvider(instPreview.getTableViewer()));
78                 AssignableMicroInstructionMemory mIMemory = m.getMicroInstructionMemory();
79                 instPreview.bindMicroInstructionMemory(mIMemory);
80                 mIMemory.registerCellModifiedListener(a -> instPreview.refresh());
81                 mIMemory.registerMemoryReassignedListener(n -> instPreview.refresh());
82
83                 GridData previewData = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
84                 instPreview.getTableViewer().getTable().setLayoutData(previewData);
85
86                 // initialize executer
87                 exec = new LogicExecuter(m.getTimeline());
88
89                 // run it
90                 exec.startLiveExecution();
91         }
92
93         private void addSimulationControlWidgets(Composite parent)
94         {
95                 Composite c = new Composite(parent, SWT.NONE);
96                 c.setLayout(new GridLayout(6, false));
97
98                 MograsimProjectCombo projectCombo = MachineContextSwtTools.createMograsimProjectSelector(c, SWT.NONE);
99                 MachineCombo machineCombo = MachineContextSwtTools.createMachineSelector(c, SWT.NONE);
100
101                 Button pauseButton = new Button(c, SWT.TOGGLE);
102                 pauseButton.setSelection(true);
103                 setPauseText(pauseButton, false);
104
105                 pauseButton.addListener(SWT.Selection, e ->
106                 {
107                         setPauseText(pauseButton, false);
108                         if (pauseButton.getSelection())
109                         {
110                                 exec.unpauseLiveExecution();
111                         } else
112                         {
113                                 exec.pauseLiveExecution();
114                         }
115                 });
116                 pauseButton.addMouseTrackListener(new MouseTrackListener()
117                 {
118                         @Override
119                         public void mouseHover(MouseEvent e)
120                         {
121                                 // nothing
122                         }
123
124                         @Override
125                         public void mouseExit(MouseEvent e)
126                         {
127                                 setPauseText(pauseButton, false);
128                         }
129
130                         @Override
131                         public void mouseEnter(MouseEvent e)
132                         {
133                                 setPauseText(pauseButton, true);
134                         }
135                 });
136
137                 Label speedLabel = new Label(c, SWT.NONE);
138                 speedLabel.setText("Simulation Speed: ");
139
140                 Slider slider = new Slider(c, SWT.NONE);
141                 slider.setMinimum(1);
142                 slider.setMaximum(100 + slider.getThumb());
143                 slider.setIncrement(1);
144
145                 Label speedPercentageLabel = new Label(c, SWT.NONE);
146                 speedPercentageLabel.setText("100%");
147
148                 slider.addListener(SWT.Selection, e ->
149                 {
150                         int selection = slider.getSelection();
151                         speedPercentageLabel.setText(selection + "%");
152
153                         exec.setSpeedPercentage(slider.getSelection());
154                 });
155                 slider.setSelection(100);
156
157                 c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
158                 c.pack();
159                 c.setVisible(true);
160         }
161
162         private void setPauseText(Button pauseButton, boolean hovered)
163         {
164                 if (hovered)
165                 {
166                         if (pauseButton.getSelection())
167                         {
168                                 pauseButton.setText("Pause?");
169                         } else
170                         {
171                                 pauseButton.setText("Resume?");
172                         }
173                 } else
174                 {
175                         if (pauseButton.getSelection())
176                         {
177                                 pauseButton.setText("Running");
178                         } else
179                         {
180                                 pauseButton.setText("Paused");
181                         }
182                 }
183         }
184
185         @Override
186         public void setFocus()
187         {
188                 ui.setFocus();
189         }
190 }