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]
This commit is contained in:
@@ -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<String, ControlKey> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user