NativeTouchHandler improvement, general coding improvements.
This commit is contained in:
@@ -15,39 +15,80 @@
|
||||
#include <Limelight.h>
|
||||
|
||||
|
||||
|
||||
@implementation NativeTouchHandler {
|
||||
StreamView* streamView;
|
||||
TemporarySettings* currentSettings;
|
||||
bool activateCoordSelector;
|
||||
// Use a Dictionary to store UITouch object's memory address as key, and pointerId as value,字典存放UITouch对象地址和pointerId映射关系
|
||||
// pointerId will be generated from a pre-defined pool
|
||||
// Use a NSSet store active pointerId,
|
||||
NSMutableDictionary *pointerIdDict; //pointerId Dict for active touches.
|
||||
NSMutableSet<NSNumber *> *activePointerIds; //pointerId Set for active touches.
|
||||
NSMutableSet<NSNumber *> *pointerIdPool; //pre-defined pool of pointerIds.
|
||||
NSMutableSet<NSNumber *> *unassignedPointerIds;
|
||||
}
|
||||
|
||||
|
||||
- (id)initWith:(StreamView*)view and:(TemporarySettings*)settings{
|
||||
self = [self init];
|
||||
- (id)initWithView:(StreamView*)view andSettings:(TemporarySettings*)settings{
|
||||
self = [super init];
|
||||
self->streamView = view;
|
||||
self->currentSettings = settings;
|
||||
self->activateCoordSelector = currentSettings.pointerVelocityModeDivider.floatValue != 1.0;
|
||||
|
||||
pointerIdDict = [NSMutableDictionary dictionary];
|
||||
pointerIdPool = [NSMutableSet set];
|
||||
for (uint8_t i = 0; i < 10; i++) { //ipadOS supports upto 11 finger touches
|
||||
[pointerIdPool addObject:@(i)];
|
||||
}
|
||||
activePointerIds = [NSMutableSet set];
|
||||
|
||||
[NativeTouchPointer setPointerVelocityDivider:settings.pointerVelocityModeDivider.floatValue];
|
||||
[NativeTouchPointer setPointerVelocityFactor:settings.touchPointerVelocityFactor.floatValue];
|
||||
[NativeTouchPointer initContextWith:streamView];
|
||||
[NativeTouchPointer initContextWithView:streamView];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
// generate & populate pointerId into NSDict & NSSet, called in touchesBegan
|
||||
- (void)populatePointerId:(UITouch*)touch{
|
||||
uintptr_t memAddrValue = (uintptr_t)touch;
|
||||
unassignedPointerIds = [pointerIdPool mutableCopy]; //reset unassignedPointerIds
|
||||
[unassignedPointerIds minusSet:activePointerIds];
|
||||
uint8_t pointerId = [[unassignedPointerIds anyObject] intValue];
|
||||
[pointerIdDict setObject:@(pointerId) forKey:@(memAddrValue)];
|
||||
[activePointerIds addObject:@(pointerId)];
|
||||
}
|
||||
|
||||
// remove pointerId in touchesEnded or touchesCancelled
|
||||
- (void)removePointerId:(UITouch*)touch{
|
||||
uintptr_t memAddrValue = (uintptr_t)touch;
|
||||
NSNumber* pointerIdObj = [pointerIdDict objectForKey:@(memAddrValue)];
|
||||
if(pointerIdObj != nil){
|
||||
[activePointerIds removeObject:pointerIdObj];
|
||||
[pointerIdDict removeObjectForKey:@(memAddrValue)];
|
||||
}
|
||||
}
|
||||
|
||||
// 从字典中获取UITouch事件对应的pointerId
|
||||
// called in method of sendTouchEvent
|
||||
- (uint32_t) retrievePointerIdFromDict:(UITouch*)touch{
|
||||
return [[pointerIdDict objectForKey:@((uintptr_t)touch)] unsignedIntValue];
|
||||
}
|
||||
|
||||
|
||||
- (void)sendTouchEvent:(UITouch*)touch withTouchtype:(uint8_t)touchType{
|
||||
CGPoint targetCoords;
|
||||
if(activateCoordSelector && touch.phase == UITouchPhaseMoved) targetCoords = [NativeTouchPointer selectCoordsFor:touch]; // coordinates of touch pointer replaced to relative ones here.
|
||||
else targetCoords = [touch locationInView:streamView];
|
||||
CGPoint location = [streamView adjustCoordinatesForVideoArea:targetCoords];
|
||||
CGSize videoSize = [streamView getVideoAreaSize];
|
||||
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]]);
|
||||
LiSendTouchEvent(touchType,[self 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)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
|
||||
for (UITouch* touch in touches){
|
||||
[NativeTouchPointer populatePointerId:touch]; //generate & populate pointerId
|
||||
[self populatePointerId:touch]; //generate & populate pointerId
|
||||
if(activateCoordSelector) [NativeTouchPointer populatePointerObjIntoDict:touch];
|
||||
[self sendTouchEvent:touch withTouchtype:LI_TOUCH_EVENT_DOWN];
|
||||
}
|
||||
@@ -66,18 +107,14 @@
|
||||
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
|
||||
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
|
||||
[self removePointerId:touch]; //then remove pointerId
|
||||
if(activateCoordSelector) [NativeTouchPointer removePointerObjFromDict:touch];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
|
||||
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];
|
||||
}
|
||||
[self touchesEnded:touches withEvent:event];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user