Android: Prevent stuck on-screen buttons

This commit is contained in:
Sean Maas 2017-05-13 22:33:25 -04:00
parent 6190b1c4e2
commit 3bd8890486
3 changed files with 28 additions and 7 deletions

View File

@ -133,23 +133,22 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
{ {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN: case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_MOVE:
// If a pointer enters the bounds of a button, press that button. // If a pointer enters the bounds of a button, press that button.
if (button.getBounds().contains((int)event.getX(pointerIndex), (int)event.getY(pointerIndex))) if (button.getBounds().contains((int)event.getX(pointerIndex), (int)event.getY(pointerIndex)))
{ {
button.setPressedState(true); button.setPressedState(true);
button.setTrackId(event.getPointerId(pointerIndex));
NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, button.getId(), ButtonState.PRESSED); NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, button.getId(), ButtonState.PRESSED);
} }
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_POINTER_UP:
// If a pointer ends, release the button it was pressing. // If a pointer ends, release the button it was pressing.
if (button.getBounds().contains((int)event.getX(pointerIndex), (int)event.getY(pointerIndex))) if (button.getTrackId() == event.getPointerId(pointerIndex))
{ {
button.setPressedState(false);
NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, button.getId(), ButtonState.RELEASED); NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, button.getId(), ButtonState.RELEASED);
} }
button.setPressedState(false);
break; break;
} }
} }
@ -161,7 +160,6 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
{ {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN: case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_MOVE:
// If a pointer enters the bounds of a button, press that button. // If a pointer enters the bounds of a button, press that button.
if (dpad.getBounds().contains((int)event.getX(pointerIndex), (int)event.getY(pointerIndex))) if (dpad.getBounds().contains((int)event.getX(pointerIndex), (int)event.getY(pointerIndex)))
{ {
@ -191,19 +189,20 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
} }
setDpadState(dpad, up, down, left, right); setDpadState(dpad, up, down, left, right);
dpad.setTrackId(event.getPointerId(pointerIndex));
} }
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_POINTER_UP:
// If a pointer ends, release the buttons. // If a pointer ends, release the buttons.
if (dpad.getBounds().contains((int)event.getX(pointerIndex), (int)event.getY(pointerIndex))) if (dpad.getTrackId() == event.getPointerId(pointerIndex))
{ {
for(int i = 0; i < 4; i++) for(int i = 0; i < 4; i++)
{ {
dpad.setState(InputOverlayDrawableDpad.STATE_DEFAULT);
NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, dpad.getId(i), ButtonState.RELEASED); NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, dpad.getId(i), ButtonState.RELEASED);
} }
} }
dpad.setState(InputOverlayDrawableDpad.STATE_DEFAULT);
break; break;
} }
} }

View File

@ -21,6 +21,7 @@ public final class InputOverlayDrawableButton
{ {
// The ID identifying what type of button this Drawable represents. // The ID identifying what type of button this Drawable represents.
private int mButtonType; private int mButtonType;
private int mTrackId;
private int mPreviousTouchX, mPreviousTouchY; private int mPreviousTouchX, mPreviousTouchY;
private int mControlPositionX, mControlPositionY; private int mControlPositionX, mControlPositionY;
private int mWidth; private int mWidth;
@ -57,6 +58,16 @@ public final class InputOverlayDrawableButton
return mButtonType; return mButtonType;
} }
public void setTrackId(int trackId)
{
mTrackId = trackId;
}
public int getTrackId()
{
return mTrackId;
}
public boolean onConfigureTouch(MotionEvent event) public boolean onConfigureTouch(MotionEvent event)
{ {
int pointerIndex = event.getActionIndex(); int pointerIndex = event.getActionIndex();

View File

@ -21,6 +21,7 @@ public final class InputOverlayDrawableDpad
{ {
// The ID identifying what type of button this Drawable represents. // The ID identifying what type of button this Drawable represents.
private int[] mButtonType = new int[4]; private int[] mButtonType = new int[4];
private int mTrackId;
private int mPreviousTouchX, mPreviousTouchY; private int mPreviousTouchX, mPreviousTouchY;
private int mControlPositionX, mControlPositionY; private int mControlPositionX, mControlPositionY;
private int mWidth; private int mWidth;
@ -135,6 +136,16 @@ public final class InputOverlayDrawableDpad
return mButtonType[direction]; return mButtonType[direction];
} }
public void setTrackId(int trackId)
{
mTrackId = trackId;
}
public int getTrackId()
{
return mTrackId;
}
public boolean onConfigureTouch(MotionEvent event) public boolean onConfigureTouch(MotionEvent event)
{ {
int pointerIndex = event.getActionIndex(); int pointerIndex = event.getActionIndex();