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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user