User modified keybindings are persistently saved
- Works on windows computers - KeyFactory processes its own XML saving/loading - Does not work with Jar yet - Changed keyFactory to static temporarily to work with existing FXML loading architecture #story[1197]
This commit is contained in:
@@ -15,6 +15,7 @@ import javafx.stage.Stage;
|
||||
import javafx.stage.WindowEvent;
|
||||
import visualiser.gameController.Keys.ControlKey;
|
||||
import visualiser.gameController.Keys.KeyFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -32,9 +33,9 @@ public class KeyBindingsController {
|
||||
private @FXML ListView lstKey;
|
||||
private @FXML ListView lstDescription;
|
||||
private @FXML AnchorPane anchor;
|
||||
private Button currentButton = null; // last button clicked
|
||||
private KeyFactory newKeyFactory;
|
||||
private Boolean changed = false; // keybindings have been modified
|
||||
private Boolean changed = false; // keyBindings have been modified
|
||||
private Button currentButton = null; // last button clicked
|
||||
|
||||
public void initialize(){
|
||||
// create new key factory to modify, keeping the existing one safe
|
||||
@@ -132,7 +133,7 @@ public class KeyBindingsController {
|
||||
for (int i = 1; i < lstKey.getItems().size(); i++) {
|
||||
Button button = (Button)lstKey.getItems().get(i);
|
||||
// update buttons text and remove key binding from command
|
||||
if (button.getText() == event.getCode().toString()) {
|
||||
if (button.getText().equals(event.getCode().toString())) {
|
||||
button.setText("");
|
||||
newKeyFactory.updateKey(button.getId(), button.getId());
|
||||
}
|
||||
@@ -181,6 +182,7 @@ public class KeyBindingsController {
|
||||
keyFactory = newKeyFactory;
|
||||
newKeyFactory = new KeyFactory();
|
||||
changed = false;
|
||||
keyFactory.save(); // save persistently
|
||||
loadNotification("Key bindings were successfully saved.", false);
|
||||
} else {
|
||||
loadNotification("One or more key bindings are missing. " +
|
||||
@@ -211,7 +213,7 @@ public class KeyBindingsController {
|
||||
*/
|
||||
public void onExit(WindowEvent we){
|
||||
// if modified KeyFactory hasn't been saved
|
||||
if (keyFactory!=newKeyFactory && changed==true){
|
||||
if (changed){
|
||||
loadNotification("Please cancel or save your changes before exiting" +
|
||||
".", true);
|
||||
we.consume();
|
||||
|
||||
@@ -90,34 +90,18 @@ public class TitleController extends Controller {
|
||||
* Called when control button is pressed. New pop up window displaying controls
|
||||
*/
|
||||
public void controlBtnPressed(){
|
||||
|
||||
// Parent root2 = loader2.load();
|
||||
// KeyBindingsController controller = loader2.getController();
|
||||
// Stage stage2 = new Stage();
|
||||
// stage2.setScene(new Scene(root2));
|
||||
// stage2.centerOnScreen();
|
||||
// stage2.initModality(Modality.APPLICATION_MODAL);
|
||||
// stage2.show();
|
||||
// stage2.setOnCloseRequest(new EventHandler<WindowEvent>() {
|
||||
// public void handle(WindowEvent we) {
|
||||
// if (we.getEventType() == WindowEvent.WINDOW_CLOSE_REQUEST) {
|
||||
// controller.onExit(we);
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
FXMLLoader loader = new FXMLLoader();
|
||||
loader.setLocation(getClass().getResource("/visualiser/scenes/keyBindings.fxml"));
|
||||
Parent layout;
|
||||
try {
|
||||
layout = loader.load();
|
||||
FXMLLoader loader = new FXMLLoader();
|
||||
loader.setLocation(getClass().getResource("/visualiser/scenes/keyBindings.fxml"));
|
||||
Parent layout = loader.load();
|
||||
Scene scene = new Scene(layout);
|
||||
Stage popupStage = new Stage();
|
||||
popupStage.setResizable(false);
|
||||
popupStage.setTitle("Game Controls");
|
||||
popupStage.initModality(Modality.WINDOW_MODAL);
|
||||
popupStage.centerOnScreen();
|
||||
popupStage.setScene(scene);
|
||||
popupStage.showAndWait();
|
||||
popupStage.show();
|
||||
KeyBindingsController controller = loader.getController();
|
||||
popupStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
|
||||
public void handle(WindowEvent we) {
|
||||
|
||||
@@ -31,7 +31,6 @@ import visualiser.gameController.Keys.KeyFactory;
|
||||
|
||||
public class App extends Application {
|
||||
|
||||
|
||||
private static Stage stage;
|
||||
private Pane splashLayout;
|
||||
private ProgressBar loadProgress;
|
||||
@@ -39,7 +38,6 @@ public class App extends Application {
|
||||
private static final int SPLASH_WIDTH = 676;
|
||||
private static final int SPLASH_HEIGHT = 227;
|
||||
public static KeyFactory keyFactory = new KeyFactory();
|
||||
|
||||
public static App app;
|
||||
|
||||
/**
|
||||
@@ -53,6 +51,8 @@ public class App extends Application {
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
keyFactory.load();
|
||||
|
||||
ImageView splash = new ImageView(new Image(
|
||||
getClass().getClassLoader().getResourceAsStream("images/splashScreen.png")
|
||||
));
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import java.beans.XMLDecoder;
|
||||
import java.beans.XMLEncoder;
|
||||
import java.io.*;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -62,4 +66,43 @@ public class KeyFactory {
|
||||
keyState.put(newKey, controlKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persistently saves the keybindings the user has set.
|
||||
*/
|
||||
public void save(){
|
||||
try {
|
||||
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
|
||||
new FileOutputStream(getClass().getResource(
|
||||
"/visualiser/userSettings/keyFactory.xml")
|
||||
.toURI().getPath())));
|
||||
encoder.writeObject(this.keyState);
|
||||
encoder.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the persistently saved keybindings the user has set.
|
||||
*/
|
||||
public void load(){
|
||||
try {
|
||||
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
|
||||
new FileInputStream(getClass().getResource(
|
||||
"/visualiser/userSettings/keyFactory.xml")
|
||||
.toURI().getPath())));
|
||||
|
||||
Map<String, ControlKey> savedKeyState
|
||||
= (Map<String, ControlKey>)decoder.readObject();
|
||||
decoder.close();
|
||||
this.keyState = savedKeyState;
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?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