Refactored KeyFactory to ensure single instance of each key state handler.

#story[1089]
main
Connor Taylor-Brown 9 years ago
parent cc264f318e
commit 86e8cb7560

@ -92,10 +92,12 @@ public class RaceController extends Controller {
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
KeyFactory keyFactory = KeyFactory.getFactory();
// Initialise keyboard handler // Initialise keyboard handler
race.addEventFilter(KeyEvent.KEY_PRESSED, event -> { race.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
String codeString = event.getCode().toString(); String codeString = event.getCode().toString();
ControlKey controlKey = KeyFactory.getKey(codeString); ControlKey controlKey = keyFactory.getKey(codeString);
if(controlKey != null) { if(controlKey != null) {
try { try {
controllerClient.sendKey(controlKey); controllerClient.sendKey(controlKey);

@ -20,10 +20,12 @@ public class InputChecker {
* @param scene Scene the controller is to run in parallel with. * @param scene Scene the controller is to run in parallel with.
*/ */
public void runWithScene(Scene scene){ public void runWithScene(Scene scene){
KeyFactory keyFactory = KeyFactory.getFactory();
scene.setOnKeyPressed(event -> { scene.setOnKeyPressed(event -> {
String codeString = event.getCode().toString(); String codeString = event.getCode().toString();
if (!currentlyActiveKeys.containsKey(codeString)) { if (!currentlyActiveKeys.containsKey(codeString)) {
ControlKey controlKey = KeyFactory.getKey(codeString); ControlKey controlKey = keyFactory.getKey(codeString);
if (controlKey != null) { if (controlKey != null) {
controlKey.onAction(); controlKey.onAction();
System.out.println(controlKey.toString() + " is Pressed."); System.out.println(controlKey.toString() + " is Pressed.");
@ -34,7 +36,7 @@ public class InputChecker {
scene.setOnKeyReleased(event -> { scene.setOnKeyReleased(event -> {
String codeString = event.getCode().toString(); String codeString = event.getCode().toString();
ControlKey controlKey = KeyFactory.getKey(codeString); ControlKey controlKey = keyFactory.getKey(codeString);
if (controlKey != null) { if (controlKey != null) {
controlKey.onRelease(); controlKey.onRelease();
System.out.println(controlKey.toString() + " is Released."); System.out.println(controlKey.toString() + " is Released.");
@ -46,7 +48,7 @@ public class InputChecker {
@Override @Override
public void handle(long now) { public void handle(long now) {
for (String key: currentlyActiveKeys.keySet()){ for (String key: currentlyActiveKeys.keySet()){
ControlKey controlKey = KeyFactory.getKey(key); ControlKey controlKey = keyFactory.getKey(key);
if (controlKey != null){ if (controlKey != null){
controlKey.onHold(); controlKey.onHold();
System.out.println(controlKey.toString() + " is Held."); System.out.println(controlKey.toString() + " is Held.");

@ -8,16 +8,13 @@ import javafx.scene.input.KeyCode;
public abstract class ControlKey { public abstract class ControlKey {
private String name; private String name;
private KeyCode keyCode;
/** /**
* Constructor for Control * Constructor for Control
* @param name name of the key * @param name name of the key
* @param keyCode key code for the key
*/ */
public ControlKey(String name, KeyCode keyCode){ public ControlKey(String name){
this.name = name; this.name = name;
this.keyCode = keyCode;
} }
/** /**

@ -1,7 +1,5 @@
package visualiser.gameController.Keys; package visualiser.gameController.Keys;
import javafx.scene.input.KeyCode;
/** /**
* Key to send downwind packet to server * Key to send downwind packet to server
*/ */
@ -9,12 +7,11 @@ public class DownWindKey extends ControlKey {
/** /**
* Constructor for Control * Constructor for Control
* @param name name of the key
* *
* @param name name of the key
* @param keyCode key code for the key
*/ */
public DownWindKey(String name, KeyCode keyCode) { public DownWindKey(String name) {
super(name, keyCode); super(name);
} }
@Override @Override

@ -1,36 +1,51 @@
package visualiser.gameController.Keys; package visualiser.gameController.Keys;
import javafx.scene.input.KeyCode; import java.util.HashMap;
import java.util.Map;
/** /**
* Factory for creating Keys, these could be predefined in the future. * Factory for creating Keys, these could be predefined in the future.
*/ */
public class KeyFactory { public class KeyFactory {
/**
* Retrieve command given key
*/
private Map<String, ControlKey> keyState;
/**
* Singleton instance to enforce consistent key state
*/
private static KeyFactory theFactory = new KeyFactory();
/**
* Singleton constructor for key state, set up initial state of each action.
*/
private 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("PAGE_UP", new UpWindKey("Upwind"));
keyState.put("PAGE_DOWN", new DownWindKey("Downwind"));
}
/**
* Get singleton instance of KeyFactory to interact with key state
* @return automatically constructed KeyFactory
*/
public static KeyFactory getFactory() {
return theFactory;
}
/** /**
* Get the Control Key in charge of a key press * Get the Control Key in charge of a key press
* @param key key pressed (String value of KeyCode) * @param key key pressed (String value of KeyCode)
* @return the Control Key behaviour of the key pressed. * @return the Control Key behaviour of the key pressed.
*/ */
public static ControlKey getKey(String key){ public ControlKey getKey(String key){
switch(key){ return keyState.get(key);
case "Z":
return new ZoomInKey("Z", KeyCode.Z);
case "X":
return new ZoomOutKey("X", KeyCode.X);
case "SPACE":
return new VMGKey("SPACE", KeyCode.SPACE);
case "SHIFT":
return new SailsToggleKey("SHIFT", KeyCode.SHIFT);
case "ENTER":
return new TackGybeKey("ENTER", KeyCode.ENTER);
case "PAGE_UP":
return new UpWindKey("PAGE_UP", KeyCode.PAGE_UP);
case "PAGE_DOWN":
return new DownWindKey("PAGE_DOWN", KeyCode.PAGE_DOWN);
default:
return null;
}
} }
} }

@ -1,7 +1,5 @@
package visualiser.gameController.Keys; package visualiser.gameController.Keys;
import javafx.scene.input.KeyCode;
/** /**
* Key to toggle the sails * Key to toggle the sails
*/ */
@ -9,12 +7,11 @@ public class SailsToggleKey extends ControlKey {
/** /**
* Constructor for Control * Constructor for Control
* @param name name of the key
* *
* @param name name of the key
* @param keyCode key code for the key
*/ */
public SailsToggleKey(String name, KeyCode keyCode) { public SailsToggleKey(String name) {
super(name, keyCode); super(name);
} }
@Override @Override

@ -1,7 +1,5 @@
package visualiser.gameController.Keys; package visualiser.gameController.Keys;
import javafx.scene.input.KeyCode;
/** /**
* key to toggle between tacking and gybing * key to toggle between tacking and gybing
*/ */
@ -9,12 +7,11 @@ public class TackGybeKey extends ControlKey {
/** /**
* Constructor for Control * Constructor for Control
* @param name name of the key
* *
* @param name name of the key
* @param keyCode key code for the key
*/ */
public TackGybeKey(String name, KeyCode keyCode) { public TackGybeKey(String name) {
super(name, keyCode); super(name);
} }
@Override @Override

@ -1,7 +1,5 @@
package visualiser.gameController.Keys; package visualiser.gameController.Keys;
import javafx.scene.input.KeyCode;
/** /**
* Key to go upwind * Key to go upwind
*/ */
@ -9,12 +7,11 @@ public class UpWindKey extends ControlKey {
/** /**
* Constructor for Control * Constructor for Control
* @param name name of the key
* *
* @param name name of the key
* @param keyCode key code for the key
*/ */
public UpWindKey(String name, KeyCode keyCode) { public UpWindKey(String name) {
super(name, keyCode); super(name);
} }
@Override @Override

@ -11,10 +11,9 @@ public class VMGKey extends ControlKey{
* Constructor for Control * Constructor for Control
* *
* @param name name of the key * @param name name of the key
* @param keyCode key code for the key
*/ */
public VMGKey(String name, KeyCode keyCode) { public VMGKey(String name) {
super(name, keyCode); super(name);
} }
@Override @Override

@ -7,8 +7,8 @@ import javafx.scene.input.KeyCode;
*/ */
public class ZoomInKey extends ControlKey { public class ZoomInKey extends ControlKey {
public ZoomInKey(String name, KeyCode keyCode) { public ZoomInKey(String name) {
super(name, keyCode); super(name);
} }
@Override @Override

@ -1,7 +1,5 @@
package visualiser.gameController.Keys; package visualiser.gameController.Keys;
import javafx.scene.input.KeyCode;
/** /**
* Key to zoom out of the game. * Key to zoom out of the game.
*/ */
@ -9,12 +7,11 @@ public class ZoomOutKey extends ControlKey{
/** /**
* Constructor for Control * Constructor for Control
* @param name name of the key
* *
* @param name name of the key
* @param keyCode key code for the key
*/ */
public ZoomOutKey(String name, KeyCode keyCode) { public ZoomOutKey(String name) {
super(name, keyCode); super(name);
} }
@Override @Override

Loading…
Cancel
Save