From ad0e26e88238ddc9cc2b677f676e0c4a9051aa64 Mon Sep 17 00:00:00 2001 From: Jessica Syder Date: Wed, 30 Aug 2017 17:16:15 +1200 Subject: [PATCH 01/11] Created scene to view/update current key bindings. - Current key bindings are shown on buttons - Added button action events to store the clicked button - Added key press event to entire pane - Button text is updated to display its key - Added functionality to keyFactory to update key bindings - CSS styling for some components #story[1197] --- .../Controllers/KeyBindingsController.java | 116 ++++++++++++++++++ .../src/main/java/visualiser/app/App.java | 12 +- .../gameController/Keys/KeyFactory.java | 8 ++ .../src/main/resources/css/keyBindings.css | 35 ++++++ .../visualiser/scenes/keyBindings.fxml | 25 ++++ 5 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 racevisionGame/src/main/java/visualiser/Controllers/KeyBindingsController.java create mode 100644 racevisionGame/src/main/resources/css/keyBindings.css create mode 100644 racevisionGame/src/main/resources/visualiser/scenes/keyBindings.fxml diff --git a/racevisionGame/src/main/java/visualiser/Controllers/KeyBindingsController.java b/racevisionGame/src/main/java/visualiser/Controllers/KeyBindingsController.java new file mode 100644 index 00000000..77b6687f --- /dev/null +++ b/racevisionGame/src/main/java/visualiser/Controllers/KeyBindingsController.java @@ -0,0 +1,116 @@ +package visualiser.Controllers; + +import javafx.application.Platform; +import javafx.collections.ObservableList; +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 visualiser.gameController.Keys.ControlKey; +import visualiser.gameController.Keys.KeyFactory; +import java.util.Map; + +public class KeyBindingsController { + @FXML Button btnSave; + @FXML Button btnCancel; + @FXML Button btnReset; + @FXML ListView lstControl; + @FXML ListView lstKey; + @FXML ListView lstDescription; + @FXML Label lblTitle; + String currentButton = null; + KeyFactory keyFactory = KeyFactory.getFactory(); + + public void initialize(){ + + lstKey.getItems().add("Key"); + lstControl.getItems().add("Command"); + lstDescription.getItems().add("Description"); + + + + for (Map.Entry entry : keyFactory.getKeyState().entrySet()) { + Button button = new Button(entry.getKey()); + button.setMinWidth(120); + button.setOnAction(new EventHandler() { + @Override public void handle(ActionEvent e) { + currentButton = button.getText(); + System.out.println("Button clicked"); + } + }); + lstKey.getItems().add(button); + lstControl.getItems().add(entry.getValue()); + lstDescription.getItems().add(entry.getValue().getProtocolCode()); + } + + lstKey.getSelectionModel().select(0); + lstControl.getSelectionModel().select(0); + lstDescription.getSelectionModel().select(0); + + lstKey.getSelectionModel().selectedItemProperty() + .addListener((observable, oldvalue, newValue) -> { + Platform.runLater(new Runnable() { + public void run() { + lstKey.getSelectionModel().select(0); + } + }); + }); + lstDescription.getSelectionModel().selectedItemProperty() + .addListener((observable, oldvalue, newValue) -> { + Platform.runLater(new Runnable() { + public void run() { + lstDescription.getSelectionModel().select(0); + } + }); + }); + lstControl.getSelectionModel().selectedItemProperty() + .addListener((observable, oldvalue, newValue) -> { + Platform.runLater(new Runnable() { + public void run() { + lstControl.getSelectionModel().select(0); + } + }); + }); + + lstKey.sceneProperty().addListener((obs, oldScene, newScene) -> { + if (newScene != null) { + newScene.getStylesheets().add("/css/keyBindings.css"); + } + + }); + } + + public void cancel(){ + System.out.println("cancel clicked"); + } + + public void reset(){ + System.out.println("reset clicked"); + } + + public void save(){ + 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/app/App.java b/racevisionGame/src/main/java/visualiser/app/App.java index eeae5a5d..92cb3096 100644 --- a/racevisionGame/src/main/java/visualiser/app/App.java +++ b/racevisionGame/src/main/java/visualiser/app/App.java @@ -3,7 +3,6 @@ package visualiser.app; import javafx.animation.FadeTransition; import javafx.application.Application; import javafx.application.Platform; -import javafx.beans.property.ReadOnlyObjectProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.concurrent.Task; @@ -15,7 +14,6 @@ import javafx.geometry.Rectangle2D; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Label; -import javafx.scene.control.ListView; import javafx.scene.control.ProgressBar; import javafx.scene.effect.DropShadow; import javafx.scene.image.Image; @@ -30,8 +28,6 @@ import javafx.stage.WindowEvent; import javafx.util.Duration; import visualiser.Controllers.MainController; -import java.io.IOException; - public class App extends Application { @@ -170,6 +166,14 @@ public class App extends Application { System.exit(0); } }); + + // TODO: remove later, just for testing + Parent root2 = FXMLLoader.load(getClass().getResource + ("/visualiser/scenes/keyBindings.fxml")); + Stage stage2 = new Stage(); + stage2.setScene(new Scene(root2)); + stage2.centerOnScreen(); + stage2.show(); } /** diff --git a/racevisionGame/src/main/java/visualiser/gameController/Keys/KeyFactory.java b/racevisionGame/src/main/java/visualiser/gameController/Keys/KeyFactory.java index be95abd3..26267d84 100644 --- a/racevisionGame/src/main/java/visualiser/gameController/Keys/KeyFactory.java +++ b/racevisionGame/src/main/java/visualiser/gameController/Keys/KeyFactory.java @@ -48,4 +48,12 @@ public class KeyFactory { return keyState.get(key); } + public Map getKeyState() { + return keyState; + } + + public void updateKey(String oldKey, String newKey){ + ControlKey controlKey = keyState.get(oldKey); + keyState.put(newKey, controlKey); + } } diff --git a/racevisionGame/src/main/resources/css/keyBindings.css b/racevisionGame/src/main/resources/css/keyBindings.css new file mode 100644 index 00000000..a1dc8242 --- /dev/null +++ b/racevisionGame/src/main/resources/css/keyBindings.css @@ -0,0 +1,35 @@ +.list-view .list-cell { + -fx-cell-size: 40; + -fx-font-family: "Tahoma"; + -fx-background-color: #fffff0; + -fx-alignment: center; +} + +.list-view .list-cell:even { + -fx-background-color: #ffffdc; +} + +.list-view .list-cell:selected { + -fx-background-color: #f9e5c3; + -fx-font-family: "Comic Sans MS"; + -fx-font-weight: bold; + -fx-font-size: 18; +} + +.button { + -fx-background-color: linear-gradient(#acdeff 50%, #a9c8ff 100%); + -fx-background-radius: 4px; + -fx-border-radius: 4px; + -fx-text-fill: #242d35; + -fx-font-size: 12px; + -fx-font-family: "Courier New"; +} + +.button:focused { + -fx-background-color: white; + -fx-border-color: #0056bd; +} + +#anchor { + -fx-background-color: #fdfac3; +} \ No newline at end of file diff --git a/racevisionGame/src/main/resources/visualiser/scenes/keyBindings.fxml b/racevisionGame/src/main/resources/visualiser/scenes/keyBindings.fxml new file mode 100644 index 00000000..9f881b21 --- /dev/null +++ b/racevisionGame/src/main/resources/visualiser/scenes/keyBindings.fxml @@ -0,0 +1,25 @@ + + + + + + + + + + + +