Keybindings can be saved and loaded, and work when Jar'd!
- trying to solve jar file reading issue - learnt a lot about jars including their lack of file structure - changed location of keybindings settings to outside of jar - added new folder/file that is generated on first run #story[1197]
This commit is contained in:
@@ -51,6 +51,7 @@ public class App extends Application {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
|
// load the user's personalised key bindings
|
||||||
keyFactory.load();
|
keyFactory.load();
|
||||||
|
|
||||||
ImageView splash = new ImageView(new Image(
|
ImageView splash = new ImageView(new Image(
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package visualiser.gameController.Keys;
|
|||||||
import java.beans.XMLDecoder;
|
import java.beans.XMLDecoder;
|
||||||
import java.beans.XMLEncoder;
|
import java.beans.XMLEncoder;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -71,16 +70,15 @@ public class KeyFactory {
|
|||||||
*/
|
*/
|
||||||
public void save(){
|
public void save(){
|
||||||
try {
|
try {
|
||||||
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
|
// open the filestream and write to it
|
||||||
new FileOutputStream(getClass().getResource(
|
FileOutputStream fos = new FileOutputStream(
|
||||||
"/visualiser/userSettings/keyFactory.xml")
|
System.getProperty("user.dir")+
|
||||||
.toURI().getPath())));
|
"/settings/keyBindings.xml");
|
||||||
encoder.writeObject(this.keyState);
|
XMLEncoder xmlEncoder = new XMLEncoder(fos);
|
||||||
encoder.close();
|
xmlEncoder.writeObject(this.keyState);
|
||||||
|
xmlEncoder.close();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,18 +87,35 @@ public class KeyFactory {
|
|||||||
*/
|
*/
|
||||||
public void load(){
|
public void load(){
|
||||||
try {
|
try {
|
||||||
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
|
// access settings folder, create if it doesn't exist
|
||||||
new FileInputStream(getClass().getResource(
|
File settingsFolder = new File(
|
||||||
"/visualiser/userSettings/keyFactory.xml")
|
System.getProperty("user.dir")+"/settings");
|
||||||
.toURI().getPath())));
|
if (!settingsFolder.exists()){
|
||||||
|
settingsFolder.mkdir();
|
||||||
|
}
|
||||||
|
|
||||||
|
// access keybindings xml file, create if it doesn't exist
|
||||||
|
File savedFile = new File(
|
||||||
|
settingsFolder+"/keyBindings.xml");
|
||||||
|
if (!savedFile.exists()){
|
||||||
|
savedFile.createNewFile();
|
||||||
|
FileOutputStream fos = new FileOutputStream(savedFile);
|
||||||
|
XMLEncoder xmlEncoder = new XMLEncoder(fos);
|
||||||
|
xmlEncoder.writeObject(this.keyState);
|
||||||
|
xmlEncoder.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// load the saved settings into the game
|
||||||
|
InputStream is = new FileInputStream(savedFile);
|
||||||
|
XMLDecoder xmlDecoder = new XMLDecoder(is);
|
||||||
Map<String, ControlKey> savedKeyState
|
Map<String, ControlKey> savedKeyState
|
||||||
= (Map<String, ControlKey>)decoder.readObject();
|
= (Map<String, ControlKey>)xmlDecoder.readObject();
|
||||||
decoder.close();
|
xmlDecoder.close();
|
||||||
this.keyState = savedKeyState;
|
this.keyState = savedKeyState;
|
||||||
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (URISyntaxException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<java version="1.8.0_121" class="java.beans.XMLDecoder">
|
|
||||||
<object class="java.util.HashMap">
|
|
||||||
<void method="put">
|
|
||||||
<string>SPACE</string>
|
|
||||||
<object class="visualiser.gameController.Keys.VMGKey"/>
|
|
||||||
</void>
|
|
||||||
<void method="put">
|
|
||||||
<string>SHIFT</string>
|
|
||||||
<object class="visualiser.gameController.Keys.SailsToggleKey"/>
|
|
||||||
</void>
|
|
||||||
<void method="put">
|
|
||||||
<string>DOWN</string>
|
|
||||||
<object class="visualiser.gameController.Keys.DownWindKey"/>
|
|
||||||
</void>
|
|
||||||
<void method="put">
|
|
||||||
<string>X</string>
|
|
||||||
<object class="visualiser.gameController.Keys.ZoomOutKey"/>
|
|
||||||
</void>
|
|
||||||
<void method="put">
|
|
||||||
<string>ENTER</string>
|
|
||||||
<object class="visualiser.gameController.Keys.TackGybeKey"/>
|
|
||||||
</void>
|
|
||||||
<void method="put">
|
|
||||||
<string>Z</string>
|
|
||||||
<object class="visualiser.gameController.Keys.ZoomInKey"/>
|
|
||||||
</void>
|
|
||||||
<void method="put">
|
|
||||||
<string>UP</string>
|
|
||||||
<object class="visualiser.gameController.Keys.UpWindKey"/>
|
|
||||||
</void>
|
|
||||||
</object>
|
|
||||||
</java>
|
|
||||||
Reference in New Issue
Block a user