v0.8.0: added save/load support to key mappings
This commit is contained in:
parent
56ad068d23
commit
11975fb6c2
|
@ -26,6 +26,8 @@ import com.badlogic.gdx.Input;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
|
//FIXME at lot of the logic here, in WndKeyBindings, and SPDAction is fairly messy
|
||||||
|
// should see about doing some refactoring to clean this up
|
||||||
public class KeyBindings {
|
public class KeyBindings {
|
||||||
|
|
||||||
private static LinkedHashMap<Integer, GameAction> bindings = new LinkedHashMap<>();
|
private static LinkedHashMap<Integer, GameAction> bindings = new LinkedHashMap<>();
|
||||||
|
|
|
@ -24,7 +24,10 @@ package com.shatteredpixel.shatteredpixeldungeon;
|
||||||
import com.badlogic.gdx.Input;
|
import com.badlogic.gdx.Input;
|
||||||
import com.watabou.input.GameAction;
|
import com.watabou.input.GameAction;
|
||||||
import com.watabou.input.KeyBindings;
|
import com.watabou.input.KeyBindings;
|
||||||
|
import com.watabou.utils.Bundle;
|
||||||
|
import com.watabou.utils.FileUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
public class SPDAction extends GameAction {
|
public class SPDAction extends GameAction {
|
||||||
|
@ -114,22 +117,124 @@ public class SPDAction extends GameAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LinkedHashMap<Integer, GameAction> getDefaults() {
|
public static LinkedHashMap<Integer, GameAction> getDefaults() {
|
||||||
return new LinkedHashMap(defaultBindings);
|
return new LinkedHashMap<>(defaultBindings);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO save functionality for changed keys
|
//we only save/loads keys which differ from the default configuration.
|
||||||
public static void initialize(){
|
private static final String BINDINGS_FILE = "keybinds.dat";
|
||||||
KeyBindings.setAllBindings(getDefaults());
|
|
||||||
}
|
|
||||||
|
|
||||||
//file name? perhaps
|
|
||||||
|
|
||||||
public static void loadBindings(){
|
public static void loadBindings(){
|
||||||
|
|
||||||
|
try {
|
||||||
|
Bundle b = FileUtils.bundleFromFile(BINDINGS_FILE);
|
||||||
|
|
||||||
|
Bundle firstKeys = b.getBundle("first_keys");
|
||||||
|
Bundle secondKeys = b.getBundle("second_keys");
|
||||||
|
|
||||||
|
LinkedHashMap<Integer, GameAction> defaults = getDefaults();
|
||||||
|
LinkedHashMap<Integer, GameAction> custom = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
for (GameAction a : allActions()) {
|
||||||
|
if (firstKeys.contains(a.name())) {
|
||||||
|
if (firstKeys.getInt(a.name()) == 0){
|
||||||
|
for (int i : defaults.keySet()){
|
||||||
|
if (defaults.get(i) == a){
|
||||||
|
defaults.remove(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
custom.put(firstKeys.getInt(a.name()), a);
|
||||||
|
defaults.remove(firstKeys.getInt(a.name()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//we store any custom second keys in defaults for the moment to preserve order
|
||||||
|
//incase the 2nd key is custom but the first one isn't
|
||||||
|
if (secondKeys.contains(a.name())) {
|
||||||
|
if (secondKeys.getInt(a.name()) == 0){
|
||||||
|
int last = 0;
|
||||||
|
for (int i : defaults.keySet()){
|
||||||
|
if (defaults.get(i) == a){
|
||||||
|
last = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defaults.remove(last);
|
||||||
|
} else {
|
||||||
|
defaults.remove(secondKeys.getInt(a.name()));
|
||||||
|
defaults.put(secondKeys.getInt(a.name()), a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//now merge them and store
|
||||||
|
for( int i : defaults.keySet()){
|
||||||
|
if (i != 0) {
|
||||||
|
custom.put(i, defaults.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KeyBindings.setAllBindings(custom);
|
||||||
|
|
||||||
|
} catch (Exception e){
|
||||||
|
KeyBindings.setAllBindings(getDefaults());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void saveBindings(){
|
public static void saveBindings(){
|
||||||
|
|
||||||
|
Bundle b = new Bundle();
|
||||||
|
|
||||||
|
Bundle firstKeys = new Bundle();
|
||||||
|
Bundle secondKeys = new Bundle();
|
||||||
|
|
||||||
|
for (GameAction a : allActions()){
|
||||||
|
int firstCur = 0;
|
||||||
|
int secondCur = 0;
|
||||||
|
int firstDef = 0;
|
||||||
|
int secondDef = 0;
|
||||||
|
|
||||||
|
for (int i : defaultBindings.keySet()){
|
||||||
|
if (defaultBindings.get(i) == a){
|
||||||
|
if(firstDef == 0){
|
||||||
|
firstDef = i;
|
||||||
|
} else {
|
||||||
|
secondDef = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LinkedHashMap<Integer, GameAction> curBindings = KeyBindings.getAllBindings();
|
||||||
|
for (int i : curBindings.keySet()){
|
||||||
|
if (curBindings.get(i) == a){
|
||||||
|
if(firstCur == 0){
|
||||||
|
firstCur = i;
|
||||||
|
} else {
|
||||||
|
secondCur = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (firstCur != firstDef){
|
||||||
|
firstKeys.put(a.name(), firstCur);
|
||||||
|
}
|
||||||
|
if (secondCur != secondDef){
|
||||||
|
secondKeys.put(a.name(), secondCur);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
b.put("first_keys", firstKeys);
|
||||||
|
b.put("second_keys", secondKeys);
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileUtils.bundleToFile(BINDINGS_FILE, b);
|
||||||
|
} catch (IOException e) {
|
||||||
|
ShatteredPixelDungeon.reportException(e);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -132,7 +132,7 @@ public class ShatteredPixelDungeon extends Game {
|
||||||
super.create();
|
super.create();
|
||||||
|
|
||||||
updateSystemUI();
|
updateSystemUI();
|
||||||
SPDAction.initialize();
|
SPDAction.loadBindings();
|
||||||
|
|
||||||
Music.INSTANCE.enable( SPDSettings.music() );
|
Music.INSTANCE.enable( SPDSettings.music() );
|
||||||
Music.INSTANCE.volume( SPDSettings.musicVol()/10f );
|
Music.INSTANCE.volume( SPDSettings.musicVol()/10f );
|
||||||
|
|
|
@ -136,6 +136,7 @@ public class WndKeyBindings extends Window {
|
||||||
@Override
|
@Override
|
||||||
protected void onClick() {
|
protected void onClick() {
|
||||||
KeyBindings.setAllBindings(changedBindings);
|
KeyBindings.setAllBindings(changedBindings);
|
||||||
|
SPDAction.saveBindings();
|
||||||
hide();
|
hide();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user