Fixed errors in making the keybinding replaceable.
- Changed KeyFactory to not be a singleton - Simplified keyBindings initializer - Modified and simplified keyListener - Added method to copy existing keyState - Changed button events - Changed to newer lambda functions - Modified updateKey method - Left option for multiple keys bound to one command - Updated and wrote new JavaDoc #story[1197]
This commit is contained in:
@@ -3,10 +3,11 @@ package visualiser.gameController;
|
||||
import javafx.animation.AnimationTimer;
|
||||
import javafx.scene.Scene;
|
||||
import visualiser.gameController.Keys.ControlKey;
|
||||
import visualiser.gameController.Keys.KeyFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static visualiser.app.App.keyFactory;
|
||||
|
||||
/**
|
||||
* Class for checking what keys are currently being used
|
||||
*/
|
||||
@@ -18,7 +19,7 @@ public class InputChecker {
|
||||
* @param scene Scene the controller is to run in parallel with.
|
||||
*/
|
||||
public void runWithScene(Scene scene){
|
||||
KeyFactory keyFactory = KeyFactory.getFactory();
|
||||
// KeyFactory keyFactory = KeyFactory.getFactory();
|
||||
|
||||
scene.setOnKeyPressed(event -> {
|
||||
String codeString = event.getCode().toString();
|
||||
|
||||
@@ -9,11 +9,9 @@ public class DownWindKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
* @param name name of the key
|
||||
*
|
||||
*/
|
||||
public DownWindKey(String name) {
|
||||
super(name, BoatActionEnum.DOWNWIND);
|
||||
public DownWindKey() {
|
||||
super("Downwind", BoatActionEnum.DOWNWIND);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,30 +13,17 @@ public class KeyFactory {
|
||||
private Map<String, ControlKey> keyState;
|
||||
|
||||
/**
|
||||
* Singleton instance to enforce consistent key state
|
||||
* Constructor for key state, set up initial state of each action.
|
||||
*/
|
||||
private static KeyFactory theFactory = new KeyFactory();
|
||||
|
||||
/**
|
||||
* Singleton constructor for key state, set up initial state of each action.
|
||||
*/
|
||||
private KeyFactory() {
|
||||
public KeyFactory() {
|
||||
this.keyState = new HashMap<>();
|
||||
keyState.put("Z", new ZoomInKey("Zoom In"));
|
||||
keyState.put("X", new ZoomOutKey("Zoom Out"));
|
||||
keyState.put("SPACE", new VMGKey("VMG"));
|
||||
keyState.put("SHIFT", new SailsToggleKey("Toggle Sails"));
|
||||
keyState.put("ENTER", new TackGybeKey("Tack/Gybe"));
|
||||
keyState.put("UP", new UpWindKey("Upwind"));
|
||||
keyState.put("DOWN", new DownWindKey("Downwind"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get singleton instance of KeyFactory to interact with key state
|
||||
* @return automatically constructed KeyFactory
|
||||
*/
|
||||
public static KeyFactory getFactory() {
|
||||
return theFactory;
|
||||
keyState.put("Z", new ZoomInKey());
|
||||
keyState.put("X", new ZoomOutKey());
|
||||
keyState.put("SPACE", new VMGKey());
|
||||
keyState.put("SHIFT", new SailsToggleKey());
|
||||
keyState.put("ENTER", new TackGybeKey());
|
||||
keyState.put("UP", new UpWindKey());
|
||||
keyState.put("DOWN", new DownWindKey());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,12 +41,21 @@ public class KeyFactory {
|
||||
|
||||
/**
|
||||
* Update the key bound to a particular command in the keystate.
|
||||
* @param oldKey the existing key to updated
|
||||
* @param newKey the new key value to replace the old
|
||||
* @param newKey the new key value for the command
|
||||
* @param command the command to be updated
|
||||
*/
|
||||
public void updateKey(String oldKey, String newKey){
|
||||
ControlKey controlKey = keyState.get(oldKey);
|
||||
public void updateKey(String newKey, String command){
|
||||
ControlKey controlKey = null;
|
||||
String oldKey = null;
|
||||
for (Map.Entry<String, ControlKey> entry : keyState.entrySet()) {
|
||||
// if this is the correct command
|
||||
if (entry.getValue().toString()==command){
|
||||
controlKey = entry.getValue();
|
||||
oldKey = entry.getKey();
|
||||
}
|
||||
}
|
||||
keyState.remove(oldKey, controlKey);
|
||||
keyState.put(newKey, controlKey);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,11 +10,9 @@ public class SailsToggleKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
* @param name name of the key
|
||||
*
|
||||
*/
|
||||
public SailsToggleKey(String name) {
|
||||
super(name, BoatActionEnum.NOT_A_STATUS);
|
||||
public SailsToggleKey() {
|
||||
super("Toggle Sails", BoatActionEnum.NOT_A_STATUS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,11 +9,9 @@ public class TackGybeKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
* @param name name of the key
|
||||
*
|
||||
*/
|
||||
public TackGybeKey(String name) {
|
||||
super(name, BoatActionEnum.TACK_GYBE);
|
||||
public TackGybeKey() {
|
||||
super("Tack/Gybe", BoatActionEnum.TACK_GYBE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,11 +9,9 @@ public class UpWindKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
* @param name name of the key
|
||||
*
|
||||
*/
|
||||
public UpWindKey(String name) {
|
||||
super(name, BoatActionEnum.UPWIND);
|
||||
public UpWindKey() {
|
||||
super("Upwind", BoatActionEnum.UPWIND);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
|
||||
/**
|
||||
@@ -10,11 +9,9 @@ public class VMGKey extends ControlKey{
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
*
|
||||
* @param name name of the key
|
||||
*/
|
||||
public VMGKey(String name) {
|
||||
super(name, BoatActionEnum.AUTO_PILOT);
|
||||
public VMGKey() {
|
||||
super("VMG", BoatActionEnum.AUTO_PILOT);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,8 +5,8 @@ package visualiser.gameController.Keys;
|
||||
*/
|
||||
public class ZoomInKey extends ControlKey {
|
||||
|
||||
public ZoomInKey(String name) {
|
||||
super(name);
|
||||
public ZoomInKey() {
|
||||
super("Zoom In");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,11 +7,9 @@ public class ZoomOutKey extends ControlKey{
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
* @param name name of the key
|
||||
*
|
||||
*/
|
||||
public ZoomOutKey(String name) {
|
||||
super(name);
|
||||
public ZoomOutKey() {
|
||||
super("Zoom Out");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user