improved coding for native touch

This commit is contained in:
True Zhuanjia committed 2024-06-20 11:51:46 +08:00
1 parent 8834f492b4
commit ee4eb35fe3
2 files changed
+34 -76

No files matched your search

+24 -66
View File
@@ -36,90 +36,48 @@
} }
- (void)sendTouchEvent:(UITouch*)event touchType:(uint8_t)touchType{ - (void)sendTouchEvent:(UITouch*)touch withTouchtype:(uint8_t)touchType{
CGPoint targetCoords; CGPoint targetCoords;
if(activateCoordSelector && event.phase == UITouchPhaseMoved) targetCoords = [NativeTouchPointer selectCoordsFor:event]; // coordinates of touch pointer replaced to relative ones here. if(activateCoordSelector && touch.phase == UITouchPhaseMoved) targetCoords = [NativeTouchPointer selectCoordsFor:touch]; // coordinates of touch pointer replaced to relative ones here.
else targetCoords = [event locationInView:streamView]; else targetCoords = [touch locationInView:streamView];
CGPoint location = [streamView adjustCoordinatesForVideoArea:targetCoords]; CGPoint location = [streamView adjustCoordinatesForVideoArea:targetCoords];
CGSize videoSize = [streamView getVideoAreaSize]; CGSize videoSize = [streamView getVideoAreaSize];
LiSendTouchEvent(touchType,[NativeTouchPointer retrievePointerIdFromDict:event],location.x / videoSize.width, location.y / videoSize.height,(event.force / event.maximumPossibleForce) / sin(event.altitudeAngle),0.0f, 0.0f,[streamView getRotationFromAzimuthAngle:[event azimuthAngleInView:streamView]]); LiSendTouchEvent(touchType,[NativeTouchPointer retrievePointerIdFromDict:touch],location.x / videoSize.width, location.y / videoSize.height,(touch.force / touch.maximumPossibleForce) / sin(touch.altitudeAngle),0.0f, 0.0f,[streamView getRotationFromAzimuthAngle:[touch azimuthAngleInView:streamView]]);
}
- (void)handleUITouch:(UITouch*)event index:(int)index{
uint8_t type;
// NSLog(@"handleUITouch %ld,%d",(long)event.phase,(uint32_t)event);
//#define LI_TOUCH_EVENT_HOVER 0x00
//#define LI_TOUCH_EVENT_DOWN 0x01
//#define LI_TOUCH_EVENT_UP 0x02
//#define LI_TOUCH_EVENT_MOVE 0x03
//#define LI_TOUCH_EVENT_CANCEL 0x04
//#define LI_TOUCH_EVENT_BUTTON_ONLY 0x05
//#define LI_TOUCH_EVENT_HOVER_LEAVE 0x06
//#define LI_TOUCH_EVENT_CANCEL_ALL 0x07
//#define LI_ROT_UNKNOWN 0xFFFF
// UITouchPhaseBegan, // whenever a finger touches the surface.
// UITouchPhaseMoved, // whenever a finger moves on the surface.
// UITouchPhaseStationary, // whenever a finger is touching the surface but hasn't moved since the previous event.
// UITouchPhaseEnded, // whenever a finger leaves the surface.
// UITouchPhaseCancelled, // whenever a touch doesn't end but we need to stop tracking (e.g. putting device to face)
// UITouchPhaseRegionEntered API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos), // whenever a touch is entering the region of a user interface
// UITouchPhaseRegionMoved API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos), // when a touch is inside the region of a user interface, but hasn’t yet made contact or left the region
// UITouchPhaseRegionExited API_AVAILABLE(ios(13.4), tvos(13.4))
switch (event.phase) {
case UITouchPhaseBegan://touchBegan
type = LI_TOUCH_EVENT_DOWN;
[NativeTouchPointer populatePointerId:event]; //generate & populate pointerId
if(activateCoordSelector) [NativeTouchPointer populatePointerObjIntoDict:event];
break;
case UITouchPhaseMoved://touchMoved
case UITouchPhaseStationary:
type = LI_TOUCH_EVENT_MOVE;
if(activateCoordSelector) [NativeTouchPointer updatePointerObjInDict:event];
break;
case UITouchPhaseEnded://touchEnded
type = LI_TOUCH_EVENT_UP;
[self sendTouchEvent:event touchType:type]; //send touch event first
[NativeTouchPointer removePointerId:event]; //then remove pointerId
if(activateCoordSelector) [NativeTouchPointer removePointerObjFromDict:event];
return;
case UITouchPhaseCancelled://touchCancelled
type = LI_TOUCH_EVENT_CANCEL;
[self sendTouchEvent:event touchType:type]; //send touch event first
[NativeTouchPointer removePointerId:event]; //then remove pointerId
if(activateCoordSelector) [NativeTouchPointer removePointerObjFromDict:event];
return;
case UITouchPhaseRegionEntered:
case UITouchPhaseRegionMoved:
type = LI_TOUCH_EVENT_HOVER;
break;
default:
return;
}
[self sendTouchEvent:event touchType:type];
} }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch* touch in touches) [self handleUITouch:touch index:0]; for (UITouch* touch in touches){
return; [NativeTouchPointer populatePointerId:touch]; //generate & populate pointerId
if(activateCoordSelector) [NativeTouchPointer populatePointerObjIntoDict:touch];
[self sendTouchEvent:touch withTouchtype:LI_TOUCH_EVENT_DOWN];
} }
return;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch* touch in touches) [self handleUITouch:touch index:0]; for (UITouch* touch in touches){
if(activateCoordSelector) [NativeTouchPointer updatePointerObjInDict:touch];
[self sendTouchEvent:touch withTouchtype:LI_TOUCH_EVENT_MOVE];
}
return; return;
} }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch* touch in touches) [self handleUITouch:touch index:0]; for (UITouch* touch in touches){
[self sendTouchEvent:touch withTouchtype:LI_TOUCH_EVENT_UP]; //send touch event before remove pointerId
[NativeTouchPointer removePointerId:touch]; //then remove pointerId
if(activateCoordSelector) [NativeTouchPointer removePointerObjFromDict:touch];
}
return; return;
} }
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch* touch in touches) [self handleUITouch:touch index:0];// Native touch (absoluteTouch) first! for (UITouch* touch in touches){
return; [self sendTouchEvent:touch withTouchtype:LI_TOUCH_EVENT_UP]; //send touch event before remove pointerId
[NativeTouchPointer removePointerId:touch]; //then remove pointerId
if(activateCoordSelector) [NativeTouchPointer removePointerObjFromDict:touch];
}
} }
+10 -10
View File
@@ -24,7 +24,7 @@ static NSMutableDictionary *pointerDict;
static CGFloat pointerVelocityFactor; static CGFloat pointerVelocityFactor;
static CGFloat pointerVelocityDivider; static CGFloat pointerVelocityDivider;
static CGFloat pointerVelocityDividerLocation; static CGFloat pointerVelocityDividerLocationByPoints;
StreamView *streamView; StreamView *streamView;
@@ -34,8 +34,8 @@ StreamView *streamView;
CGPoint previousPoint; CGPoint previousPoint;
CGPoint latestRelativePoint; CGPoint latestRelativePoint;
CGPoint previousRelativePoint; CGPoint previousRelativePoint;
CGFloat velocityX; // CGFloat velocityX;
CGFloat velocityY; // CGFloat velocityY;
} }
+ (void)setPointerVelocityDivider:(CGFloat)dividerLocation{ + (void)setPointerVelocityDivider:(CGFloat)dividerLocation{
@@ -58,11 +58,11 @@ StreamView *streamView;
- (void)updatePointerCoords:(UITouch *)touch{ - (void)updatePointerCoords:(UITouch *)touch{
previousPoint = latestPoint; previousPoint = latestPoint;
latestPoint = [touch locationInView:streamView]; latestPoint = [touch locationInView:streamView];
velocityX = latestPoint.x - previousPoint.x; // velocityX = latestPoint.x - previousPoint.x;
velocityY = latestPoint.y - previousPoint.y; // velocityY = latestPoint.y - previousPoint.y;
previousRelativePoint = latestRelativePoint; previousRelativePoint = latestRelativePoint;
latestRelativePoint.x = previousRelativePoint.x + pointerVelocityFactor * velocityX; latestRelativePoint.x = previousRelativePoint.x + pointerVelocityFactor * (latestPoint.x - previousPoint.x);
latestRelativePoint.y = previousRelativePoint.y + pointerVelocityFactor * velocityY; latestRelativePoint.y = previousRelativePoint.y + pointerVelocityFactor * (latestPoint.y - previousPoint.y);
} }
+ (void)initContextWith:(StreamView *)view{ + (void)initContextWith:(StreamView *)view{
@@ -70,9 +70,9 @@ StreamView *streamView;
pointerIdDict = [NSMutableDictionary dictionary]; pointerIdDict = [NSMutableDictionary dictionary];
pointerIdSet = [NSMutableSet set]; pointerIdSet = [NSMutableSet set];
pointerDict = [NSMutableDictionary dictionary]; pointerDict = [NSMutableDictionary dictionary];
pointerVelocityDividerLocation = CGRectGetWidth([[UIScreen mainScreen] bounds]) * pointerVelocityDivider; pointerVelocityDividerLocationByPoints = CGRectGetWidth([[UIScreen mainScreen] bounds]) * pointerVelocityDivider;
NSLog(@"pointerVelocityDivider: %.2f", pointerVelocityDivider); NSLog(@"pointerVelocityDivider: %.2f", pointerVelocityDivider);
NSLog(@"pointerVelocityDividerLocation: %.2f", pointerVelocityDividerLocation); NSLog(@"pointerVelocityDividerLocationByPoints: %.2f", pointerVelocityDividerLocationByPoints);
} }
+ (void)populatePointerObjIntoDict:(UITouch*)touch{ + (void)populatePointerObjIntoDict:(UITouch*)touch{
@@ -95,7 +95,7 @@ StreamView *streamView;
+ (CGPoint)selectCoordsFor:(UITouch *)touch{ + (CGPoint)selectCoordsFor:(UITouch *)touch{
NativeTouchPointer *pointer = [pointerDict objectForKey:@((uint64_t)touch)]; NativeTouchPointer *pointer = [pointerDict objectForKey:@((uint64_t)touch)];
if((pointer -> initialPoint).x > pointerVelocityDividerLocation){ //if first contact coords locates on the right side of divider. if((pointer -> initialPoint).x > pointerVelocityDividerLocationByPoints){ //if first contact coords locates on the right side of divider.
return pointer->latestRelativePoint; return pointer->latestRelativePoint;
} }
return [touch locationInView:streamView]; return [touch locationInView:streamView];