summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLTouchManager.m
blob: b661f35630d1eb4dd8ca3bc7ffb242357090c328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
//  SDLTouchManager.m
//  SmartDeviceLink-iOS
//
//  Created by Muller, Alexander (A.) on 6/14/16.
//  Copyright © 2016 smartdevicelink. All rights reserved.
//

#import "SDLTouchManager.h"

#import "CGPoint_Util.h"
#import "dispatch_timer.h"

#import "SDLDebugTool.h"
#import "SDLOnTouchEvent.h"
#import "SDLPinchGesture.h"
#import "SDLTouch.h"
#import "SDLTouchCoord.h"
#import "SDLTouchEvent.h"
#import "SDLTouchType.h"

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, SDLPerformingTouchType) {
    SDLPerformingTouchTypeNone,
    SDLPerformingTouchTypeSingleTouch,
    SDLPerformingTouchTypeMultiTouch,
    SDLPerformingTouchTypePanningTouch
};

/*!
 *  @abstract 
 *      Touch Manager will ignore touches that represent more than 2 fingers on the screen.
 */
static NSUInteger const MaximumNumberOfTouches = 2;

@interface SDLTouchManager ()

/*!
 *  @abstract 
 *      First Touch received from onOnTouchEvent.
 */
@property (nonatomic, strong, nullable) SDLTouch *previousTouch;

/*!
 * @abstract 
 *      Cached previous single tap used for double tap detection.
 */
@property (nonatomic, strong, nullable) SDLTouch *singleTapTouch;

/*!
 *  @abstract
 *      Distance of a previously generated pinch event. Used to calculate the scale of zoom motion.
 */
@property (nonatomic, assign) CGFloat previousPinchDistance;

/*!
 *  @abstract
 *      Current in-progress pinch gesture.
 */
@property (nonatomic, strong, nullable) SDLPinchGesture *currentPinchGesture;

/*!
 *  @abstract
 *      Timer used for distinguishing between single & double taps.
 */
@property (nonatomic, strong, nullable) dispatch_source_t singleTapTimer;

/*!
 *  @abstract
 *      Current touch type being performed.
 */
@property (nonatomic, assign) SDLPerformingTouchType performingTouchType;

@end

@implementation SDLTouchManager

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }

    _movementTimeThreshold = 0.05f;
    _tapTimeThreshold = 0.4f;
    _tapDistanceThreshold = 50.0f;
    _touchEnabled = YES;

    return self;
}

#pragma mark - Public
- (void)cancelPendingTouches {
    [self sdl_cancelSingleTapTimer];
}

#pragma mark - SDLProxyListener Delegate
- (void)onProxyOpened {
}
- (void)onProxyClosed {
}
- (void)onOnHMIStatus:(SDLOnHMIStatus *)notification {
}
- (void)onOnDriverDistraction:(SDLOnDriverDistraction *)notification {
}

- (void)onOnTouchEvent:(SDLOnTouchEvent *)notification {
    if (!self.isTouchEnabled) {
        return;
    }

    SDLTouchEvent *touchEvent = notification.event.firstObject;

    SDLTouch *touch = [[SDLTouch alloc] initWithTouchEvent:touchEvent];

    if (touch.identifier > MaximumNumberOfTouches) {
        return;
    }

    if ([notification.type isEqualToString:SDLTouchTypeBegin]) {
        [self sdl_handleTouchBegan:touch];
    } else if ([notification.type isEqualToString:SDLTouchTypeMove]) {
        [self sdl_handleTouchMoved:touch];
    } else if ([notification.type isEqualToString:SDLTouchTypeEnd]) {
        [self sdl_handleTouchEnded:touch];
    }
}

#pragma mark - Private
- (void)sdl_handleTouchBegan:(SDLTouch *)touch {
    if (!touch.isFirstFinger && !self.isTouchEnabled) {
        return; // no-op
    }

    _performingTouchType = SDLPerformingTouchTypeSingleTouch;

    switch (touch.identifier) {
        case SDLTouchIdentifierFirstFinger:
            self.previousTouch = touch;
            break;
        case SDLTouchIdentifierSecondFinger:
            _performingTouchType = SDLPerformingTouchTypeMultiTouch;
            self.currentPinchGesture = [[SDLPinchGesture alloc] initWithFirstTouch:self.previousTouch
                                                                       secondTouch:touch];
            self.previousPinchDistance = self.currentPinchGesture.distance;
            if ([self.touchEventDelegate respondsToSelector:@selector(touchManager:pinchDidStartAtCenterPoint:)]) {
                [self.touchEventDelegate touchManager:self
                           pinchDidStartAtCenterPoint:self.currentPinchGesture.center];
            }
            break;
    }
}

- (void)sdl_handleTouchMoved:(SDLTouch *)touch {
    if ((touch.timeStamp - self.previousTouch.timeStamp) <= (self.movementTimeThreshold * NSEC_PER_USEC) || !self.isTouchEnabled) {
        return; // no-op
    }

    switch (self.performingTouchType) {
        case SDLPerformingTouchTypeMultiTouch:
            switch (touch.identifier) {
                case SDLTouchIdentifierFirstFinger:
                    self.currentPinchGesture.firstTouch = touch;
                    break;
                case SDLTouchIdentifierSecondFinger:
                    self.currentPinchGesture.secondTouch = touch;
                    break;
            }


            if ([self.touchEventDelegate respondsToSelector:@selector(touchManager:didReceivePinchAtCenterPoint:withScale:)]) {
                CGFloat scale = self.currentPinchGesture.distance / self.previousPinchDistance;
                [self.touchEventDelegate touchManager:self
                         didReceivePinchAtCenterPoint:self.currentPinchGesture.center
                                            withScale:scale];
            }

            self.previousPinchDistance = self.currentPinchGesture.distance;
            break;
        case SDLPerformingTouchTypeSingleTouch:
            _performingTouchType = SDLPerformingTouchTypePanningTouch;
            if ([self.touchEventDelegate respondsToSelector:@selector(touchManager:panningDidStartAtPoint:)]) {
                [self.touchEventDelegate touchManager:self
                               panningDidStartAtPoint:touch.location];
            }
            break;
        case SDLPerformingTouchTypePanningTouch:
            if ([self.touchEventDelegate respondsToSelector:@selector(touchManager:didReceivePanningFromPoint:toPoint:)]) {
                [self.touchEventDelegate touchManager:self
                           didReceivePanningFromPoint:self.previousTouch.location
                                              toPoint:touch.location];
            }
            break;
        case SDLPerformingTouchTypeNone:
            break;
    }

    self.previousTouch = touch;
}

- (void)sdl_handleTouchEnded:(SDLTouch *)touch {
    if (!self.isTouchEnabled) {
        return; // no-op
    }

    switch (self.performingTouchType) {
        case SDLPerformingTouchTypeMultiTouch:
            switch (touch.identifier) {
                case SDLTouchIdentifierFirstFinger:
                    self.currentPinchGesture.firstTouch = touch;
                    break;
                case SDLTouchIdentifierSecondFinger:
                    self.currentPinchGesture.secondTouch = touch;
                    break;
            }

            if (self.currentPinchGesture.isValid) {
                if ([self.touchEventDelegate respondsToSelector:@selector(touchManager:pinchDidEndAtCenterPoint:)]) {
                    [self.touchEventDelegate touchManager:self
                                 pinchDidEndAtCenterPoint:self.currentPinchGesture.center];
                }
                self.currentPinchGesture = nil;
            }
            break;
        case SDLPerformingTouchTypePanningTouch:
            if ([self.touchEventDelegate respondsToSelector:@selector(touchManager:panningDidEndAtPoint:)]) {
                [self.touchEventDelegate touchManager:self
                                 panningDidEndAtPoint:touch.location];
            }
            break;
        case SDLPerformingTouchTypeSingleTouch:
            if (self.singleTapTimer == nil) { // Initial Tap
                self.singleTapTouch = touch;
                [self sdl_initializeSingleTapTimerAtPoint:self.singleTapTouch.location];
            } else { // Double Tap
                [self sdl_cancelSingleTapTimer];

                NSUInteger timeStampDelta = touch.timeStamp - self.singleTapTouch.timeStamp;
                CGFloat xDelta = fabs(touch.location.x - self.singleTapTouch.location.x);
                CGFloat yDelta = fabs(touch.location.y - self.singleTapTouch.location.y);

                if (timeStampDelta <= self.tapTimeThreshold * NSEC_PER_USEC && xDelta <= self.tapDistanceThreshold && yDelta <= self.tapDistanceThreshold) {
                    CGPoint centerPoint = CGPointCenterOfPoints(touch.location,
                                                                self.singleTapTouch.location);
                    if ([self.touchEventDelegate respondsToSelector:@selector(touchManager:didReceiveDoubleTapAtPoint:)]) {
                        [self.touchEventDelegate touchManager:self
                                   didReceiveDoubleTapAtPoint:centerPoint];
                    }
                }

                self.singleTapTouch = nil;
            }
            break;
        case SDLPerformingTouchTypeNone:
            break;
    }
    self.previousTouch = nil;
    _performingTouchType = SDLPerformingTouchTypeNone;
}

- (void)sdl_initializeSingleTapTimerAtPoint:(CGPoint)point {
    __weak typeof(self) weakSelf = self;
    self.singleTapTimer = dispatch_create_timer(self.tapTimeThreshold, NO, ^{
        typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.singleTapTouch = nil;
        [strongSelf sdl_cancelSingleTapTimer];
        if ([strongSelf.touchEventDelegate respondsToSelector:@selector(touchManager:didReceiveSingleTapAtPoint:)]) {
            [strongSelf.touchEventDelegate touchManager:strongSelf
                             didReceiveSingleTapAtPoint:point];
        }
    });
}

- (void)sdl_cancelSingleTapTimer {
    if (self.singleTapTimer == NULL) {
        return;
    }
    dispatch_stop_timer(self.singleTapTimer);
    self.singleTapTimer = NULL;
}

@end

NS_ASSUME_NONNULL_END