From 26eb7899013bc52e4501ee97422d79d6e1ea2b9c Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Thu, 26 Sep 2019 14:41:34 +0200 Subject: [PATCH] PauseableTimeFunction now supports non-integral speed factors --- .../core/timeline/PauseableTimeFunction.java | 17 ++++-- .../mograsim/logic/model/LogicExecuter.java | 11 ++-- .../plugin/editors/SimulationViewEditor.java | 57 ++++++++++--------- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/PauseableTimeFunction.java b/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/PauseableTimeFunction.java index 86f4ce99..4a99310c 100644 --- a/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/PauseableTimeFunction.java +++ b/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/PauseableTimeFunction.java @@ -6,7 +6,7 @@ public class PauseableTimeFunction implements LongSupplier { private boolean paused = false; private long unpausedSysTime = 0, lastPausedInternalTime = 0; - private int speedPercentage = 100; + private double speedFactor = 1; public void pause() { @@ -29,18 +29,25 @@ public class PauseableTimeFunction implements LongSupplier @Override public long getAsLong() { - return paused ? lastPausedInternalTime - : lastPausedInternalTime + ((System.currentTimeMillis() - unpausedSysTime) * speedPercentage) / 100; + return (long) (paused ? lastPausedInternalTime + : lastPausedInternalTime + (System.currentTimeMillis() - unpausedSysTime) * speedFactor); } - public void setSpeedPercentage(int percentage) + public long simulTimeDeltaToRealTimeMillis(long simulTime) { + return paused ? -1 : (long) (simulTime / speedFactor); + } + + public void setSpeedFactor(double factor) + { + if (factor <= 0) + throw new IllegalArgumentException("time factor can't be smaller than 1"); if (!paused) { pause(); unpause(); } - this.speedPercentage = Integer.min(100, Integer.max(percentage, 1)); + this.speedFactor = factor; } public boolean isPaused() diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicExecuter.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicExecuter.java index eb67f8dc..77a09352 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicExecuter.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicExecuter.java @@ -43,15 +43,16 @@ public class LogicExecuter { // always execute to keep timeline from "hanging behind" for too long long current = tf.getAsLong(); - timeline.executeUntil(timeline.laterThan(current), current + 10); + timeline.executeUntil(timeline.laterThan(current), System.currentTimeMillis() + 10); + long nextEventTime = timeline.nextEventTime(); long sleepTime; if (timeline.hasNext()) - sleepTime = timeline.nextEventTime() - current; + sleepTime = tf.simulTimeDeltaToRealTimeMillis(nextEventTime - current); else sleepTime = 10000; try { - nextExecSimulTime.set(current + sleepTime); + nextExecSimulTime.set(nextEventTime); if (sleepTime > 0) Thread.sleep(sleepTime); @@ -130,9 +131,9 @@ public class LogicExecuter return isPaused.get(); } - public void setSpeedPercentage(int percentage) + public void setSpeedFactor(double factor) { - tf.setSpeedPercentage(percentage); + tf.setSpeedFactor(factor); } private void waitForIsRunning(boolean expectedState) 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 05016cca..06fadc00 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 @@ -52,6 +52,7 @@ public class SimulationViewEditor extends EditorPart private Button resetButton; private Button sbseButton; private Button pauseButton; + private Label speedFactorLabel; private Slider simSpeedSlider; private Composite canvasParent; private LogicUICanvas canvas; @@ -151,6 +152,8 @@ public class SimulationViewEditor extends EditorPart // initialize executer exec = new LogicExecuter(machine.getTimeline()); + updateSpeedFactor(); + updatePausedState(); exec.startLiveExecution(); } else { @@ -176,6 +179,7 @@ public class SimulationViewEditor extends EditorPart 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); @@ -199,17 +203,7 @@ public class SimulationViewEditor extends EditorPart 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 @@ -231,28 +225,39 @@ 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.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) -- 2.17.1