save settings on enter, revert on leave

This commit is contained in:
yugecin 2016-11-13 00:14:38 +01:00
parent 15f560ee68
commit d869f2ef48
3 changed files with 38 additions and 2 deletions

View File

@ -1449,6 +1449,7 @@ public class Game extends BasicGameState {
sbOverlay.setGameObjects(gameObjects); sbOverlay.setGameObjects(gameObjects);
if (!checkpointLoaded) { if (!checkpointLoaded) {
sbOverlay.enter();
sbOverlay.updateIndex(0); sbOverlay.updateIndex(0);
} }
@ -1467,6 +1468,8 @@ public class Game extends BasicGameState {
throws SlickException { throws SlickException {
// container.setMouseGrabbed(false); // container.setMouseGrabbed(false);
sbOverlay.leave();
Cursor.lastObjColor = Color.white; Cursor.lastObjColor = Color.white;
Cursor.lastMirroredObjColor = Color.white; Cursor.lastMirroredObjColor = Color.white;

View File

@ -77,6 +77,10 @@ public class OptionsOverlay {
list = new ItemList(); list = new ItemList();
} }
public Options.GameOption[] getSavedOptionList() {
return options;
}
public void init(GameContainer container, Input input, int width, int height) { public void init(GameContainer container, Input input, int width, int height) {
list.init(container); list.init(container);
this.input = input; this.input = input;

View File

@ -29,9 +29,7 @@ import org.newdawn.slick.Input;
import org.newdawn.slick.state.StateBasedGame; import org.newdawn.slick.state.StateBasedGame;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class SBOverlay { public class SBOverlay {
@ -47,6 +45,7 @@ public class SBOverlay {
private int speed; private int speed;
private GameObject[] gameObjects; private GameObject[] gameObjects;
private HashMap[] optionsMap; private HashMap[] optionsMap;
private HashMap<Options.GameOption, String> initialOptions;
private int index; private int index;
@ -56,6 +55,7 @@ public class SBOverlay {
public SBOverlay(Game game) { public SBOverlay(Game game) {
this.game = game; this.game = game;
options = new OptionsOverlay(this); options = new OptionsOverlay(this);
initialOptions = new HashMap<>();
} }
public void init(GameContainer container, Input input, int width, int height) { public void init(GameContainer container, Input input, int width, int height) {
@ -121,13 +121,25 @@ public class SBOverlay {
} else if (key == Input.KEY_J && index > 0) { } else if (key == Input.KEY_J && index > 0) {
index--; index--;
setMusicPosition(); setMusicPosition();
goBackOneSBIndex();
} else if (key == Input.KEY_K && index < gameObjects.length - 1) { } else if (key == Input.KEY_K && index < gameObjects.length - 1) {
index++; index++;
setMusicPosition(); setMusicPosition();
updateIndex(index);
} }
return false; return false;
} }
private void goBackOneSBIndex() {
if (optionsMap[index + 1] != null) {
// new options on previous index, so to revert then we have to reload them all to this point..
final int thisIndex = index;
for (int i = 0; i <= thisIndex; i++) {
updateIndex(thisIndex);
}
}
}
private void setMusicPosition() { private void setMusicPosition() {
game.setObjectIndex(index); game.setObjectIndex(index);
if (speed != 0) { if (speed != 0) {
@ -178,4 +190,21 @@ public class SBOverlay {
public boolean mouseWheelMoved(int newValue) { public boolean mouseWheelMoved(int newValue) {
return menu && options.mouseWheenMoved(newValue); return menu && options.mouseWheenMoved(newValue);
} }
public void enter() {
// enter, save current settings
for (Options.GameOption o : options.getSavedOptionList()) {
initialOptions.put(o, o.write());
}
}
public void leave() {
// leave, revert the settings saved before entering
for (Options.GameOption o : options.getSavedOptionList()) {
if (initialOptions.containsKey(o)) {
o.read(initialOptions.get(o));
}
}
}
} }