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]
This commit is contained in:
Jessica Syder
2017-08-30 17:16:15 +12:00
parent 7086af6057
commit ad0e26e882
5 changed files with 192 additions and 4 deletions
@@ -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<String, ControlKey> entry : keyFactory.getKeyState().entrySet()) {
Button button = new Button(entry.getKey());
button.setMinWidth(120);
button.setOnAction(new EventHandler<ActionEvent>() {
@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;
}
}
}
@@ -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();
}
/**
@@ -48,4 +48,12 @@ public class KeyFactory {
return keyState.get(key);
}
public Map<String, ControlKey> getKeyState() {
return keyState;
}
public void updateKey(String oldKey, String newKey){
ControlKey controlKey = keyState.get(oldKey);
keyState.put(newKey, controlKey);
}
}