v0.8.0: refactored KeyAction into int constants so it can be expanded

This commit is contained in:
Evan Debenham 2019-12-13 23:22:44 -05:00
parent cb76fb8188
commit f269d68a27
15 changed files with 176 additions and 114 deletions

View File

@ -21,32 +21,125 @@
package com.watabou.input; package com.watabou.input;
import com.badlogic.gdx.Input;
//FIXME this is describing actions defined in the core module, it should probably be moved there. //FIXME this is describing actions defined in the core module, it should probably be moved there.
public enum KeyAction { public class KeyAction {
BACK,
MENU,
HERO_INFO, public static final int NONE = 0;
JOURNAL,
WAIT, public static final int BACK = 1;
SEARCH, public static final int MENU = 2;
INVENTORY, public static final int HERO_INFO = 3;
QUICKSLOT_1, public static final int JOURNAL = 4;
QUICKSLOT_2,
QUICKSLOT_3,
QUICKSLOT_4,
TAG_ATTACK, public static final int WAIT = 5;
TAG_DANGER, public static final int SEARCH = 6;
TAG_ACTION,
TAG_LOOT,
TAG_RESUME,
ZOOM_IN, public static final int INVENTORY = 7;
ZOOM_OUT, public static final int QUICKSLOT_1 = 8;
ZOOM_DEFAULT, public static final int QUICKSLOT_2 = 9;
public static final int QUICKSLOT_3 = 10;
public static final int QUICKSLOT_4 = 11;
N, NE, E, SE, S, SW, W, NW public static final int TAG_ATTACK = 12;
public static final int TAG_DANGER = 13;
public static final int TAG_ACTION = 14;
public static final int TAG_LOOT = 15;
public static final int TAG_RESUME = 16;
public static final int ZOOM_IN = 17;
public static final int ZOOM_OUT = 18;
public static final int ZOOM_DEFAULT= 19;
public static final int N = 20;
public static final int NE = 21;
public static final int E = 22;
public static final int SE = 23;
public static final int S = 24;
public static final int SW = 25;
public static final int W = 26;
public static final int NW = 27;
public static void initialize() {
KeyBindings.addName(NONE, "none");
KeyBindings.addName(BACK, "back");
KeyBindings.addName(MENU, "menu");
KeyBindings.addName(HERO_INFO, "hero_info");
KeyBindings.addName(JOURNAL, "journal");
KeyBindings.addName(WAIT, "wait");
KeyBindings.addName(SEARCH, "search");
KeyBindings.addName(INVENTORY, "inventory");
KeyBindings.addName(QUICKSLOT_1, "quickslot_1");
KeyBindings.addName(QUICKSLOT_2, "quickslot_2");
KeyBindings.addName(QUICKSLOT_3, "quickslot_3");
KeyBindings.addName(QUICKSLOT_4, "quickslot_4");
KeyBindings.addName(TAG_ATTACK, "tag_attack");
KeyBindings.addName(TAG_DANGER, "tag_danger");
KeyBindings.addName(TAG_ACTION, "tag_action");
KeyBindings.addName(TAG_LOOT, "tag_loot");
KeyBindings.addName(TAG_RESUME, "tag_resume");
KeyBindings.addName(ZOOM_IN, "zoom_in");
KeyBindings.addName(ZOOM_OUT, "zoom_out");
KeyBindings.addName(ZOOM_DEFAULT, "zoom_default");
KeyBindings.addName(N, "none");
KeyBindings.addName(NE, "none");
KeyBindings.addName(E, "none");
KeyBindings.addName(SE, "none");
KeyBindings.addName(S, "none");
KeyBindings.addName(SW, "none");
KeyBindings.addName(W, "none");
KeyBindings.addName(NW, "none");
//default key bindings
KeyBindings.addBinding( Input.Keys.BACK, KeyAction.BACK );
KeyBindings.addBinding( Input.Keys.MENU, KeyAction.MENU );
KeyBindings.addBinding( Input.Keys.H, KeyAction.HERO_INFO );
KeyBindings.addBinding( Input.Keys.J, KeyAction.JOURNAL );
KeyBindings.addBinding( Input.Keys.NUMPAD_5, KeyAction.WAIT );
KeyBindings.addBinding( Input.Keys.SPACE, KeyAction.WAIT );
KeyBindings.addBinding( Input.Keys.S, KeyAction.SEARCH );
KeyBindings.addBinding( Input.Keys.I, KeyAction.INVENTORY );
KeyBindings.addBinding( Input.Keys.Q, KeyAction.QUICKSLOT_1 );
KeyBindings.addBinding( Input.Keys.W, KeyAction.QUICKSLOT_2 );
KeyBindings.addBinding( Input.Keys.E, KeyAction.QUICKSLOT_3 );
KeyBindings.addBinding( Input.Keys.R, KeyAction.QUICKSLOT_4 );
KeyBindings.addBinding( Input.Keys.A, KeyAction.TAG_ATTACK );
KeyBindings.addBinding( Input.Keys.TAB, KeyAction.TAG_DANGER );
KeyBindings.addBinding( Input.Keys.D, KeyAction.TAG_ACTION );
KeyBindings.addBinding( Input.Keys.ENTER, KeyAction.TAG_LOOT );
KeyBindings.addBinding( Input.Keys.T, KeyAction.TAG_RESUME );
KeyBindings.addBinding( Input.Keys.PLUS, KeyAction.ZOOM_IN );
KeyBindings.addBinding( Input.Keys.EQUALS, KeyAction.ZOOM_IN );
KeyBindings.addBinding( Input.Keys.MINUS, KeyAction.ZOOM_OUT );
KeyBindings.addBinding( Input.Keys.SLASH, KeyAction.ZOOM_DEFAULT );
KeyBindings.addBinding( Input.Keys.UP, KeyAction.N );
KeyBindings.addBinding( Input.Keys.RIGHT, KeyAction.E );
KeyBindings.addBinding( Input.Keys.DOWN, KeyAction.S );
KeyBindings.addBinding( Input.Keys.LEFT, KeyAction.W );
KeyBindings.addBinding( Input.Keys.NUMPAD_8, KeyAction.N );
KeyBindings.addBinding( Input.Keys.NUMPAD_9, KeyAction.NE );
KeyBindings.addBinding( Input.Keys.NUMPAD_6, KeyAction.E );
KeyBindings.addBinding( Input.Keys.NUMPAD_3, KeyAction.SE );
KeyBindings.addBinding( Input.Keys.NUMPAD_2, KeyAction.S );
KeyBindings.addBinding( Input.Keys.NUMPAD_1, KeyAction.SW );
KeyBindings.addBinding( Input.Keys.NUMPAD_4, KeyAction.W );
KeyBindings.addBinding( Input.Keys.NUMPAD_7, KeyAction.NW );
}
} }

View File

@ -21,64 +21,31 @@
package com.watabou.input; package com.watabou.input;
import com.badlogic.gdx.Input;
import java.util.HashMap; import java.util.HashMap;
public class KeyBindings { public class KeyBindings {
private static HashMap<Integer, KeyAction> bindings; private static HashMap<Integer, Integer> bindings = new HashMap<>();
private static HashMap<Integer, String> names = new HashMap<>();
static { public static void addBinding( int keyCode, int keyAction){
bindings = new HashMap<>(); bindings.put(keyCode, keyAction);
bindings.put( Input.Keys.BACK, KeyAction.BACK );
bindings.put( Input.Keys.MENU, KeyAction.MENU );
bindings.put( Input.Keys.H, KeyAction.HERO_INFO );
bindings.put( Input.Keys.J, KeyAction.JOURNAL );
bindings.put( Input.Keys.NUMPAD_5, KeyAction.WAIT );
bindings.put( Input.Keys.SPACE, KeyAction.WAIT );
bindings.put( Input.Keys.S, KeyAction.SEARCH );
bindings.put( Input.Keys.I, KeyAction.INVENTORY );
bindings.put( Input.Keys.Q, KeyAction.QUICKSLOT_1 );
bindings.put( Input.Keys.W, KeyAction.QUICKSLOT_2 );
bindings.put( Input.Keys.E, KeyAction.QUICKSLOT_3 );
bindings.put( Input.Keys.R, KeyAction.QUICKSLOT_4 );
bindings.put( Input.Keys.A, KeyAction.TAG_ATTACK );
bindings.put( Input.Keys.TAB, KeyAction.TAG_DANGER );
bindings.put( Input.Keys.D, KeyAction.TAG_ACTION );
bindings.put( Input.Keys.ENTER, KeyAction.TAG_LOOT );
bindings.put( Input.Keys.T, KeyAction.TAG_RESUME );
bindings.put( Input.Keys.PLUS, KeyAction.ZOOM_IN );
bindings.put( Input.Keys.EQUALS, KeyAction.ZOOM_IN );
bindings.put( Input.Keys.MINUS, KeyAction.ZOOM_OUT );
bindings.put( Input.Keys.SLASH, KeyAction.ZOOM_DEFAULT );
bindings.put( Input.Keys.UP, KeyAction.N );
bindings.put( Input.Keys.RIGHT, KeyAction.E );
bindings.put( Input.Keys.DOWN, KeyAction.S );
bindings.put( Input.Keys.LEFT, KeyAction.W );
bindings.put( Input.Keys.NUMPAD_8, KeyAction.N );
bindings.put( Input.Keys.NUMPAD_9, KeyAction.NE );
bindings.put( Input.Keys.NUMPAD_6, KeyAction.E );
bindings.put( Input.Keys.NUMPAD_3, KeyAction.SE );
bindings.put( Input.Keys.NUMPAD_2, KeyAction.S );
bindings.put( Input.Keys.NUMPAD_1, KeyAction.SW );
bindings.put( Input.Keys.NUMPAD_4, KeyAction.W );
bindings.put( Input.Keys.NUMPAD_7, KeyAction.NW );
} }
public static boolean isBound( int keyCode ){ public static boolean isBound( int keyCode ){
return bindings.keySet().contains( keyCode ); return bindings.keySet().contains( keyCode );
} }
public static KeyAction getBinding( KeyEvent event ){ public static int getBinding( KeyEvent event ){
return bindings.get( event.code ); return bindings.get( event.code );
} }
public static void addName( int keyAction, String name ){
names.put(keyAction, name);
}
public static String getName( int keyAction ){
return names.get( keyAction );
}
} }

View File

@ -30,7 +30,7 @@ import com.watabou.gltextures.TextureCache;
import com.watabou.glwrap.Blending; import com.watabou.glwrap.Blending;
import com.watabou.glwrap.Vertexbuffer; import com.watabou.glwrap.Vertexbuffer;
import com.watabou.input.InputHandler; import com.watabou.input.InputHandler;
import com.watabou.input.KeyEvent; import com.watabou.input.KeyAction;
import com.watabou.noosa.audio.Music; import com.watabou.noosa.audio.Music;
import com.watabou.noosa.audio.Sample; import com.watabou.noosa.audio.Sample;
import com.watabou.utils.Callback; import com.watabou.utils.Callback;
@ -100,6 +100,7 @@ public class Game implements ApplicationListener {
Blending.useDefault(); Blending.useDefault();
inputHandler = new InputHandler( Gdx.input ); inputHandler = new InputHandler( Gdx.input );
KeyAction.initialize();
//refreshes texture and vertex data stored on the gpu //refreshes texture and vertex data stored on the gpu
TextureCache.reload(); TextureCache.reload();

View File

@ -21,6 +21,7 @@
package com.watabou.noosa; package com.watabou.noosa;
import com.watabou.input.KeyAction;
import com.watabou.input.KeyBindings; import com.watabou.input.KeyBindings;
import com.watabou.input.KeyEvent; import com.watabou.input.KeyEvent;
import com.watabou.utils.Signal; import com.watabou.utils.Signal;
@ -35,10 +36,10 @@ public class Scene extends Group {
public boolean onSignal( KeyEvent event ) { public boolean onSignal( KeyEvent event ) {
if (Game.instance != null && event.pressed) { if (Game.instance != null && event.pressed) {
switch (KeyBindings.getBinding( event )) { switch (KeyBindings.getBinding( event )) {
case BACK: case KeyAction.BACK:
onBackPressed(); onBackPressed();
break; break;
case MENU: case KeyAction.MENU:
onMenuPressed(); onMenuPressed();
break; break;
} }

View File

@ -66,7 +66,7 @@ public class Button extends Component {
KeyEvent.addKeyListener( keyListener = new Signal.Listener<KeyEvent>() { KeyEvent.addKeyListener( keyListener = new Signal.Listener<KeyEvent>() {
@Override @Override
public boolean onSignal ( KeyEvent event ) { public boolean onSignal ( KeyEvent event ) {
if ( active && !event.pressed && KeyBindings.getBinding( event ) != null if ( active && !event.pressed
&& KeyBindings.getBinding( event ) == keyAction()){ && KeyBindings.getBinding( event ) == keyAction()){
onClick(); onClick();
return true; return true;
@ -78,8 +78,8 @@ public class Button extends Component {
private Signal.Listener<KeyEvent> keyListener; private Signal.Listener<KeyEvent> keyListener;
public KeyAction keyAction(){ public int keyAction(){
return null; return KeyAction.NONE;
} }
@Override @Override

View File

@ -208,27 +208,27 @@ public class CellSelector extends ScrollArea {
} }
private KeyAction heldAction = null; private int heldAction = KeyAction.NONE;
private int heldTurns = 0; private int heldTurns = 0;
private Signal.Listener<KeyEvent> keyListener = new Signal.Listener<KeyEvent>() { private Signal.Listener<KeyEvent> keyListener = new Signal.Listener<KeyEvent>() {
@Override @Override
public boolean onSignal(KeyEvent event) { public boolean onSignal(KeyEvent event) {
KeyAction action = KeyBindings.getBinding( event ); int action = KeyBindings.getBinding( event );
if (!event.pressed){ if (!event.pressed){
if (heldAction != null && heldAction == action) { if (heldAction != -1 && heldAction == action) {
resetKeyHold(); resetKeyHold();
return true; return true;
} else { } else {
switch (action){ switch (action){
case ZOOM_IN: case KeyAction.ZOOM_IN:
zoom( camera.zoom+1 ); zoom( camera.zoom+1 );
return true; return true;
case ZOOM_OUT: case KeyAction.ZOOM_OUT:
zoom( camera.zoom-1 ); zoom( camera.zoom-1 );
return true; return true;
case ZOOM_DEFAULT: case KeyAction.ZOOM_DEFAULT:
zoom( PixelScene.defaultZoom ); zoom( PixelScene.defaultZoom );
return true; return true;
} }
@ -242,33 +242,33 @@ public class CellSelector extends ScrollArea {
} }
}; };
private boolean moveFromKey(KeyAction event){ private boolean moveFromKey(int event){
boolean moved = true; boolean moved = true;
int cell = Dungeon.hero.pos; int cell = Dungeon.hero.pos;
//TODO implement game actions, instead of using keys directly //TODO implement game actions, instead of using keys directly
switch (event){ switch (event){
case N: case KeyAction.N:
cell += -Dungeon.level.width(); cell += -Dungeon.level.width();
break; break;
case NE: case KeyAction.NE:
cell += +1-Dungeon.level.width(); cell += +1-Dungeon.level.width();
break; break;
case E: case KeyAction.E:
cell += +1; cell += +1;
break; break;
case SE: case KeyAction.SE:
cell += +1+Dungeon.level.width(); cell += +1+Dungeon.level.width();
break; break;
case S: case KeyAction.S:
cell += +Dungeon.level.width(); cell += +Dungeon.level.width();
break; break;
case SW: case KeyAction.SW:
cell += -1+Dungeon.level.width(); cell += -1+Dungeon.level.width();
break; break;
case W: case KeyAction.W:
cell += -1; cell += -1;
break; break;
case NW: case KeyAction.NW:
cell += -1-Dungeon.level.width(); cell += -1-Dungeon.level.width();
break; break;
default: default:
@ -287,7 +287,7 @@ public class CellSelector extends ScrollArea {
} }
public void processKeyHold(){ public void processKeyHold(){
if (heldAction != null){ if (heldAction != KeyAction.NONE){
enabled = true; enabled = true;
heldTurns++; heldTurns++;
moveFromKey(heldAction); moveFromKey(heldAction);
@ -295,7 +295,7 @@ public class CellSelector extends ScrollArea {
} }
public void resetKeyHold(){ public void resetKeyHold(){
heldAction = null; heldAction = KeyAction.NONE;
heldTurns = 0; heldTurns = 0;
CharSprite.setMoveInterval( CharSprite.DEFAULT_MOVE_INTERVAL ); CharSprite.setMoveInterval( CharSprite.DEFAULT_MOVE_INTERVAL );
} }

View File

@ -43,7 +43,7 @@ public class ActionIndicator extends Tag {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.TAG_ACTION; return KeyAction.TAG_ACTION;
} }

View File

@ -59,7 +59,7 @@ public class AttackIndicator extends Tag {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.TAG_ATTACK; return KeyAction.TAG_ATTACK;
} }

View File

@ -49,7 +49,7 @@ public class DangerIndicator extends Tag {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.TAG_DANGER; return KeyAction.TAG_DANGER;
} }

View File

@ -42,7 +42,7 @@ public class LootIndicator extends Tag {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.TAG_LOOT; return KeyAction.TAG_LOOT;
} }

View File

@ -95,7 +95,7 @@ public class QuickSlotButton extends Button implements WndBag.Listener {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return QuickSlotButton.this.keyAction(); return QuickSlotButton.this.keyAction();
} }
@Override @Override
@ -142,7 +142,7 @@ public class QuickSlotButton extends Button implements WndBag.Listener {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
switch (slotNum){ switch (slotNum){
case 0: case 0:
return KeyAction.QUICKSLOT_1; return KeyAction.QUICKSLOT_1;

View File

@ -40,7 +40,7 @@ public class ResumeIndicator extends Tag {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.TAG_RESUME; return KeyAction.TAG_RESUME;
} }

View File

@ -38,7 +38,6 @@ import com.watabou.noosa.Camera;
import com.watabou.noosa.Game; import com.watabou.noosa.Game;
import com.watabou.noosa.Image; import com.watabou.noosa.Image;
import com.watabou.noosa.NinePatch; import com.watabou.noosa.NinePatch;
import com.watabou.noosa.PointerArea;
import com.watabou.noosa.audio.Sample; import com.watabou.noosa.audio.Sample;
import com.watabou.noosa.particles.Emitter; import com.watabou.noosa.particles.Emitter;
import com.watabou.noosa.ui.Button; import com.watabou.noosa.ui.Button;
@ -90,7 +89,7 @@ public class StatusPane extends Component {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.HERO_INFO; return KeyAction.HERO_INFO;
} }
}.setRect( 0, 1, 30, 30 )); }.setRect( 0, 1, 30, 30 ));
@ -265,7 +264,7 @@ public class StatusPane extends Component {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.JOURNAL; return KeyAction.JOURNAL;
} }

View File

@ -88,7 +88,7 @@ public class Toolbar extends Component {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.WAIT; return KeyAction.WAIT;
} }
@ -112,7 +112,7 @@ public class Toolbar extends Component {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.SEARCH; return KeyAction.SEARCH;
} }
@ -132,7 +132,7 @@ public class Toolbar extends Component {
} }
@Override @Override
public KeyAction keyAction() { public int keyAction() {
return KeyAction.INVENTORY; return KeyAction.INVENTORY;
} }

View File

@ -24,6 +24,7 @@ package com.shatteredpixel.shatteredpixeldungeon.ui;
import com.shatteredpixel.shatteredpixeldungeon.Chrome; import com.shatteredpixel.shatteredpixeldungeon.Chrome;
import com.shatteredpixel.shatteredpixeldungeon.effects.ShadowBox; import com.shatteredpixel.shatteredpixeldungeon.effects.ShadowBox;
import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene; import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene;
import com.watabou.input.KeyAction;
import com.watabou.input.KeyBindings; import com.watabou.input.KeyBindings;
import com.watabou.input.KeyEvent; import com.watabou.input.KeyEvent;
import com.watabou.input.PointerEvent; import com.watabou.input.PointerEvent;
@ -158,10 +159,10 @@ public class Window extends Group implements Signal.Listener<KeyEvent> {
public boolean onSignal( KeyEvent event ) { public boolean onSignal( KeyEvent event ) {
if (event.pressed) { if (event.pressed) {
switch (KeyBindings.getBinding( event )) { switch (KeyBindings.getBinding( event )) {
case BACK: case KeyAction.BACK:
onBackPressed(); onBackPressed();
return true; return true;
case MENU: case KeyAction.MENU:
onMenuPressed(); onMenuPressed();
return true; return true;
} }