v1.3.0: added a separate bindings window for controller

This commit is contained in:
Evan Debenham
2022-06-03 12:33:17 -04:00
parent 833237eab8
commit 8653439a4f
6 changed files with 247 additions and 33 deletions

View File

@@ -164,10 +164,11 @@ public class ControllerHandler implements ControllerListener {
if (btnCode == mapping.buttonR1) return Input.Keys.BUTTON_R1;
if (btnCode == mapping.buttonR2) return Input.Keys.BUTTON_R2;
if (btnCode == mapping.buttonDpadUp) return Input.Keys.DPAD_UP;
if (btnCode == mapping.buttonDpadLeft) return Input.Keys.DPAD_LEFT;
if (btnCode == mapping.buttonDpadDown) return Input.Keys.DPAD_DOWN;
if (btnCode == mapping.buttonDpadRight) return Input.Keys.DPAD_RIGHT;
//we add 1000 here to make these keys distinct from Keys.UP, Keys.DOWN, etc..
if (btnCode == mapping.buttonDpadUp) return Input.Keys.DPAD_UP + 1000;
if (btnCode == mapping.buttonDpadDown) return Input.Keys.DPAD_DOWN + 1000;
if (btnCode == mapping.buttonDpadLeft) return Input.Keys.DPAD_LEFT + 1000;
if (btnCode == mapping.buttonDpadRight) return Input.Keys.DPAD_RIGHT + 1000;
if (btnCode == mapping.buttonLeftStick) return Input.Keys.BUTTON_THUMBL;
if (btnCode == mapping.buttonRightStick)return Input.Keys.BUTTON_THUMBR;
@@ -180,7 +181,7 @@ public class ControllerHandler implements ControllerListener {
return true;
}
else if (keyCode >= Input.Keys.DPAD_UP && keyCode <= Input.Keys.DPAD_LEFT){
if (keyCode >= Input.Keys.DPAD_UP+1000 && keyCode <= Input.Keys.DPAD_RIGHT+1000){
return true;
}
@@ -200,6 +201,16 @@ public class ControllerHandler implements ControllerListener {
}
}
if (keyCode == Input.Keys.DPAD_UP + 1000){
return Input.Keys.toString(Input.Keys.DPAD_UP);
} else if (keyCode == Input.Keys.DPAD_DOWN + 1000){
return Input.Keys.toString(Input.Keys.DPAD_DOWN);
} else if (keyCode == Input.Keys.DPAD_LEFT + 1000){
return Input.Keys.toString(Input.Keys.DPAD_LEFT);
} else if (keyCode == Input.Keys.DPAD_RIGHT + 1000){
return Input.Keys.toString(Input.Keys.DPAD_RIGHT);
}
return null;
}
}

View File

@@ -30,8 +30,12 @@ import java.util.LinkedHashMap;
// should see about doing some refactoring to clean this up
public class KeyBindings {
//for keyboard keys
private static LinkedHashMap<Integer, GameAction> bindings = new LinkedHashMap<>();
//for controller buttons
private static LinkedHashMap<Integer, GameAction> controllerBindings = new LinkedHashMap<>();
public static LinkedHashMap<Integer, GameAction> getAllBindings(){
return new LinkedHashMap<>(bindings);
}
@@ -40,6 +44,14 @@ public class KeyBindings {
bindings = new LinkedHashMap<>(newBindings);
}
public static LinkedHashMap<Integer, GameAction> getAllControllerBindings(){
return new LinkedHashMap<>(controllerBindings);
}
public static void setAllControllerBindings(LinkedHashMap<Integer, GameAction> newBindings){
controllerBindings = new LinkedHashMap<>(newBindings);
}
//these are special keybinding that are not user-configurable
private static LinkedHashMap<Integer, GameAction> hardBindings = new LinkedHashMap<>();
@@ -53,12 +65,17 @@ public class KeyBindings {
if (keyCode < 0 || (keyCode > 255 && keyCode < 1000)){
return false;
}
return bindingKey || bindings.containsKey( keyCode ) || hardBindings.containsKey( keyCode );
return bindingKey
|| bindings.containsKey( keyCode )
|| controllerBindings.containsKey( keyCode )
|| hardBindings.containsKey( keyCode );
}
public static GameAction getActionForKey(KeyEvent event){
if (bindings.containsKey( event.code )){
if (bindings.containsKey( event.code )) {
return bindings.get( event.code );
} else if (controllerBindings.containsKey( event.code )){
return controllerBindings.get( event.code );
} else if (hardBindings.containsKey( event.code )) {
return hardBindings.get( event.code );
}
@@ -67,11 +84,36 @@ public class KeyBindings {
public static ArrayList<Integer> getBoundKeysForAction(GameAction action){
ArrayList<Integer> result = new ArrayList<>();
for( int i : bindings.keySet()){
for( int i : bindings.keySet() ){
if (bindings.get(i) == action){
result.add(i);
}
}
for( int i : controllerBindings.keySet() ){
if (controllerBindings.get(i) == action){
result.add(i);
}
}
return result;
}
public static ArrayList<Integer> getKeyboardKeysForAction(GameAction action){
ArrayList<Integer> result = new ArrayList<>();
for( int i : bindings.keySet() ){
if (bindings.get(i) == action){
result.add(i);
}
}
return result;
}
public static ArrayList<Integer> getControllerKeysForAction(GameAction action){
ArrayList<Integer> result = new ArrayList<>();
for( int i : controllerBindings.keySet() ){
if (controllerBindings.get(i) == action){
result.add(i);
}
}
return result;
}