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