diff --git a/doric-android/doric/src/main/java/pub/doric/shader/GestureContainerNode.java b/doric-android/doric/src/main/java/pub/doric/shader/GestureContainerNode.java index dc9e4fe0..0a11d703 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/GestureContainerNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/GestureContainerNode.java @@ -24,6 +24,7 @@ import android.view.ScaleGestureDetector; import android.widget.FrameLayout; import com.github.pengfeizhou.jscore.JSDecoder; +import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSValue; import java.util.concurrent.Callable; @@ -169,23 +170,26 @@ public class GestureContainerNode extends StackNode { @Override public boolean onSingleTapConfirmed(MotionEvent e) { - if (onSingleTap != null) + if (onSingleTap != null) { callJSResponse(onSingleTap); + } return super.onSingleTapConfirmed(e); } @Override public boolean onDoubleTap(MotionEvent e) { - if (onDoubleTap != null) + if (onDoubleTap != null) { callJSResponse(onDoubleTap); + } return super.onDoubleTap(e); } @Override public void onLongPress(MotionEvent e) { super.onLongPress(e); - if (onLongPress != null) + if (onLongPress != null) { callJSResponse(onLongPress); + } } @Override @@ -196,13 +200,14 @@ public class GestureContainerNode extends StackNode { } // handle scrolling - if (onPan != null) + if (onPan != null) { jsDispatcher.dispatch(new Callable>() { @Override public AsyncResult call() throws Exception { return callJSResponse(onPan, DoricUtils.px2dp(distanceX), DoricUtils.px2dp(distanceY)); } }); + } return true; } @@ -240,13 +245,14 @@ public class GestureContainerNode extends StackNode { scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.OnScaleGestureListener() { @Override public boolean onScale(final ScaleGestureDetector scaleGestureDetector) { - if (onPinch != null) + if (onPinch != null) { jsDispatcher.dispatch(new Callable>() { @Override public AsyncResult call() throws Exception { return callJSResponse(onPinch, scaleGestureDetector.getScaleFactor()); } }); + } return false; } @@ -266,13 +272,14 @@ public class GestureContainerNode extends StackNode { @Override public boolean onRotate(final float degrees, float focusX, float focusY) { - if (onRotate != null) + if (onRotate != null) { jsDispatcher.dispatch(new Callable>() { @Override public AsyncResult call() throws Exception { return callJSResponse(onRotate, degrees / 180f); } }); + } return false; } @@ -283,34 +290,51 @@ public class GestureContainerNode extends StackNode { @Override public boolean onTouchEvent(MotionEvent event) { // handle touch event conflict when in a scroll view or other similar containers + final float x = DoricUtils.px2dp(event.getX()); + final float y = DoricUtils.px2dp(event.getY()); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: - if (onTouchDown != null) - callJSResponse(onTouchDown, DoricUtils.px2dp(event.getX()), DoricUtils.px2dp(event.getY())); + if (onTouchDown != null) { + callJSResponse(onTouchDown, new JSONBuilder() + .put("x", x) + .put("y", y) + .toJSONObject()); + } break; case MotionEvent.ACTION_MOVE: getParent().requestDisallowInterceptTouchEvent(true); - final float x = DoricUtils.px2dp(event.getX()); - final float y = DoricUtils.px2dp(event.getY()); - - if (onTouchMove != null) + if (onTouchMove != null) { jsDispatcher.dispatch(new Callable>() { @Override public AsyncResult call() throws Exception { - return callJSResponse(onTouchMove, x, y); + return callJSResponse(onTouchMove, new JSONBuilder() + .put("x", x) + .put("y", y) + .toJSONObject()); } }); + } + break; case MotionEvent.ACTION_UP: - if (onTouchUp != null) - callJSResponse(onTouchUp, DoricUtils.px2dp(event.getX()), DoricUtils.px2dp(event.getY())); + if (onTouchUp != null) { + callJSResponse(onTouchUp, new JSONBuilder() + .put("x", x) + .put("y", y) + .toJSONObject()); + } case MotionEvent.ACTION_CANCEL: getParent().requestDisallowInterceptTouchEvent(false); - if (onTouchCancel != null) - callJSResponse(onTouchCancel, DoricUtils.px2dp(event.getX()), DoricUtils.px2dp(event.getY())); + if (onTouchCancel != null) { + callJSResponse(onTouchCancel, new JSONBuilder() + .put("x", x) + .put("y", y) + .toJSONObject()); + } + break; } @@ -322,23 +346,27 @@ public class GestureContainerNode extends StackNode { } private void onSwipeLeft() { - if (onSwipe != null) + if (onSwipe != null) { callJSResponse(onSwipe, SwipeOrientation.LEFT.value); + } } private void onSwipeRight() { - if (onSwipe != null) + if (onSwipe != null) { callJSResponse(onSwipe, SwipeOrientation.RIGHT.value); + } } private void onSwipeTop() { - if (onSwipe != null) + if (onSwipe != null) { callJSResponse(onSwipe, SwipeOrientation.TOP.value); + } } private void onSwipeBottom() { - if (onSwipe != null) + if (onSwipe != null) { callJSResponse(onSwipe, SwipeOrientation.BOTTOM.value); + } } } } diff --git a/doric-demo/src/GestureDemo.ts b/doric-demo/src/GestureDemo.ts index c16f5bc1..92b3e17f 100644 --- a/doric-demo/src/GestureDemo.ts +++ b/doric-demo/src/GestureDemo.ts @@ -47,18 +47,18 @@ class SimpleDemo extends Panel { gestureContainer([ touchChild ], { - onTouchDown: (x: number, y: number) => { - modal(context).toast("onTouchDown x=" + x + " y=" + y) + onTouchDown: (event: { x: number, y: number }) => { + modal(context).toast("onTouchDown x=" + event.x + " y=" + event.y) }, - onTouchMove: (x: number, y: number) => { - touchChild.x = x - 50 - touchChild.y = y - 50 + onTouchMove: (event: { x: number, y: number }) => { + touchChild.x = event.x - 50 + touchChild.y = event.y - 50 }, - onTouchUp: (x: number, y: number) => { - modal(context).toast("onTouchUp x=" + x + " y=" + y) + onTouchUp: (event: { x: number, y: number }) => { + modal(context).toast("onTouchUp x=" + event.x + " y=" + event.y) }, - onTouchCancel: (x: number, y: number) => { - modal(context).toast("onTouchCancel x=" + x + " y=" + y) + onTouchCancel: (event: { x: number, y: number }) => { + modal(context).toast("onTouchCancel x=" + event.x + " y=" + event.y) } }).apply({ layoutConfig: layoutConfig().just().configAlignment(Gravity.Center), diff --git a/doric-iOS/Pod/Classes/Shader/DoricGestureContainerNode.m b/doric-iOS/Pod/Classes/Shader/DoricGestureContainerNode.m index 143a3d94..13769557 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricGestureContainerNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricGestureContainerNode.m @@ -37,7 +37,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) { if (self.node.onTouchDown) { CGPoint currentLocation = [obj locationInView:self]; - [self.node callJSResponse: self.node.onTouchDown, @(currentLocation.x), @(currentLocation.y), nil]; + [self.node callJSResponse: self.node.onTouchDown, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil]; *stop = YES; } }]; @@ -55,7 +55,7 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { __weak typeof(self) __self = self; [self.node.jsDispatcher dispatch:^DoricAsyncResult * { __strong typeof(__self) self = __self; - return [self.node callJSResponse: self.node.onTouchMove, @(currentLocation.x), @(currentLocation.y), nil]; + return [self.node callJSResponse: self.node.onTouchMove, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil]; }]; *stop = YES; } @@ -68,7 +68,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) { if (self.node.onTouchUp) { CGPoint currentLocation = [obj locationInView:self]; - [self.node callJSResponse: self.node.onTouchUp, @(currentLocation.x), @(currentLocation.y), nil]; + [self.node callJSResponse: self.node.onTouchUp, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil]; *stop = YES; } }]; @@ -80,7 +80,7 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event [touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) { if (self.node.onTouchCancel) { CGPoint currentLocation = [obj locationInView:self]; - [self.node callJSResponse: self.node.onTouchCancel, @(currentLocation.x), @(currentLocation.y), nil]; + [self.node callJSResponse: self.node.onTouchCancel, @{@"x": @(currentLocation.x), @"y": @(currentLocation.y)}, nil]; *stop = YES; } }]; @@ -141,19 +141,22 @@ - (UIView *)build { } -(void)singleTapAction:(UITapGestureRecognizer *)sender{ - if (self.onSingleTap) + if (self.onSingleTap) { [self callJSResponse:self.onSingleTap, nil]; + } } -(void)doubleTapAction:(UITapGestureRecognizer *)sender{ - if (self.onDoubleTap) + if (self.onDoubleTap) { [self callJSResponse:self.onDoubleTap, nil]; + } } -(void)longPressAction:(UILongPressGestureRecognizer *)sender{ if (sender.state == UIGestureRecognizerStateBegan) { - if (self.onLongPress) + if (self.onLongPress) { [self callJSResponse:self.onLongPress, nil]; + } } } @@ -198,17 +201,21 @@ -(void)panAction:(UIPanGestureRecognizer *)sender{ CGPoint vel = [sender velocityInView:sender.view]; if (vel.x < SWIPE_LEFT_THRESHOLD) { - if (self.onSwipe) + if (self.onSwipe) { [self callJSResponse:self.onSwipe, @(0), nil]; + } } else if (vel.x > SWIPE_RIGHT_THRESHOLD) { - if (self.onSwipe) + if (self.onSwipe) { [self callJSResponse:self.onSwipe, @(1), nil]; + } } else if (vel.y < SWIPE_UP_THRESHOLD) { - if (self.onSwipe) + if (self.onSwipe) { [self callJSResponse:self.onSwipe, @(2), nil]; + } } else if (vel.y > SWIPE_DOWN_THRESHOLD) { - if (self.onSwipe) + if (self.onSwipe) { [self callJSResponse:self.onSwipe, @(3), nil]; + } } else { // TODO: // Here, the user lifted the finger/fingers but didn't swipe.