From 83ac3e4e3398a6199951a227f765486146ed1db9 Mon Sep 17 00:00:00 2001 From: hba56 Date: Wed, 12 Jul 2017 11:59:59 +1200 Subject: [PATCH] updated the game controller to use the visualiser as the scene #story[1006] --- .../Controllers/MainWindowController.java | 7 -- controller/src/main/java/seng302/Main.java | 101 ------------------ controller/src/main/resources/mainWindow.fxml | 10 -- {controller => gameController}/pom.xml | 0 .../src/main/java/seng302/InputChecker.java | 49 +++++++++ .../src/main/java/seng302/InputKeys.java | 20 ++++ 6 files changed, 69 insertions(+), 118 deletions(-) delete mode 100644 controller/src/main/java/seng302/Controllers/MainWindowController.java delete mode 100644 controller/src/main/java/seng302/Main.java delete mode 100644 controller/src/main/resources/mainWindow.fxml rename {controller => gameController}/pom.xml (100%) create mode 100644 gameController/src/main/java/seng302/InputChecker.java create mode 100644 gameController/src/main/java/seng302/InputKeys.java diff --git a/controller/src/main/java/seng302/Controllers/MainWindowController.java b/controller/src/main/java/seng302/Controllers/MainWindowController.java deleted file mode 100644 index f5fdbcf2..00000000 --- a/controller/src/main/java/seng302/Controllers/MainWindowController.java +++ /dev/null @@ -1,7 +0,0 @@ -package seng302.Controllers; - -/** - * Controller for the main window fxml of the Game Controller - */ -public class MainWindowController { -} diff --git a/controller/src/main/java/seng302/Main.java b/controller/src/main/java/seng302/Main.java deleted file mode 100644 index c2b7720b..00000000 --- a/controller/src/main/java/seng302/Main.java +++ /dev/null @@ -1,101 +0,0 @@ -package seng302; - -import javafx.animation.AnimationTimer; -import javafx.application.Application; -import javafx.fxml.FXMLLoader; -import javafx.scene.Parent; -import javafx.scene.Scene; -import javafx.stage.Stage; - -import java.io.IOException; -import java.util.HashMap; - -/** - * Entry point for the controller side of the application - */ -public class Main extends Application { - //holds a boolean state for each important key to show if pressed down or not - private HashMap currentlyActiveKeys = new HashMap<>(); - - /** - * Entry point for running the programme - * - * @param args for starting the programme - */ - public static void main(String[] args) { - launch(args); - } - - @Override - public void start(Stage stage) { - try { - FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("mainWindow.fxml")); - Parent root = loader.load(); - - Scene scene = new Scene(root, 200, 100); - stage.setScene(scene); - stage.setTitle("Controller"); - - scene.setOnKeyPressed(event -> { - String codeString = event.getCode().toString(); - if (!currentlyActiveKeys.containsKey(codeString)) { - currentlyActiveKeys.put(codeString, true); - } - }); - - scene.setOnKeyReleased(event -> - currentlyActiveKeys.remove(event.getCode().toString()) - ); - - new AnimationTimer() { - @Override - public void handle(long now) { - if (removeActiveKey("Z")) { - System.out.println("zoom in"); - } - - if (removeActiveKey("X")) { - System.out.println("zoom out"); - } - - if (removeActiveKey("SPACE")) { - System.out.println("VMG"); - } - - if (removeActiveKey("SHIFT")) { - System.out.println("sails in/out"); - } - - if (removeActiveKey("Enter")) { - System.out.println("tack/gybe"); - } - - if (removeActiveKey("PAGE_UP")) { - System.out.println("up wind"); - } - - if (removeActiveKey("PAGE_DOWN")) { - System.out.println("down wind"); - } - } - }.start(); - - stage.show(); - } catch (IOException e) { - //Catch all exceptions, print, and exit. - e.printStackTrace(); - System.exit(1); - } - } - - private boolean removeActiveKey(String codeString) { - Boolean isActive = currentlyActiveKeys.get(codeString); - - if (isActive != null && isActive) { - currentlyActiveKeys.put(codeString, false); - return true; - } else { - return false; - } - } -} diff --git a/controller/src/main/resources/mainWindow.fxml b/controller/src/main/resources/mainWindow.fxml deleted file mode 100644 index f7eeebd1..00000000 --- a/controller/src/main/resources/mainWindow.fxml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - -
- -
-
diff --git a/controller/pom.xml b/gameController/pom.xml similarity index 100% rename from controller/pom.xml rename to gameController/pom.xml diff --git a/gameController/src/main/java/seng302/InputChecker.java b/gameController/src/main/java/seng302/InputChecker.java new file mode 100644 index 00000000..7a01be06 --- /dev/null +++ b/gameController/src/main/java/seng302/InputChecker.java @@ -0,0 +1,49 @@ +package seng302; + +import javafx.animation.AnimationTimer; +import javafx.scene.Scene; + +import java.util.HashMap; + +/** + * Class for checking what keys are currently being used + */ +public class InputChecker { + private HashMap currentlyActiveKeys = new HashMap<>(); + + public void runWithScene(Scene scene){ + scene.setOnKeyPressed(event -> { + String codeString = event.getCode().toString(); + if (!currentlyActiveKeys.containsKey(codeString)) { + currentlyActiveKeys.put(codeString, true); + } + }); + + scene.setOnKeyReleased(event -> + currentlyActiveKeys.remove(event.getCode().toString()) + ); + + new AnimationTimer() { + @Override + public void handle(long now) { + for (String key : InputKeys.listOfKeys){ + if (removeActiveKey(key)) { + System.out.println(key); + } + } + + } + }.start(); + } + + private boolean removeActiveKey(String codeString) { + Boolean isActive = currentlyActiveKeys.get(codeString); + + if (isActive != null && isActive) { + currentlyActiveKeys.put(codeString, false); + return true; + } else { + return false; + } + } +} diff --git a/gameController/src/main/java/seng302/InputKeys.java b/gameController/src/main/java/seng302/InputKeys.java new file mode 100644 index 00000000..57270017 --- /dev/null +++ b/gameController/src/main/java/seng302/InputKeys.java @@ -0,0 +1,20 @@ +package seng302; + +import java.util.Arrays; +import java.util.List; + +/** + * Class to store the basic keys used by this controller + */ +public class InputKeys { + protected static String zoomIn = "Z"; + protected static String zoomOut = "X"; + protected static String vmg = "SPACE"; + protected static String sailsToggle = "SHIFT"; + protected static String tackGybe = "Enter"; + protected static String upWind = "PAGE_UP"; + protected static String downWind = "Page_Down"; + + protected static List listOfKeys = Arrays.asList(zoomIn, zoomOut, vmg, + sailsToggle, tackGybe, upWind, downWind); +}