native response parameter changed

This commit is contained in:
王劲鹏 2021-09-23 16:06:07 +08:00 committed by osborn
parent d85f77402c
commit bf0de5e02a
3 changed files with 76 additions and 41 deletions

View File

@ -24,6 +24,7 @@ import android.view.ScaleGestureDetector;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -169,24 +170,27 @@ public class GestureContainerNode extends StackNode {
@Override @Override
public boolean onSingleTapConfirmed(MotionEvent e) { public boolean onSingleTapConfirmed(MotionEvent e) {
if (onSingleTap != null) if (onSingleTap != null) {
callJSResponse(onSingleTap); callJSResponse(onSingleTap);
}
return super.onSingleTapConfirmed(e); return super.onSingleTapConfirmed(e);
} }
@Override @Override
public boolean onDoubleTap(MotionEvent e) { public boolean onDoubleTap(MotionEvent e) {
if (onDoubleTap != null) if (onDoubleTap != null) {
callJSResponse(onDoubleTap); callJSResponse(onDoubleTap);
}
return super.onDoubleTap(e); return super.onDoubleTap(e);
} }
@Override @Override
public void onLongPress(MotionEvent e) { public void onLongPress(MotionEvent e) {
super.onLongPress(e); super.onLongPress(e);
if (onLongPress != null) if (onLongPress != null) {
callJSResponse(onLongPress); callJSResponse(onLongPress);
} }
}
@Override @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, final float distanceX, final float distanceY) { public boolean onScroll(MotionEvent e1, MotionEvent e2, final float distanceX, final float distanceY) {
@ -196,13 +200,14 @@ public class GestureContainerNode extends StackNode {
} }
// handle scrolling // handle scrolling
if (onPan != null) if (onPan != null) {
jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() { jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override @Override
public AsyncResult<JSDecoder> call() throws Exception { public AsyncResult<JSDecoder> call() throws Exception {
return callJSResponse(onPan, DoricUtils.px2dp(distanceX), DoricUtils.px2dp(distanceY)); return callJSResponse(onPan, DoricUtils.px2dp(distanceX), DoricUtils.px2dp(distanceY));
} }
}); });
}
return true; return true;
} }
@ -240,13 +245,14 @@ public class GestureContainerNode extends StackNode {
scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.OnScaleGestureListener() { scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.OnScaleGestureListener() {
@Override @Override
public boolean onScale(final ScaleGestureDetector scaleGestureDetector) { public boolean onScale(final ScaleGestureDetector scaleGestureDetector) {
if (onPinch != null) if (onPinch != null) {
jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() { jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override @Override
public AsyncResult<JSDecoder> call() throws Exception { public AsyncResult<JSDecoder> call() throws Exception {
return callJSResponse(onPinch, scaleGestureDetector.getScaleFactor()); return callJSResponse(onPinch, scaleGestureDetector.getScaleFactor());
} }
}); });
}
return false; return false;
} }
@ -266,13 +272,14 @@ public class GestureContainerNode extends StackNode {
@Override @Override
public boolean onRotate(final float degrees, float focusX, float focusY) { public boolean onRotate(final float degrees, float focusX, float focusY) {
if (onRotate != null) if (onRotate != null) {
jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() { jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override @Override
public AsyncResult<JSDecoder> call() throws Exception { public AsyncResult<JSDecoder> call() throws Exception {
return callJSResponse(onRotate, degrees / 180f); return callJSResponse(onRotate, degrees / 180f);
} }
}); });
}
return false; return false;
} }
@ -283,34 +290,51 @@ public class GestureContainerNode extends StackNode {
@Override @Override
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {
// handle touch event conflict when in a scroll view or other similar containers // 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()) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
if (onTouchDown != null) if (onTouchDown != null) {
callJSResponse(onTouchDown, DoricUtils.px2dp(event.getX()), DoricUtils.px2dp(event.getY())); callJSResponse(onTouchDown, new JSONBuilder()
.put("x", x)
.put("y", y)
.toJSONObject());
}
break; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
getParent().requestDisallowInterceptTouchEvent(true); getParent().requestDisallowInterceptTouchEvent(true);
final float x = DoricUtils.px2dp(event.getX()); if (onTouchMove != null) {
final float y = DoricUtils.px2dp(event.getY());
if (onTouchMove != null)
jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() { jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override @Override
public AsyncResult<JSDecoder> call() throws Exception { public AsyncResult<JSDecoder> call() throws Exception {
return callJSResponse(onTouchMove, x, y); return callJSResponse(onTouchMove, new JSONBuilder()
.put("x", x)
.put("y", y)
.toJSONObject());
} }
}); });
}
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
if (onTouchUp != null) if (onTouchUp != null) {
callJSResponse(onTouchUp, DoricUtils.px2dp(event.getX()), DoricUtils.px2dp(event.getY())); callJSResponse(onTouchUp, new JSONBuilder()
.put("x", x)
.put("y", y)
.toJSONObject());
}
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
getParent().requestDisallowInterceptTouchEvent(false); getParent().requestDisallowInterceptTouchEvent(false);
if (onTouchCancel != null) if (onTouchCancel != null) {
callJSResponse(onTouchCancel, DoricUtils.px2dp(event.getX()), DoricUtils.px2dp(event.getY())); callJSResponse(onTouchCancel, new JSONBuilder()
.put("x", x)
.put("y", y)
.toJSONObject());
}
break; break;
} }
@ -322,23 +346,27 @@ public class GestureContainerNode extends StackNode {
} }
private void onSwipeLeft() { private void onSwipeLeft() {
if (onSwipe != null) if (onSwipe != null) {
callJSResponse(onSwipe, SwipeOrientation.LEFT.value); callJSResponse(onSwipe, SwipeOrientation.LEFT.value);
} }
}
private void onSwipeRight() { private void onSwipeRight() {
if (onSwipe != null) if (onSwipe != null) {
callJSResponse(onSwipe, SwipeOrientation.RIGHT.value); callJSResponse(onSwipe, SwipeOrientation.RIGHT.value);
} }
}
private void onSwipeTop() { private void onSwipeTop() {
if (onSwipe != null) if (onSwipe != null) {
callJSResponse(onSwipe, SwipeOrientation.TOP.value); callJSResponse(onSwipe, SwipeOrientation.TOP.value);
} }
}
private void onSwipeBottom() { private void onSwipeBottom() {
if (onSwipe != null) if (onSwipe != null) {
callJSResponse(onSwipe, SwipeOrientation.BOTTOM.value); callJSResponse(onSwipe, SwipeOrientation.BOTTOM.value);
} }
} }
} }
}

View File

@ -47,18 +47,18 @@ class SimpleDemo extends Panel {
gestureContainer([ gestureContainer([
touchChild touchChild
], { ], {
onTouchDown: (x: number, y: number) => { onTouchDown: (event: { x: number, y: number }) => {
modal(context).toast("onTouchDown x=" + x + " y=" + y) modal(context).toast("onTouchDown x=" + event.x + " y=" + event.y)
}, },
onTouchMove: (x: number, y: number) => { onTouchMove: (event: { x: number, y: number }) => {
touchChild.x = x - 50 touchChild.x = event.x - 50
touchChild.y = y - 50 touchChild.y = event.y - 50
}, },
onTouchUp: (x: number, y: number) => { onTouchUp: (event: { x: number, y: number }) => {
modal(context).toast("onTouchUp x=" + x + " y=" + y) modal(context).toast("onTouchUp x=" + event.x + " y=" + event.y)
}, },
onTouchCancel: (x: number, y: number) => { onTouchCancel: (event: { x: number, y: number }) => {
modal(context).toast("onTouchCancel x=" + x + " y=" + y) modal(context).toast("onTouchCancel x=" + event.x + " y=" + event.y)
} }
}).apply({ }).apply({
layoutConfig: layoutConfig().just().configAlignment(Gravity.Center), layoutConfig: layoutConfig().just().configAlignment(Gravity.Center),

View File

@ -37,7 +37,7 @@ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) { [touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
if (self.node.onTouchDown) { if (self.node.onTouchDown) {
CGPoint currentLocation = [obj locationInView:self]; 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; *stop = YES;
} }
}]; }];
@ -55,7 +55,7 @@ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
__weak typeof(self) __self = self; __weak typeof(self) __self = self;
[self.node.jsDispatcher dispatch:^DoricAsyncResult * { [self.node.jsDispatcher dispatch:^DoricAsyncResult * {
__strong typeof(__self) self = __self; __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; *stop = YES;
} }
@ -68,7 +68,7 @@ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) { [touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
if (self.node.onTouchUp) { if (self.node.onTouchUp) {
CGPoint currentLocation = [obj locationInView:self]; 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; *stop = YES;
} }
}]; }];
@ -80,7 +80,7 @@ - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
[touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) { [touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
if (self.node.onTouchCancel) { if (self.node.onTouchCancel) {
CGPoint currentLocation = [obj locationInView:self]; 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; *stop = YES;
} }
}]; }];
@ -141,21 +141,24 @@ - (UIView *)build {
} }
-(void)singleTapAction:(UITapGestureRecognizer *)sender{ -(void)singleTapAction:(UITapGestureRecognizer *)sender{
if (self.onSingleTap) if (self.onSingleTap) {
[self callJSResponse:self.onSingleTap, nil]; [self callJSResponse:self.onSingleTap, nil];
} }
}
-(void)doubleTapAction:(UITapGestureRecognizer *)sender{ -(void)doubleTapAction:(UITapGestureRecognizer *)sender{
if (self.onDoubleTap) if (self.onDoubleTap) {
[self callJSResponse:self.onDoubleTap, nil]; [self callJSResponse:self.onDoubleTap, nil];
} }
}
-(void)longPressAction:(UILongPressGestureRecognizer *)sender{ -(void)longPressAction:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateBegan) { if (sender.state == UIGestureRecognizerStateBegan) {
if (self.onLongPress) if (self.onLongPress) {
[self callJSResponse:self.onLongPress, nil]; [self callJSResponse:self.onLongPress, nil];
} }
} }
}
-(void)pinchAction:(UIPinchGestureRecognizer *)sender{ -(void)pinchAction:(UIPinchGestureRecognizer *)sender{
if (self.onPinch) { if (self.onPinch) {
@ -198,17 +201,21 @@ -(void)panAction:(UIPanGestureRecognizer *)sender{
CGPoint vel = [sender velocityInView:sender.view]; CGPoint vel = [sender velocityInView:sender.view];
if (vel.x < SWIPE_LEFT_THRESHOLD) { if (vel.x < SWIPE_LEFT_THRESHOLD) {
if (self.onSwipe) if (self.onSwipe) {
[self callJSResponse:self.onSwipe, @(0), nil]; [self callJSResponse:self.onSwipe, @(0), nil];
}
} else if (vel.x > SWIPE_RIGHT_THRESHOLD) { } else if (vel.x > SWIPE_RIGHT_THRESHOLD) {
if (self.onSwipe) if (self.onSwipe) {
[self callJSResponse:self.onSwipe, @(1), nil]; [self callJSResponse:self.onSwipe, @(1), nil];
}
} else if (vel.y < SWIPE_UP_THRESHOLD) { } else if (vel.y < SWIPE_UP_THRESHOLD) {
if (self.onSwipe) if (self.onSwipe) {
[self callJSResponse:self.onSwipe, @(2), nil]; [self callJSResponse:self.onSwipe, @(2), nil];
}
} else if (vel.y > SWIPE_DOWN_THRESHOLD) { } else if (vel.y > SWIPE_DOWN_THRESHOLD) {
if (self.onSwipe) if (self.onSwipe) {
[self callJSResponse:self.onSwipe, @(3), nil]; [self callJSResponse:self.onSwipe, @(3), nil];
}
} else { } else {
// TODO: // TODO:
// Here, the user lifted the finger/fingers but didn't swipe. // Here, the user lifted the finger/fingers but didn't swipe.