import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
+import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import net.mograsim.machine.Memory.MemoryCellModifiedListener;
import net.mograsim.machine.mi.AssignableMicroInstructionMemory;
import net.mograsim.plugin.nature.MachineContext;
+import net.mograsim.plugin.nature.MachineContext.ActiveMachineListener;
import net.mograsim.plugin.nature.ProjectMachineContext;
import net.mograsim.plugin.tables.DisplaySettings;
import net.mograsim.plugin.tables.mi.ActiveInstructionPreviewContentProvider;
import net.mograsim.plugin.tables.mi.InstructionTable;
+import net.mograsim.preferences.Preferences;
//TODO what if we open multiple editors?
//TODO actually save / load register and latch states
private Machine machine;
private Composite parent;
+ private Button resetButton;
private Button sbseButton;
private Button pauseButton;
+ private Label speedFactorLabel;
private Slider simSpeedSlider;
+ private Composite canvasParent;
private LogicUICanvas canvas;
+ private InstructionTable instPreview;
private Label noMachineLabel;
- private MemoryCellModifiedListener currentRegisteredCellListener;
+ private ActiveMachineListener activeMachineListener;
+ private MemoryCellModifiedListener memCellListener;
+ private LogicObserver clockObserver;
+
+ public SimulationViewEditor()
+ {
+ activeMachineListener = m -> recreateContextDependentControls();
+ memCellListener = a -> instPreview.refresh();
+ clockObserver = o ->
+ {
+ if (((CoreClock) o).isOn())
+ {
+ exec.pauseLiveExecution();
+ if (!pauseButton.isDisposed())
+ Display.getDefault().asyncExec(() ->
+ {
+ if (!pauseButton.isDisposed())
+ pauseButton.setSelection(false);
+ setPauseText(pauseButton, false);
+ });
+ }
+ };
+ }
@Override
public void createPartControl(Composite parent)
noMachineLabel = new Label(parent, SWT.NONE);
noMachineLabel.setText("No machine present...");// TODO internationalize?
addSimulationControlWidgets(parent);
+ canvasParent = new Composite(parent, SWT.NONE);
+ canvasParent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ canvasParent.setLayout(new FillLayout());
+ addInstructionPreviewControlWidgets(parent);
recreateContextDependentControls();
}
// createPartControls has not been called yet
return;
+ double offX;
+ double offY;
+ double zoom;
+ stopExecAndDeregisterContextDependentListeners();
if (canvas != null)
+ {
+ offX = canvas.getOffX();
+ offY = canvas.getOffY();
+ zoom = canvas.getZoom();
canvas.dispose();
- if (exec != null)
- exec.stopLiveExecution();
+ } else
+ {
+ offX = 0;
+ offY = 0;
+ zoom = -1;
+ }
Optional<Machine> machineOptional;
if (context != null && (machineOptional = context.getActiveMachine()).isPresent())
{
noMachineLabel.setVisible(false);
+ resetButton.setEnabled(true);
sbseButton.setEnabled(true);
pauseButton.setEnabled(true);
simSpeedSlider.setEnabled(true);
- if (machine != null)
- machine.getMicroInstructionMemory().deregisterCellModifiedListener(currentRegisteredCellListener);
-
machine = machineOptional.get();
- canvas = new LogicUICanvas(parent, SWT.NONE, machine.getModel());
- canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ canvas = new LogicUICanvas(canvasParent, SWT.NONE, machine.getModel());
ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
- userInput.buttonDrag = 3;
- userInput.buttonZoom = 2;
+ userInput.buttonDrag = Preferences.current().getInt("net.mograsim.logic.model.button.drag");
+ userInput.buttonZoom = Preferences.current().getInt("net.mograsim.logic.model.button.zoom");
userInput.enableUserInput();
+ if (zoom > 0)
+ {
+ canvas.moveTo(offX, offY, zoom);
+ canvas.commitTransform();
+ }
- // initialize Instruction preview
- InstructionTable instPreview = new InstructionTable(parent, new DisplaySettings());
- instPreview.getTableViewer().getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
- instPreview.setContentProvider(new ActiveInstructionPreviewContentProvider(instPreview.getTableViewer()));
AssignableMicroInstructionMemory mIMemory = machine.getMicroInstructionMemory();
instPreview.bindMicroInstructionMemory(mIMemory);
- currentRegisteredCellListener = a -> instPreview.refresh();
- mIMemory.registerCellModifiedListener(currentRegisteredCellListener);
+ mIMemory.registerCellModifiedListener(memCellListener);
- GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
- canvas.setLayoutData(uiData);
+ canvasParent.layout();
// initialize executer
exec = new LogicExecuter(machine.getTimeline());
+ updateSpeedFactor();
+ updatePausedState();
exec.startLiveExecution();
} else
{
noMachineLabel.setVisible(true);
+ resetButton.setEnabled(false);
sbseButton.setEnabled(false);
pauseButton.setEnabled(false);
simSpeedSlider.setEnabled(false);
}
}
+ private void stopExecAndDeregisterContextDependentListeners()
+ {
+ if (exec != null)
+ exec.stopLiveExecution();
+ if (machine != null)
+ {
+ machine.getMicroInstructionMemory().deregisterCellModifiedListener(memCellListener);
+ machine.getClock().deregisterObserver(clockObserver);
+ }
+ }
+
private void addSimulationControlWidgets(Composite parent)
{
Composite c = new Composite(parent, SWT.NONE);
+ c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
c.setLayout(new GridLayout(7, false));
+ resetButton = new Button(c, SWT.PUSH);
+ resetButton.setText("Reset machine");
+ resetButton.addListener(SWT.Selection, e -> context.getActiveMachine().get().reset());
+
sbseButton = new Button(c, SWT.CHECK);
pauseButton = new Button(c, SWT.TOGGLE);
- LogicObserver clockObserver = o ->
- {
- if (((CoreClock) o).isOn())
- {
- exec.pauseLiveExecution();
- if (!pauseButton.isDisposed())
- Display.getDefault().asyncExec(() ->
- {
- if (!pauseButton.isDisposed())
- pauseButton.setSelection(false);
- setPauseText(pauseButton, false);
- });
- }
- };
sbseButton.setText("Step by step execution");
sbseButton.addListener(SWT.Selection, e ->
pauseButton.setSelection(true);
setPauseText(pauseButton, false);
- pauseButton.addListener(SWT.Selection, e ->
- {
- setPauseText(pauseButton, false);
- if (pauseButton.getSelection())
- {
- exec.unpauseLiveExecution();
- } else
- {
- exec.pauseLiveExecution();
- }
- });
+ pauseButton.addListener(SWT.Selection, e -> updatePausedState());
pauseButton.addMouseTrackListener(new MouseTrackListener()
{
@Override
}
});
- Label speedLabel = new Label(c, SWT.NONE);
- speedLabel.setText("Simulation Speed: ");
+ new Label(c, SWT.NONE).setText("Simulation Speed: ");
simSpeedSlider = new Slider(c, SWT.NONE);
- simSpeedSlider.setMinimum(1);
- simSpeedSlider.setMaximum(100 + simSpeedSlider.getThumb());
+ simSpeedSlider.setMinimum(0);
+ simSpeedSlider.setMaximum(50 + simSpeedSlider.getThumb());
simSpeedSlider.setIncrement(1);
+ simSpeedSlider.setSelection(0);
- Label speedPercentageLabel = new Label(c, SWT.NONE);
- speedPercentageLabel.setText("100%");
+ speedFactorLabel = new Label(c, SWT.NONE);
+ speedFactorLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
- simSpeedSlider.addListener(SWT.Selection, e ->
- {
- int selection = simSpeedSlider.getSelection();
- speedPercentageLabel.setText(selection + "%");
+ simSpeedSlider.addListener(SWT.Selection, e -> updateSpeedFactor());
+ updateSpeedFactor();
- exec.setSpeedPercentage(simSpeedSlider.getSelection());
- });
- simSpeedSlider.setSelection(100);
+ c.layout();
+ }
- c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
- c.pack();
+ private void updatePausedState()
+ {
+ setPauseText(pauseButton, false);
+ if (exec != null)
+ if (pauseButton.getSelection())
+ exec.unpauseLiveExecution();
+ else
+ exec.pauseLiveExecution();
+ }
+
+ private void updateSpeedFactor()
+ {
+ double factor = Math.pow(1.32, simSpeedSlider.getSelection() - 50);
+ speedFactorLabel.setText(String.format("%f", factor));
+ if (exec != null)
+ exec.setSpeedFactor(factor);
+ }
+
+ private void addInstructionPreviewControlWidgets(Composite parent)
+ {
+ instPreview = new InstructionTable(parent, new DisplaySettings());
+ instPreview.getTableViewer().getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+ instPreview.setContentProvider(new ActiveInstructionPreviewContentProvider(instPreview.getTableViewer()));
}
private static void setPauseText(Button pauseButton, boolean hovered)
{
IFileEditorInput fileInput = (IFileEditorInput) input;
context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
+ context.activateMachine();
+ context.addActiveMachineListener(activeMachineListener);
recreateContextDependentControls();
setPartName(fileInput.getName());
@Override
public void dispose()
{
- exec.stopLiveExecution();
+ stopExecAndDeregisterContextDependentListeners();
+ context.removeActiveMachineListener(activeMachineListener);
super.dispose();
}
}
\ No newline at end of file