v0.7.4b: refactored input event handling structure

This commit is contained in:
Evan Debenham
2019-07-25 19:50:35 -04:00
parent 2a523f2ea2
commit 05d7f354dd
36 changed files with 502 additions and 506 deletions

View File

@@ -24,97 +24,65 @@ package com.watabou.input;
import com.badlogic.gdx.InputAdapter;
import com.watabou.noosa.Game;
import java.util.ArrayList;
public class InputHandler extends InputAdapter {
// Accumulated touch events
protected ArrayList<Touchscreen.Touch> touchEvents = new ArrayList<>();
// Accumulated key events
protected ArrayList<Keys.Key> keyEvents = new ArrayList<>();
@Override
public boolean keyDown( int keyCode ) {
if (keyCode != Keys.BACK &&
keyCode != Keys.MENU) {
return false;
}
synchronized (keyEvents) {
keyEvents.add( new Keys.Key(keyCode, true) );
}
return true;
}
@Override
public boolean keyUp( int keyCode ) {
if (keyCode != Keys.BACK &&
keyCode != Keys.MENU) {
return false;
}
synchronized (keyEvents) {
keyEvents.add( new Keys.Key(keyCode, false) );
}
return true;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
synchronized (touchEvents) {
touchEvents.add(new Touchscreen.Touch(screenX, screenY, pointer, true));
}
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
synchronized (touchEvents) {
touchEvents.add(new Touchscreen.Touch(screenX, screenY, pointer, false));
}
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
synchronized (touchEvents) {
touchEvents.add(new Touchscreen.Touch(screenX, screenY, pointer, true));
}
return true;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
public void processAllEvents(){
synchronized (touchEvents) {
Touchscreen.processTouchEvents( touchEvents );
touchEvents.clear();
}
synchronized (keyEvents) {
Keys.processKeyEvents( keyEvents );
keyEvents.clear();
}
PointerEvent.processPointerEvents();
KeyEvent.processKeyEvents();
}
// *********************
// *** Pointer Input ***
// *********************
@Override
public synchronized boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true));
return true;
}
@Override
public synchronized boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, false));
return true;
}
@Override
public synchronized boolean touchDragged(int screenX, int screenY, int pointer) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true));
return true;
}
// *****************
// *** Key Input ***
// *****************
@Override
public synchronized boolean keyDown( int keyCode ) {
if (keyCode != KeyEvent.BACK && keyCode != KeyEvent.MENU) {
return false;
}
KeyEvent.addKeyEvent( new KeyEvent(keyCode, true) );
return true;
}
@Override
public synchronized boolean keyUp( int keyCode ) {
if (keyCode != KeyEvent.BACK && keyCode != KeyEvent.MENU) {
return false;
}
KeyEvent.addKeyEvent( new KeyEvent(keyCode, false) );
return true;
}
}

View File

@@ -26,28 +26,48 @@ import com.watabou.utils.Signal;
import java.util.ArrayList;
//TODO probably want to merge this into a central input processor class
public class Keys {
public class KeyEvent {
public int code;
public boolean pressed;
public KeyEvent( int code, boolean pressed ) {
this.code = code;
this.pressed = pressed;
}
// **********************
// *** Static members ***
// **********************
public static final int BACK = Input.Keys.BACK;
public static final int MENU = Input.Keys.MENU;
public static Signal<Key> event = new Signal<>( true );
public static void processKeyEvents( ArrayList<Key> events ){
for (Key k : events){
event.dispatch(k);
}
private static Signal<KeyEvent> keySignal = new Signal<>( true );
public static void addKeyListener( Signal.Listener<KeyEvent> listener ){
keySignal.add(listener);
}
public static class Key {
public int code;
public boolean pressed;
public Key( int code, boolean pressed ) {
this.code = code;
this.pressed = pressed;
public static void removeKeyListener( Signal.Listener<KeyEvent> listener ){
keySignal.remove(listener);
}
public static void clearListeners(){
keySignal.removeAll();
}
//Accumulated key events
private static ArrayList<KeyEvent> keyEvents = new ArrayList<>();
public static synchronized void addKeyEvent( KeyEvent event ){
keyEvents.add( event );
}
public static synchronized void processKeyEvents(){
for (KeyEvent k : keyEvents){
keySignal.dispatch(k);
}
keyEvents.clear();
}
}

View File

@@ -0,0 +1,104 @@
/*
* 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.watabou.utils.PointF;
import com.watabou.utils.Signal;
import java.util.ArrayList;
import java.util.HashMap;
public class PointerEvent {
public PointF start;
public PointF current;
public int id;
public boolean down;
public PointerEvent( int x, int y, int id, boolean down){
start = current = new PointF(x, y);
this.id = id;
this.down = down;
}
public void update( PointerEvent other ){
this.current = other.current;
}
public void update( int x, int y ){
current.set( x, y );
}
public PointerEvent up() {
down = false;
return this;
}
// **********************
// *** Static members ***
// **********************
private static Signal<PointerEvent> pointerSignal = new Signal<>( true );
public static void addPointerListener( Signal.Listener<PointerEvent> listener ){
pointerSignal.add(listener);
}
public static void removePointerListener( Signal.Listener<PointerEvent> listener ){
pointerSignal.remove(listener);
}
public static void clearListeners(){
pointerSignal.removeAll();
}
// Accumulated pointer events
private static ArrayList<PointerEvent> pointerEvents = new ArrayList<>();
private static HashMap<Integer, PointerEvent> activePointers = new HashMap<>();
public static synchronized void addPointerEvent( PointerEvent event ){
pointerEvents.add( event );
}
public static synchronized void processPointerEvents(){
for (PointerEvent p : pointerEvents){
if (activePointers.containsKey(p.id)){
PointerEvent existing = activePointers.get(p.id);
existing.current = p.current;
if (existing.down == p.down){
pointerSignal.dispatch( null );
} else if (p.down) {
pointerSignal.dispatch( existing );
} else {
activePointers.remove(existing.id);
pointerSignal.dispatch(existing.up());
}
} else {
if (p.down) {
activePointers.put(p.id, p);
}
pointerSignal.dispatch(p);
}
}
pointerEvents.clear();
}
}

View File

@@ -1,86 +0,0 @@
/*
* 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.watabou.utils.PointF;
import com.watabou.utils.Signal;
import java.util.ArrayList;
import java.util.HashMap;
//TODO integrate into a central input handler class
public class Touchscreen {
public static Signal<Touch> event = new Signal<>( true );
public static HashMap<Integer,Touch> pointers = new HashMap<>();
public static void processTouchEvents( ArrayList<Touch> events ) {
for (Touch t : events){
if (pointers.containsKey(t.id)){
Touch existing = pointers.get(t.id);
existing.current = t.current;
if (existing.down == t.down){
event.dispatch( null );
} else if (t.down) {
event.dispatch( existing );
} else {
pointers.remove(existing.id);
event.dispatch(existing.up());
}
} else {
if (t.down) {
pointers.put(t.id, t);
}
event.dispatch(t);
}
}
}
public static class Touch {
public PointF start;
public PointF current;
public int id;
public boolean down;
public Touch( int x, int y, int id, boolean down){
start = current = new PointF(x, y);
this.id = id;
this.down = down;
}
public void update( Touch other ){
this.current = other.current;
}
public void update( int x, int y ){
current.set( x, y );
}
public Touch up() {
down = false;
return this;
}
}
}