X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.plugin.core%2Fsrc%2Fnet%2Fmograsim%2Fplugin%2Feditors%2FSimulationViewEditor.java;h=651852edfad7d8aaed6f0c3f923a26d78172b939;hb=0eb525202d3c871a2a20f789af1728248f3cff11;hp=ba3dfd7aa66b89c3bdff78610d29b29f1e9188e6;hpb=500771048185ce356aed280970f5ff0f4473a146;p=Mograsim.git diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java index ba3dfd7a..651852ed 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java @@ -17,47 +17,78 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Slider; +import org.eclipse.swt.widgets.Scale; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorSite; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.EditorPart; +import net.haspamelodica.swt.helper.input.DoubleInput; import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput; import net.mograsim.logic.core.LogicObserver; import net.mograsim.logic.core.components.CoreClock; import net.mograsim.logic.model.LogicExecuter; import net.mograsim.logic.model.LogicUICanvas; +import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; import net.mograsim.machine.Machine; 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 public class SimulationViewEditor extends EditorPart { + private static final int SIM_SPEED_SCALE_STEPS = 50; + private static final double SIM_SPEED_SCALE_STEP_FACTOR = 1.32; + private static final double SIM_SPEED_SCALE_STEP_FACTOR_LOG = Math.log(SIM_SPEED_SCALE_STEP_FACTOR); + private MachineContext context; private LogicExecuter exec; private Machine machine; private Composite parent; + private Button resetButton; private Button sbseButton; private Button pauseButton; - private Slider simSpeedSlider; + private Scale simSpeedScale; + private DoubleInput simSpeedInput; private Composite canvasParent; private LogicUICanvas canvas; private InstructionTable instPreview; private Label noMachineLabel; - private MemoryCellModifiedListener currentRegisteredCellListener; - private LogicObserver currentClockObserver; + private ActiveMachineListener activeMachineListener; + private MemoryCellModifiedListener memCellListener; + private LogicObserver clockObserver; + + public SimulationViewEditor() + { + activeMachineListener = (oldM, newM) -> 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) @@ -85,13 +116,7 @@ public class SimulationViewEditor extends EditorPart double offX; double offY; double zoom; - if (exec != null) - exec.stopLiveExecution(); - if (machine != null) - { - machine.getMicroInstructionMemory().deregisterCellModifiedListener(currentRegisteredCellListener); - machine.getClock().deregisterObserver(currentClockObserver); - } + stopExecAndDeregisterContextDependentListeners(); if (canvas != null) { offX = canvas.getOffX(); @@ -109,15 +134,18 @@ public class SimulationViewEditor extends EditorPart if (context != null && (machineOptional = context.getActiveMachine()).isPresent()) { noMachineLabel.setVisible(false); + resetButton.setEnabled(true); sbseButton.setEnabled(true); pauseButton.setEnabled(true); - simSpeedSlider.setEnabled(true); + simSpeedScale.setEnabled(true); + simSpeedInput.setEnabled(true); machine = machineOptional.get(); canvas = new LogicUICanvas(canvasParent, SWT.NONE, machine.getModel()); + canvas.addListener(SWT.MouseDown, e -> canvas.setFocus()); 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) { @@ -127,70 +155,77 @@ public class SimulationViewEditor extends EditorPart AssignableMicroInstructionMemory mIMemory = machine.getMicroInstructionMemory(); instPreview.bindMicroInstructionMemory(mIMemory); - currentRegisteredCellListener = a -> instPreview.refresh(); - mIMemory.registerCellModifiedListener(currentRegisteredCellListener); + mIMemory.registerCellModifiedListener(memCellListener); canvasParent.layout(); + // update preview + ((ActiveInstructionPreviewContentProvider) instPreview.getTableViewer().getContentProvider()).setMachine(machine); + // initialize executer exec = new LogicExecuter(machine.getTimeline()); + updateSpeedFactorFromInput(simSpeedInput.getValue()); + updatePausedState(); exec.startLiveExecution(); } else { noMachineLabel.setVisible(true); + resetButton.setEnabled(false); sbseButton.setEnabled(false); pauseButton.setEnabled(false); - simSpeedSlider.setEnabled(false); + simSpeedScale.setEnabled(false); + simSpeedInput.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()); + + // TODO do we want this button in the final product? + Button reloadMachineButton = new Button(c, SWT.PUSH); + reloadMachineButton.setText("Reload machine"); + reloadMachineButton.addListener(SWT.Selection, e -> + { + IndirectModelComponentCreator.clearComponentCache(); + context.setActiveMachine(context.getMachineDefinition().get().createNew()); + }); + sbseButton = new Button(c, SWT.CHECK); pauseButton = new Button(c, SWT.TOGGLE); - currentClockObserver = 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 -> { CoreClock cl = machine.getClock(); if (sbseButton.getSelection()) - cl.registerObserver(currentClockObserver); + cl.registerObserver(clockObserver); else - cl.deregisterObserver(currentClockObserver); + cl.deregisterObserver(clockObserver); }); sbseButton.setSelection(false); 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 @@ -212,35 +247,65 @@ public class SimulationViewEditor extends EditorPart } }); - 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.setIncrement(1); + simSpeedScale = new Scale(c, SWT.NONE); + simSpeedScale.setMinimum(0); + simSpeedScale.setMaximum(SIM_SPEED_SCALE_STEPS); + simSpeedScale.setIncrement(1); + simSpeedScale.setSelection(0); + simSpeedScale.addListener(SWT.Selection, e -> updateSpeedFactorFromScale()); - Label speedPercentageLabel = new Label(c, SWT.NONE); - speedPercentageLabel.setText("100%"); + simSpeedInput = new DoubleInput(c, SWT.NONE); + simSpeedInput.setPrecision(Preferences.current().getInt("net.mograsim.plugin.core.simspeedprecision")); + simSpeedInput.addChangeListener(this::updateSpeedFactorFromInput); - simSpeedSlider.addListener(SWT.Selection, e -> - { - int selection = simSpeedSlider.getSelection(); - speedPercentageLabel.setText(selection + "%"); + updateSpeedFactorFromScale(); - exec.setSpeedPercentage(simSpeedSlider.getSelection()); - }); - simSpeedSlider.setSelection(100); + c.layout(); + } + + private void updatePausedState() + { + setPauseText(pauseButton, false); + if (exec != null) + if (pauseButton.getSelection()) + exec.unpauseLiveExecution(); + else + exec.pauseLiveExecution(); + } - c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL)); - c.pack(); + private void updateSpeedFactorFromScale() + { + double factor = Math.pow(SIM_SPEED_SCALE_STEP_FACTOR, simSpeedScale.getSelection() - SIM_SPEED_SCALE_STEPS); + simSpeedInput.setValue(factor); + if (exec != null) + exec.setSpeedFactor(factor); + } + + private void updateSpeedFactorFromInput(double factor) + { + double factorCheckedFor0; + if (factor != 0) + factorCheckedFor0 = factor; + else + { + factorCheckedFor0 = Math.pow(10, -simSpeedInput.getPrecision()); + simSpeedInput.setValue(factorCheckedFor0); + } + int closestScalePos = (int) Math.round(Math.log(factorCheckedFor0) / SIM_SPEED_SCALE_STEP_FACTOR_LOG + SIM_SPEED_SCALE_STEPS); + simSpeedScale.setSelection(Math.min(Math.max(closestScalePos, 0), SIM_SPEED_SCALE_STEPS)); + if (exec != null) + exec.setSpeedFactor(factorCheckedFor0); } private void addInstructionPreviewControlWidgets(Composite parent) { - instPreview = new InstructionTable(parent, new DisplaySettings()); + instPreview = new InstructionTable(parent, new DisplaySettings(), getSite().getWorkbenchWindow().getWorkbench().getThemeManager()); instPreview.getTableViewer().getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); - instPreview.setContentProvider(new ActiveInstructionPreviewContentProvider(instPreview.getTableViewer())); + ActiveInstructionPreviewContentProvider cProv; + instPreview.setContentProvider(cProv = new ActiveInstructionPreviewContentProvider(instPreview.getTableViewer())); + cProv.setMachine(machine); } private static void setPauseText(Button pauseButton, boolean hovered) @@ -264,7 +329,7 @@ public class SimulationViewEditor extends EditorPart IFileEditorInput fileInput = (IFileEditorInput) input; context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject()); context.activateMachine(); - context.addActiveMachineListener(m -> recreateContextDependentControls()); + context.addActiveMachineListener(activeMachineListener); recreateContextDependentControls(); setPartName(fileInput.getName()); @@ -323,7 +388,8 @@ public class SimulationViewEditor extends EditorPart @Override public void dispose() { - exec.stopLiveExecution(); + stopExecAndDeregisterContextDependentListeners(); + context.removeActiveMachineListener(activeMachineListener); super.dispose(); } } \ No newline at end of file