v1.2.0: added hover-based pointer events
This commit is contained in:
@@ -27,7 +27,6 @@ import com.badlogic.gdx.InputAdapter;
|
||||
import com.badlogic.gdx.InputMultiplexer;
|
||||
import com.badlogic.gdx.InputProcessor;
|
||||
import com.watabou.noosa.Game;
|
||||
import com.watabou.utils.PointF;
|
||||
|
||||
public class InputHandler extends InputAdapter {
|
||||
|
||||
@@ -91,29 +90,25 @@ public class InputHandler extends InputAdapter {
|
||||
@Override
|
||||
public synchronized boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
||||
Gdx.input.setOnscreenKeyboardVisible(false); //in-game events never need keyboard, so hide it
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true));
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, PointerEvent.Type.DOWN));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, false));
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, PointerEvent.Type.UP));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean touchDragged(int screenX, int screenY, int pointer) {
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true));
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, PointerEvent.Type.DOWN));
|
||||
return true;
|
||||
}
|
||||
|
||||
//TODO tracking this should probably be in PointerEvent
|
||||
private static PointF pointerHoverPos = new PointF();
|
||||
|
||||
@Override
|
||||
public boolean mouseMoved(int screenX, int screenY) {
|
||||
pointerHoverPos.x = screenX;
|
||||
pointerHoverPos.y = screenY;
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, -1, PointerEvent.Type.HOVER));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -147,7 +142,7 @@ public class InputHandler extends InputAdapter {
|
||||
|
||||
@Override
|
||||
public boolean scrolled(float amountX, float amountY) {
|
||||
ScrollEvent.addScrollEvent( new ScrollEvent(pointerHoverPos, amountY));
|
||||
ScrollEvent.addScrollEvent( new ScrollEvent(PointerEvent.lastHoverPos, amountY));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,16 +28,24 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PointerEvent {
|
||||
|
||||
|
||||
public enum Type {
|
||||
DOWN,
|
||||
UP,
|
||||
HOVER
|
||||
}
|
||||
|
||||
public PointF start;
|
||||
public PointF current;
|
||||
public int id;
|
||||
public boolean down;
|
||||
public Type type;
|
||||
public boolean handled; //for hover events, to ensure hover always ends even with overlapping elements
|
||||
|
||||
public PointerEvent( int x, int y, int id, boolean down){
|
||||
public PointerEvent( int x, int y, int id, Type type){
|
||||
start = current = new PointF(x, y);
|
||||
this.id = id;
|
||||
this.down = down;
|
||||
this.type = type;
|
||||
handled = false;
|
||||
}
|
||||
|
||||
public void update( PointerEvent other ){
|
||||
@@ -49,7 +57,12 @@ public class PointerEvent {
|
||||
}
|
||||
|
||||
public PointerEvent up() {
|
||||
down = false;
|
||||
if (type == Type.DOWN) type = Type.UP;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PointerEvent handle(){
|
||||
handled = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -74,6 +87,7 @@ public class PointerEvent {
|
||||
// Accumulated pointer events
|
||||
private static ArrayList<PointerEvent> pointerEvents = new ArrayList<>();
|
||||
private static HashMap<Integer, PointerEvent> activePointers = new HashMap<>();
|
||||
static PointF lastHoverPos = new PointF();
|
||||
|
||||
public static synchronized void addPointerEvent( PointerEvent event ){
|
||||
pointerEvents.add( event );
|
||||
@@ -81,19 +95,22 @@ public class PointerEvent {
|
||||
|
||||
public static synchronized void processPointerEvents(){
|
||||
for (PointerEvent p : pointerEvents){
|
||||
if (p.type == Type.HOVER){
|
||||
lastHoverPos.set(p.current);
|
||||
}
|
||||
if (activePointers.containsKey(p.id)){
|
||||
PointerEvent existing = activePointers.get(p.id);
|
||||
existing.current = p.current;
|
||||
if (existing.down == p.down){
|
||||
if (existing.type == p.type){
|
||||
pointerSignal.dispatch( null );
|
||||
} else if (p.down) {
|
||||
} else if (p.type == Type.DOWN) {
|
||||
pointerSignal.dispatch( existing );
|
||||
} else {
|
||||
activePointers.remove(existing.id);
|
||||
pointerSignal.dispatch(existing.up());
|
||||
}
|
||||
} else {
|
||||
if (p.down) {
|
||||
if (p.type == Type.DOWN) {
|
||||
activePointers.put(p.id, p);
|
||||
}
|
||||
pointerSignal.dispatch(p);
|
||||
|
||||
Reference in New Issue
Block a user