v0.7.4b: Initial LibGDX commit! more details below:
Large sections of game logic are now working through libgdx instead of android libraries. There is still work to do but this is the first major step. Big changes include: - Graphics code is now through LibGDX (except for text rendering) - Initialization and screen-handling logic is now mostly through LibGDX - Audio is now through LibGDX - Input handling is now through LibGDX - Most misc functions are now through LibGDX
This commit is contained in:
120
SPD-classes/src/main/java/com/watabou/input/InputHandler.java
Normal file
120
SPD-classes/src/main/java/com/watabou/input/InputHandler.java
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* 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.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,34 +21,22 @@
|
||||
|
||||
package com.watabou.input;
|
||||
|
||||
import android.view.KeyEvent;
|
||||
|
||||
import com.badlogic.gdx.Input;
|
||||
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 static final int BACK = KeyEvent.KEYCODE_BACK;
|
||||
public static final int MENU = KeyEvent.KEYCODE_MENU;
|
||||
public static final int BACK = Input.Keys.BACK;
|
||||
public static final int MENU = Input.Keys.MENU;
|
||||
|
||||
public static Signal<Key> event = new Signal<Key>( true );
|
||||
public static Signal<Key> event = new Signal<>( true );
|
||||
|
||||
public static void processTouchEvents( ArrayList<KeyEvent> events ) {
|
||||
|
||||
int size = events.size();
|
||||
for (int i=0; i < size; i++) {
|
||||
|
||||
KeyEvent e = events.get( i );
|
||||
|
||||
switch (e.getAction()) {
|
||||
case KeyEvent.ACTION_DOWN:
|
||||
event.dispatch( new Key( e.getKeyCode(), true ) );
|
||||
break;
|
||||
case KeyEvent.ACTION_UP:
|
||||
event.dispatch( new Key( e.getKeyCode(), false ) );
|
||||
break;
|
||||
}
|
||||
public static void processKeyEvents( ArrayList<Key> events ){
|
||||
for (Key k : events){
|
||||
event.dispatch(k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,71 +21,38 @@
|
||||
|
||||
package com.watabou.input;
|
||||
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import com.watabou.noosa.Game;
|
||||
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<Touch>( true );
|
||||
public static Signal<Touch> event = new Signal<>( true );
|
||||
|
||||
public static HashMap<Integer,Touch> pointers = new HashMap<Integer, Touch>();
|
||||
public static HashMap<Integer,Touch> pointers = new HashMap<>();
|
||||
|
||||
public static float x;
|
||||
public static float y;
|
||||
public static boolean touched;
|
||||
|
||||
public static void processTouchEvents( ArrayList<MotionEvent> events ) {
|
||||
|
||||
int size = events.size();
|
||||
for (int i=0; i < size; i++) {
|
||||
|
||||
MotionEvent e = events.get( i );
|
||||
Touch touch;
|
||||
|
||||
switch (e.getAction() & MotionEvent.ACTION_MASK) {
|
||||
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
touched = true;
|
||||
touch = new Touch( e, 0 );
|
||||
pointers.put( e.getPointerId( 0 ), touch );
|
||||
event.dispatch( touch );
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_POINTER_DOWN:
|
||||
int index = e.getActionIndex();
|
||||
touch = new Touch( e, index );
|
||||
pointers.put( e.getPointerId( index ), touch );
|
||||
event.dispatch( touch );
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
int count = e.getPointerCount();
|
||||
for (int j=0; j < count; j++) {
|
||||
if (pointers.containsKey(e.getPointerId(j))) {
|
||||
pointers.get(e.getPointerId(j)).update(e, j);
|
||||
}
|
||||
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());
|
||||
}
|
||||
event.dispatch( null );
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_POINTER_UP:
|
||||
event.dispatch( pointers.remove( e.getPointerId( e.getActionIndex() ) ).up() );
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_UP:
|
||||
touched = false;
|
||||
event.dispatch( pointers.remove( e.getPointerId( 0 ) ).up() );
|
||||
break;
|
||||
|
||||
} else {
|
||||
if (t.down) {
|
||||
pointers.put(t.id, t);
|
||||
}
|
||||
event.dispatch(t);
|
||||
}
|
||||
|
||||
e.recycle();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,29 +60,20 @@ public class Touchscreen {
|
||||
|
||||
public PointF start;
|
||||
public PointF current;
|
||||
public int id;
|
||||
public boolean down;
|
||||
|
||||
public Touch( MotionEvent e, int index ) {
|
||||
|
||||
float x = e.getX( index );
|
||||
float y = e.getY( index );
|
||||
|
||||
x /= (Game.dispWidth / (float)Game.width);
|
||||
y /= (Game.dispHeight / (float)Game.height);
|
||||
|
||||
start = new PointF( x, y );
|
||||
current = new PointF( x, y );
|
||||
|
||||
down = true;
|
||||
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( MotionEvent e, int index ) {
|
||||
float x = e.getX( index );
|
||||
float y = e.getY( index );
|
||||
|
||||
x /= (Game.dispWidth / (float)Game.width);
|
||||
y /= (Game.dispHeight / (float)Game.height);
|
||||
|
||||
public void update( Touch other ){
|
||||
this.current = other.current;
|
||||
}
|
||||
|
||||
public void update( int x, int y ){
|
||||
current.set( x, y );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user