diff --git a/engine/src/main/java/org/destinationsol/GameOptions.java b/engine/src/main/java/org/destinationsol/GameOptions.java index d8493f5f8..5a7a61e6b 100644 --- a/engine/src/main/java/org/destinationsol/GameOptions.java +++ b/engine/src/main/java/org/destinationsol/GameOptions.java @@ -18,9 +18,18 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.google.common.base.Enums; +import org.destinationsol.input.DefaultControls; +import org.destinationsol.input.InputControls; import org.destinationsol.menu.Resolution; import org.destinationsol.menu.ResolutionProvider; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + import static java.util.Arrays.asList; public class GameOptions { @@ -96,29 +105,6 @@ public Volume advance() { } public static final String FILE_NAME = "settings.ini"; - public static final String DEFAULT_MOUSE_UP = "W"; - public static final String DEFAULT_MOUSE_DOWN = "S"; - public static final String DEFAULT_UP = "Up"; - public static final String DEFAULT_DOWN = "Down"; - public static final String DEFAULT_LEFT = "Left"; - public static final String DEFAULT_RIGHT = "Right"; - public static final String DEFAULT_SHOOT = "Space"; - public static final String DEFAULT_SHOOT2 = "L-Ctrl"; - public static final String DEFAULT_ABILITY = "L-Shift"; - public static final String DEFAULT_ESCAPE = "Escape"; - public static final String DEFAULT_MAP = "Tab"; - public static final String DEFAULT_INVENTORY = "I"; - public static final String DEFAULT_TALK = "T"; - public static final String DEFAULT_PAUSE = "P"; - public static final String DEFAULT_DROP = "D"; - public static final String DEFAULT_SELL = "S"; - public static final String DEFAULT_BUY = "B"; - public static final String DEFAULT_CHANGE_SHIP = "C"; - public static final String DEFAULT_HIRE_SHIP = "H"; - public static final String DEFAULT_MERCENARY_INTERACTION = "M"; - public static final String DEFAULT_FREE_CAMERA_MOVEMENT = "V"; - public static final String DEFAULT_ZOOM_IN = "Page Up"; - public static final String DEFAULT_ZOOM_OUT = "Page Down"; public static final int DEFAULT_AXIS_SHOOT = 1; public static final int DEFAULT_AXIS_SHOOT2 = 0; public static final int DEFAULT_AXIS_ABILITY = -1; @@ -144,29 +130,6 @@ public Volume advance() { public Volume sfxVolume; public Volume musicVolume; public boolean canSellEquippedItems; - private String keyUpMouseName; - private String keyDownMouseName; - private String keyUpName; - private String keyDownName; - private String keyLeftName; - private String keyRightName; - private String keyShootName; - private String keyShoot2Name; - private String keyAbilityName; - private String keyEscapeName; - private String keyMapName; - private String keyInventoryName; - private String keyTalkName; - private String keyPauseName; - private String keyDropName; - private String keySellMenuName; - private String keyBuyMenuName; - private String keyChangeShipMenuName; - private String keyHireShipMenuName; - private String keyMercenaryInteractionName; - private String keyFreeCameraMovementName; - private String keyZoomInName; - private String keyZoomOutName; private int controllerAxisShoot; private int controllerAxisShoot2; private int controllerAxisAbility; @@ -183,41 +146,29 @@ public Volume advance() { private int controllerButtonDown; private int mapScrollSpeed; public float nuiUiScale; + private Map controls; private ResolutionProvider resolutionProvider; public GameOptions(boolean mobile, SolFileReader solFileReader) { + controls = new HashMap<>(); IniReader reader = new IniReader(FILE_NAME, solFileReader); + for (DefaultControls control : DefaultControls.values()) { + String controlValue = reader.getString(control.getControlName(), ""); + if (controlValue.isEmpty()) { + controls.put(control, control.getDefaultInputs()); + } else { + controls.put(control, parseKeyboardControl(controlValue)); + } + } + x = reader.getInt("x", 1366); y = reader.getInt("y", 768); fullscreen = reader.getBoolean("fullscreen", false); controlType = mobile ? ControlType.KEYBOARD : Enums.getIfPresent(ControlType.class, reader.getString("controlType", "MIXED")).or(ControlType.MIXED); sfxVolume = Enums.getIfPresent(Volume.class, reader.getString("sfxVolume", "MAX")).or(Volume.MAX); musicVolume = Enums.getIfPresent(Volume.class, reader.getString("musicVolume", "MAX")).or(Volume.MAX); - keyUpMouseName = reader.getString("keyUpMouse", DEFAULT_MOUSE_UP); - keyDownMouseName = reader.getString("keyDownMouse", DEFAULT_MOUSE_DOWN); - keyUpName = reader.getString("keyUp", DEFAULT_UP); - keyDownName = reader.getString("keyDown", DEFAULT_DOWN); - keyLeftName = reader.getString("keyLeft", DEFAULT_LEFT); - keyRightName = reader.getString("keyRight", DEFAULT_RIGHT); - keyShootName = reader.getString("keyShoot", DEFAULT_SHOOT); - keyShoot2Name = reader.getString("keyShoot2", DEFAULT_SHOOT2); - keyAbilityName = reader.getString("keyAbility", DEFAULT_ABILITY); - keyEscapeName = reader.getString("keyEscape", DEFAULT_ESCAPE); - keyMapName = reader.getString("keyMap", DEFAULT_MAP); - keyInventoryName = reader.getString("keyInventory", DEFAULT_INVENTORY); - keyTalkName = reader.getString("keyTalk", DEFAULT_TALK); - keyPauseName = reader.getString("keyPause", DEFAULT_PAUSE); - keyDropName = reader.getString("keyDrop", DEFAULT_DROP); - keySellMenuName = reader.getString("keySellMenu", DEFAULT_SELL); - keyBuyMenuName = reader.getString("keyBuyMenu", DEFAULT_BUY); - keyChangeShipMenuName = reader.getString("keyChangeShipMenu", DEFAULT_CHANGE_SHIP); - keyHireShipMenuName = reader.getString("keyHireShipMenu", DEFAULT_HIRE_SHIP); - keyMercenaryInteractionName = reader.getString("keyMercenaryInteraction", DEFAULT_MERCENARY_INTERACTION); - keyFreeCameraMovementName = reader.getString("keyFreeCameraMovement", DEFAULT_FREE_CAMERA_MOVEMENT); - keyZoomInName = reader.getString("keyZoomIn", DEFAULT_ZOOM_IN); - keyZoomOutName = reader.getString("keyZoomOut", DEFAULT_ZOOM_OUT); controllerAxisShoot = reader.getInt("controllerAxisShoot", DEFAULT_AXIS_SHOOT); controllerAxisShoot2 = reader.getInt("controllerAxisShoot2", DEFAULT_AXIS_SHOOT2); controllerAxisAbility = reader.getInt("controllerAxisAbility", DEFAULT_AXIS_ABILITY); @@ -237,6 +188,15 @@ public GameOptions(boolean mobile, SolFileReader solFileReader) { nuiUiScale = reader.getFloat("nuiUiScale", DEFAULT_NUI_UI_SCALE); } + private int[] parseKeyboardControl(String controlString) { + String[] inputNames = controlString.split(","); + int[] inputKeys = new int[inputNames.length]; + for (int inputNo = 0; inputNo < inputNames.length; inputNo++) { + inputKeys[inputNo] = Input.Keys.valueOf(inputNames[inputNo]); + } + return inputKeys; + } + public void advanceResolution() { //lazy initialize provider because graphics is not available at the constructor if (resolutionProvider == null) { @@ -290,14 +250,9 @@ public void advanceNuiUiScale() { * Save the configuration settings to file. */ public void save() { - IniReader.write(FILE_NAME, "x", x, "y", y, "fullscreen", fullscreen, "controlType", controlType, + List iniValues = new ArrayList<>(); + Collections.addAll(iniValues, "x", x, "y", y, "fullscreen", fullscreen, "controlType", controlType, "sfxVolume", sfxVolume, "musicVolume", musicVolume, "canSellEquippedItems", canSellEquippedItems, - "keyUpMouse", getKeyUpMouseName(), "keyDownMouse", getKeyDownMouseName(), "keyUp", getKeyUpName(), "keyDown", keyDownName, - "keyLeft", keyLeftName, "keyRight", keyRightName, "keyShoot", keyShootName, "keyShoot2", getKeyShoot2Name(), - "keyAbility", getKeyAbilityName(), "keyEscape", getKeyEscapeName(), "keyMap", keyMapName, "keyInventory", keyInventoryName, - "keyTalk", getKeyTalkName(), "keyPause", getKeyPauseName(), "keyDrop", getKeyDropName(), "keySellMenu", getKeySellMenuName(), - "keyBuyMenu", getKeyBuyMenuName(), "keyChangeShipMenu", getKeyChangeShipMenuName(), "keyHireShipMenu", getKeyHireShipMenuName(), - "keyZoomIn", getKeyZoomInName(), "keyZoomOut", getKeyZoomOutName(), "controllerAxisShoot", getControllerAxisShoot(), "controllerAxisShoot2", getControllerAxisShoot2(), "controllerAxisAbility", getControllerAxisAbility(), "controllerAxisLeftRight", getControllerAxisLeftRight(), "isControllerAxisLeftRightInverted", isControllerAxisLeftRightInverted(), "controllerAxisUpDown", getControllerAxisUpDown(), @@ -306,6 +261,22 @@ public void save() { "controllerButtonLeft", getControllerButtonLeft(), "controllerButtonRight", getControllerButtonRight(), "controllerButtonUp", getControllerButtonUp(), "controllerButtonDown", getControllerButtonDown(), "mapScrollSpeed", getMapScrollSpeed(), "nuiUiScale", getNuiUiScale()); + + StringBuilder inputsStringBuilder = new StringBuilder(); + for (Map.Entry control : controls.entrySet()) { + iniValues.add(control.getKey().getControlName()); + + int[] inputs = control.getValue(); + for (int inputNo = 0; inputNo < inputs.length - 1; inputNo++) { + inputsStringBuilder.append(Input.Keys.toString(inputs[inputNo])); + inputsStringBuilder.append(","); + } + inputsStringBuilder.append(Input.Keys.toString(inputs[inputs.length - 1])); + iniValues.add(inputsStringBuilder.toString()); + + inputsStringBuilder.delete(0, inputsStringBuilder.length()); + } + IniReader.write(FILE_NAME, iniValues.toArray(new Object[0])); } /** @@ -314,7 +285,7 @@ public void save() { * @return int The keycode as defined in Input.Keys */ public int getKeyUpMouse() { - return Input.Keys.valueOf(getKeyUpMouseName()); + return getControl(DefaultControls.MOUSE_UP)[0]; } /** @@ -324,7 +295,7 @@ public int getKeyUpMouse() { * @return int The keycode as defined in Input.Keys */ public int getKeyDownMouse() { - return Input.Keys.valueOf(getKeyDownMouseName()); + return getControl(DefaultControls.MOUSE_DOWN)[0]; } /** @@ -334,11 +305,11 @@ public int getKeyDownMouse() { * @return String The readable name as defined in Input.Keys */ public String getKeyUpMouseName() { - return keyUpMouseName; + return Input.Keys.toString(getKeyUpMouse()); } public void setKeyUpMouseName(String keyUpMouseName) { - this.keyUpMouseName = keyUpMouseName; + setControl(DefaultControls.MOUSE_UP, new int[] { Input.Keys.valueOf(keyUpMouseName) }); } /** @@ -348,11 +319,11 @@ public void setKeyUpMouseName(String keyUpMouseName) { * @return String The readable name as defined in Input.Keys */ public String getKeyDownMouseName() { - return keyDownMouseName; + return Input.Keys.toString(getKeyDownMouse()); } public void setKeyDownMouseName(String keyDownMouseName) { - this.keyDownMouseName = keyDownMouseName; + setControl(DefaultControls.MOUSE_DOWN, new int[] { Input.Keys.valueOf(keyDownMouseName) }); } /** @@ -361,7 +332,7 @@ public void setKeyDownMouseName(String keyDownMouseName) { * @return int The keycode as defined in Input.Keys */ public int getKeyUp() { - return Input.Keys.valueOf(getKeyUpName()); + return getControl(DefaultControls.UP)[0]; } /** @@ -371,11 +342,11 @@ public int getKeyUp() { * @return String The readable name as defined in Input.Keys */ public String getKeyUpName() { - return keyUpName; + return Input.Keys.toString(getKeyUp()); } public void setKeyUpName(String keyUpName) { - this.keyUpName = keyUpName; + setControl(DefaultControls.UP, new int[] { Input.Keys.valueOf(keyUpName) }); } /** @@ -384,7 +355,7 @@ public void setKeyUpName(String keyUpName) { * @return int The keycode as defined in Input.Keys */ public int getKeyDown() { - return Input.Keys.valueOf(keyDownName); + return getControl(DefaultControls.DOWN)[0]; } /** @@ -393,11 +364,11 @@ public int getKeyDown() { * @return String The readable name as defined in Input.Keys */ public String getKeyDownName() { - return keyDownName; + return Input.Keys.toString(getKeyDown()); } public void setKeyDownName(String keyDownName) { - this.keyDownName = keyDownName; + setControl(DefaultControls.DOWN, new int[] { Input.Keys.valueOf(keyDownName) }); } /** @@ -406,7 +377,7 @@ public void setKeyDownName(String keyDownName) { * @return int The keycode as defined in Input.Keys */ public int getKeyLeft() { - return Input.Keys.valueOf(keyLeftName); + return getControl(DefaultControls.LEFT)[0]; } /** @@ -415,11 +386,11 @@ public int getKeyLeft() { * @return String The readable name as defined in Input.Keys */ public String getKeyLeftName() { - return keyLeftName; + return Input.Keys.toString(getKeyLeft()); } public void setKeyLeftName(String keyLeftName) { - this.keyLeftName = keyLeftName; + setControl(DefaultControls.LEFT, new int[] { Input.Keys.valueOf(keyLeftName) }); } /** @@ -428,7 +399,7 @@ public void setKeyLeftName(String keyLeftName) { * @return int The keycode as defined in Input.Keys */ public int getKeyRight() { - return Input.Keys.valueOf(keyRightName); + return getControl(DefaultControls.RIGHT)[0]; } /** @@ -437,11 +408,11 @@ public int getKeyRight() { * @return String The readable name as defined in Input.Keys */ public String getKeyRightName() { - return keyRightName; + return Input.Keys.toString(getKeyRight()); } public void setKeyRightName(String keyRightName) { - this.keyRightName = keyRightName; + setControl(DefaultControls.RIGHT, new int[] { Input.Keys.valueOf(keyRightName) }); } /** @@ -450,7 +421,7 @@ public void setKeyRightName(String keyRightName) { * @return int The keycode as defined in Input.Keys */ public int getKeyShoot() { - return Input.Keys.valueOf(keyShootName); + return getControl(DefaultControls.SHOOT)[0]; } /** @@ -459,11 +430,11 @@ public int getKeyShoot() { * @return String The readable name as defined in Input.Keys */ public String getKeyShootName() { - return keyShootName; + return Input.Keys.toString(getKeyShoot()); } public void setKeyShootName(String keyShootName) { - this.keyShootName = keyShootName; + setControl(DefaultControls.SHOOT, new int[] { Input.Keys.valueOf(keyShootName) }); } /** @@ -472,7 +443,7 @@ public void setKeyShootName(String keyShootName) { * @return int The keycode as defined in Input.Keys */ public int getKeyShoot2() { - return Input.Keys.valueOf(getKeyShoot2Name()); + return getControl(DefaultControls.SHOOT2)[0]; } /** @@ -481,11 +452,11 @@ public int getKeyShoot2() { * @return String The readable name as defined in Input.Keys */ public String getKeyShoot2Name() { - return keyShoot2Name; + return Input.Keys.toString(getKeyShoot2()); } public void setKeyShoot2Name(String keyShoot2Name) { - this.keyShoot2Name = keyShoot2Name; + setControl(DefaultControls.SHOOT2, new int[] { Input.Keys.valueOf(keyShoot2Name) }); } /** @@ -495,7 +466,7 @@ public void setKeyShoot2Name(String keyShoot2Name) { * @return int The keycode as defined in Input.Keys */ public int getKeyEquip() { - return Input.Keys.valueOf(keyShootName); + return getKeyShoot(); } /** @@ -524,7 +495,7 @@ public int getKeyEquip2() { * @return int The keycode as defined in Input.Keys */ public int getKeyAbility() { - return Input.Keys.valueOf(getKeyAbilityName()); + return getControl(DefaultControls.ABILITY)[0]; } /** @@ -533,11 +504,11 @@ public int getKeyAbility() { * @return String The readable name as defined in Input.Keys */ public String getKeyAbilityName() { - return keyAbilityName; + return Input.Keys.toString(getKeyAbility()); } public void setKeyAbilityName(String keyAbilityName) { - this.keyAbilityName = keyAbilityName; + setControl(DefaultControls.ABILITY, new int[] { Input.Keys.valueOf(keyAbilityName) }); } /** @@ -546,7 +517,7 @@ public void setKeyAbilityName(String keyAbilityName) { * @return int The keycode as defined in Input.Keys */ public int getKeyEscape() { - return Input.Keys.valueOf(getKeyEscapeName()); + return getControl(DefaultControls.ESCAPE)[0]; } /** @@ -556,11 +527,11 @@ public int getKeyEscape() { * @return String The readable name as defined in Input.Keys */ public String getKeyEscapeName() { - return keyEscapeName; + return Input.Keys.toString(getKeyEscape()); } public void setKeyEscapeName(String keyEscapeName) { - this.keyEscapeName = keyEscapeName; + setControl(DefaultControls.ESCAPE, new int[] { Input.Keys.valueOf(keyEscapeName) }); } /** @@ -650,7 +621,7 @@ public int getKeyChangeShip() { * @return int The keycode as defined in Input.Keys */ public int getKeyZoomIn() { - return Input.Keys.valueOf(keyZoomInName); + return getControl(DefaultControls.ZOOM_IN)[0]; } /** @@ -660,7 +631,7 @@ public int getKeyZoomIn() { * @return String The readable name as defined in Input.Keys */ public String getKeyZoomInName() { - return keyZoomInName; + return Input.Keys.toString(getKeyZoomIn()); } /** @@ -670,7 +641,7 @@ public String getKeyZoomInName() { * @return int The keycode as defined in Input.Keys */ public int getKeyZoomOut() { - return Input.Keys.valueOf(keyZoomOutName); + return getControl(DefaultControls.ZOOM_OUT)[0]; } /** @@ -680,7 +651,7 @@ public int getKeyZoomOut() { * @return String The readable name as defined in Input.Keys */ public String getKeyZoomOutName() { - return keyZoomOutName; + return Input.Keys.toString(getKeyZoomOut()); } /** @@ -689,7 +660,7 @@ public String getKeyZoomOutName() { * @return int The keycode as defined in Input.Keys */ public int getKeyMap() { - return Input.Keys.valueOf(keyMapName); + return getControl(DefaultControls.MAP)[0]; } /** @@ -698,11 +669,11 @@ public int getKeyMap() { * @return String The readable name as defined in Input.Keys */ public String getKeyMapName() { - return keyMapName; + return Input.Keys.toString(getKeyMap()); } public void setKeyMapName(String keyMapName) { - this.keyMapName = keyMapName; + setControl(DefaultControls.MAP, new int[] { Input.Keys.valueOf(keyMapName) }); } /** @@ -711,7 +682,7 @@ public void setKeyMapName(String keyMapName) { * @return int The keycode as defined in Input.Keys */ public int getKeyInventory() { - return Input.Keys.valueOf(keyInventoryName); + return getControl(DefaultControls.INVENTORY)[0]; } /** @@ -720,11 +691,11 @@ public int getKeyInventory() { * @return String The readable name as defined in Input.Keys */ public String getKeyInventoryName() { - return keyInventoryName; + return Input.Keys.toString(getKeyInventory()); } public void setKeyInventoryName(String keyInventoryName) { - this.keyInventoryName = keyInventoryName; + setControl(DefaultControls.INVENTORY, new int[] { Input.Keys.valueOf(keyInventoryName) }); } /** @@ -733,7 +704,7 @@ public void setKeyInventoryName(String keyInventoryName) { * @return int The keycode as defined in Input.Keys */ public int getKeyMercenaryInteraction() { - return Input.Keys.valueOf(keyMercenaryInteractionName); + return getControl(DefaultControls.MERCENARY_INTERACTION)[0]; } /** @@ -742,23 +713,23 @@ public int getKeyMercenaryInteraction() { * @return String The readable name as defined in Input.Keys */ public String getKeyMercenaryInterationName() { - return keyMercenaryInteractionName; + return Input.Keys.toString(getKeyMercenaryInteraction()); } public void setKeyMercenaryInteractionName(String keyMercenaryInteractionName) { - this.keyMercenaryInteractionName = keyMercenaryInteractionName; + setControl(DefaultControls.MERCENARY_INTERACTION, new int[] { Input.Keys.valueOf(keyMercenaryInteractionName) }); } public int getKeyFreeCameraMovement() { - return Input.Keys.valueOf(getKeyFreeCameraMovementName()); + return getControl(DefaultControls.FREE_CAMERA_MOVEMENT)[0]; } public String getKeyFreeCameraMovementName() { - return keyFreeCameraMovementName; + return Input.Keys.toString(getKeyFreeCameraMovement()); } public void setKeyFreeCameraMovementName(String keyFreeCameraMovementName) { - this.keyFreeCameraMovementName = keyFreeCameraMovementName; + setControl(DefaultControls.FREE_CAMERA_MOVEMENT, new int[] { Input.Keys.valueOf(keyFreeCameraMovementName) }); } /** @@ -767,7 +738,7 @@ public void setKeyFreeCameraMovementName(String keyFreeCameraMovementName) { * @return int The keycode as defined in Input.Keys */ public int getKeyTalk() { - return Input.Keys.valueOf(getKeyTalkName()); + return getControl(DefaultControls.TALK)[0]; } /** @@ -776,11 +747,11 @@ public int getKeyTalk() { * @return String The readable name as defined in Input.Keys */ public String getKeyTalkName() { - return keyTalkName; + return Input.Keys.toString(getKeyTalk()); } public void setKeyTalkName(String keyTalkName) { - this.keyTalkName = keyTalkName; + setControl(DefaultControls.TALK, new int[] { Input.Keys.valueOf(keyTalkName) }); } /** @@ -789,7 +760,7 @@ public void setKeyTalkName(String keyTalkName) { * @return int The keycode as defined in Input.Keys */ public int getKeyPause() { - return Input.Keys.valueOf(getKeyPauseName()); + return getControl(DefaultControls.PAUSE)[0]; } /** @@ -798,11 +769,11 @@ public int getKeyPause() { * @return String The readable name as defined in Input.Keys */ public String getKeyPauseName() { - return keyPauseName; + return Input.Keys.toString(getKeyPause()); } public void setKeyPauseName(String keyPauseName) { - this.keyPauseName = keyPauseName; + setControl(DefaultControls.PAUSE, new int[] { Input.Keys.valueOf(keyPauseName) }); } /** @@ -811,7 +782,7 @@ public void setKeyPauseName(String keyPauseName) { * @return int The keycode as defined in Input.Keys */ public int getKeyDrop() { - return Input.Keys.valueOf(getKeyDropName()); + return getControl(DefaultControls.DROP)[0]; } /** @@ -820,11 +791,11 @@ public int getKeyDrop() { * @return String The readable name as defined in Input.Keys */ public String getKeyDropName() { - return keyDropName; + return Input.Keys.toString(getKeyDrop()); } public void setKeyDropName(String keyDropName) { - this.keyDropName = keyDropName; + setControl(DefaultControls.DROP, new int[] { Input.Keys.valueOf(keyDropName) }); } /** @@ -833,7 +804,7 @@ public void setKeyDropName(String keyDropName) { * @return int The keycode as defined in Input.Keys */ public int getKeySellMenu() { - return Input.Keys.valueOf(getKeySellMenuName()); + return getControl(DefaultControls.SELL)[0]; } /** @@ -842,11 +813,11 @@ public int getKeySellMenu() { * @return String The readable name as defined in Input.Keys */ public String getKeySellMenuName() { - return keySellMenuName; + return Input.Keys.toString(getKeySellMenu()); } public void setKeySellMenuName(String keySellMenuName) { - this.keySellMenuName = keySellMenuName; + setControl(DefaultControls.SELL, new int[] { Input.Keys.valueOf(keySellMenuName) }); } /** @@ -855,7 +826,7 @@ public void setKeySellMenuName(String keySellMenuName) { * @return int The keycode as defined in Input.Keys */ public int getKeyBuyMenu() { - return Input.Keys.valueOf(getKeyBuyMenuName()); + return getControl(DefaultControls.BUY)[0]; } /** @@ -864,11 +835,11 @@ public int getKeyBuyMenu() { * @return String The readable name as defined in Input.Keys */ public String getKeyBuyMenuName() { - return keyBuyMenuName; + return Input.Keys.toString(getKeyBuyMenu()); } public void setKeyBuyMenuName(String keyBuyMenuName) { - this.keyBuyMenuName = keyBuyMenuName; + setControl(DefaultControls.BUY, new int[] { Input.Keys.valueOf(keyBuyMenuName) }); } /** @@ -877,7 +848,7 @@ public void setKeyBuyMenuName(String keyBuyMenuName) { * @return int The keycode as defined in Input.Keys */ public int getKeyChangeShipMenu() { - return Input.Keys.valueOf(getKeyChangeShipMenuName()); + return getControl(DefaultControls.CHANGE_SHIP)[0]; } /** @@ -886,11 +857,11 @@ public int getKeyChangeShipMenu() { * @return String The readable name as defined in Input.Keys */ public String getKeyChangeShipMenuName() { - return keyChangeShipMenuName; + return Input.Keys.toString(getKeyChangeShipMenu()); } public void setKeyChangeShipMenuName(String keyChangeShipMenuName) { - this.keyChangeShipMenuName = keyChangeShipMenuName; + setControl(DefaultControls.CHANGE_SHIP, new int[] { Input.Keys.valueOf(keyChangeShipMenuName) }); } /** @@ -899,7 +870,7 @@ public void setKeyChangeShipMenuName(String keyChangeShipMenuName) { * @return int The keycode as defined in Input.Keys */ public int getKeyHireShipMenu() { - return Input.Keys.valueOf(getKeyHireShipMenuName()); + return getControl(DefaultControls.HIRE_SHIP)[0]; } /** @@ -908,11 +879,23 @@ public int getKeyHireShipMenu() { * @return String The readable name as defined in Input.Keys */ public String getKeyHireShipMenuName() { - return keyHireShipMenuName; + return Input.Keys.toString(getKeyHireShipMenu()); } public void setKeyHireShipMenuName(String keyHireShipMenuName) { - this.keyHireShipMenuName = keyHireShipMenuName; + setControl(DefaultControls.HIRE_SHIP, new int[] { Input.Keys.valueOf(keyHireShipMenuName) }); + } + + public Set> getControls() { + return controls.entrySet(); + } + + public int[] getControl(InputControls control) { + return controls.get(control); + } + + public void setControl(InputControls control, int[] keys) { + controls.put(control, keys); } public int getControllerAxisShoot() { diff --git a/engine/src/main/java/org/destinationsol/input/DefaultControls.java b/engine/src/main/java/org/destinationsol/input/DefaultControls.java new file mode 100644 index 000000000..f45b7ed11 --- /dev/null +++ b/engine/src/main/java/org/destinationsol/input/DefaultControls.java @@ -0,0 +1,80 @@ +/* + * Copyright 2022 The Terasology Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.destinationsol.input; + +import com.badlogic.gdx.Input; +import org.destinationsol.GameOptions; + +import java.util.EnumSet; + +/** + * These are the default controls used in the base game. + * You can obtain the inputs used to trigger these controls using {@link GameOptions#getControl(InputControls)}. + */ +public enum DefaultControls implements InputControls { + MOUSE_UP("keyUpMouse", "Up", EnumSet.of(GameOptions.ControlType.MIXED), Input.Keys.W), + MOUSE_DOWN("keyDownMouse", "Down", EnumSet.of(GameOptions.ControlType.MIXED), Input.Keys.S), + UP("keyUp", "Up", EnumSet.of(GameOptions.ControlType.KEYBOARD), Input.Keys.UP), + DOWN("keyDown", "Down", EnumSet.of(GameOptions.ControlType.KEYBOARD), Input.Keys.DOWN), + LEFT("keyLeft", "Left", EnumSet.of(GameOptions.ControlType.KEYBOARD), Input.Keys.LEFT), + RIGHT("keyRight", "Right", EnumSet.of(GameOptions.ControlType.KEYBOARD), Input.Keys.RIGHT), + SHOOT("keyShoot", "Shoot", EnumSet.of(GameOptions.ControlType.KEYBOARD), Input.Keys.SPACE), + SHOOT2("keyShoot2", "Shoot Secondary", EnumSet.of(GameOptions.ControlType.KEYBOARD), Input.Keys.CONTROL_LEFT), + ABILITY("keyAbility", "Ability", EnumSet.of(GameOptions.ControlType.KEYBOARD), Input.Keys.SHIFT_LEFT), + ESCAPE("keyEscape", "Escape", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.ESCAPE), + MAP("keyMap", "Map", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.TAB), + INVENTORY("keyInventory", "Inventory", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.I), + TALK("keyTalk", "Talk", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.T), + PAUSE("keyPause", "Pause", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.P), + DROP("keyDrop", "Drop", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.D), + SELL("keySellMenu", "Sell", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.S), + BUY("keyBuyMenu", "Buy", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.B), + CHANGE_SHIP("keyChangeShipMenu", "Change Ship", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.C), + HIRE_SHIP("keyHireShipMenu", "Hire Ship", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.H), + MERCENARY_INTERACTION("keyMercenaryInteraction", "Hire Ship", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.M), + FREE_CAMERA_MOVEMENT("keyFreeCameraMovement", "Free Camera", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.V), + ZOOM_IN("keyZoomIn", "Zoom In", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.PAGE_UP), + ZOOM_OUT("keyZoomOut", "Zoom Out", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.PAGE_DOWN); + + private final String controlName; + private final String displayName; + private final EnumSet controlTypes; + private final int[] defaultInputs; + + DefaultControls(String controlName, String displayName, EnumSet controlTypes, int... defaultInputs) { + this.controlName = controlName; + this.displayName = displayName; + this.controlTypes = controlTypes; + this.defaultInputs = defaultInputs; + } + + public String getControlName() { + return controlName; + } + + public String getDisplayName() { + return displayName; + } + + public EnumSet getControlTypes() { + return controlTypes; + } + + public int[] getDefaultInputs() { + return defaultInputs; + } +} \ No newline at end of file diff --git a/engine/src/main/java/org/destinationsol/input/InputControls.java b/engine/src/main/java/org/destinationsol/input/InputControls.java new file mode 100644 index 000000000..e43223ff9 --- /dev/null +++ b/engine/src/main/java/org/destinationsol/input/InputControls.java @@ -0,0 +1,90 @@ +/* + * Copyright 2022 The Terasology Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.destinationsol.input; + +import org.destinationsol.GameOptions; + +import java.util.EnumSet; + +/** + * The base interface implementing new controls that conform to certain control types. + * + * It is recommended that you create an enum implementing this interface per-module to contain all the new controls you add to the game. + *
+ * Example Implementation: + *
+ * public enum ModuleControls implements InputControls {
+ *     CONTROL_1("module_control1", "Control 1", EnumSet.of(GameOptions.ControlType.KEYBOARD), Input.Keys.W),
+ *     CONTROL_2("module_control2", "Control 2", EnumSet.of(GameOptions.ControlType.MIXED), Input.Keys.S),
+ *     CONTROL_2("module_control3", "Control 3", EnumSet.allOf(GameOptions.ControlType.class), Input.Keys.PAGE_DOWN);
+ *
+ *     private final String controlName;
+ *     private final String displayName;
+ *     private final EnumSet controlTypes;
+ *     private final int[] defaultInputs;
+ *
+ *     ModuleControls(String controlName, String displayName, EnumSet controlTypes, int... defaultInputs) {
+ *         this.controlName = controlName;
+ *         this.displayName = displayName;
+ *         this.controlTypes = controlTypes;
+ *         this.defaultInputs = defaultInputs;
+ *     }
+ *
+ *     public String getControlName() {
+ *         return controlName;
+ *     }
+ *
+ *     public String getDisplayName() {
+ *         return displayName;
+ *     }
+ *
+ *     public EnumSet getControlTypes() {
+ *         return controlTypes;
+ *     }
+ *
+ *     public int[] getDefaultInputs() {
+ *         return defaultInputs;
+ *     }
+ * }
+ * 
+ * @see DefaultControls the built-in game controls + */ +public interface InputControls { + /** + * Returns the control name. This is the name used internally to serialise and de-serialise the control values. + * @return the control name + */ + String getControlName(); + + /** + * Returns the control's display name. This name is used when referencing the control in user-facing text. + * @return the control's display name + */ + String getDisplayName(); + + /** + * Returns the supported control schemes for this control. + * @return the supported control schemes + */ + EnumSet getControlTypes(); + + /** + * Returns the default assigned input values for this control. Any one of these values can trigger the control. + * @return the default assigned input values + */ + int[] getDefaultInputs(); +} diff --git a/engine/src/main/java/org/destinationsol/menu/InputMapControllerScreen.java b/engine/src/main/java/org/destinationsol/menu/InputMapControllerScreen.java index d1a75087c..7633c2e63 100644 --- a/engine/src/main/java/org/destinationsol/menu/InputMapControllerScreen.java +++ b/engine/src/main/java/org/destinationsol/menu/InputMapControllerScreen.java @@ -25,6 +25,7 @@ import com.badlogic.gdx.controllers.Controllers; import org.destinationsol.GameOptions; import org.destinationsol.SolApplication; +import org.destinationsol.input.DefaultControls; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -137,39 +138,39 @@ public void resetToDefaults(GameOptions gameOptions) { itemsList.set(index++, InitItem(GameOptions.DEFAULT_AXIS_ABILITY, GameOptions.DEFAULT_BUTTON_ABILITY, "Ability")); InputConfigItem item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_PAUSE); + item.setInputKey(Input.Keys.toString(DefaultControls.PAUSE.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_MAP); + item.setInputKey(Input.Keys.toString(DefaultControls.MAP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_INVENTORY); + item.setInputKey(Input.Keys.toString(DefaultControls.INVENTORY.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_DROP); + item.setInputKey(Input.Keys.toString(DefaultControls.DROP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_TALK); + item.setInputKey(Input.Keys.toString(DefaultControls.TALK.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_SELL); + item.setInputKey(Input.Keys.toString(DefaultControls.SELL.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_BUY); + item.setInputKey(Input.Keys.toString(DefaultControls.BUY.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_CHANGE_SHIP); + item.setInputKey(Input.Keys.toString(DefaultControls.CHANGE_SHIP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_HIRE_SHIP); + item.setInputKey(Input.Keys.toString(DefaultControls.HIRE_SHIP.getDefaultInputs()[0])); itemsList.set(index++, item); } diff --git a/engine/src/main/java/org/destinationsol/menu/InputMapKeyboardScreen.java b/engine/src/main/java/org/destinationsol/menu/InputMapKeyboardScreen.java index 91d0a9b1e..ddcbec1e5 100644 --- a/engine/src/main/java/org/destinationsol/menu/InputMapKeyboardScreen.java +++ b/engine/src/main/java/org/destinationsol/menu/InputMapKeyboardScreen.java @@ -22,6 +22,7 @@ import com.badlogic.gdx.InputProcessor; import org.destinationsol.GameOptions; import org.destinationsol.SolApplication; +import org.destinationsol.input.DefaultControls; import org.destinationsol.ui.SolInputManager; import org.destinationsol.ui.SolUiControl; @@ -107,67 +108,67 @@ public void resetToDefaults(GameOptions gameOptions) { // This needs to be in the same order the list is initialised InputConfigItem item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_UP); + item.setInputKey(Input.Keys.toString(DefaultControls.UP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_DOWN); + item.setInputKey(Input.Keys.toString(DefaultControls.DOWN.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_LEFT); + item.setInputKey(Input.Keys.toString(DefaultControls.LEFT.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_RIGHT); + item.setInputKey(Input.Keys.toString(DefaultControls.RIGHT.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_SHOOT); + item.setInputKey(Input.Keys.toString(DefaultControls.SHOOT.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_SHOOT2); + item.setInputKey(Input.Keys.toString(DefaultControls.SHOOT2.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_ABILITY); + item.setInputKey(Input.Keys.toString(DefaultControls.ABILITY.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_PAUSE); + item.setInputKey(Input.Keys.toString(DefaultControls.PAUSE.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_MAP); + item.setInputKey(Input.Keys.toString(DefaultControls.MAP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_INVENTORY); + item.setInputKey(Input.Keys.toString(DefaultControls.INVENTORY.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_DROP); + item.setInputKey(Input.Keys.toString(DefaultControls.DROP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_TALK); + item.setInputKey(Input.Keys.toString(DefaultControls.TALK.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_SELL); + item.setInputKey(Input.Keys.toString(DefaultControls.SELL.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_BUY); + item.setInputKey(Input.Keys.toString(DefaultControls.BUY.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_CHANGE_SHIP); + item.setInputKey(Input.Keys.toString(DefaultControls.CHANGE_SHIP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_HIRE_SHIP); + item.setInputKey(Input.Keys.toString(DefaultControls.HIRE_SHIP.getDefaultInputs()[0])); itemsList.set(index++, item); } diff --git a/engine/src/main/java/org/destinationsol/menu/InputMapMixedScreen.java b/engine/src/main/java/org/destinationsol/menu/InputMapMixedScreen.java index 91add46e7..817b946b9 100644 --- a/engine/src/main/java/org/destinationsol/menu/InputMapMixedScreen.java +++ b/engine/src/main/java/org/destinationsol/menu/InputMapMixedScreen.java @@ -22,6 +22,7 @@ import com.badlogic.gdx.InputProcessor; import org.destinationsol.GameOptions; import org.destinationsol.SolApplication; +import org.destinationsol.input.DefaultControls; import org.destinationsol.ui.SolInputManager; import org.destinationsol.ui.SolUiControl; @@ -90,47 +91,47 @@ public void resetToDefaults(GameOptions gameOptions) { // This needs to be in the same order the list is initialised InputConfigItem item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_MOUSE_UP); + item.setInputKey(Input.Keys.toString(DefaultControls.MOUSE_UP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_MOUSE_DOWN); + item.setInputKey(Input.Keys.toString(DefaultControls.MOUSE_DOWN.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_PAUSE); + item.setInputKey(Input.Keys.toString(DefaultControls.PAUSE.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_MAP); + item.setInputKey(Input.Keys.toString(DefaultControls.MAP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_INVENTORY); + item.setInputKey(Input.Keys.toString(DefaultControls.INVENTORY.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_DROP); + item.setInputKey(Input.Keys.toString(DefaultControls.DROP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_TALK); + item.setInputKey(Input.Keys.toString(DefaultControls.TALK.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_SELL); + item.setInputKey(Input.Keys.toString(DefaultControls.SELL.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_BUY); + item.setInputKey(Input.Keys.toString(DefaultControls.BUY.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_CHANGE_SHIP); + item.setInputKey(Input.Keys.toString(DefaultControls.CHANGE_SHIP.getDefaultInputs()[0])); itemsList.set(index++, item); item = itemsList.get(index); - item.setInputKey(GameOptions.DEFAULT_HIRE_SHIP); + item.setInputKey(Input.Keys.toString(DefaultControls.HIRE_SHIP.getDefaultInputs()[0])); itemsList.set(index, item); }