v0.8.0: refactored GameAction classes to more closely resemble enums
This commit is contained in:
@@ -21,13 +21,47 @@
|
||||
|
||||
package com.watabou.input;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
//This is similar to an enum, but we don't use that because subclasses should be able to add actions
|
||||
public class GameAction {
|
||||
|
||||
public static final int NONE = 0;
|
||||
private static final ArrayList<GameAction> ALL_ACTIONS = new ArrayList<>();
|
||||
|
||||
public static final int BACK = 1;
|
||||
public static final int MENU = 2;
|
||||
private int code;
|
||||
private String name;
|
||||
|
||||
public static final int TOTAL_ACTIONS = 3;
|
||||
protected GameAction( String name ){
|
||||
code = ALL_ACTIONS.size();
|
||||
this.name = name;
|
||||
|
||||
ALL_ACTIONS.add(this);
|
||||
}
|
||||
|
||||
public int code(){
|
||||
return code;
|
||||
}
|
||||
|
||||
public String name(){
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof GameAction && ((GameAction) o).code == code;
|
||||
}
|
||||
|
||||
public static final GameAction NONE = new GameAction( "none" );
|
||||
|
||||
public static final GameAction BACK = new GameAction( "back" );
|
||||
public static final GameAction MENU = new GameAction( "menu" );
|
||||
|
||||
public static ArrayList<GameAction> allActions(){
|
||||
return new ArrayList<>(ALL_ACTIONS);
|
||||
}
|
||||
|
||||
public static int totalActions(){
|
||||
return ALL_ACTIONS.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public class InputHandler extends InputAdapter {
|
||||
|
||||
@Override
|
||||
public synchronized boolean keyDown( int keyCode ) {
|
||||
if (KeyBindings.isBound( keyCode )) {
|
||||
if (KeyBindings.isKeyBound( keyCode )) {
|
||||
KeyEvent.addKeyEvent( new KeyEvent( keyCode, true ) );
|
||||
return true;
|
||||
} else {
|
||||
@@ -96,7 +96,7 @@ public class InputHandler extends InputAdapter {
|
||||
|
||||
@Override
|
||||
public synchronized boolean keyUp( int keyCode ) {
|
||||
if (KeyBindings.isBound( keyCode )) {
|
||||
if (KeyBindings.isKeyBound( keyCode )) {
|
||||
KeyEvent.addKeyEvent( new KeyEvent( keyCode, false ) );
|
||||
return true;
|
||||
} else {
|
||||
|
||||
@@ -24,31 +24,28 @@ package com.watabou.input;
|
||||
import com.badlogic.gdx.Input;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
//TODO maybe just move to game action?
|
||||
public class KeyBindings {
|
||||
|
||||
private static LinkedHashMap<Integer, Integer> bindings = new LinkedHashMap<>();
|
||||
private static HashMap<Integer, String> names = new HashMap<>();
|
||||
private static LinkedHashMap<Integer, GameAction> bindings = new LinkedHashMap<>();
|
||||
|
||||
public static void addBinding( int keyCode, int keyAction){
|
||||
bindings.put(keyCode, keyAction);
|
||||
public static void addKeyBinding(int keyCode, GameAction action){
|
||||
bindings.put(keyCode, action);
|
||||
}
|
||||
|
||||
public static boolean isBound( int keyCode ){
|
||||
return bindings.keySet().contains( keyCode );
|
||||
public static boolean isKeyBound(int keyCode ){
|
||||
return bindings.containsKey( keyCode );
|
||||
}
|
||||
|
||||
public static int getBinding( KeyEvent event ){
|
||||
public static GameAction getActionForKey(KeyEvent event ){
|
||||
return bindings.get( event.code );
|
||||
}
|
||||
|
||||
public static ArrayList<Integer> getBindings( int gameAction ){
|
||||
public static ArrayList<Integer> getKeysForAction(GameAction action ){
|
||||
ArrayList<Integer> result = new ArrayList<>();
|
||||
for( int i : bindings.keySet()){
|
||||
if (bindings.get(i) == gameAction){
|
||||
if (bindings.get(i) == action){
|
||||
result.add(i);
|
||||
}
|
||||
}
|
||||
@@ -65,12 +62,4 @@ public class KeyBindings {
|
||||
}
|
||||
}
|
||||
|
||||
public static void addName( int keyAction, String name ){
|
||||
names.put(keyAction, name);
|
||||
}
|
||||
|
||||
public static String getName( int keyAction ){
|
||||
return names.get( keyAction );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,13 +35,12 @@ public class Scene extends Group {
|
||||
@Override
|
||||
public boolean onSignal( KeyEvent event ) {
|
||||
if (Game.instance != null && event.pressed) {
|
||||
switch (KeyBindings.getBinding( event )) {
|
||||
case GameAction.BACK:
|
||||
onBackPressed();
|
||||
break;
|
||||
case GameAction.MENU:
|
||||
onMenuPressed();
|
||||
break;
|
||||
if (KeyBindings.getActionForKey( event ) == GameAction.BACK){
|
||||
onBackPressed();
|
||||
|
||||
} else if (KeyBindings.getActionForKey( event ) == GameAction.MENU){
|
||||
onMenuPressed();
|
||||
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -67,7 +67,7 @@ public class Button extends Component {
|
||||
@Override
|
||||
public boolean onSignal ( KeyEvent event ) {
|
||||
if ( active && !event.pressed
|
||||
&& KeyBindings.getBinding( event ) == keyAction()){
|
||||
&& KeyBindings.getActionForKey( event ) == keyAction()){
|
||||
onClick();
|
||||
return true;
|
||||
}
|
||||
@@ -78,7 +78,7 @@ public class Button extends Component {
|
||||
|
||||
private Signal.Listener<KeyEvent> keyListener;
|
||||
|
||||
public int keyAction(){
|
||||
public GameAction keyAction(){
|
||||
return GameAction.NONE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user