1 package net.mograsim.plugin.views;
3 import javax.inject.Inject;
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;
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;
31 public class LogicUIPart extends ViewPart
36 private LogicExecuter exec;
37 private LogicUICanvas ui;
43 exec.stopLiveExecution();
47 public void createPartControl(Composite parent)
50 Preferences.setPreferences(new EclipsePreferences(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(),
51 MograsimActivator.instance().getPreferenceStore()));
53 Machine m = MachineContext.getInstance().getMachine();
56 GridLayout layout = new GridLayout(1, true);
57 parent.setLayout(layout);
59 addSimulationControlWidgets(parent);
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();
68 GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
69 ui.setLayoutData(uiData);
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());
79 GridData previewData = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
80 instPreview.getTableViewer().getTable().setLayoutData(previewData);
82 // initialize executer
83 exec = new LogicExecuter(m.getTimeline());
86 exec.startLiveExecution();
89 private void addSimulationControlWidgets(Composite parent)
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);
97 pauseButton.addListener(SWT.Selection, e ->
99 setPauseText(pauseButton, false);
100 if (pauseButton.getSelection())
102 exec.unpauseLiveExecution();
105 exec.pauseLiveExecution();
108 pauseButton.addMouseTrackListener(new MouseTrackListener()
111 public void mouseHover(MouseEvent e)
117 public void mouseExit(MouseEvent e)
119 setPauseText(pauseButton, false);
123 public void mouseEnter(MouseEvent e)
125 setPauseText(pauseButton, true);
129 Label speedLabel = new Label(c, SWT.NONE);
130 speedLabel.setText("Simulation Speed: ");
132 Slider slider = new Slider(c, SWT.NONE);
133 slider.setMinimum(1);
134 slider.setMaximum(100 + slider.getThumb());
135 slider.setIncrement(1);
137 Label speedPercentageLabel = new Label(c, SWT.NONE);
138 speedPercentageLabel.setText("100%");
140 slider.addListener(SWT.Selection, e ->
142 int selection = slider.getSelection();
143 speedPercentageLabel.setText(selection + "%");
145 exec.setSpeedPercentage(slider.getSelection());
147 slider.setSelection(100);
149 c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
154 private void setPauseText(Button pauseButton, boolean hovered)
158 if (pauseButton.getSelection())
160 pauseButton.setText("Pause?");
163 pauseButton.setText("Resume?");
167 if (pauseButton.getSelection())
169 pauseButton.setText("Running");
172 pauseButton.setText("Paused");
178 public void setFocus()