v0.8.0: added basic key action and key mapping support

This commit is contained in:
Evan Debenham 2019-10-28 21:06:02 -04:00
parent 4f430015c3
commit df3a0a010b
8 changed files with 136 additions and 35 deletions

View File

@ -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;
}
}
// ********************

View File

@ -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 <http://www.gnu.org/licenses/>
*/
package com.watabou.input;
public enum KeyAction {
BACK,
MENU,
N, NE, E, SE, S, SW, W, NW,
UNKNOWN
}

View File

@ -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 <http://www.gnu.org/licenses/>
*/
package com.watabou.input;
import com.badlogic.gdx.Input;
import java.util.HashMap;
public class KeyBindings {
private static HashMap<Integer, KeyAction> 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 );
}
}

View File

@ -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<KeyEvent> keySignal = new Signal<>( true );
public static void addKeyListener( Signal.Listener<KeyEvent> listener ){

View File

@ -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();

View File

@ -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;

View File

@ -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;
}

View File

@ -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<KeyEvent> {
@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;
}