3 changed files with 145 additions and 18 deletions
@ -0,0 +1,6 @@ |
|||
#keyboard controls file |
|||
#Sat Feb 27 11:58:30 CET 2021 |
|||
up=87 |
|||
right=68 |
|||
left=65 |
|||
down=83 |
@ -1,21 +1,141 @@ |
|||
package client.events; |
|||
|
|||
import java.io.FileInputStream; |
|||
import java.io.FileNotFoundException; |
|||
import java.io.FileOutputStream; |
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.util.Collections; |
|||
import java.util.Map; |
|||
import java.util.Properties; |
|||
import java.util.TreeMap; |
|||
|
|||
import org.lwjgl.glfw.GLFW; |
|||
|
|||
public class KBControls { |
|||
public int getUPKey() { |
|||
return GLFW.GLFW_KEY_W; |
|||
import game.Game; |
|||
|
|||
/** |
|||
* |
|||
* @author Mathieu |
|||
* |
|||
* |
|||
* class to load, acces, modify and save keyboard controls |
|||
*/ |
|||
public final class KBControls { |
|||
public static final String UP = "up"; |
|||
public static final String DOWN = "down"; |
|||
public static final String LEFT = "left"; |
|||
public static final String RIGHT = "right"; |
|||
|
|||
private final TreeMap<String, Integer> keys; |
|||
private static final String PROPERTIES_FILENAME = "controls.properties"; |
|||
|
|||
/** |
|||
* create controls by loading the PROPERTIES_FILENAME file |
|||
*/ |
|||
public KBControls() { |
|||
keys = loadKeysFromPropertiesFile(PROPERTIES_FILENAME); |
|||
} |
|||
|
|||
/** |
|||
* save keys to the disk |
|||
*/ |
|||
public void save() { |
|||
saveProperties(); |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param action |
|||
* one of the actions that are declared public final on the class |
|||
* head |
|||
* @return the string name of the key associated with the given action |
|||
*/ |
|||
public String getKeyNameForAction(String action) { |
|||
return GLFW.glfwGetKeyName(getKey(action), 0); |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param action |
|||
* one of the actions that are declared public final on the class |
|||
* head |
|||
* @return the key associated with the given action |
|||
*/ |
|||
public int getKey(String action) { |
|||
return keys.get(action); |
|||
} |
|||
|
|||
public int getDOWNKey() { |
|||
return GLFW.GLFW_KEY_S; |
|||
/** |
|||
* sets a key for an action |
|||
* |
|||
* @param action |
|||
* the given action |
|||
* @param key |
|||
* the key to be associated with the action |
|||
*/ |
|||
public void setKey(String action, int key) { |
|||
keys.put(action, key); |
|||
} |
|||
|
|||
public int getLEFTKey() { |
|||
return GLFW.GLFW_KEY_A; |
|||
/** |
|||
* loads default values for all keys |
|||
*/ |
|||
public void loadDefaultValues() { |
|||
setKey(UP, GLFW.GLFW_KEY_W); |
|||
setKey(DOWN, GLFW.GLFW_KEY_S); |
|||
setKey(LEFT, GLFW.GLFW_KEY_A); |
|||
setKey(RIGHT, GLFW.GLFW_KEY_D); |
|||
} |
|||
|
|||
public int getRIGHTKey() { |
|||
return GLFW.GLFW_KEY_D; |
|||
/// private methods
|
|||
|
|||
private TreeMap<String, Integer> loadKeysFromPropertiesFile(String filename) { |
|||
|
|||
Properties prop = new Properties(); |
|||
|
|||
try (InputStream is = new FileInputStream(filename)) { |
|||
prop.load(is); |
|||
|
|||
return fillKeys(prop); |
|||
|
|||
} catch (FileNotFoundException e) { |
|||
Game.error("missing file: " + filename); |
|||
return new TreeMap<>(); |
|||
} catch (IOException e) { |
|||
Game.error(filename + ": corrupted file"); |
|||
return new TreeMap<>(); |
|||
} |
|||
} |
|||
|
|||
private void saveProperties() { |
|||
Properties prop = fillPropertiesWithKeys(); |
|||
|
|||
try (FileOutputStream os = new FileOutputStream(PROPERTIES_FILENAME)) { |
|||
prop.store(os, "keyboard controls file"); |
|||
} catch (IOException e) { |
|||
Game.error("cannot save keyboard controls"); |
|||
} |
|||
} |
|||
|
|||
private TreeMap<String, Integer> fillKeys(Properties prop) { |
|||
TreeMap<String, Integer> ret = new TreeMap<>(); |
|||
|
|||
for (Object o : Collections.list(prop.propertyNames())) { |
|||
String name = (String) o; |
|||
|
|||
ret.put(name, Integer.parseInt(prop.getProperty(name))); |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
private Properties fillPropertiesWithKeys() { |
|||
Properties prop = new Properties(); |
|||
|
|||
for (Map.Entry<String, Integer> couple : keys.entrySet()) |
|||
prop.setProperty(couple.getKey(), Integer.toString(couple.getValue())); |
|||
|
|||
return prop; |
|||
} |
|||
} |
Loading…
Reference in new issue