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.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;
34 public class LogicUIPart extends ViewPart
39 private LogicExecuter exec;
40 private LogicUICanvas ui;
46 exec.stopLiveExecution();
51 public void createPartControl(Composite parent)
54 Preferences.setPreferences(new EclipsePreferences(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(),
55 MograsimActivator.instance().getPreferenceStore()));
57 Machine m = MachineContext.getInstance().getMachine();
60 GridLayout layout = new GridLayout(1, true);
61 parent.setLayout(layout);
63 addSimulationControlWidgets(parent);
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();
72 GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
73 ui.setLayoutData(uiData);
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());
83 GridData previewData = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
84 instPreview.getTableViewer().getTable().setLayoutData(previewData);
86 // initialize executer
87 exec = new LogicExecuter(m.getTimeline());
90 exec.startLiveExecution();
93 private void addSimulationControlWidgets(Composite parent)
95 Composite c = new Composite(parent, SWT.NONE);
96 c.setLayout(new GridLayout(6, false));
98 MograsimProjectCombo projectCombo = MachineContextSwtTools.createMograsimProjectSelector(c, SWT.NONE);
99 MachineCombo machineCombo = MachineContextSwtTools.createMachineSelector(c, SWT.NONE);
101 Button pauseButton = new Button(c, SWT.TOGGLE);
102 pauseButton.setSelection(true);
103 setPauseText(pauseButton, false);
105 pauseButton.addListener(SWT.Selection, e ->
107 setPauseText(pauseButton, false);
108 if (pauseButton.getSelection())
110 exec.unpauseLiveExecution();
113 exec.pauseLiveExecution();
116 pauseButton.addMouseTrackListener(new MouseTrackListener()
119 public void mouseHover(MouseEvent e)
125 public void mouseExit(MouseEvent e)
127 setPauseText(pauseButton, false);
131 public void mouseEnter(MouseEvent e)
133 setPauseText(pauseButton, true);
137 Label speedLabel = new Label(c, SWT.NONE);
138 speedLabel.setText("Simulation Speed: ");
140 Slider slider = new Slider(c, SWT.NONE);
141 slider.setMinimum(1);
142 slider.setMaximum(100 + slider.getThumb());
143 slider.setIncrement(1);
145 Label speedPercentageLabel = new Label(c, SWT.NONE);
146 speedPercentageLabel.setText("100%");
148 slider.addListener(SWT.Selection, e ->
150 int selection = slider.getSelection();
151 speedPercentageLabel.setText(selection + "%");
153 exec.setSpeedPercentage(slider.getSelection());
155 slider.setSelection(100);
157 c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
162 private void setPauseText(Button pauseButton, boolean hovered)
166 if (pauseButton.getSelection())
168 pauseButton.setText("Pause?");
171 pauseButton.setText("Resume?");
175 if (pauseButton.getSelection())
177 pauseButton.setText("Running");
180 pauseButton.setText("Paused");
186 public void setFocus()