Skip to content

Commit a599c2a

Browse files
committed
Remember the animation mode
1 parent f35e936 commit a599c2a

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public class HackController
128128
private static final String SPACES = " ";
129129
private static final String DIRECTORY = "directory";
130130
private static final String SPEED = "speed";
131+
private static final String ANIMATION_MODE = "animation_mode";
132+
private final Preferences preferences;
131133

132134
// The controller's GUI
133135
protected ControllerGUI gui;
@@ -230,6 +232,7 @@ public class HackController
230232
* The script will be executed and the final result will be printed.
231233
*/
232234
public HackController(HackSimulator simulator, String scriptFileName) {
235+
this.preferences = Preferences.userNodeForPackage(simulator.getClass());
233236
File file = new File(scriptFileName);
234237
if (!file.exists())
235238
displayMessage(scriptFileName + " doesn't exist", true);
@@ -261,7 +264,7 @@ public HackController(HackSimulator simulator, String scriptFileName) {
261264
public HackController(ControllerGUI gui, HackSimulator simulator, String defaultScriptName)
262265
throws ScriptException, ControllerException {
263266

264-
final Preferences preferences = Preferences.userNodeForPackage(simulator.getClass());
267+
this.preferences = Preferences.userNodeForPackage(simulator.getClass());
265268
this.gui = gui;
266269
this.simulator = simulator;
267270
singleStepTask = new SingleStepTask();
@@ -279,7 +282,7 @@ public HackController(ControllerGUI gui, HackSimulator simulator, String default
279282
delays[i] = (int)(MAX_MS - SPEED_FUNCTION[i] * (float)(MAX_MS - MIN_MS));
280283

281284
currentSpeedUnit = preferences.getInt(SPEED, INITIAL_SPEED_UNIT);
282-
animationMode = simulator.getInitialAnimationMode();
285+
animationMode = preferences.getInt(ANIMATION_MODE, simulator.getInitialAnimationMode());
283286
simulator.setAnimationMode(animationMode);
284287
simulator.setAnimationSpeed(currentSpeedUnit);
285288
simulator.setNumericFormat(simulator.getInitialNumericFormat());
@@ -290,7 +293,7 @@ public HackController(ControllerGUI gui, HackSimulator simulator, String default
290293
gui.setTitle(simulator.getName() + getVersionString());
291294

292295
// load and set working dir
293-
File file = loadWorkingDir(preferences);
296+
File file = new File(preferences.get(DIRECTORY, "."));
294297
simulator.setWorkingDir(file);
295298
gui.setWorkingDir(file);
296299

@@ -795,7 +798,8 @@ private void setSpeed(int newSpeedUnit) {
795798
currentSpeedUnit = newSpeedUnit;
796799
timer.setDelay(delays[currentSpeedUnit - 1]);
797800
simulator.setAnimationSpeed(newSpeedUnit);
798-
saveSpeedUnit(newSpeedUnit);
801+
preferences.putInt(SPEED, newSpeedUnit);
802+
savePreferences();
799803
}
800804

801805
// Sets the animation mode with the given one.
@@ -809,6 +813,8 @@ private void setAnimationMode(int newAnimationMode) {
809813

810814
gui.setAnimationMode(newAnimationMode);
811815
animationMode = newAnimationMode;
816+
preferences.putInt(ANIMATION_MODE, newAnimationMode);
817+
savePreferences();
812818
}
813819

814820
// Sets the numeric format with the given code.
@@ -868,12 +874,6 @@ private void displayMessage(String message, boolean error) {
868874
}
869875
}
870876

871-
// Returns the working dir that is saved in the data file, or "" if data file doesn't exist.
872-
private File loadWorkingDir(Preferences preferences) {
873-
final String dir = preferences.get(DIRECTORY, ".");
874-
return new File(dir);
875-
}
876-
877877
// Saves the given working dir into the data file and gui's.
878878
private void saveWorkingDir(File file) {
879879
final File parent = file.getParentFile();
@@ -885,17 +885,11 @@ private void saveWorkingDir(File file) {
885885

886886
final File dir = file.isDirectory() ? file : parent;
887887

888-
final Preferences preferences = Preferences.userNodeForPackage(simulator.getClass());
889888
preferences.put(DIRECTORY, dir.toString());
890-
try {
891-
preferences.sync();
892-
} catch (BackingStoreException ignored) {
893-
}
889+
savePreferences();
894890
}
895891

896-
private void saveSpeedUnit(int speedUnit) {
897-
final Preferences preferences = Preferences.userNodeForPackage(simulator.getClass());
898-
preferences.putInt(SPEED, speedUnit);
892+
private void savePreferences() {
899893
try {
900894
preferences.sync();
901895
} catch (BackingStoreException ignored) {

0 commit comments

Comments
 (0)