Slightly optimised code style and add more functionality

- optimised UI
- check conflicts when change key bind if the key has already been in use
- abstract keybind as a separate singleton class so all class can access it
- [WIP] turning mode is need to be finished

#story[1245]
This commit is contained in:
Haoming Yin
2017-09-23 17:52:48 +12:00
parent 957821f1f2
commit 4011295b8b
7 changed files with 200 additions and 188 deletions
@@ -0,0 +1,78 @@
package seng302.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javafx.scene.input.KeyCode;
public class GameKeyBind {
private static GameKeyBind instance;
private Map<KeyCode, KeyAction> keyToActionMap;
private Map<KeyAction, KeyCode> actionToKeyMap;
private boolean continuouslyTurning;
private GameKeyBind() {
setToDefault();
}
public void setToDefault() {
actionToKeyMap = new HashMap<>();
keyToActionMap = new HashMap<>();
continuouslyTurning = false;
// default key bindings
ArrayList<KeyCode> keys = new ArrayList<>();
keys.add(KeyCode.Z);
keys.add(KeyCode.X);
keys.add(KeyCode.SPACE);
keys.add(KeyCode.SHIFT);
keys.add(KeyCode.ENTER);
keys.add(KeyCode.PAGE_UP);
keys.add(KeyCode.PAGE_DOWN);
for (int i = 0; i < 7; i++) {
actionToKeyMap.put(KeyAction.getType(i + 1), keys.get(i));
keyToActionMap.put(keys.get(i), KeyAction.getType(i + 1));
}
}
public static GameKeyBind getInstance() {
if (instance == null) {
instance = new GameKeyBind();
}
return instance;
}
public KeyCode getKeyCode(KeyAction keyAction) {
return instance.actionToKeyMap.get(keyAction);
}
/**
* Binds a key to a key action
*
* @return true if successfully bind
*/
public boolean bindKeyToAction(KeyCode keyCode, KeyAction keyAction) {
if (instance.keyToActionMap.containsKey(keyCode)) {
// if the key has been bound to other action, return false
return false;
} else {
instance.keyToActionMap.put(keyCode, keyAction); // add key -> action
KeyCode oldKeyCode = instance.actionToKeyMap
.get(keyAction); // get old key for the action
instance.keyToActionMap.remove(oldKeyCode); // remove the old key -> action
instance.actionToKeyMap
.replace(keyAction, keyCode); // replace the old key by the newer one
return true;
}
}
public void toggleTurningMode() {
continuouslyTurning = !continuouslyTurning;
}
public boolean isContinuouslyTurning() {
return continuouslyTurning;
}
}
@@ -3,7 +3,7 @@ package seng302.model;
import java.util.HashMap;
import java.util.Map;
public enum GameClientAction {
public enum KeyAction {
ZOOM_IN(1),
ZOOM_OUT(2),
VMG(3),
@@ -13,19 +13,19 @@ public enum GameClientAction {
DOWNWIND(7);
private final int type;
private static final Map<Integer, GameClientAction> intToTypeMap = new HashMap<>();
private static final Map<Integer, KeyAction> intToTypeMap = new HashMap<>();
static {
for (GameClientAction type : GameClientAction.values()) {
for (KeyAction type : KeyAction.values()) {
intToTypeMap.put(type.getValue(), type);
}
}
GameClientAction(int type) {
KeyAction(int type) {
this.type = type;
}
public static GameClientAction getType(int value) {
public static KeyAction getType(int value) {
return intToTypeMap.get(value);
}