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.InputMultiplexer;
|
||||||
import com.badlogic.gdx.InputProcessor;
|
import com.badlogic.gdx.InputProcessor;
|
||||||
import com.watabou.noosa.Game;
|
import com.watabou.noosa.Game;
|
||||||
import com.watabou.utils.PointF;
|
|
||||||
|
|
||||||
public class InputHandler extends InputAdapter {
|
public class InputHandler extends InputAdapter {
|
||||||
|
|
||||||
@@ -91,29 +90,25 @@ public class InputHandler extends InputAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public synchronized boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
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
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean touchDragged(int screenX, int screenY, int pointer) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO tracking this should probably be in PointerEvent
|
|
||||||
private static PointF pointerHoverPos = new PointF();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean mouseMoved(int screenX, int screenY) {
|
public boolean mouseMoved(int screenX, int screenY) {
|
||||||
pointerHoverPos.x = screenX;
|
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, -1, PointerEvent.Type.HOVER));
|
||||||
pointerHoverPos.y = screenY;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +142,7 @@ public class InputHandler extends InputAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean scrolled(float amountX, float amountY) {
|
public boolean scrolled(float amountX, float amountY) {
|
||||||
ScrollEvent.addScrollEvent( new ScrollEvent(pointerHoverPos, amountY));
|
ScrollEvent.addScrollEvent( new ScrollEvent(PointerEvent.lastHoverPos, amountY));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,16 +28,24 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class PointerEvent {
|
public class PointerEvent {
|
||||||
|
|
||||||
|
public enum Type {
|
||||||
|
DOWN,
|
||||||
|
UP,
|
||||||
|
HOVER
|
||||||
|
}
|
||||||
|
|
||||||
public PointF start;
|
public PointF start;
|
||||||
public PointF current;
|
public PointF current;
|
||||||
public int id;
|
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);
|
start = current = new PointF(x, y);
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.down = down;
|
this.type = type;
|
||||||
|
handled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update( PointerEvent other ){
|
public void update( PointerEvent other ){
|
||||||
@@ -49,7 +57,12 @@ public class PointerEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public PointerEvent up() {
|
public PointerEvent up() {
|
||||||
down = false;
|
if (type == Type.DOWN) type = Type.UP;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PointerEvent handle(){
|
||||||
|
handled = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +87,7 @@ public class PointerEvent {
|
|||||||
// Accumulated pointer events
|
// Accumulated pointer events
|
||||||
private static ArrayList<PointerEvent> pointerEvents = new ArrayList<>();
|
private static ArrayList<PointerEvent> pointerEvents = new ArrayList<>();
|
||||||
private static HashMap<Integer, PointerEvent> activePointers = new HashMap<>();
|
private static HashMap<Integer, PointerEvent> activePointers = new HashMap<>();
|
||||||
|
static PointF lastHoverPos = new PointF();
|
||||||
|
|
||||||
public static synchronized void addPointerEvent( PointerEvent event ){
|
public static synchronized void addPointerEvent( PointerEvent event ){
|
||||||
pointerEvents.add( event );
|
pointerEvents.add( event );
|
||||||
@@ -81,19 +95,22 @@ public class PointerEvent {
|
|||||||
|
|
||||||
public static synchronized void processPointerEvents(){
|
public static synchronized void processPointerEvents(){
|
||||||
for (PointerEvent p : pointerEvents){
|
for (PointerEvent p : pointerEvents){
|
||||||
|
if (p.type == Type.HOVER){
|
||||||
|
lastHoverPos.set(p.current);
|
||||||
|
}
|
||||||
if (activePointers.containsKey(p.id)){
|
if (activePointers.containsKey(p.id)){
|
||||||
PointerEvent existing = activePointers.get(p.id);
|
PointerEvent existing = activePointers.get(p.id);
|
||||||
existing.current = p.current;
|
existing.current = p.current;
|
||||||
if (existing.down == p.down){
|
if (existing.type == p.type){
|
||||||
pointerSignal.dispatch( null );
|
pointerSignal.dispatch( null );
|
||||||
} else if (p.down) {
|
} else if (p.type == Type.DOWN) {
|
||||||
pointerSignal.dispatch( existing );
|
pointerSignal.dispatch( existing );
|
||||||
} else {
|
} else {
|
||||||
activePointers.remove(existing.id);
|
activePointers.remove(existing.id);
|
||||||
pointerSignal.dispatch(existing.up());
|
pointerSignal.dispatch(existing.up());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (p.down) {
|
if (p.type == Type.DOWN) {
|
||||||
activePointers.put(p.id, p);
|
activePointers.put(p.id, p);
|
||||||
}
|
}
|
||||||
pointerSignal.dispatch(p);
|
pointerSignal.dispatch(p);
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ public class PointerArea extends Visual implements Signal.Listener<PointerEvent>
|
|||||||
public Visual target;
|
public Visual target;
|
||||||
|
|
||||||
protected PointerEvent curEvent = null;
|
protected PointerEvent curEvent = null;
|
||||||
|
protected boolean hovered = false;
|
||||||
|
|
||||||
public int blockLevel = BLOCK_WHEN_ACTIVE;
|
public int blockLevel = BLOCK_WHEN_ACTIVE;
|
||||||
public static final int ALWAYS_BLOCK = 0; //Always block input to overlapping elements
|
public static final int ALWAYS_BLOCK = 0; //Always block input to overlapping elements
|
||||||
@@ -54,7 +55,7 @@ public class PointerArea extends Visual implements Signal.Listener<PointerEvent>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onSignal( PointerEvent event ) {
|
public boolean onSignal( PointerEvent event ) {
|
||||||
|
|
||||||
boolean hit = event != null && target.overlapsScreenPoint( (int)event.current.x, (int)event.current.y );
|
boolean hit = event != null && target.overlapsScreenPoint( (int)event.current.x, (int)event.current.y );
|
||||||
|
|
||||||
if (!isActive()) {
|
if (!isActive()) {
|
||||||
@@ -63,16 +64,16 @@ public class PointerArea extends Visual implements Signal.Listener<PointerEvent>
|
|||||||
|
|
||||||
if (hit) {
|
if (hit) {
|
||||||
|
|
||||||
boolean returnValue = (event.down || event == curEvent);
|
boolean returnValue = (event.type == PointerEvent.Type.DOWN || event == curEvent);
|
||||||
|
|
||||||
if (event.down) {
|
if (event.type == PointerEvent.Type.DOWN) {
|
||||||
|
|
||||||
if (curEvent == null) {
|
if (curEvent == null) {
|
||||||
curEvent = event;
|
curEvent = event;
|
||||||
}
|
}
|
||||||
onPointerDown( event );
|
onPointerDown( event );
|
||||||
|
|
||||||
} else {
|
} else if (event.type == PointerEvent.Type.UP) {
|
||||||
|
|
||||||
onPointerUp( event );
|
onPointerUp( event );
|
||||||
|
|
||||||
@@ -81,6 +82,15 @@ public class PointerArea extends Visual implements Signal.Listener<PointerEvent>
|
|||||||
onClick( event );
|
onClick( event );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if (event.type == PointerEvent.Type.HOVER) {
|
||||||
|
if (event.handled && hovered){
|
||||||
|
hovered = false;
|
||||||
|
onHoverEnd(event);
|
||||||
|
} else if (!event.handled && !hovered){
|
||||||
|
hovered = true;
|
||||||
|
onHoverStart(event);
|
||||||
|
}
|
||||||
|
event.handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnValue && blockLevel != NEVER_BLOCK;
|
return returnValue && blockLevel != NEVER_BLOCK;
|
||||||
@@ -89,11 +99,14 @@ public class PointerArea extends Visual implements Signal.Listener<PointerEvent>
|
|||||||
|
|
||||||
if (event == null && curEvent != null) {
|
if (event == null && curEvent != null) {
|
||||||
onDrag(curEvent);
|
onDrag(curEvent);
|
||||||
}
|
|
||||||
|
} else if (curEvent != null && event.type == PointerEvent.Type.UP) {
|
||||||
else if (curEvent != null && !event.down) {
|
|
||||||
onPointerUp( event );
|
onPointerUp( event );
|
||||||
curEvent = null;
|
curEvent = null;
|
||||||
|
|
||||||
|
} else if (event != null && event.type == PointerEvent.Type.HOVER && hovered){
|
||||||
|
hovered = false;
|
||||||
|
onHoverEnd(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -108,6 +121,10 @@ public class PointerArea extends Visual implements Signal.Listener<PointerEvent>
|
|||||||
protected void onClick( PointerEvent event ) { }
|
protected void onClick( PointerEvent event ) { }
|
||||||
|
|
||||||
protected void onDrag( PointerEvent event ) { }
|
protected void onDrag( PointerEvent event ) { }
|
||||||
|
|
||||||
|
protected void onHoverStart( PointerEvent event ) { }
|
||||||
|
|
||||||
|
protected void onHoverEnd( PointerEvent event ) { }
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
curEvent = null;
|
curEvent = null;
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ public class CellSelector extends ScrollArea {
|
|||||||
|
|
||||||
if (event != curEvent && another == null) {
|
if (event != curEvent && another == null) {
|
||||||
|
|
||||||
if (!curEvent.down) {
|
if (curEvent.type == PointerEvent.Type.UP) {
|
||||||
curEvent = event;
|
curEvent = event;
|
||||||
onPointerDown( event );
|
onPointerDown( event );
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user