Merge remote-tracking branch 'remotes/origin/master' into css
This commit is contained in:
@@ -4,6 +4,7 @@ import network.Messages.Enums.XMLMessageType;
|
||||
import network.Messages.LatestMessages;
|
||||
import network.Messages.XMLMessage;
|
||||
import org.mockito.Mock;
|
||||
import visualiser.gameController.ControllerServer;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -75,7 +76,9 @@ public class ConnectionAcceptor implements Runnable {
|
||||
Socket mockSocket = serverSocket.accept();
|
||||
DataOutputStream outToVisualiser = new DataOutputStream(mockSocket.getOutputStream());
|
||||
MockOutput mockOutput = new MockOutput(latestMessages, outToVisualiser);
|
||||
ControllerServer controllerServer = new ControllerServer(mockSocket);
|
||||
new Thread(mockOutput).start();
|
||||
new Thread(controllerServer).start();
|
||||
mockOutputList.add(mockOutput);
|
||||
System.out.println(String.format("%d number of Visualisers Connected.", mockOutputList.size()));
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -43,7 +43,7 @@ public class Event {
|
||||
private Event() {
|
||||
try {
|
||||
this.raceXML = getRaceXMLAtCurrentTime(XMLReader.readXMLFileToString("mock/mockXML/raceTest.xml", StandardCharsets.UTF_8));
|
||||
this.boatXML = XMLReader.readXMLFileToString("mock/mockXML/boatTest.xml", StandardCharsets.UTF_8);
|
||||
this.boatXML = XMLReader.readXMLFileToString("mock/mockXML/boatsSinglePlayer.xml", StandardCharsets.UTF_8);
|
||||
this.regattaXML = XMLReader.readXMLFileToString("mock/mockXML/regattaTest.xml", StandardCharsets.UTF_8);
|
||||
this.xmlFileType = XMLFileType.Contents;
|
||||
|
||||
@@ -94,7 +94,6 @@ public class Event {
|
||||
MockRace newRace = new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale);
|
||||
|
||||
new Thread(newRace).start();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -193,6 +193,10 @@ public class BinaryMessageDecoder {
|
||||
AverageWindDecoder awDecoder = new AverageWindDecoder(messageBody);
|
||||
return awDecoder.getAverageWind();
|
||||
|
||||
case BOATACTION:
|
||||
BoatActionDecoder baDecoder = new BoatActionDecoder(messageBody);
|
||||
return new BoatAction(baDecoder.getBoatAction());
|
||||
|
||||
default:
|
||||
//System.out.println("Broken Message!");
|
||||
//throw new InvalidMessageException("Broken message! Did not recognise message type: " + headerMessageType + ".");
|
||||
|
||||
@@ -94,6 +94,15 @@ public class BinaryMessageEncoder {
|
||||
this.fullMessage = tempFullMessageByteBuffer.array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a binary message from message type and message body.
|
||||
* @param headerMessageType of message
|
||||
* @param messageBody of message
|
||||
*/
|
||||
public BinaryMessageEncoder(MessageType headerMessageType, byte[] messageBody) {
|
||||
this(headerMessageType, System.currentTimeMillis(), 69, (short)messageBody.length, messageBody);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full encoded message. This includes the header, body, and CRC.
|
||||
* @return Full encoded message.
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BoatActionDecoder {
|
||||
byte byteBoatAction;
|
||||
BoatActionEnum boatAction;
|
||||
|
||||
public BoatActionDecoder(byte[] encodedBoatAction) {
|
||||
byteBoatAction = encodedBoatAction[0];
|
||||
|
||||
boatAction = BoatActionEnum.fromByte(byteBoatAction);
|
||||
}
|
||||
|
||||
public BoatActionEnum getBoatAction() {
|
||||
return boatAction;
|
||||
}
|
||||
}
|
||||
@@ -339,4 +339,11 @@ public class RaceVisionByteEncoder {
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public static byte[] boatActionMessage(BoatAction boatAction){
|
||||
ByteBuffer boatActionMessage = ByteBuffer.allocate(1);
|
||||
boatActionMessage.put(intToBytes(boatAction.getBoatAction(), 1));
|
||||
byte [] result = boatActionMessage.array();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package network.Messages;
|
||||
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
import network.Messages.Enums.MessageType;
|
||||
|
||||
/**
|
||||
* Created by David on 10/07/2017.
|
||||
*/
|
||||
public class BoatAction extends AC35Data {
|
||||
|
||||
private byte boatAction;
|
||||
|
||||
public BoatAction(BoatActionEnum boatAction){
|
||||
super(MessageType.BOATACTION);
|
||||
this.boatAction = boatAction.getValue();
|
||||
}
|
||||
|
||||
public byte getBoatAction() {
|
||||
return boatAction;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package network.Messages.Enums;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Boat actions
|
||||
*/
|
||||
public enum BoatActionEnum {
|
||||
NOT_A_STATUS(-1),
|
||||
AUTO_PILOT(1),
|
||||
SAILS_IN(2),
|
||||
SAILS_OUT(3),
|
||||
TACK_GYBE(4),
|
||||
UPWIND(5),
|
||||
DOWNWIND(6);
|
||||
|
||||
private byte value;
|
||||
|
||||
/**
|
||||
* Ctor. Creates a BoatActionEnum from a given primitive integer value, cast to a byte.
|
||||
* @param value Integer, which is cast to byte, to construct from.
|
||||
*/
|
||||
private BoatActionEnum(int value) {
|
||||
this.value = (byte) value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the primitive value of the enum.
|
||||
* @return Primitive value of the enum.
|
||||
*/
|
||||
public byte getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stores a mapping between Byte values and BoatActionEnum values.
|
||||
*/
|
||||
private static final Map<Byte, BoatActionEnum> byteToStatusMap = new HashMap<>();
|
||||
|
||||
|
||||
/*
|
||||
Static initialization block. Initializes the byteToStatusMap.
|
||||
*/
|
||||
static {
|
||||
for (BoatActionEnum type : BoatActionEnum.values()) {
|
||||
BoatActionEnum.byteToStatusMap.put(type.value, type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the enumeration value which corresponds to a given byte value.
|
||||
* @param boatActionEnum Byte value to convert to a BoatActionEnum value.
|
||||
* @return The BoatActionEnum value which corresponds to the given byte value.
|
||||
*/
|
||||
public static BoatActionEnum fromByte(byte boatActionEnum) {
|
||||
//Gets the corresponding MessageType from the map.
|
||||
BoatActionEnum type = BoatActionEnum.byteToStatusMap.get(boatActionEnum);
|
||||
|
||||
if (type == null) {
|
||||
//If the byte value wasn't found, return the NOT_A_STATUS boatActionEnum.
|
||||
return BoatActionEnum.NOT_A_STATUS;
|
||||
} else {
|
||||
//Otherwise, return the boatActionEnum.
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ public enum MessageType {
|
||||
MARKROUNDING(38),
|
||||
COURSEWIND(44),
|
||||
AVGWIND(47),
|
||||
BOATACTION(100),
|
||||
NOTAMESSAGE(0);
|
||||
|
||||
///Primitive value of the enum.
|
||||
|
||||
@@ -81,6 +81,13 @@ public class ConnectionController extends Controller {
|
||||
|
||||
private ObservableList<RaceConnection> connections;
|
||||
|
||||
|
||||
/**
|
||||
* Represents whether the client is currently hosting a game already - this is to ensure they don't launch multiple servers.
|
||||
*/
|
||||
private boolean currentlyHostingGame = false;
|
||||
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
// TODO - replace with config file
|
||||
@@ -91,7 +98,7 @@ public class ConnectionController extends Controller {
|
||||
statusColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());
|
||||
|
||||
connectionTable.getSelectionModel().selectedItemProperty().addListener((obs, prev, curr) -> {
|
||||
if (curr != null && ((RaceConnection)curr).check()) connectButton.setDisable(false);
|
||||
if (curr != null && curr.check()) connectButton.setDisable(false);
|
||||
else connectButton.setDisable(true);
|
||||
});
|
||||
connectButton.setDisable(true);
|
||||
@@ -142,12 +149,17 @@ public class ConnectionController extends Controller {
|
||||
*/
|
||||
public void addLocal() {
|
||||
try {
|
||||
Event game = Event.getEvent();
|
||||
urlField.textProperty().set(game.getAddress());
|
||||
portField.textProperty().set(Integer.toString(game.getPort()));
|
||||
//We don't want to host more than one game.
|
||||
if (!currentlyHostingGame) {
|
||||
Event game = Event.getEvent();
|
||||
urlField.textProperty().set(game.getAddress());
|
||||
portField.textProperty().set(Integer.toString(game.getPort()));
|
||||
|
||||
game.start();
|
||||
addConnection();
|
||||
game.start();
|
||||
addConnection();
|
||||
|
||||
currentlyHostingGame = true;
|
||||
}
|
||||
} catch (InvalidRaceDataException e) {
|
||||
e.printStackTrace();
|
||||
} catch (XMLReaderException e) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import visualiser.app.VisualiserInput;
|
||||
import visualiser.gameController.ControllerClient;
|
||||
import visualiser.model.VisualiserBoat;
|
||||
import visualiser.model.VisualiserRace;
|
||||
|
||||
@@ -38,8 +39,8 @@ public class MainController extends Controller {
|
||||
* @param visualiserInput The object used to read packets from the race server.
|
||||
* @param visualiserRace The object modelling the race.
|
||||
*/
|
||||
public void beginRace(VisualiserInput visualiserInput, VisualiserRace visualiserRace) {
|
||||
raceController.startRace(visualiserInput, visualiserRace);
|
||||
public void beginRace(VisualiserInput visualiserInput, VisualiserRace visualiserRace, ControllerClient controllerClient) {
|
||||
raceController.startRace(visualiserInput, visualiserRace, controllerClient);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,11 +19,13 @@ import javafx.util.Callback;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import shared.model.Leg;
|
||||
import visualiser.app.VisualiserInput;
|
||||
import visualiser.gameController.ControllerClient;
|
||||
import visualiser.gameController.Keys.ControlKey;
|
||||
import visualiser.gameController.Keys.KeyFactory;
|
||||
import visualiser.model.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ResourceBundle;
|
||||
@@ -58,6 +60,11 @@ public class RaceController extends Controller {
|
||||
*/
|
||||
private Sparkline sparkline;
|
||||
|
||||
/**
|
||||
* Service for sending keystrokes to server
|
||||
*/
|
||||
private ControllerClient controllerClient;
|
||||
|
||||
|
||||
@FXML private GridPane canvasBase;
|
||||
@FXML private Pane arrow;
|
||||
@@ -85,12 +92,20 @@ public class RaceController extends Controller {
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
KeyFactory keyFactory = KeyFactory.getFactory();
|
||||
|
||||
// Initialise keyboard handler
|
||||
race.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
|
||||
String codeString = event.getCode().toString();
|
||||
ControlKey controlKey = KeyFactory.getKey(codeString);
|
||||
ControlKey controlKey = keyFactory.getKey(codeString);
|
||||
if(controlKey != null) {
|
||||
System.out.println(controlKey.toString() + " is Pressed.");
|
||||
try {
|
||||
controllerClient.sendKey(controlKey);
|
||||
controlKey.onAction(); // Change key state if applicable
|
||||
event.consume();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -324,10 +339,11 @@ public class RaceController extends Controller {
|
||||
* @param visualiserInput Object used to read packets from server.
|
||||
* @param visualiserRace Object modelling the race.
|
||||
*/
|
||||
public void startRace(VisualiserInput visualiserInput, VisualiserRace visualiserRace) {
|
||||
public void startRace(VisualiserInput visualiserInput, VisualiserRace visualiserRace, ControllerClient controllerClient) {
|
||||
|
||||
this.visualiserInput = visualiserInput;
|
||||
this.visualiserRace = visualiserRace;
|
||||
this.controllerClient = controllerClient;
|
||||
|
||||
initialiseRace();
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import shared.exceptions.InvalidRaceDataException;
|
||||
import shared.exceptions.InvalidRegattaDataException;
|
||||
import shared.exceptions.XMLReaderException;
|
||||
import visualiser.app.VisualiserInput;
|
||||
import visualiser.gameController.ControllerClient;
|
||||
import visualiser.model.VisualiserBoat;
|
||||
import visualiser.model.VisualiserRace;
|
||||
|
||||
@@ -76,6 +77,8 @@ public class StartController extends Controller implements Observer {
|
||||
*/
|
||||
private VisualiserRace visualiserRace;
|
||||
|
||||
private ControllerClient controllerClient;
|
||||
|
||||
/**
|
||||
* An array of colors used to assign colors to each boat - passed in to the VisualiserRace constructor.
|
||||
*/
|
||||
@@ -258,7 +261,7 @@ public class StartController extends Controller implements Observer {
|
||||
startWrapper.setVisible(false);
|
||||
start.setVisible(false);
|
||||
|
||||
parent.beginRace(visualiserInput, visualiserRace);
|
||||
parent.beginRace(visualiserInput, visualiserRace, controllerClient);
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
@@ -308,6 +311,8 @@ public class StartController extends Controller implements Observer {
|
||||
try {
|
||||
//Begin reading packets from the socket/server.
|
||||
this.visualiserInput = new VisualiserInput(socket);
|
||||
//Send controller input to server
|
||||
this.controllerClient = new ControllerClient(socket);
|
||||
//Store a reference to latestMessages so that we can observe it.
|
||||
LatestMessages latestMessages = this.visualiserInput.getLatestMessages();
|
||||
latestMessages.addObserver(this);
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package visualiser.gameController;
|
||||
|
||||
import network.BinaryMessageEncoder;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.BoatAction;
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
import network.Messages.Enums.MessageType;
|
||||
import visualiser.gameController.Keys.ControlKey;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Basic service for sending key presses to game server
|
||||
*/
|
||||
public class ControllerClient {
|
||||
/**
|
||||
* Socket to server
|
||||
*/
|
||||
Socket socket;
|
||||
|
||||
/**
|
||||
* Output stream wrapper for socket to server
|
||||
*/
|
||||
DataOutputStream outputStream;
|
||||
|
||||
/**
|
||||
* Initialise controller client with live socket.
|
||||
* @param socket to server
|
||||
*/
|
||||
public ControllerClient(Socket socket) {
|
||||
this.socket = socket;
|
||||
|
||||
try {
|
||||
this.outputStream = new DataOutputStream(socket.getOutputStream());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a keypress to server
|
||||
* @param key to send
|
||||
* @throws IOException if socket write fails
|
||||
*/
|
||||
public void sendKey(ControlKey key) throws IOException {
|
||||
int protocolCode = key.getProtocolCode();
|
||||
if(protocolCode > -1) {
|
||||
|
||||
byte[] bytes = new byte[4];
|
||||
ByteBuffer.wrap(bytes).putInt(key.getProtocolCode());
|
||||
BoatActionEnum boatActionEnum = BoatActionEnum.fromByte(bytes[3]);
|
||||
|
||||
BoatAction boatAction = new BoatAction(boatActionEnum);
|
||||
|
||||
byte[] encodedBoatAction = RaceVisionByteEncoder.boatActionMessage(boatAction);
|
||||
|
||||
BinaryMessageEncoder binaryMessage = new BinaryMessageEncoder(MessageType.BOATACTION, System.currentTimeMillis(), 0,
|
||||
(short) encodedBoatAction.length, encodedBoatAction);
|
||||
|
||||
System.out.println("Sending out key: " + boatActionEnum);
|
||||
outputStream.write(binaryMessage.getFullMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package visualiser.gameController;
|
||||
|
||||
import network.BinaryMessageDecoder;
|
||||
import network.MessageDecoders.BoatActionDecoder;
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
import visualiser.gameController.Keys.ControlKey;
|
||||
import visualiser.gameController.Keys.KeyFactory;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* Service for dispatching key press data to race from client
|
||||
*/
|
||||
public class ControllerServer implements Runnable {
|
||||
/**
|
||||
* Socket to client
|
||||
*/
|
||||
private Socket socket;
|
||||
/**
|
||||
* Wrapper for input from client
|
||||
*/
|
||||
private DataInputStream inputStream;
|
||||
|
||||
/**
|
||||
* Initialise server-side controller with live client socket
|
||||
* @param socket to client
|
||||
*/
|
||||
public ControllerServer(Socket socket) {
|
||||
this.socket = socket;
|
||||
try {
|
||||
this.inputStream = new DataInputStream(this.socket.getInputStream());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for controller key input from client and loop.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while(true) {
|
||||
byte[] message = new byte[20];
|
||||
try {
|
||||
if (inputStream.available() > 0) {
|
||||
inputStream.read(message);
|
||||
BinaryMessageDecoder encodedMessage = new BinaryMessageDecoder(message);
|
||||
BoatActionDecoder boatActionDecoder = new BoatActionDecoder(encodedMessage.getMessageBody());
|
||||
BoatActionEnum decodedMessage = boatActionDecoder.getBoatAction();
|
||||
System.out.println("Received key: " + decodedMessage);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,10 +20,12 @@ public class InputChecker {
|
||||
* @param scene Scene the controller is to run in parallel with.
|
||||
*/
|
||||
public void runWithScene(Scene scene){
|
||||
KeyFactory keyFactory = KeyFactory.getFactory();
|
||||
|
||||
scene.setOnKeyPressed(event -> {
|
||||
String codeString = event.getCode().toString();
|
||||
if (!currentlyActiveKeys.containsKey(codeString)) {
|
||||
ControlKey controlKey = KeyFactory.getKey(codeString);
|
||||
ControlKey controlKey = keyFactory.getKey(codeString);
|
||||
if (controlKey != null) {
|
||||
controlKey.onAction();
|
||||
System.out.println(controlKey.toString() + " is Pressed.");
|
||||
@@ -34,7 +36,7 @@ public class InputChecker {
|
||||
|
||||
scene.setOnKeyReleased(event -> {
|
||||
String codeString = event.getCode().toString();
|
||||
ControlKey controlKey = KeyFactory.getKey(codeString);
|
||||
ControlKey controlKey = keyFactory.getKey(codeString);
|
||||
if (controlKey != null) {
|
||||
controlKey.onRelease();
|
||||
System.out.println(controlKey.toString() + " is Released.");
|
||||
@@ -46,7 +48,7 @@ public class InputChecker {
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
for (String key: currentlyActiveKeys.keySet()){
|
||||
ControlKey controlKey = KeyFactory.getKey(key);
|
||||
ControlKey controlKey = keyFactory.getKey(key);
|
||||
if (controlKey != null){
|
||||
controlKey.onHold();
|
||||
System.out.println(controlKey.toString() + " is Held.");
|
||||
|
||||
@@ -8,16 +8,29 @@ import javafx.scene.input.KeyCode;
|
||||
public abstract class ControlKey {
|
||||
|
||||
private String name;
|
||||
private KeyCode keyCode;
|
||||
protected int protocolCode;
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
* @param name name of the key
|
||||
* @param keyCode key code for the key
|
||||
* Constructor for key state with specified protocol code
|
||||
* @param name of action
|
||||
* @param protocolCode -1 if not sent
|
||||
*/
|
||||
public ControlKey(String name, KeyCode keyCode){
|
||||
public ControlKey(String name, int protocolCode) {
|
||||
this.name = name;
|
||||
this.keyCode = keyCode;
|
||||
this.protocolCode = protocolCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for key state not sent over network
|
||||
* @param name name of the key
|
||||
*/
|
||||
public ControlKey(String name){
|
||||
this.name = name;
|
||||
this.protocolCode = -1;
|
||||
}
|
||||
|
||||
public int getProtocolCode() {
|
||||
return protocolCode;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key to send downwind packet to server
|
||||
*/
|
||||
@@ -9,12 +7,11 @@ public class DownWindKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
super(name, keyCode);
|
||||
public DownWindKey(String name) {
|
||||
super(name, 6);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,36 +1,51 @@
|
||||
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.
|
||||
*/
|
||||
public class KeyFactory {
|
||||
/**
|
||||
* Retrieve command given key
|
||||
*/
|
||||
private Map<String, ControlKey> keyState;
|
||||
|
||||
/**
|
||||
* Get the Control Key incharge of a key press
|
||||
* 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
|
||||
* @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;
|
||||
}
|
||||
public ControlKey getKey(String key){
|
||||
return keyState.get(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key to toggle the sails
|
||||
*/
|
||||
@@ -9,17 +7,19 @@ public class SailsToggleKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
super(name, keyCode);
|
||||
public SailsToggleKey(String name) {
|
||||
super(name, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle command associated with sails key
|
||||
*/
|
||||
@Override
|
||||
public void onAction() {
|
||||
|
||||
protocolCode = protocolCode == 2? 3 : 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* key to toggle between tacking and gybing
|
||||
*/
|
||||
@@ -9,12 +7,11 @@ public class TackGybeKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
super(name, keyCode);
|
||||
public TackGybeKey(String name) {
|
||||
super(name, 4);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key to go upwind
|
||||
*/
|
||||
@@ -9,12 +7,11 @@ public class UpWindKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
super(name, keyCode);
|
||||
public UpWindKey(String name) {
|
||||
super(name, 5);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -11,10 +11,9 @@ 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);
|
||||
public VMGKey(String name) {
|
||||
super(name, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,8 +7,8 @@ import javafx.scene.input.KeyCode;
|
||||
*/
|
||||
public class ZoomInKey extends ControlKey {
|
||||
|
||||
public ZoomInKey(String name, KeyCode keyCode) {
|
||||
super(name, keyCode);
|
||||
public ZoomInKey(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key to zoom out of the game.
|
||||
*/
|
||||
@@ -9,12 +7,11 @@ public class ZoomOutKey extends ControlKey{
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
super(name, keyCode);
|
||||
public ZoomOutKey(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user