diff --git a/SPD-classes/src/main/java/com/watabou/input/InputHandler.java b/SPD-classes/src/main/java/com/watabou/input/InputHandler.java index ebfe4691a..b79f0bb6e 100644 --- a/SPD-classes/src/main/java/com/watabou/input/InputHandler.java +++ b/SPD-classes/src/main/java/com/watabou/input/InputHandler.java @@ -21,12 +21,19 @@ package com.watabou.input; +import com.badlogic.gdx.Input; import com.badlogic.gdx.InputAdapter; import com.watabou.noosa.Game; import com.watabou.utils.PointF; public class InputHandler extends InputAdapter { + public InputHandler( Input input ){ + input.setInputProcessor( this ); + input.setCatchKey( Input.Keys.BACK, true); + input.setCatchKey( Input.Keys.MENU, true); + } + public void processAllEvents(){ PointerEvent.processPointerEvents(); KeyEvent.processKeyEvents(); @@ -79,16 +86,22 @@ public class InputHandler extends InputAdapter { @Override public synchronized boolean keyDown( int keyCode ) { - //TODO should check if key is mapped and ignore if it isn't? - KeyEvent.addKeyEvent( new KeyEvent(keyCode, true) ); - return true; + if (KeyBindings.isBound( keyCode )) { + KeyEvent.addKeyEvent( new KeyEvent( keyCode, true ) ); + return true; + } else { + return false; + } } @Override public synchronized boolean keyUp( int keyCode ) { - //TODO should check if key is mapped and ignore if it isn't? - KeyEvent.addKeyEvent( new KeyEvent(keyCode, false) ); - return true; + if (KeyBindings.isBound( keyCode )) { + KeyEvent.addKeyEvent( new KeyEvent( keyCode, false ) ); + return true; + } else { + return false; + } } // ******************** diff --git a/SPD-classes/src/main/java/com/watabou/input/KeyAction.java b/SPD-classes/src/main/java/com/watabou/input/KeyAction.java new file mode 100644 index 000000000..8e6c9979b --- /dev/null +++ b/SPD-classes/src/main/java/com/watabou/input/KeyAction.java @@ -0,0 +1,31 @@ +/* + * Pixel Dungeon + * Copyright (C) 2012-2015 Oleg Dolya + * + * Shattered Pixel Dungeon + * Copyright (C) 2014-2019 Evan Debenham + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.watabou.input; + +public enum KeyAction { + BACK, + MENU, + + N, NE, E, SE, S, SW, W, NW, + + UNKNOWN +} diff --git a/SPD-classes/src/main/java/com/watabou/input/KeyBindings.java b/SPD-classes/src/main/java/com/watabou/input/KeyBindings.java new file mode 100644 index 000000000..3586c0ffa --- /dev/null +++ b/SPD-classes/src/main/java/com/watabou/input/KeyBindings.java @@ -0,0 +1,61 @@ +/* + * Pixel Dungeon + * Copyright (C) 2012-2015 Oleg Dolya + * + * Shattered Pixel Dungeon + * Copyright (C) 2014-2019 Evan Debenham + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.watabou.input; + +import com.badlogic.gdx.Input; + +import java.util.HashMap; + +public class KeyBindings { + + private static HashMap bindings; + + static { + bindings = new HashMap<>(); + + bindings.put( Input.Keys.BACK, KeyAction.BACK ); + bindings.put( Input.Keys.MENU, KeyAction.MENU ); + + 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 ){ + return bindings.keySet().contains( keyCode ); + } + + public static KeyAction getBinding( KeyEvent event ){ + return bindings.get( event.code ); + } + +} diff --git a/SPD-classes/src/main/java/com/watabou/input/KeyEvent.java b/SPD-classes/src/main/java/com/watabou/input/KeyEvent.java index 4b67ac53e..9c9c4cece 100644 --- a/SPD-classes/src/main/java/com/watabou/input/KeyEvent.java +++ b/SPD-classes/src/main/java/com/watabou/input/KeyEvent.java @@ -21,7 +21,6 @@ package com.watabou.input; -import com.badlogic.gdx.Input; import com.watabou.utils.Signal; import java.util.ArrayList; @@ -40,9 +39,6 @@ public class KeyEvent { // *** Static members *** // ********************** - public static final int BACK = Input.Keys.BACK; - public static final int MENU = Input.Keys.MENU; - private static Signal keySignal = new Signal<>( true ); public static void addKeyListener( Signal.Listener listener ){ diff --git a/SPD-classes/src/main/java/com/watabou/noosa/Game.java b/SPD-classes/src/main/java/com/watabou/noosa/Game.java index 0eacd0a16..e0d50facd 100644 --- a/SPD-classes/src/main/java/com/watabou/noosa/Game.java +++ b/SPD-classes/src/main/java/com/watabou/noosa/Game.java @@ -99,10 +99,7 @@ public class Game implements ApplicationListener { Blending.useDefault(); - inputHandler = new InputHandler(); - Gdx.input.setInputProcessor(inputHandler); - Gdx.input.setCatchKey(KeyEvent.BACK, true); - Gdx.input.setCatchKey(KeyEvent.MENU, true); + inputHandler = new InputHandler( Gdx.input ); //refreshes texture and vertex data stored on the gpu TextureCache.reload(); diff --git a/SPD-classes/src/main/java/com/watabou/noosa/Scene.java b/SPD-classes/src/main/java/com/watabou/noosa/Scene.java index 2504d056d..7eb4871a6 100644 --- a/SPD-classes/src/main/java/com/watabou/noosa/Scene.java +++ b/SPD-classes/src/main/java/com/watabou/noosa/Scene.java @@ -21,6 +21,7 @@ package com.watabou.noosa; +import com.watabou.input.KeyBindings; import com.watabou.input.KeyEvent; import com.watabou.utils.Signal; @@ -33,13 +34,13 @@ public class Scene extends Group { @Override public boolean onSignal( KeyEvent event ) { if (Game.instance != null && event.pressed) { - switch (event.code) { - case KeyEvent.BACK: - onBackPressed(); - break; - case KeyEvent.MENU: - onMenuPressed(); - break; + switch (KeyBindings.getBinding( event )) { + case BACK: + onBackPressed(); + break; + case MENU: + onMenuPressed(); + break; } } return false; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/CellSelector.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/CellSelector.java index 9454f9461..ceb21524e 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/CellSelector.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/CellSelector.java @@ -30,6 +30,7 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.Mob; import com.shatteredpixel.shatteredpixeldungeon.items.Heap; import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite; import com.shatteredpixel.shatteredpixeldungeon.tiles.DungeonTilemap; +import com.watabou.input.KeyBindings; import com.watabou.input.KeyEvent; import com.watabou.input.PointerEvent; import com.watabou.input.ScrollEvent; @@ -231,31 +232,31 @@ public class CellSelector extends ScrollArea { boolean moved = true; int cell = Dungeon.hero.pos; //TODO implement game actions, instead of using keys directly - switch (event.code){ - case Input.Keys.NUMPAD_7: - cell += -1-Dungeon.level.width(); - break; - case Input.Keys.UP: case Input.Keys.NUMPAD_8: + switch (KeyBindings.getBinding( event )){ + case N: cell += -Dungeon.level.width(); break; - case Input.Keys.NUMPAD_9: + case NE: cell += +1-Dungeon.level.width(); break; - case Input.Keys.RIGHT: case Input.Keys.NUMPAD_6: + case E: cell += +1; break; - case Input.Keys.NUMPAD_3: + case SE: cell += +1+Dungeon.level.width(); break; - case Input.Keys.DOWN: case Input.Keys.NUMPAD_2: + case S: cell += +Dungeon.level.width(); break; - case Input.Keys.NUMPAD_1: + case SW: cell += -1+Dungeon.level.width(); break; - case Input.Keys.LEFT: case Input.Keys.NUMPAD_4: + case W: cell += -1; break; + case NW: + cell += -1-Dungeon.level.width(); + break; default: moved = false; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/Window.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/Window.java index 6b255f516..4fd07cba6 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/Window.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/Window.java @@ -24,6 +24,7 @@ package com.shatteredpixel.shatteredpixeldungeon.ui; import com.shatteredpixel.shatteredpixeldungeon.Chrome; import com.shatteredpixel.shatteredpixeldungeon.effects.ShadowBox; import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene; +import com.watabou.input.KeyBindings; import com.watabou.input.KeyEvent; import com.watabou.input.PointerEvent; import com.watabou.noosa.Camera; @@ -156,11 +157,11 @@ public class Window extends Group implements Signal.Listener { @Override public boolean onSignal( KeyEvent event ) { if (event.pressed) { - switch (event.code) { - case KeyEvent.BACK: + switch (KeyBindings.getBinding( event )) { + case BACK: onBackPressed(); return true; - case KeyEvent.MENU: + case MENU: onMenuPressed(); return true; }