Skip to content

Commit f35e936

Browse files
committed
Remember the animation speed setting
1 parent 30eb278 commit f35e936

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

HackPackage/src/main/java/Hack/Controller/HackController.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
/**
3939
* A Controller for HackSimulators. Executes scripts written in a special scripting language
40-
* that controlls the features of the simulators.
40+
* that controls the features of the simulators.
4141
* Constructed with a GUI that enables the execution control of the script.
4242
*/
4343
public class HackController
@@ -56,7 +56,7 @@ public class HackController
5656
/**
5757
* The speed function for fast forward mode.
5858
*/
59-
public static final int[] FASTFORWARD_SPEED_FUNCTION = {500, 1000, 2000, 4000, 15000};
59+
private static final int[] FASTFORWARD_SPEED_FUNCTION = {500, 1000, 2000, 4000, 15000};
6060

6161
// ANIMATION MODES:
6262

@@ -127,6 +127,7 @@ public class HackController
127127
// A helper string with spaces
128128
private static final String SPACES = " ";
129129
private static final String DIRECTORY = "directory";
130+
private static final String SPEED = "speed";
130131

131132
// The controller's GUI
132133
protected ControllerGUI gui;
@@ -260,6 +261,7 @@ public HackController(HackSimulator simulator, String scriptFileName) {
260261
public HackController(ControllerGUI gui, HackSimulator simulator, String defaultScriptName)
261262
throws ScriptException, ControllerException {
262263

264+
final Preferences preferences = Preferences.userNodeForPackage(simulator.getClass());
263265
this.gui = gui;
264266
this.simulator = simulator;
265267
singleStepTask = new SingleStepTask();
@@ -276,10 +278,10 @@ public HackController(ControllerGUI gui, HackSimulator simulator, String default
276278
for (int i = 0; i < NUMBER_OF_SPEED_UNITS; i++)
277279
delays[i] = (int)(MAX_MS - SPEED_FUNCTION[i] * (float)(MAX_MS - MIN_MS));
278280

279-
currentSpeedUnit = INITIAL_SPEED_UNIT;
281+
currentSpeedUnit = preferences.getInt(SPEED, INITIAL_SPEED_UNIT);
280282
animationMode = simulator.getInitialAnimationMode();
281283
simulator.setAnimationMode(animationMode);
282-
simulator.setAnimationSpeed(INITIAL_SPEED_UNIT);
284+
simulator.setAnimationSpeed(currentSpeedUnit);
283285
simulator.setNumericFormat(simulator.getInitialNumericFormat());
284286
timer = new Timer(delays[currentSpeedUnit - 1], this);
285287

@@ -288,7 +290,7 @@ public HackController(ControllerGUI gui, HackSimulator simulator, String default
288290
gui.setTitle(simulator.getName() + getVersionString());
289291

290292
// load and set working dir
291-
File file = loadWorkingDir();
293+
File file = loadWorkingDir(preferences);
292294
simulator.setWorkingDir(file);
293295
gui.setWorkingDir(file);
294296

@@ -736,7 +738,7 @@ private void outputAndCompare(String line) throws ControllerException {
736738
}
737739

738740
// loads the given script file and restarts the GUI.
739-
protected void loadNewScript(File file, boolean displayMessage)
741+
private void loadNewScript(File file, boolean displayMessage)
740742
throws ControllerException, ScriptException {
741743
currentScriptFile = file;
742744
script = new Script(file.getPath());
@@ -793,6 +795,7 @@ private void setSpeed(int newSpeedUnit) {
793795
currentSpeedUnit = newSpeedUnit;
794796
timer.setDelay(delays[currentSpeedUnit - 1]);
795797
simulator.setAnimationSpeed(newSpeedUnit);
798+
saveSpeedUnit(newSpeedUnit);
796799
}
797800

798801
// Sets the animation mode with the given one.
@@ -866,14 +869,13 @@ private void displayMessage(String message, boolean error) {
866869
}
867870

868871
// Returns the working dir that is saved in the data file, or "" if data file doesn't exist.
869-
protected File loadWorkingDir() {
870-
final Preferences preferences = Preferences.userNodeForPackage(simulator.getClass());
872+
private File loadWorkingDir(Preferences preferences) {
871873
final String dir = preferences.get(DIRECTORY, ".");
872874
return new File(dir);
873875
}
874876

875877
// Saves the given working dir into the data file and gui's.
876-
protected void saveWorkingDir(File file) {
878+
private void saveWorkingDir(File file) {
877879
final File parent = file.getParentFile();
878880

879881
if (gui != null)
@@ -891,6 +893,15 @@ protected void saveWorkingDir(File file) {
891893
}
892894
}
893895

896+
private void saveSpeedUnit(int speedUnit) {
897+
final Preferences preferences = Preferences.userNodeForPackage(simulator.getClass());
898+
preferences.putInt(SPEED, speedUnit);
899+
try {
900+
preferences.sync();
901+
} catch (BackingStoreException ignored) {
902+
}
903+
}
904+
894905
// Returns the version string
895906
private static String getVersionString() {
896907
return " (" + Definitions.version + ")";
@@ -1077,7 +1088,7 @@ protected void doUnknownAction(byte action, Object data) throws ControllerExcept
10771088
}
10781089

10791090
// Performs the single step task
1080-
class SingleStepTask implements Runnable {
1091+
private class SingleStepTask implements Runnable {
10811092

10821093
public void run() {
10831094
singleStep();
@@ -1100,7 +1111,7 @@ public void run() {
11001111
}
11011112

11021113
// Performs the fast forward task
1103-
class FastForwardTask implements Runnable {
1114+
private class FastForwardTask implements Runnable {
11041115
public synchronized void run() {
11051116
try {
11061117
System.runFinalization();
@@ -1132,7 +1143,7 @@ public synchronized void run() {
11321143
}
11331144

11341145
// Sets the animation mode
1135-
class SetAnimationModeTask implements Runnable {
1146+
private class SetAnimationModeTask implements Runnable {
11361147

11371148
private int animationMode;
11381149

@@ -1146,7 +1157,7 @@ public void run() {
11461157
}
11471158

11481159
// Sets the numeric format
1149-
class SetNumericFormatTask implements Runnable {
1160+
private class SetNumericFormatTask implements Runnable {
11501161

11511162
private int numericFormat;
11521163

0 commit comments

Comments
 (0)