Merge branch 'cbt-multi-controller' into css

# Conflicts:
#	racevisionGame/src/main/java/mock/app/App.java
#	racevisionGame/src/main/java/visualiser/Controllers/ConnectionController.java
#	racevisionGame/src/main/java/visualiser/app/App.java
#	racevisionGame/src/main/java/visualiser/model/RaceConnection.java
#	racevisionGame/src/main/resources/visualiser/scenes/connect.fxml
This commit is contained in:
zwu18
2017-07-26 20:54:07 +12:00
23 changed files with 757 additions and 219 deletions
@@ -5,12 +5,18 @@ import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import mock.app.Event;
import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.InvalidRegattaDataException;
import shared.exceptions.XMLReaderException;
import visualiser.model.RaceConnection;
import javax.xml.soap.Text;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ResourceBundle;
/**
@@ -79,8 +85,6 @@ public class ConnectionController extends Controller {
public void initialize(URL location, ResourceBundle resources) {
// TODO - replace with config file
connections = FXCollections.observableArrayList();
connections.add(new RaceConnection("livedata.americascup.com", 4941, null));
connections.add(new RaceConnection("localhost", 4942, null));
connectionTable.setItems(connections);
hostnameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty());
@@ -113,6 +117,7 @@ public class ConnectionController extends Controller {
try{
RaceConnection connection = connectionTable.getSelectionModel().getSelectedItem();
Socket socket = new Socket(connection.getHostname(), connection.getPort());
socket.setKeepAlive(true);
connectionWrapper.setVisible(false);
parent.enterLobby(socket);
} catch (IOException e) { /* Never reached */ }
@@ -130,6 +135,29 @@ public class ConnectionController extends Controller {
}catch(NumberFormatException e){
System.err.println("Port number entered is not a number");
}
}
/**
* Sets up a new host
*/
public void addLocal() {
try {
Event game = Event.getEvent();
urlField.textProperty().set(game.getAddress());
portField.textProperty().set(Integer.toString(game.getPort()));
game.start();
addConnection();
} catch (InvalidRaceDataException e) {
e.printStackTrace();
} catch (XMLReaderException e) {
e.printStackTrace();
} catch (InvalidBoatDataException e) {
e.printStackTrace();
} catch (InvalidRegattaDataException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
@@ -5,9 +5,12 @@ import javafx.animation.AnimationTimer;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.chart.LineChart;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
@@ -16,8 +19,11 @@ import javafx.util.Callback;
import network.Messages.Enums.RaceStatusEnum;
import shared.model.Leg;
import visualiser.app.VisualiserInput;
import visualiser.gameController.Keys.ControlKey;
import visualiser.gameController.Keys.KeyFactory;
import visualiser.model.*;
import java.awt.*;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ResourceBundle;
@@ -79,6 +85,14 @@ public class RaceController extends Controller {
@Override
public void initialize(URL location, ResourceBundle resources) {
// Initialise keyboard handler
race.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
String codeString = event.getCode().toString();
ControlKey controlKey = KeyFactory.getKey(codeString);
if(controlKey != null) {
System.out.println(controlKey.toString() + " is Pressed.");
}
});
}
@@ -278,8 +292,6 @@ public class RaceController extends Controller {
//Add to scene.
canvasBase.getChildren().add(0, raceCanvas);
}
@@ -345,7 +357,6 @@ public class RaceController extends Controller {
}
/**
* Timer which monitors the race.
*/
@@ -20,6 +20,7 @@ import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import static network.Utils.ByteConverter.bytesToShort;
@@ -372,6 +373,4 @@ public class VisualiserInput implements Runnable {
}
}
}
@@ -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() {
}
}
@@ -17,6 +17,12 @@ public class RaceConnection {
private final StringProperty gamename;
public RaceConnection(String hostname, int port, String gamename) {
/**
* Constructor for remote host connections.
* @param hostname URL for remote host
* @param port port for game feed
*/
public RaceConnection(String hostname, int port) {
this.hostname = new SimpleStringProperty(hostname);
this.port = port;
this.status = new SimpleStringProperty("");
@@ -35,6 +41,10 @@ public class RaceConnection {
try (Socket s = new Socket()){
s.connect(i, 750);//TODO this should be at least a second or two, once moved to its own thread
status.set("Ready");
s.shutdownInput();
s.shutdownOutput();
s.close();
//System.out.println(String.valueOf(s.isClosed()));
return true;
} catch (IOException e) {
}