Merge pull request #5423 from SeannyM/misc-touch

Android: Various touch control improvements
This commit is contained in:
Markus Wick 2017-05-17 11:05:33 +02:00 committed by GitHub
commit 392936a5e5
10 changed files with 134 additions and 77 deletions

View File

@ -133,25 +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);
invalidate();
} }
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);
invalidate();
break; break;
} }
} }
@ -163,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)))
{ {
@ -193,21 +189,20 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
} }
setDpadState(dpad, up, down, left, right); setDpadState(dpad, up, down, left, right);
invalidate(); 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);
invalidate();
break; break;
} }
} }
@ -224,6 +219,8 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
} }
} }
invalidate();
return true; return true;
} }
@ -333,11 +330,9 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
mJoystickBeingConfigured = null; mJoystickBeingConfigured = null;
} }
break; break;
} }
} }
return true; return true;
} }
@ -414,15 +409,13 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
} }
if (mPreferences.getBoolean("buttonToggleGc9", true)) if (mPreferences.getBoolean("buttonToggleGc9", true))
{ {
overlayJoysticks.add(initializeOverlayJoystick(getContext(), overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.gcwii_joystick_range,
R.drawable.gcwii_joystick_range, R.drawable.gcwii_joystick, R.drawable.gcwii_joystick, R.drawable.gcwii_joystick_pressed, ButtonType.STICK_MAIN));
ButtonType.STICK_MAIN));
} }
if (mPreferences.getBoolean("buttonToggleGc10", true)) if (mPreferences.getBoolean("buttonToggleGc10", true))
{ {
overlayJoysticks.add(initializeOverlayJoystick(getContext(), overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.gcwii_joystick_range,
R.drawable.gcwii_joystick_range, R.drawable.gcpad_c, R.drawable.gcpad_c, R.drawable.gcpad_c_pressed, ButtonType.STICK_C));
ButtonType.STICK_C));
} }
} }
@ -487,9 +480,8 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
} }
if (mPreferences.getBoolean("buttonToggleWii10", true)) if (mPreferences.getBoolean("buttonToggleWii10", true))
{ {
overlayJoysticks.add(initializeOverlayJoystick(getContext(), overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.gcwii_joystick_range,
R.drawable.gcwii_joystick_range, R.drawable.gcwii_joystick, R.drawable.gcwii_joystick, R.drawable.gcwii_joystick_pressed, ButtonType.NUNCHUK_STICK));
ButtonType.NUNCHUK_STICK));
} }
} }
@ -548,15 +540,13 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
} }
if (mPreferences.getBoolean("buttonToggleClassic12", true)) if (mPreferences.getBoolean("buttonToggleClassic12", true))
{ {
overlayJoysticks.add(initializeOverlayJoystick(getContext(), overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.gcwii_joystick_range,
R.drawable.gcwii_joystick_range, R.drawable.gcwii_joystick, R.drawable.gcwii_joystick, R.drawable.gcwii_joystick_pressed, ButtonType.CLASSIC_STICK_LEFT));
ButtonType.CLASSIC_STICK_LEFT));
} }
if (mPreferences.getBoolean("buttonToggleClassic13", true)) if (mPreferences.getBoolean("buttonToggleClassic13", true))
{ {
overlayJoysticks.add(initializeOverlayJoystick(getContext(), overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.gcwii_joystick_range,
R.drawable.gcwii_joystick_range, R.drawable.gcwii_joystick, R.drawable.gcwii_joystick, R.drawable.gcwii_joystick_pressed, ButtonType.CLASSIC_STICK_RIGHT));
ButtonType.CLASSIC_STICK_RIGHT));
} }
} }
@ -787,12 +777,13 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
* *
* @param context The current {@link Context} * @param context The current {@link Context}
* @param resOuter Resource ID for the outer image of the joystick (the static image that shows the circular bounds). * @param resOuter Resource ID for the outer image of the joystick (the static image that shows the circular bounds).
* @param resInner Resource ID for the inner image of the joystick (the one you actually move around). * @param defaultResInner Resource ID for the default inner image of the joystick (the one you actually move around).
* @param pressedResInner Resource ID for the pressed inner image of the joystick.
* @param joystick Identifier for which joystick this is. * @param joystick Identifier for which joystick this is.
* *
* @return the initialized {@link InputOverlayDrawableJoystick}. * @return the initialized {@link InputOverlayDrawableJoystick}.
*/ */
private static InputOverlayDrawableJoystick initializeOverlayJoystick(Context context, int resOuter, int resInner, int joystick) private static InputOverlayDrawableJoystick initializeOverlayJoystick(Context context, int resOuter, int defaultResInner, int pressedResInner, int joystick)
{ {
// Resources handle for fetching the initial Drawable resource. // Resources handle for fetching the initial Drawable resource.
final Resources res = context.getResources(); final Resources res = context.getResources();
@ -807,7 +798,8 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
// Initialize the InputOverlayDrawableJoystick. // Initialize the InputOverlayDrawableJoystick.
final Bitmap bitmapOuter = resizeBitmap(context, BitmapFactory.decodeResource(res, resOuter), scale); final Bitmap bitmapOuter = resizeBitmap(context, BitmapFactory.decodeResource(res, resOuter), scale);
final Bitmap bitmapInner = BitmapFactory.decodeResource(res, resInner); final Bitmap bitmapInnerDefault = BitmapFactory.decodeResource(res, defaultResInner);
final Bitmap bitmapInnerPressed = BitmapFactory.decodeResource(res, pressedResInner);
// The X and Y coordinates of the InputOverlayDrawableButton on the InputOverlay. // The X and Y coordinates of the InputOverlayDrawableButton on the InputOverlay.
// These were set in the input overlay configuration menu. // These were set in the input overlay configuration menu.
@ -835,10 +827,9 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
// Send the drawableId to the joystick so it can be referenced when saving control position. // Send the drawableId to the joystick so it can be referenced when saving control position.
final InputOverlayDrawableJoystick overlayDrawable final InputOverlayDrawableJoystick overlayDrawable
= new InputOverlayDrawableJoystick(res, = new InputOverlayDrawableJoystick(res, bitmapOuter,
bitmapOuter, bitmapInner, bitmapInnerDefault, bitmapInnerPressed,
outerRect, innerRect, outerRect, innerRect, joystick);
joystick);
// Need to set the image's position // Need to set the image's position
overlayDrawable.setPosition(drawableX, drawableY); overlayDrawable.setPosition(drawableX, drawableY);

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();

View File

@ -18,42 +18,52 @@ import android.view.View;
* Custom {@link BitmapDrawable} that is capable * Custom {@link BitmapDrawable} that is capable
* of storing it's own ID. * of storing it's own ID.
*/ */
public final class InputOverlayDrawableJoystick extends BitmapDrawable public final class InputOverlayDrawableJoystick
{ {
private final int[] axisIDs = {0, 0, 0, 0}; private final int[] axisIDs = {0, 0, 0, 0};
private final float[] axises = {0f, 0f}; private final float[] axises = {0f, 0f};
private final BitmapDrawable ringInner;
private int trackId = -1; private int trackId = -1;
private int mJoystickType; private int mJoystickType;
private int mControlPositionX, mControlPositionY; private int mControlPositionX, mControlPositionY;
private int mPreviousTouchX, mPreviousTouchY; private int mPreviousTouchX, mPreviousTouchY;
private int mWidth;
private int mHeight;
private BitmapDrawable mOuterBitmap;
private BitmapDrawable mDefaultStateInnerBitmap;
private BitmapDrawable mPressedStateInnerBitmap;
private boolean mPressedState = false;
/** /**
* Constructor * Constructor
* *
* @param res {@link Resources} instance. * @param res {@link Resources} instance.
* @param bitmapOuter {@link Bitmap} which represents the outer non-movable part of the joystick. * @param bitmapOuter {@link Bitmap} which represents the outer non-movable part of the joystick.
* @param bitmapInner {@link Bitmap} which represents the inner movable part of the joystick. * @param bitmapInnerDefault {@link Bitmap} which represents the default inner movable part of the joystick.
* @param bitmapInnerPressed {@link Bitmap} which represents the pressed inner movable part of the joystick.
* @param rectOuter {@link Rect} which represents the outer joystick bounds. * @param rectOuter {@link Rect} which represents the outer joystick bounds.
* @param rectInner {@link Rect} which represents the inner joystick bounds. * @param rectInner {@link Rect} which represents the inner joystick bounds.
* @param joystick Identifier for which joystick this is. * @param joystick Identifier for which joystick this is.
*/ */
public InputOverlayDrawableJoystick(Resources res, public InputOverlayDrawableJoystick(Resources res, Bitmap bitmapOuter,
Bitmap bitmapOuter, Bitmap bitmapInner, Bitmap bitmapInnerDefault, Bitmap bitmapInnerPressed,
Rect rectOuter, Rect rectInner, Rect rectOuter, Rect rectInner, int joystick)
int joystick)
{ {
super(res, bitmapOuter); axisIDs[0] = joystick + 1;
this.setBounds(rectOuter); axisIDs[1] = joystick + 2;
axisIDs[2] = joystick + 3;
this.ringInner = new BitmapDrawable(res, bitmapInner); axisIDs[3] = joystick + 4;
this.ringInner.setBounds(rectInner);
SetInnerBounds();
this.axisIDs[0] = joystick + 1;
this.axisIDs[1] = joystick + 2;
this.axisIDs[2] = joystick + 3;
this.axisIDs[3] = joystick + 4;
mJoystickType = joystick; mJoystickType = joystick;
mOuterBitmap = new BitmapDrawable(res, bitmapOuter);
mDefaultStateInnerBitmap = new BitmapDrawable(res, bitmapInnerDefault);
mPressedStateInnerBitmap = new BitmapDrawable(res, bitmapInnerPressed);
mWidth = bitmapOuter.getWidth();
mHeight = bitmapOuter.getHeight();
setBounds(rectOuter);
mDefaultStateInnerBitmap.setBounds(rectInner);
mPressedStateInnerBitmap.setBounds(rectInner);
SetInnerBounds();
} }
/** /**
@ -66,11 +76,10 @@ public final class InputOverlayDrawableJoystick extends BitmapDrawable
return mJoystickType; return mJoystickType;
} }
@Override
public void draw(Canvas canvas) public void draw(Canvas canvas)
{ {
super.draw(canvas); mOuterBitmap.draw(canvas);
ringInner.draw(canvas); getCurrentStateBitmapDrawable().draw(canvas);
} }
public void TrackEvent(MotionEvent event) public void TrackEvent(MotionEvent event)
@ -82,12 +91,16 @@ public final class InputOverlayDrawableJoystick extends BitmapDrawable
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN: case MotionEvent.ACTION_POINTER_DOWN:
if (getBounds().contains((int)event.getX(pointerIndex), (int)event.getY(pointerIndex))) if (getBounds().contains((int)event.getX(pointerIndex), (int)event.getY(pointerIndex)))
{
mPressedState = true;
trackId = event.getPointerId(pointerIndex); trackId = event.getPointerId(pointerIndex);
}
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_POINTER_UP:
if (trackId == event.getPointerId(pointerIndex)) if (trackId == event.getPointerId(pointerIndex))
{ {
mPressedState = false;
axises[0] = axises[1] = 0.0f; axises[0] = axises[1] = 0.0f;
SetInnerBounds(); SetInnerBounds();
trackId = -1; trackId = -1;
@ -136,12 +149,13 @@ public final class InputOverlayDrawableJoystick extends BitmapDrawable
int deltaY = fingerPositionY - mPreviousTouchY; int deltaY = fingerPositionY - mPreviousTouchY;
mControlPositionX += deltaX; mControlPositionX += deltaX;
mControlPositionY += deltaY; mControlPositionY += deltaY;
setBounds(new Rect(mControlPositionX, mControlPositionY, getBitmap().getWidth() + mControlPositionX, getBitmap().getHeight() + mControlPositionY)); setBounds(new Rect(mControlPositionX, mControlPositionY,
mOuterBitmap.getIntrinsicWidth() + mControlPositionX,
mOuterBitmap.getIntrinsicHeight() + mControlPositionY));
SetInnerBounds(); SetInnerBounds();
mPreviousTouchX = fingerPositionX; mPreviousTouchX = fingerPositionX;
mPreviousTouchY = fingerPositionY; mPreviousTouchY = fingerPositionY;
break; break;
} }
return true; return true;
} }
@ -164,17 +178,22 @@ public final class InputOverlayDrawableJoystick extends BitmapDrawable
private void SetInnerBounds() private void SetInnerBounds()
{ {
float floatX = this.getBounds().centerX(); int X = getBounds().centerX() + (int)((axises[1]) * (getBounds().width() / 2));
float floatY = this.getBounds().centerY(); int Y = getBounds().centerY() + (int)((axises[0]) * (getBounds().height() / 2));
floatY += axises[0] * (this.getBounds().height() / 2);
floatX += axises[1] * (this.getBounds().width() / 2); if (X > getBounds().centerX() + (getBounds().width() / 2))
int X = (int)(floatX); X = getBounds().centerX() + (getBounds().width() / 2);
int Y = (int)(floatY); if (X < getBounds().centerX() - (getBounds().width() / 2))
int width = this.ringInner.getBounds().width() / 2; X = getBounds().centerX() - (getBounds().width() / 2);
int height = this.ringInner.getBounds().height() / 2; if (Y > getBounds().centerY() + (getBounds().height() / 2))
this.ringInner.setBounds(X - width, Y - height, Y = getBounds().centerY() + (getBounds().height() / 2);
X + width, Y + height); if (Y < getBounds().centerY() - (getBounds().height() / 2))
ringInner.invalidateSelf(); Y = getBounds().centerY() - (getBounds().height() / 2);
int width = mPressedStateInnerBitmap.getBounds().width() / 2;
int height = mPressedStateInnerBitmap.getBounds().height() / 2;
mDefaultStateInnerBitmap.setBounds(X - width, Y - height, X + width, Y + height);
mPressedStateInnerBitmap.setBounds(mDefaultStateInnerBitmap.getBounds());
} }
public void setPosition(int x, int y) public void setPosition(int x, int y)
@ -182,4 +201,29 @@ public final class InputOverlayDrawableJoystick extends BitmapDrawable
mControlPositionX = x; mControlPositionX = x;
mControlPositionY = y; mControlPositionY = y;
} }
private BitmapDrawable getCurrentStateBitmapDrawable()
{
return mPressedState ? mPressedStateInnerBitmap : mDefaultStateInnerBitmap;
}
public void setBounds(Rect bounds)
{
mOuterBitmap.setBounds(bounds);
}
public Rect getBounds()
{
return mOuterBitmap.getBounds();
}
public int getWidth()
{
return mWidth;
}
public int getHeight()
{
return mHeight;
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB