From 8250226bdec95461c6b5f6b480d3973edab4a22b Mon Sep 17 00:00:00 2001 From: Jessica Syder Date: Thu, 31 Aug 2017 15:00:37 +1200 Subject: [PATCH] Key bindings can be updated with any key. - Changed key press from FXML key event to a listener - UP, DOWN, ENTER keys now work - Keys no longer change cells or press buttons - Added placeholder buttons Cancel, Reset and Save - Added some JavaDoc #story[1197] --- .../Controllers/KeyBindingsController.java | 65 +++++++++++-------- .../gameController/Keys/KeyFactory.java | 6 ++ .../visualiser/scenes/keyBindings.fxml | 4 +- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/racevisionGame/src/main/java/visualiser/Controllers/KeyBindingsController.java b/racevisionGame/src/main/java/visualiser/Controllers/KeyBindingsController.java index 77b6687f..356430a0 100644 --- a/racevisionGame/src/main/java/visualiser/Controllers/KeyBindingsController.java +++ b/racevisionGame/src/main/java/visualiser/Controllers/KeyBindingsController.java @@ -6,13 +6,16 @@ import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.scene.control.Button; -import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.input.KeyEvent; +import javafx.scene.layout.AnchorPane; import visualiser.gameController.Keys.ControlKey; import visualiser.gameController.Keys.KeyFactory; import java.util.Map; +/** + * Controller for the scene used to display and update current key bindings. + */ public class KeyBindingsController { @FXML Button btnSave; @FXML Button btnCancel; @@ -20,18 +23,21 @@ public class KeyBindingsController { @FXML ListView lstControl; @FXML ListView lstKey; @FXML ListView lstDescription; - @FXML Label lblTitle; + @FXML AnchorPane anchor; String currentButton = null; KeyFactory keyFactory = KeyFactory.getFactory(); public void initialize(){ + // headings for each column lstKey.getItems().add("Key"); lstControl.getItems().add("Command"); lstDescription.getItems().add("Description"); + lstKey.getSelectionModel().select(0); + lstControl.getSelectionModel().select(0); + lstDescription.getSelectionModel().select(0); - - + // populate columns with current key bindings and buttons to update for (Map.Entry entry : keyFactory.getKeyState().entrySet()) { Button button = new Button(entry.getKey()); button.setMinWidth(120); @@ -46,10 +52,7 @@ public class KeyBindingsController { lstDescription.getItems().add(entry.getValue().getProtocolCode()); } - lstKey.getSelectionModel().select(0); - lstControl.getSelectionModel().select(0); - lstDescription.getSelectionModel().select(0); - + // stop the columns from being selectable, so only the buttons are lstKey.getSelectionModel().selectedItemProperty() .addListener((observable, oldvalue, newValue) -> { Platform.runLater(new Runnable() { @@ -75,11 +78,39 @@ public class KeyBindingsController { }); }); + // add CSS stylesheet once the scene has been created lstKey.sceneProperty().addListener((obs, oldScene, newScene) -> { if (newScene != null) { newScene.getStylesheets().add("/css/keyBindings.css"); } + }); + + setKeyListener(); + } + /** + * Creates a listener for the base anchorpane for key presses. + * It updates the current key bindings if required. + */ + public void setKeyListener(){ + anchor.addEventFilter(KeyEvent.KEY_PRESSED, event -> { + // if a button was clicked + if (currentButton != null) { + // update text on the button + ObservableList buttons = lstKey.getItems(); + for (int i = 1; i < buttons.size(); i++) { + if (currentButton == ((Button)buttons.get(i)).getText()) { + ((Button)buttons.get(i)).setText(event.getCode().toString + ()); + break; + } + } + // update the control key + keyFactory.updateKey(currentButton, event.getCode().toString()); + // remove current button selection + currentButton = null; + } + event.consume(); }); } @@ -95,22 +126,4 @@ public class KeyBindingsController { System.out.println("save clicked"); } - public void onKey(KeyEvent e){ - System.out.println("key pressed"); - // if a button was clicked - if (currentButton!=null){ - // update text on the button - ObservableList buttons = lstKey.getItems(); - for (int i = 1; i < buttons.size(); i++) { - if (currentButton == ((Button)buttons.get(i)).getText()) { - ((Button)buttons.get(i)).setText(e.getCode().toString()); - break; - } - } - // update the control key - keyFactory.updateKey(currentButton, e.getCode().toString()); - // remove current button selection - currentButton = null; - } - } } diff --git a/racevisionGame/src/main/java/visualiser/gameController/Keys/KeyFactory.java b/racevisionGame/src/main/java/visualiser/gameController/Keys/KeyFactory.java index 26267d84..555cafff 100644 --- a/racevisionGame/src/main/java/visualiser/gameController/Keys/KeyFactory.java +++ b/racevisionGame/src/main/java/visualiser/gameController/Keys/KeyFactory.java @@ -52,8 +52,14 @@ public class KeyFactory { return keyState; } + /** + * Update the key bound to a particular command in the keystate. + * @param oldKey the existing key to updated + * @param newKey the new key value to replace the old + */ public void updateKey(String oldKey, String newKey){ ControlKey controlKey = keyState.get(oldKey); + keyState.remove(oldKey, controlKey); keyState.put(newKey, controlKey); } } diff --git a/racevisionGame/src/main/resources/visualiser/scenes/keyBindings.fxml b/racevisionGame/src/main/resources/visualiser/scenes/keyBindings.fxml index 9f881b21..6ce15890 100644 --- a/racevisionGame/src/main/resources/visualiser/scenes/keyBindings.fxml +++ b/racevisionGame/src/main/resources/visualiser/scenes/keyBindings.fxml @@ -6,9 +6,7 @@ - +