Skip to content

Commit

Permalink
feat(android): add raw point for touch event dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and siguangli committed Jan 25, 2024
1 parent 6915e3d commit 175ed15
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public boolean needHandle(String type) {
return false;
}

@Override
public void handle(String type, MotionEvent event) {

}

@Override
public void handle(String type, float x, float y) {
if (TextUtils.equals(type, NodeProps.ON_PRESS_IN)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class NativeGestureDispatcher implements NativeGestureProcessor.Callback
private static final String KEY_TAG_ID = "id";
private static final String KEY_PAGE_X = "page_x";
private static final String KEY_PAGE_Y = "page_y";

private static final String KEY_RAW_X = "rawx";
private static final String KEY_RAW_Y = "rawy";

private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout();

private static final View.OnClickListener mOnClickListener = new View.OnClickListener() {
Expand Down Expand Up @@ -208,53 +212,113 @@ public static void handlePressOut(HippyEngineContext context, int tagId) {
}

public static void handleTouchDown(HippyEngineContext context, int mTagId, float x, float y,
int viewId) {
int viewId) {
int[] viewCoords = new int[2];
getLocationInWindow(context, viewId, viewCoords);
HippyMap params = new HippyMap();
params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_DOWN);
params.pushInt(KEY_TAG_ID, mTagId);
params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + x));
params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + y));
context.getModuleManager().getJavaScriptModule(EventDispatcher.class)
.receiveNativeGesture(params);
}

public static void handleTouchDown(HippyEngineContext context, int mTagId, MotionEvent event,
int viewId) {
int[] viewCoords = new int[2];
getLocationInWindow(context, viewId, viewCoords);
HippyMap params = new HippyMap();
params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_DOWN);
params.pushInt(KEY_TAG_ID, mTagId);
params.pushDouble(KEY_RAW_X, PixelUtil.px2dp(event.getRawX()));
params.pushDouble(KEY_RAW_Y, PixelUtil.px2dp(event.getRawY()));
params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + event.getX()));
params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + event.getY()));
context.getModuleManager().getJavaScriptModule(EventDispatcher.class)
.receiveNativeGesture(params);
}

public static void handleTouchMove(HippyEngineContext context, int mTagId, float x, float y,
int viewId) {
int viewId) {
int[] viewCoords = new int[2];
getLocationInWindow(context, viewId, viewCoords);
HippyMap params = new HippyMap();
params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_MOVE);
params.pushInt(KEY_TAG_ID, mTagId);
params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + x));
params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + y));
context.getModuleManager().getJavaScriptModule(EventDispatcher.class)
.receiveNativeGesture(params);
}

public static void handleTouchMove(HippyEngineContext context, int mTagId, MotionEvent event,
int viewId) {
int[] viewCoords = new int[2];
getLocationInWindow(context, viewId, viewCoords);
HippyMap params = new HippyMap();
params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_MOVE);
params.pushInt(KEY_TAG_ID, mTagId);
params.pushDouble(KEY_RAW_X, PixelUtil.px2dp(event.getRawX()));
params.pushDouble(KEY_RAW_Y, PixelUtil.px2dp(event.getRawY()));
params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + event.getX()));
params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + event.getY()));
context.getModuleManager().getJavaScriptModule(EventDispatcher.class)
.receiveNativeGesture(params);
}

public static void handleTouchEnd(HippyEngineContext context, int mTagId, float x, float y,
int viewId) {
int viewId) {
int[] viewCoords = new int[2];
getLocationInWindow(context, viewId, viewCoords);
HippyMap params = new HippyMap();
params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_END);
params.pushInt(KEY_TAG_ID, mTagId);
params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + x));
params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + y));
context.getModuleManager().getJavaScriptModule(EventDispatcher.class)
.receiveNativeGesture(params);
}

public static void handleTouchEnd(HippyEngineContext context, int mTagId, MotionEvent event,
int viewId) {
int[] viewCoords = new int[2];
getLocationInWindow(context, viewId, viewCoords);
HippyMap params = new HippyMap();
params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_END);
params.pushInt(KEY_TAG_ID, mTagId);
params.pushDouble(KEY_RAW_X, PixelUtil.px2dp(event.getRawX()));
params.pushDouble(KEY_RAW_Y, PixelUtil.px2dp(event.getRawY()));
params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + event.getX()));
params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + event.getY()));
context.getModuleManager().getJavaScriptModule(EventDispatcher.class)
.receiveNativeGesture(params);
}

public static void handleTouchCancel(HippyEngineContext context, int mTagId, float x, float y,
int viewId) {
int viewId) {
int[] viewCoords = new int[2];
getLocationInWindow(context, viewId, viewCoords);
HippyMap params = new HippyMap();
params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_CANCEL);
params.pushInt(KEY_TAG_ID, mTagId);
params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + x));
params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + y));
context.getModuleManager().getJavaScriptModule(EventDispatcher.class)
.receiveNativeGesture(params);
}

public static void handleTouchCancel(HippyEngineContext context, int mTagId, MotionEvent event,
int viewId) {
int[] viewCoords = new int[2];
getLocationInWindow(context, viewId, viewCoords);
HippyMap params = new HippyMap();
params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_CANCEL);
params.pushInt(KEY_TAG_ID, mTagId);
params.pushDouble(KEY_RAW_X, PixelUtil.px2dp(event.getRawX()));
params.pushDouble(KEY_RAW_Y, PixelUtil.px2dp(event.getRawY()));
params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + event.getX()));
params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + event.getY()));
context.getModuleManager().getJavaScriptModule(EventDispatcher.class)
.receiveNativeGesture(params);
}
Expand Down Expand Up @@ -328,16 +392,42 @@ public void handle(String type, float x, float y) {
handlePressOut(mEngineContext, mTargetView.getId());
} else if (TextUtils.equals(type, NodeProps.ON_TOUCH_DOWN)) {
NativeGestureDispatcher
.handleTouchDown(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId());
.handleTouchDown(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId());
} else if (TextUtils.equals(type, NodeProps.ON_TOUCH_MOVE)) {
NativeGestureDispatcher
.handleTouchMove(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId());
} else if (TextUtils.equals(type, NodeProps.ON_TOUCH_END)) {
NativeGestureDispatcher
.handleTouchEnd(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId());
} else if (TextUtils.equals(type, NodeProps.ON_TOUCH_CANCEL)) {
NativeGestureDispatcher
.handleTouchCancel(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId());
}
}

@Override
public void handle(String type, MotionEvent event) {
if (mTargetView == null) {
LogUtils.e("NativeGestureDispatcher", "handle!!! but view is null!!!!");
return;
}

if (TextUtils.equals(type, NodeProps.ON_PRESS_IN)) {
handlePressIn(mEngineContext, mTargetView.getId());
} else if (TextUtils.equals(type, NodeProps.ON_PRESS_OUT)) {
handlePressOut(mEngineContext, mTargetView.getId());
} else if (TextUtils.equals(type, NodeProps.ON_TOUCH_DOWN)) {
NativeGestureDispatcher
.handleTouchDown(mEngineContext, mTargetView.getId(), event, mTargetView.getId());
} else if (TextUtils.equals(type, NodeProps.ON_TOUCH_MOVE)) {
NativeGestureDispatcher
.handleTouchMove(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId());
.handleTouchMove(mEngineContext, mTargetView.getId(), event, mTargetView.getId());
} else if (TextUtils.equals(type, NodeProps.ON_TOUCH_END)) {
NativeGestureDispatcher
.handleTouchEnd(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId());
.handleTouchEnd(mEngineContext, mTargetView.getId(), event, mTargetView.getId());
} else if (TextUtils.equals(type, NodeProps.ON_TOUCH_CANCEL)) {
NativeGestureDispatcher
.handleTouchCancel(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId());
.handleTouchCancel(mEngineContext, mTargetView.getId(), event, mTargetView.getId());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean onTouchEvent(MotionEvent event) {
}

if (mCallback.needHandle(NodeProps.ON_TOUCH_DOWN)) {
mCallback.handle(NodeProps.ON_TOUCH_DOWN, event.getX(), event.getY());
mCallback.handle(NodeProps.ON_TOUCH_DOWN, event);
handle = true;
}

Expand All @@ -89,7 +89,7 @@ public boolean onTouchEvent(MotionEvent event) {
}
case MotionEvent.ACTION_MOVE: {
if (mCallback.needHandle(NodeProps.ON_TOUCH_MOVE)) {
mCallback.handle(NodeProps.ON_TOUCH_MOVE, event.getX(), event.getY());
mCallback.handle(NodeProps.ON_TOUCH_MOVE, event);
handle = true;
}

Expand All @@ -114,12 +114,12 @@ public boolean onTouchEvent(MotionEvent event) {
}
case MotionEvent.ACTION_UP: {
if (mCallback.needHandle(NodeProps.ON_TOUCH_END)) {
mCallback.handle(NodeProps.ON_TOUCH_END, event.getX(), event.getY());
mCallback.handle(NodeProps.ON_TOUCH_END, event);
handle = true;
}

if (mNoPressIn && mCallback.needHandle(NodeProps.ON_PRESS_OUT)) {
mCallback.handle(NodeProps.ON_PRESS_OUT, event.getX(), event.getY());
mCallback.handle(NodeProps.ON_PRESS_OUT, event);
handle = true;
} else if (!mNoPressIn && mCallback.needHandle(NodeProps.ON_PRESS_OUT)) {
getGestureHandler().sendEmptyMessageDelayed(PRESS_OUT, TAP_TIMEOUT);
Expand All @@ -131,12 +131,12 @@ public boolean onTouchEvent(MotionEvent event) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE: {
if (mCallback.needHandle(NodeProps.ON_TOUCH_CANCEL)) {
mCallback.handle(NodeProps.ON_TOUCH_CANCEL, event.getX(), event.getY());
mCallback.handle(NodeProps.ON_TOUCH_CANCEL, event);
handle = true;
}

if (mNoPressIn && mCallback.needHandle(NodeProps.ON_PRESS_OUT)) {
mCallback.handle(NodeProps.ON_PRESS_OUT, event.getX(), event.getY());
mCallback.handle(NodeProps.ON_PRESS_OUT, event);
handle = true;
} else if (!mNoPressIn && mCallback.needHandle(NodeProps.ON_PRESS_OUT)) {
if (getGestureHandler().hasMessages(PRESS_IN)) {
Expand All @@ -156,6 +156,8 @@ public interface Callback {

boolean needHandle(String type);

void handle(String type, MotionEvent event);

void handle(String type, float x, float y);
}

Expand Down

0 comments on commit 175ed15

Please sign in to comment.