Touch movements scaled to match screen scaling.

This commit is contained in:
James Sanford 2013-07-26 02:51:20 -07:00
parent 19dfbdd245
commit 2726b3f2c7
2 changed files with 46 additions and 29 deletions

View File

@ -391,6 +391,8 @@ public class KegsMain extends SherlockFragmentActivity implements KegsKeyboard.S
mKegsView.updateScreenSize(bitmapSize);
// Update scale of mouse movements.
mKegsTouch.updateScale(bitmapSize.getScaleX(), bitmapSize.getScaleY());
// Update special click zone that toggles the ActionBar.
final TouchSpecialZone zone = new TouchSpecialZone(getSpecialActionBarRect(bitmapSize)) {

View File

@ -21,13 +21,16 @@ class KegsTouch {
private boolean mPrimaryPastSlop = false;
private boolean mPrimaryLongPress = false;
private int mPrimaryX = -1; // original X
private int mPrimaryY = -1; // original Y
private int mPrimaryLastX = -1; // last seen X
private int mPrimaryLastY = -1; // last seen Y
private float mPrimaryX = -1.0f; // original X
private float mPrimaryY = -1.0f; // original Y
private float mPrimaryLastX = -1.0f; // last seen X
private float mPrimaryLastY = -1.0f; // last seen Y
private int mSecondaryX = -1; // in case of promotion to primary
private int mSecondaryY = -1; // in case of promotion to primary
private float mSecondaryX = -1.0f; // in case of promotion to primary
private float mSecondaryY = -1.0f; // in case of promotion to primary
private float mScaleX = 1.0f;
private float mScaleY = 1.0f;
private static final int LONG_PRESS = 1;
private static final int LONG_PRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout();
@ -42,6 +45,11 @@ class KegsTouch {
mTouchSlopSquare = touchSlop * touchSlop;
}
public void updateScale(float x, float y) {
mScaleX = x;
mScaleY = y;
}
public void setSpecialZone(TouchSpecialZone zone) {
mSpecialZone = zone;
}
@ -60,8 +68,8 @@ class KegsTouch {
// For promotion of secondary to primary. Crazy stuff.
private boolean checkSecondarySlop(MotionEvent newPoint, int index) {
final int deltaX = (int)newPoint.getX(index) - mSecondaryX;
final int deltaY = (int)newPoint.getY(index) - mSecondaryY;
final int deltaX = (int)newPoint.getX(index) - (int)mSecondaryX;
final int deltaY = (int)newPoint.getY(index) - (int)mSecondaryY;
final int distance = (deltaX * deltaX) + (deltaY * deltaY);
if (distance > mTouchSlopSquare) {
return true;
@ -75,8 +83,8 @@ class KegsTouch {
return true;
}
final int deltaX = (int)newPoint.getX(index) - mPrimaryX;
final int deltaY = (int)newPoint.getY(index) - mPrimaryY;
final int deltaX = (int)newPoint.getX(index) - (int)mPrimaryX;
final int deltaY = (int)newPoint.getY(index) - (int)mPrimaryY;
final int distance = (deltaX * deltaX) + (deltaY * deltaY);
if (distance > mTouchSlopSquare) {
mPrimaryPastSlop = true;
@ -96,8 +104,8 @@ class KegsTouch {
if (mPrimaryId == -1 && mSecondaryId != pointerId) {
// First new finger down becomes movement.
mPrimaryId = pointerId;
mPrimaryX = (int)event.getX(pointerIndex);
mPrimaryY = (int)event.getY(pointerIndex);
mPrimaryX = event.getX(pointerIndex);
mPrimaryY = event.getY(pointerIndex);
mPrimaryLastX = mPrimaryX;
mPrimaryLastY = mPrimaryY;
mPrimaryPastSlop = false;
@ -109,8 +117,8 @@ class KegsTouch {
} else {
// Any subequent fingers become the mouse button.
mSecondaryId = pointerId;
mSecondaryX = (int)event.getX(pointerIndex);
mSecondaryY = (int)event.getY(pointerIndex);
mSecondaryX = event.getX(pointerIndex);
mSecondaryY = event.getY(pointerIndex);
mButton1 = 1;
mEventQueue.add(new Event.MouseKegsEvent(0, 0, mButton1, 1));
}
@ -144,8 +152,8 @@ class KegsTouch {
mButton1 = 0;
mEventQueue.add(new Event.MouseKegsEvent(0, 0, mButton1, 1));
mSecondaryId = -1;
mSecondaryX = -1;
mSecondaryY = -1;
mSecondaryX = -1.0f;
mSecondaryY = -1.0f;
return true;
}
} else if (action == MotionEvent.ACTION_CANCEL) {
@ -161,8 +169,8 @@ class KegsTouch {
return true;
} else if (pointerId == mSecondaryId) {
mSecondaryId = -1;
mSecondaryX = -1;
mSecondaryY = -1;
mSecondaryX = -1.0f;
mSecondaryY = -1.0f;
if ((mButton1 & 1) == 1) {
mButton1 = 0;
mEventQueue.add(new Event.MouseKegsEvent(0, 0, mButton1, 1));
@ -176,7 +184,7 @@ class KegsTouch {
for(moveIndex = 0; moveIndex < event.getPointerCount(); moveIndex++) {
moveId = event.getPointerId(moveIndex);
if (moveId == mSecondaryId && mPrimaryId != -1 && !mPrimaryPastSlop && mSecondaryX != -1 && mSecondaryY != -1) {
if (moveId == mSecondaryId && mPrimaryId != -1 && !mPrimaryPastSlop && mSecondaryX != -1.0f && mSecondaryY != -1.0f) {
// If the secondary goes past slop now, swap primary and secondary...
// This allows you to click with one finger, and then drag the next
// and that drag becomes the primary finger.
@ -189,24 +197,31 @@ class KegsTouch {
mPrimaryY = mSecondaryY;
mPrimaryLastX = mSecondaryX;
mPrimaryLastY = mSecondaryY;
mSecondaryX = -1;
mSecondaryY = -1;
mSecondaryX = -1.0f;
mSecondaryY = -1.0f;
}
// Let this fall through to process the event.
}
if (moveId == mPrimaryId) {
if (checkPrimarySlop(event, moveIndex)) {
mHandler.removeMessages(LONG_PRESS);
final int currentX = (int)event.getX(moveIndex);
final int currentY = (int)event.getY(moveIndex);
final int changeX = currentX - mPrimaryLastX;
final int changeY = currentY - mPrimaryLastY;
mPrimaryLastX = currentX;
mPrimaryLastY = currentY;
final float currentX = event.getX(moveIndex);
final float currentY = event.getY(moveIndex);
int changeX = (int)((currentX - mPrimaryLastX) / mScaleX);
int changeY = (int)((currentY - mPrimaryLastY) / mScaleY);
// Only track the last value when we send a change.
// That way if it keeps moving 'one' point at a time,
// it will 'eventually' cause a change.
if (changeX != 0) {
mPrimaryLastX = currentX;
}
if (changeY != 0) {
mPrimaryLastY = currentY;
}
mEventQueue.add(new Event.MouseKegsEvent(changeX, changeY, mButton1, 1));
// Once we have had an active primary, don't allow promotions.
mSecondaryX = -1;
mSecondaryY = -1;
mSecondaryX = -1.0f;
mSecondaryY = -1.0f;
}
}
}