- Created keybindingcontroller which can detect keypress and can save the keybind throughout the app

- Changed keybindingglyph to keyboard icon
- Fix button hover CSS to change text fill

#story[1278]
This commit is contained in:
Zhi You Tan
2017-09-20 02:42:02 +12:00
parent 8084a61333
commit a1933c2869
4 changed files with 137 additions and 13 deletions
@@ -5,10 +5,16 @@ import com.jfoenix.controls.JFXDecorator;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXDialog.DialogTransition;
import com.jfoenix.svg.SVGGlyph;
import java.io.IOException;
import java.util.HashMap;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
@@ -21,9 +27,6 @@ import seng302.utilities.BonjourInstallChecker;
import seng302.utilities.Sounds;
import seng302.visualiser.GameClient;
import java.io.IOException;
import java.util.HashMap;
public class ViewManager {
private static ViewManager instance;
@@ -147,12 +150,12 @@ public class ViewManager {
"M13.5,9 C13.5,7.2 12.5,5.7 11,5 L11,7.2 L13.5,9.7 L13.5,9 L13.5,9 Z M16,9 C16,9.9 15.8,10.8 15.5,11.6 L17,13.1 C17.7,11.9 18,10.4 18,8.9 C18,4.6 15,1 11,0.1 L11,2.2 C13.9,3.2 16,5.8 16,9 L16,9 Z M1.3,0 L0,1.3 L4.7,6 L0,6 L0,12 L4,12 L9,17 L9,10.3 L13.3,14.6 C12.6,15.1 11.9,15.5 11,15.8 L11,17.9 C12.4,17.6 13.6,17 14.7,16.1 L16.7,18.1 L18,16.8 L9,7.8 L1.3,0 L1.3,0 Z M9,1 L6.9,3.1 L9,5.2 L9,1 L9,1 Z",
Color.WHITE);
SVGGlyph keyBindingGlyph = new SVGGlyph(0, "KEY_BINDING",
"M19.43 12.98c.04-.32.07-.64.07-.98s-.03-.66-.07-.98l2.11-1.65c.19-.15.24-.42.12-.64l-2-3.46c-.12-.22-.39-.3-.61-.22l-2.49 1c-.52-.4-1.08-.73-1.69-.98l-.38-2.65C14.46 2.18 14.25 2 14 2h-4c-.25 0-.46.18-.49.42l-.38 2.65c-.61.25-1.17.59-1.69.98l-2.49-1c-.23-.09-.49 0-.61.22l-2 3.46c-.13.22-.07.49.12.64l2.11 1.65c-.04.32-.07.65-.07.98s.03.66.07.98l-2.11 1.65c-.19.15-.24.42-.12.64l2 3.46c.12.22.39.3.61.22l2.49-1c.52.4 1.08.73 1.69.98l.38 2.65c.03.24.24.42.49.42h4c.25 0 .46-.18.49-.42l.38-2.65c.61-.25 1.17-.59 1.69-.98l2.49 1c.23.09.49 0 .61-.22l2-3.46c.12-.22.07-.49-.12-.64l-2.11-1.65zM12 15.5c-1.93 0-3.5-1.57-3.5-3.5s1.57-3.5 3.5-3.5 3.5 1.57 3.5 3.5-1.57 3.5-3.5 3.5z",
"M20 5H4c-1.1 0-1.99.9-1.99 2L2 17c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm-9 3h2v2h-2V8zm0 3h2v2h-2v-2zM8 8h2v2H8V8zm0 3h2v2H8v-2zm-1 2H5v-2h2v2zm0-3H5V8h2v2zm9 7H8v-2h8v2zm0-4h-2v-2h2v2zm0-3h-2V8h2v2zm3 3h-2v-2h2v2zm0-3h-2V8h2v2z",
Color.WHITE);
volumeOn.setSize(16, 16);
volumeOff.setSize(16, 16);
spacer.setSize(40, 16);
keyBindingGlyph.setSize(16,16);
keyBindingGlyph.setSize(24, 16);
// Determine which graphic should go on the button
if (Sounds.isMusicMuted() && Sounds.isSoundEffectsMuted()) {
@@ -0,0 +1,115 @@
package seng302.visualiser.controllers.dialogs;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import java.net.URL;
import java.util.*;
public class KeyBindingDialogController implements Initializable {
//--------FXML BEGIN--------//
@FXML
private Label keyBindingDialogHeader;
@FXML
private JFXButton zoomInbtn;
@FXML
private JFXButton zoomOutBtn;
@FXML
private JFXButton vmgBtn;
@FXML
private JFXButton sailInOutBtn;
@FXML
private JFXButton tackGybeBtn;
@FXML
private JFXButton upwindBtn;
@FXML
private JFXButton downwindBtn;
//---------FXML END---------//
private static Map<JFXButton, KeyCode> keys; // static button and saved keybinding pair
private List<JFXButton> buttons = new ArrayList<>();
@Override
public void initialize(URL location, ResourceBundle resources) {
buttons = new ArrayList<>();
Collections
.addAll(buttons, zoomInbtn, zoomOutBtn, vmgBtn, sailInOutBtn, tackGybeBtn, upwindBtn,
downwindBtn);
initializeKeys();
initializeButtons();
}
/**
* Initialise default button-keybinding pair if not exist, else rebind the existing keybinding
* to the new button which is created when javafx reinitialise a new controller.
*/
private void initializeKeys() {
if (keys == null) {
initializeDefaultKeys();
} else {
Map<JFXButton, KeyCode> tempKey = new LinkedHashMap<>();
int buttonIndex = 0;
for (JFXButton jfxButton : keys.keySet()) {
tempKey.put(buttons.get(buttonIndex), keys.get(jfxButton));
buttonIndex++;
}
keys = tempKey;
}
}
/**
* Initialise default keybinding to a button.
*/
private void initializeDefaultKeys() {
keys = new LinkedHashMap<>();
keys.put(zoomInbtn, KeyCode.Z);
keys.put(zoomOutBtn, KeyCode.X);
keys.put(vmgBtn, KeyCode.SPACE);
keys.put(sailInOutBtn, KeyCode.SHIFT);
keys.put(tackGybeBtn, KeyCode.ENTER);
keys.put(upwindBtn, KeyCode.PAGE_UP);
keys.put(downwindBtn, KeyCode.PAGE_DOWN);
}
/**
* Change button text to match current keybinding. Adds focusedProperty and keyPressed listener
* to each buttons.
*/
private void initializeButtons() {
for (JFXButton jfxButton : buttons) {
if (keys.get(jfxButton) != null) {
jfxButton.setText(keys.get(jfxButton).getName());
} else {
jfxButton.setText("");
}
jfxButton.focusedProperty().addListener((observable, oldValue, newValue) -> {
jfxButton.setOnKeyPressed(event -> {
event.consume();
doSomething(event, jfxButton);
});
});
}
}
/**
* HAOMING PLEASE CHANGE THIS DOCSTRING AND THE FUNCTION NAME!!!
*
* @param event BLAH
* @param button BLAH
*/
private void doSomething(KeyEvent event, JFXButton button) {
if (keys.containsValue(event.getCode())) {
keys.replace(button, null);
button.setText("");
} else {
keys.replace(button, event.getCode());
button.setText(event.getCode().getName());
}
}
}
@@ -19,6 +19,7 @@ Label {
JFXButton:hover {
-fx-font-size: 20px;
-fx-background-color: -fx-pp-light-theme-color;
-fx-text-fill: -fx-pp-light-text-color;
}
JFXButton:focused {
@@ -1,13 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import com.jfoenix.controls.*?>
<?import java.lang.*?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXDialogLayout?>
<?import java.net.URL?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<JFXDialogLayout maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="550.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<JFXDialogLayout maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="600.0" prefWidth="550.0" xmlns="http://javafx.com/javafx/8.0.111"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="seng302.visualiser.controllers.dialogs.KeyBindingDialogController">
<children>
<GridPane>
<columnConstraints>