commit
a27c16d413
@ -0,0 +1,81 @@
|
|||||||
|
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 javafx.application.Application.launch;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for checking what keys are currently being used
|
||||||
|
*/
|
||||||
|
public class InputChecker {
|
||||||
|
private HashMap<String, Boolean> currentlyActiveKeys = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller loop that detects key presses that runs parallel to the main scene.
|
||||||
|
* @param scene Scene the controller is to run in parallel with.
|
||||||
|
*/
|
||||||
|
public void runWithScene(Scene scene){
|
||||||
|
scene.setOnKeyPressed(event -> {
|
||||||
|
String codeString = event.getCode().toString();
|
||||||
|
if (!currentlyActiveKeys.containsKey(codeString)) {
|
||||||
|
ControlKey controlKey = KeyFactory.getKey(codeString);
|
||||||
|
if (controlKey != null) {
|
||||||
|
controlKey.onAction();
|
||||||
|
System.out.println(controlKey.toString() + " is Pressed.");
|
||||||
|
}
|
||||||
|
currentlyActiveKeys.put(codeString, true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
scene.setOnKeyReleased(event -> {
|
||||||
|
String codeString = event.getCode().toString();
|
||||||
|
ControlKey controlKey = KeyFactory.getKey(codeString);
|
||||||
|
if (controlKey != null) {
|
||||||
|
controlKey.onRelease();
|
||||||
|
System.out.println(controlKey.toString() + " is Released.");
|
||||||
|
}
|
||||||
|
currentlyActiveKeys.remove(event.getCode().toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
new AnimationTimer() {
|
||||||
|
@Override
|
||||||
|
public void handle(long now) {
|
||||||
|
for (String key: currentlyActiveKeys.keySet()){
|
||||||
|
ControlKey controlKey = KeyFactory.getKey(key);
|
||||||
|
if (controlKey != null){
|
||||||
|
controlKey.onHold();
|
||||||
|
System.out.println(controlKey.toString() + " is Held.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// for (String key : InputKeys.stringKeysMap.keySet()){
|
||||||
|
// if (removeActiveKey(key)) {
|
||||||
|
// System.out.println(key);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* removes a key from the active dictionary
|
||||||
|
* @param codeString string of the key press to remove
|
||||||
|
* @return whether or not the key has been removed or not.
|
||||||
|
*/
|
||||||
|
private boolean removeActiveKey(String codeString) {
|
||||||
|
Boolean isActive = currentlyActiveKeys.get(codeString);
|
||||||
|
|
||||||
|
if (isActive != null && isActive) {
|
||||||
|
currentlyActiveKeys.put(codeString, false);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key for the controller, part of the abstract factory KeyFactory
|
||||||
|
*/
|
||||||
|
public abstract class ControlKey {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private KeyCode keyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public ControlKey(String name, KeyCode keyCode){
|
||||||
|
this.name = name;
|
||||||
|
this.keyCode = keyCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To String method
|
||||||
|
* @return returns the name of the key
|
||||||
|
*/
|
||||||
|
public String toString(){
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What this key should do when the command is issued for it to do its job.
|
||||||
|
*/
|
||||||
|
public abstract void onAction();//may want to make it take in a visualiser and stuff in the future.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What to do when the key is held
|
||||||
|
*/
|
||||||
|
public abstract void onHold();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What to do when the key is released.
|
||||||
|
*/
|
||||||
|
public abstract void onRelease();
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to send downwind packet to server
|
||||||
|
*/
|
||||||
|
public class DownWindKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public DownWindKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory for creating Keys, these could be predefined in the future.
|
||||||
|
*/
|
||||||
|
public class KeyFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Control Key incharge of a key press
|
||||||
|
* @param key key pressed (String value of KeyCode)
|
||||||
|
* @return the Control Key behaviour of the key pressed.
|
||||||
|
*/
|
||||||
|
public static ControlKey getKey(String key){
|
||||||
|
switch(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to toggle the sails
|
||||||
|
*/
|
||||||
|
public class SailsToggleKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public SailsToggleKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key to toggle between tacking and gybing
|
||||||
|
*/
|
||||||
|
public class TackGybeKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public TackGybeKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to go upwind
|
||||||
|
*/
|
||||||
|
public class UpWindKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public UpWindKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to trigger auto VMG
|
||||||
|
*/
|
||||||
|
public class VMGKey extends ControlKey{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public VMGKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key to zoom into the game
|
||||||
|
*/
|
||||||
|
public class ZoomInKey extends ControlKey {
|
||||||
|
|
||||||
|
public ZoomInKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to zoom out of the game.
|
||||||
|
*/
|
||||||
|
public class ZoomOutKey extends ControlKey{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public ZoomOutKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import javafx.application.Application;
|
||||||
|
import javafx.application.Platform;
|
||||||
|
import javafx.event.EventHandler;
|
||||||
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.layout.GridPane;
|
||||||
|
import javafx.stage.Stage;
|
||||||
|
import javafx.stage.WindowEvent;
|
||||||
|
import visualiser.gameController.InputChecker;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start to manually test the game controller
|
||||||
|
*/
|
||||||
|
public class GameControllerManualTest extends Application {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Stage stage) throws Exception {
|
||||||
|
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
|
||||||
|
@Override
|
||||||
|
public void handle(WindowEvent event) {
|
||||||
|
Platform.exit();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
GridPane root = new GridPane();
|
||||||
|
Scene scene = new Scene(root, 1200, 800);
|
||||||
|
|
||||||
|
InputChecker inputChecker = new InputChecker();
|
||||||
|
inputChecker.runWithScene(scene);
|
||||||
|
|
||||||
|
stage.setScene(scene);
|
||||||
|
stage.setTitle("RaceVision - Team 7 - Input Tester Manual Test");
|
||||||
|
stage.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
launch(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue