parent
cc264f318e
commit
86e8cb7560
@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue