Skip to content

Commit

Permalink
v3.0.0: added proper support for touches cancelled by system gestures
Browse files Browse the repository at this point in the history
  • Loading branch information
00-Evan committed Jan 23, 2025
1 parent 38d2731 commit 0a602a9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
12 changes: 7 additions & 5 deletions SPD-classes/src/main/java/com/watabou/input/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ public synchronized boolean touchUp(int screenX, int screenY, int pointer, int b

@Override
public boolean touchCancelled(int screenX, int screenY, int pointer, int button) {
//currently emulating functionality from libGDX 1.11.0, do we keep this?
//in particular this is probably a more graceful way to handle things like system swipes on iOS
//whereas previously they generated garbage inputs sometimes
//which were then fixed in v2.2.2
return touchUp(screenX, screenY, pointer, button);

if (button >= 3 && KeyBindings.isKeyBound( button + 1000 )) {
KeyEvent.addKeyEvent( new KeyEvent( button + 1000, false ) );
} else if (button < 3) {
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, PointerEvent.Type.CANCEL, button));
}
return true;
}

@Override
Expand Down
19 changes: 10 additions & 9 deletions SPD-classes/src/main/java/com/watabou/input/PointerEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class PointerEvent {
public enum Type {
DOWN,
UP,
CANCEL,
HOVER
}

Expand Down Expand Up @@ -82,6 +83,11 @@ public PointerEvent up() {
return this;
}

public PointerEvent cancel() {
if (type == Type.DOWN) type = Type.CANCEL;
return this;
}

public PointerEvent handle(){
handled = true;
return this;
Expand Down Expand Up @@ -162,9 +168,12 @@ public static synchronized void processPointerEvents(){
pointerSignal.dispatch( null );
} else if (p.type == Type.DOWN) {
pointerSignal.dispatch( existing );
} else {
} else if (p.type == Type.UP){
activePointers.remove(existing.id);
pointerSignal.dispatch(existing.up());
} else if (p.type == Type.CANCEL){
activePointers.remove(existing.id);
pointerSignal.dispatch(existing.cancel());
}
} else {
if (p.type == Type.DOWN) {
Expand All @@ -185,12 +194,4 @@ public static synchronized void processPointerEvents(){
}
}

public static synchronized void clearPointerEvents(){
pointerEvents.clear();
for (PointerEvent p : activePointers.values()){
p.current = p.start = new PointF(-1, -1);
pointerSignal.dispatch(p.up());
}
activePointers.clear();
}
}
9 changes: 1 addition & 8 deletions SPD-classes/src/main/java/com/watabou/noosa/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.watabou.glwrap.Vertexbuffer;
import com.watabou.input.ControllerHandler;
import com.watabou.input.InputHandler;
import com.watabou.input.PointerEvent;
import com.watabou.noosa.audio.Music;
import com.watabou.noosa.audio.Sample;
import com.watabou.utils.Callback;
Expand Down Expand Up @@ -146,10 +145,7 @@ public void resize(int width, int height) {
}
}

///justResumed is used for two purposes:
//firstly, to clear pointer events when the game is resumed,
// this helps with input errors caused by system gestures on iOS/Android
//secondly, as a bit of a hack to improve start time metrics on Android,
//justResumed is a bit of a hack to improve start time metrics on Android,
// as texture refreshing leads to slow warm starts. TODO would be nice to fix this properly
private boolean justResumed = true;

Expand All @@ -162,7 +158,6 @@ public void render() {
}

if (justResumed){
PointerEvent.clearPointerEvents();
justResumed = false;
if (DeviceCompat.isAndroid()) return;
}
Expand All @@ -180,8 +175,6 @@ public void render() {

@Override
public void pause() {
PointerEvent.clearPointerEvents();

if (scene != null) {
scene.onPause();
}
Expand Down
14 changes: 12 additions & 2 deletions SPD-classes/src/main/java/com/watabou/noosa/PointerArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ public boolean onSignal( PointerEvent event ) {
curEvent = null;
onClick( event );
}


//similar to up, but no click
} else if (event.type == PointerEvent.Type.CANCEL) {

onPointerUp( event );

if (curEvent == event) {
curEvent = null;
}

} else if (event.type == PointerEvent.Type.HOVER) {
if (event.handled && hovered){
hovered = false;
Expand All @@ -100,7 +109,8 @@ public boolean onSignal( PointerEvent event ) {
if (event == null && curEvent != null) {
onDrag(curEvent);

} else if (curEvent != null && event.type == PointerEvent.Type.UP) {
} else if (curEvent != null &&
(event.type == PointerEvent.Type.UP || event.type == PointerEvent.Type.CANCEL)) {
onPointerUp( event );
curEvent = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.robovm.apple.glkit.GLKViewDrawableColorFormat;
import org.robovm.apple.glkit.GLKViewDrawableDepthFormat;
import org.robovm.apple.uikit.UIApplication;
import org.robovm.apple.uikit.UIRectEdge;

import java.io.File;

Expand Down Expand Up @@ -102,12 +101,6 @@ protected IOSApplication createApplication() {
config.hideHomeIndicator = SPDSettings.fullscreen();
config.overrideRingerSwitch = SPDSettings.ignoreSilentMode();

//game has to ignore input from system gestures itself, otherwise there is lag on
//every button press on the corner of the screen. Currently this is accomplished via
//clearing all pointer events on the first frame after the game is resumed.
//TODO this may not be needed anymore with libgdx 1.12.1
config.screenEdgesDeferringSystemGestures = UIRectEdge.All;

CGRect statusBarFrame = UIApplication.getSharedApplication().getStatusBarFrame();
double statusBarHeight = Math.min(statusBarFrame.getWidth(), statusBarFrame.getHeight());

Expand Down

0 comments on commit 0a602a9

Please sign in to comment.