Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature android touch updates #3159

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,9 @@ public static void onActivityResult(int requestCode, int resultCode,Intent inten
public static native void onTouchUp(int id,float x,float y,float pressure);
public static native void onTouchMoved(int id,float x,float y,float pressure);
public static native void onTouchCancelled(int id,float x,float y);

public static native void onSwipe(int id, int swipeDir);
public static native void onTouchDragged(int id, int dragDir, float x,float y);
public static native void onSwipe(int id, int swipeDir, float velocityX, float velocityY);


public static native void onKeyDown(int keyCode);
public static native void onKeyUp(int keyCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,70 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve

if(velocityX > OFGestureListener.swipe_Min_Velocity && xDistance > OFGestureListener.swipe_Min_Distance){
if(e1.getX() > e2.getX()) // right to left
OFAndroid.onSwipe(e1.getPointerId(0),SWIPE_LEFT);
OFAndroid.onSwipe(e1.getPointerId(0),SWIPE_LEFT, velocityX, velocityY);
else
OFAndroid.onSwipe(e1.getPointerId(0),SWIPE_RIGHT);
OFAndroid.onSwipe(e1.getPointerId(0),SWIPE_RIGHT, velocityX, velocityY);

result = true;
}else if(velocityY > OFGestureListener.swipe_Min_Velocity && yDistance > OFGestureListener.swipe_Min_Distance){
if(e1.getY() > e2.getY()) // bottom to up
OFAndroid.onSwipe(e1.getPointerId(0),SWIPE_UP);
OFAndroid.onSwipe(e1.getPointerId(0),SWIPE_UP, velocityX, velocityY);
else
OFAndroid.onSwipe(e1.getPointerId(0),SWIPE_DOWN);
OFAndroid.onSwipe(e1.getPointerId(0),SWIPE_DOWN, velocityX, velocityY);

result = true;
}

return result;
}



@Override
public void onLongPress(MotionEvent arg0) {
}

@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
return super.onScroll(arg0, arg1, arg2, arg3);
public boolean onScroll(MotionEvent eventStart, MotionEvent event,
float distanceX, float distanceY) {

final int action = event.getAction();
final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = event.getPointerId(pointerIndex);
boolean result = false;

float absDX = Math.abs(distanceX);
float absDY = Math.abs(distanceY);
switch ((action & MotionEvent.ACTION_MASK)) {
case MotionEvent.ACTION_MOVE: {
if (absDY > absDX) {

if (distanceY > 0) // bottom to up
OFAndroid.onTouchDragged(pointerId, DRAG_UP,
distanceX, distanceY);
else
OFAndroid.onTouchDragged(pointerId, DRAG_DOWN,
distanceX, distanceY);

result = true;

} else {
if (distanceX > 0) // right to left
OFAndroid.onTouchDragged(pointerId, DRAG_LEFT,
distanceX, distanceY);
else
OFAndroid.onTouchDragged(pointerId, DRAG_RIGHT,
distanceX, distanceY);

result = true;
}
break;
}
}
return result;
}


@Override
public void onShowPress(MotionEvent arg0) {
}
Expand All @@ -137,10 +175,14 @@ public boolean onSingleTapUp(MotionEvent event) {
private GestureDetector gestureDetector;
View.OnTouchListener touchListener;
public static int swipe_Min_Distance = 100;
public static int swipe_Max_Distance = 350;
public static int swipe_Min_Velocity = 100;
public static int swipe_Max_Distance = 2560;
public static int swipe_Min_Velocity = 50;
public final static int SWIPE_UP = 1;
public final static int SWIPE_DOWN = 2;
public final static int SWIPE_LEFT = 3;
public final static int SWIPE_RIGHT = 4;
public final static int DRAG_UP = 5;
public final static int DRAG_DOWN = 6;
public final static int DRAG_LEFT = 7;
public final static int DRAG_RIGHT = 8;
}
13 changes: 11 additions & 2 deletions addons/ofxAndroid/src/ofAppAndroidWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,9 @@ Java_cc_openframeworks_OFAndroid_onTouchDoubleTap(JNIEnv* env, jclass thiz, ji
}

void
Java_cc_openframeworks_OFAndroid_onSwipe(JNIEnv* env, jclass thiz, jint id, jint swipeDir){
Java_cc_openframeworks_OFAndroid_onSwipe(JNIEnv* env, jclass thiz, jint id, jint swipeDir, jfloat vX, jfloat vY){
if(androidApp){
androidApp->swipe((ofxAndroidSwipeDir)swipeDir,id);
androidApp->swipe((ofxAndroidSwipeDir)swipeDir,id, vX, vY);
}
}

Expand All @@ -530,6 +530,15 @@ Java_cc_openframeworks_OFAndroid_onKeyUp(JNIEnv* env, jobject thiz, jint keyC
ofNotifyKeyReleased(keyCode);
}

void
Java_cc_openframeworks_OFAndroid_onTouchDragged(JNIEnv* env, jclass thiz,
jint id, jint dragDir, jfloat x, jfloat y) {
if (androidApp) {
androidApp->touchDragged((ofxAndroidDragDir) dragDir, id, x, y);
}
}


jboolean
Java_cc_openframeworks_OFAndroid_onBackPressed(){
ofLogVerbose("ofAppAndroidWindow") << "back pressed";
Expand Down
26 changes: 22 additions & 4 deletions addons/ofxAndroid/src/ofxAndroidApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ enum ofxAndroidSwipeDir{
OFX_ANDROID_SWIPE_RIGHT = 4
};

enum ofxAndroidDragDir{
OFX_ANDROID_DRAG_UP = 5,
OFX_ANDROID_DRAG_DOWN = 6,
OFX_ANDROID_DRAG_LEFT = 7,
OFX_ANDROID_DRAG_RIGHT = 8
};


class ofxAndroidApp: public ofBaseApp{
public:
virtual void pause(){};
Expand All @@ -32,26 +40,36 @@ class ofxAndroidApp: public ofBaseApp{
virtual void touchUp(int x, int y, int id) {};
virtual void touchDoubleTap(int x, int y, int id) {};
virtual void touchCancelled(int x, int y, int id) {};
OF_DEPRECATED_MSG("swipe(ofxAndroidSwipeDir swipeDir, int id) is deprecated, please update to swipe(ofxAndroidSwipeDir swipeDir, int id, float velocityX, float velocityY)",virtual void swipe(ofxAndroidSwipeDir swipeDir, int id){};)

virtual void touchDown(ofTouchEventArgs & touch) {
touchDown(touch.x, touch.y, touch.id);
};

virtual void touchMoved(ofTouchEventArgs & touch) {
touchMoved(touch.x, touch.y, touch.id);
};

virtual void touchUp(ofTouchEventArgs & touch) {
touchUp(touch.x, touch.y, touch.id);
};

virtual void touchDoubleTap(ofTouchEventArgs & touch) {
touchDoubleTap(touch.x, touch.y, touch.id);
};

virtual void touchCancelled(ofTouchEventArgs & touch){
touchCancelled(touch.x, touch.y, touch.id);
}
virtual void swipe(ofxAndroidSwipeDir swipeDir, int id){

}
};

virtual void swipe(ofxAndroidSwipeDir swipeDir, int id, float velocityX, float velocityY){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to keep the old function and call it from the new one, so we don't break old code

swipe(swipeDir, id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey, I guess I'm confused and/or not familiar enough with the Android intricacies, but why do we get a new method with new parameters, when we just discard the new parameters in the next line? Is this just for overloading/child-class implementation?

};

virtual void touchDragged(ofxAndroidDragDir swipeDir, int id, float dx, float dy) {

};

virtual bool backPressed(){
return false;
}
Expand Down