Keybinding now works in the actual race. A map of keybind is shared between GameClient and KeyBindDialogController.

#story[1278]
This commit is contained in:
Zhi You Tan
2017-09-20 11:02:13 +12:00
parent a1933c2869
commit d4d7ddf8e2
4 changed files with 121 additions and 38 deletions
@@ -0,0 +1,35 @@
package seng302.model;
import java.util.HashMap;
import java.util.Map;
public enum GameClientAction {
ZOOM_IN(1),
ZOOM_OUT(2),
VMG(3),
SAILS_STATE(4),
TACK_GYBE(5),
UPWIND(6),
DOWNWIND(7);
private final int type;
private static final Map<Integer, GameClientAction> intToTypeMap = new HashMap<>();
static {
for (GameClientAction type : GameClientAction.values()) {
intToTypeMap.put(type.getValue(), type);
}
}
GameClientAction(int type) {
this.type = type;
}
public static GameClientAction getType(int value) {
return intToTypeMap.get(value);
}
public int getValue() {
return this.type;
}
}
@@ -7,6 +7,7 @@ import java.time.ZoneOffset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
@@ -28,6 +29,7 @@ import seng302.gameServer.messages.BoatAction;
import seng302.gameServer.messages.BoatStatus; import seng302.gameServer.messages.BoatStatus;
import seng302.gameServer.messages.YachtEventType; import seng302.gameServer.messages.YachtEventType;
import seng302.model.ClientYacht; import seng302.model.ClientYacht;
import seng302.model.GameClientAction;
import seng302.model.RaceState; import seng302.model.RaceState;
import seng302.model.stream.packets.StreamPacket; import seng302.model.stream.packets.StreamPacket;
import seng302.model.stream.parser.MarkRoundingData; import seng302.model.stream.parser.MarkRoundingData;
@@ -65,6 +67,7 @@ public class GameClient {
private RaceViewController raceViewController; private RaceViewController raceViewController;
private ArrayList<ClientYacht> finishedBoats = new ArrayList<>(); private ArrayList<ClientYacht> finishedBoats = new ArrayList<>();
private Map<GameClientAction, KeyCode> keyBind = new HashMap<>();
private ObservableList<String> clientLobbyList = FXCollections.observableArrayList(); private ObservableList<String> clientLobbyList = FXCollections.observableArrayList();
@@ -75,6 +78,21 @@ public class GameClient {
*/ */
public GameClient(Pane holder) { public GameClient(Pane holder) {
this.holderPane = holder; this.holderPane = holder;
initialiseKeyBind();
}
/**
* Initialise default keybind.
*/
private void initialiseKeyBind() {
keyBind = new HashMap<>();
keyBind.put(GameClientAction.ZOOM_IN, KeyCode.Z);
keyBind.put(GameClientAction.ZOOM_OUT, KeyCode.X);
keyBind.put(GameClientAction.VMG, KeyCode.SPACE);
keyBind.put(GameClientAction.SAILS_STATE, KeyCode.SHIFT);
keyBind.put(GameClientAction.TACK_GYBE, KeyCode.ENTER);
keyBind.put(GameClientAction.UPWIND, KeyCode.PAGE_UP);
keyBind.put(GameClientAction.DOWNWIND, KeyCode.PAGE_DOWN);
} }
/** /**
@@ -373,16 +391,16 @@ public class GameClient {
} }
return; return;
} }
switch (e.getCode()) {
case SPACE: // align with vmg if (keyBind.get(GameClientAction.VMG) == e.getCode()) { // align with vmg
socketThread.sendBoatAction(BoatAction.VMG); break; socketThread.sendBoatAction(BoatAction.VMG);
case PAGE_UP: // upwind } else if (keyBind.get(GameClientAction.UPWIND) == e.getCode()) { // upwind
socketThread.sendBoatAction(BoatAction.UPWIND); break; socketThread.sendBoatAction(BoatAction.UPWIND);
case PAGE_DOWN: // downwind } else if (keyBind.get(GameClientAction.DOWNWIND) == e.getCode()) { // downwind
socketThread.sendBoatAction(BoatAction.DOWNWIND); break; socketThread.sendBoatAction(BoatAction.DOWNWIND);
case ENTER: // tack/gybe } else if (keyBind.get(GameClientAction.TACK_GYBE) == e.getCode()) { // tack/gybe
// if chat box is active take whatever is in there and send it to server // if chat box is active take whatever is in there and send it to server
socketThread.sendBoatAction(BoatAction.TACK_GYBE); break; socketThread.sendBoatAction(BoatAction.TACK_GYBE);
} }
} }
@@ -391,15 +409,13 @@ public class GameClient {
if (raceView.isChatInputFocused()) { if (raceView.isChatInputFocused()) {
return; return;
} }
switch (e.getCode()) {
//TODO 12/07/17 Determine the sail state and send the appropriate packet (eg. if sails are in, send a sail out packet) if (keyBind.get(GameClientAction.SAILS_STATE) == e.getCode()) { // sails in/sails out
case SHIFT: // sails in/sails out
socketThread.sendBoatAction(BoatAction.SAILS_IN); socketThread.sendBoatAction(BoatAction.SAILS_IN);
allBoatsMap.get(socketThread.getClientId()).toggleSail(); allBoatsMap.get(socketThread.getClientId()).toggleSail();
break; } else if (keyBind.get(GameClientAction.UPWIND) == e.getCode()
case PAGE_UP: || keyBind.get(GameClientAction.DOWNWIND) == e.getCode()) {
case PAGE_DOWN: socketThread.sendBoatAction(BoatAction.MAINTAIN_HEADING);
socketThread.sendBoatAction(BoatAction.MAINTAIN_HEADING); break;
} }
} }
@@ -456,4 +472,8 @@ public class GameClient {
public Map<Integer, ClientYacht> getAllBoatsMap() { public Map<Integer, ClientYacht> getAllBoatsMap() {
return allBoatsMap; return allBoatsMap;
} }
public Map<GameClientAction, KeyCode> getKeyBind() {
return keyBind;
}
} }
@@ -26,6 +26,7 @@ import seng302.gameServer.ServerAdvertiser;
import seng302.utilities.BonjourInstallChecker; import seng302.utilities.BonjourInstallChecker;
import seng302.utilities.Sounds; import seng302.utilities.Sounds;
import seng302.visualiser.GameClient; import seng302.visualiser.GameClient;
import seng302.visualiser.controllers.dialogs.KeyBindingDialogController;
public class ViewManager { public class ViewManager {
@@ -207,6 +208,9 @@ public class ViewManager {
JFXDialog dialog = new JFXDialog((StackPane) node, JFXDialog dialog = new JFXDialog((StackPane) node,
dialogContent.load(), dialogContent.load(),
DialogTransition.CENTER); DialogTransition.CENTER);
KeyBindingDialogController keyBindingDialogController = dialogContent
.getController();
keyBindingDialogController.init(gameClient.getKeyBind());
dialog.show(); dialog.show();
Sounds.playButtonClick(); Sounds.playButtonClick();
} }
@@ -9,6 +9,7 @@ import javafx.scene.input.KeyEvent;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
import seng302.model.GameClientAction;
public class KeyBindingDialogController implements Initializable { public class KeyBindingDialogController implements Initializable {
@@ -31,11 +32,25 @@ public class KeyBindingDialogController implements Initializable {
private JFXButton downwindBtn; private JFXButton downwindBtn;
//---------FXML END---------// //---------FXML END---------//
private static Map<JFXButton, KeyCode> keys; // static button and saved keybinding pair private Map<JFXButton, KeyCode> keys;
private List<JFXButton> buttons = new ArrayList<>(); private List<JFXButton> buttons = new ArrayList<>();
private Map<GameClientAction, KeyCode> keyBind;
private LinkedHashMap<JFXButton, GameClientAction> buttonAndGameClientActionMap;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
}
/**
* HAOMING HELP!!! CHANGE FUNCTION NAME PLS :))
*
* Takes in a map from GameClient and initialise button mapping to the keys.
*
* @param keyBind a map with GameClientAction and KeyCode pair to be used in GameClient.
*/
public void init(Map<GameClientAction, KeyCode> keyBind) {
this.keyBind = keyBind;
buttons = new ArrayList<>(); buttons = new ArrayList<>();
Collections Collections
.addAll(buttons, zoomInbtn, zoomOutBtn, vmgBtn, sailInOutBtn, tackGybeBtn, upwindBtn, .addAll(buttons, zoomInbtn, zoomOutBtn, vmgBtn, sailInOutBtn, tackGybeBtn, upwindBtn,
@@ -50,17 +65,24 @@ public class KeyBindingDialogController implements Initializable {
* to the new button which is created when javafx reinitialise a new controller. * to the new button which is created when javafx reinitialise a new controller.
*/ */
private void initializeKeys() { private void initializeKeys() {
if (keys == null) { initButtonAndGameClientActionMap();
initializeDefaultKeys(); initializeDefaultKeys();
} else {
Map<JFXButton, KeyCode> tempKey = new LinkedHashMap<>();
int buttonIndex = 0;
for (JFXButton jfxButton : keys.keySet()) {
tempKey.put(buttons.get(buttonIndex), keys.get(jfxButton));
buttonIndex++;
}
keys = tempKey;
} }
/**
* HAOMING CHANGE FUNCTION NAME HERE TOO :) OR BETTER, REMOVE THIS FUNCTION
*
* Link buttons and the GameClientAction to be used in accessing keys.
*/
private void initButtonAndGameClientActionMap() {
buttonAndGameClientActionMap = new LinkedHashMap<>();
buttonAndGameClientActionMap.put(zoomInbtn, GameClientAction.ZOOM_IN);
buttonAndGameClientActionMap.put(zoomOutBtn, GameClientAction.ZOOM_OUT);
buttonAndGameClientActionMap.put(vmgBtn, GameClientAction.VMG);
buttonAndGameClientActionMap.put(sailInOutBtn, GameClientAction.SAILS_STATE);
buttonAndGameClientActionMap.put(tackGybeBtn, GameClientAction.TACK_GYBE);
buttonAndGameClientActionMap.put(upwindBtn, GameClientAction.UPWIND);
buttonAndGameClientActionMap.put(downwindBtn, GameClientAction.DOWNWIND);
} }
/** /**
@@ -68,13 +90,13 @@ public class KeyBindingDialogController implements Initializable {
*/ */
private void initializeDefaultKeys() { private void initializeDefaultKeys() {
keys = new LinkedHashMap<>(); keys = new LinkedHashMap<>();
keys.put(zoomInbtn, KeyCode.Z); keys.put(zoomInbtn, keyBind.get(GameClientAction.ZOOM_IN));
keys.put(zoomOutBtn, KeyCode.X); keys.put(zoomOutBtn, keyBind.get(GameClientAction.ZOOM_OUT));
keys.put(vmgBtn, KeyCode.SPACE); keys.put(vmgBtn, keyBind.get(GameClientAction.VMG));
keys.put(sailInOutBtn, KeyCode.SHIFT); keys.put(sailInOutBtn, keyBind.get(GameClientAction.SAILS_STATE));
keys.put(tackGybeBtn, KeyCode.ENTER); keys.put(tackGybeBtn, keyBind.get(GameClientAction.TACK_GYBE));
keys.put(upwindBtn, KeyCode.PAGE_UP); keys.put(upwindBtn, keyBind.get(GameClientAction.UPWIND));
keys.put(downwindBtn, KeyCode.PAGE_DOWN); keys.put(downwindBtn, keyBind.get(GameClientAction.DOWNWIND));
} }
/** /**
@@ -106,9 +128,11 @@ public class KeyBindingDialogController implements Initializable {
private void doSomething(KeyEvent event, JFXButton button) { private void doSomething(KeyEvent event, JFXButton button) {
if (keys.containsValue(event.getCode())) { if (keys.containsValue(event.getCode())) {
keys.replace(button, null); keys.replace(button, null);
keyBind.replace(buttonAndGameClientActionMap.get(button), null);
button.setText(""); button.setText("");
} else { } else {
keys.replace(button, event.getCode()); keys.replace(button, event.getCode());
keyBind.replace(buttonAndGameClientActionMap.get(button), event.getCode());
button.setText(event.getCode().getName()); button.setText(event.getCode().getName());
} }
} }