summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLStreamingVideoScaleManager.m
blob: d9c2158d976fea6c6d8bb5c94f9682ea883bb835 (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
//
//  SDLStreamingVideoScaleManager.m
//  SmartDeviceLink
//
//  Created by Nicole on 10/1/19.
//  Copyright © 2019 smartdevicelink. All rights reserved.
//

#import "SDLStreamingVideoScaleManager.h"

#import "SDLOnTouchEvent.h"
#import "SDLRectangle.h"
#import "SDLTouchCoord.h"
#import "SDLTouchEvent.h"
#import "SDLHapticRect.h"
#import "SDLNotificationConstants.h"

NS_ASSUME_NONNULL_BEGIN

@interface SDLStreamingVideoScaleManager ()

@property (assign, nonatomic, readwrite) CGRect appViewportFrame;

@end

@implementation SDLStreamingVideoScaleManager

const float SDLDefaultScaleValue = 1.0;
CGSize const SDLDefaultDisplayViewportResolution = {0, 0};

- (instancetype)init {
    return [[self.class alloc] initWithScale:SDLDefaultScaleValue displayViewportResolution:SDLDefaultDisplayViewportResolution];
}

- (void)stop {
    self.displayViewportResolution = SDLDefaultDisplayViewportResolution;
    self.scale = SDLDefaultScaleValue;
}

- (instancetype)initWithScale:(float)scale displayViewportResolution:(CGSize)displayViewportResolution {
    self = [super init];
    if (!self) {
        return nil;
    }

    _scale = [self.class validateScale:scale];
    _displayViewportResolution = displayViewportResolution;

    return self;
}

- (SDLOnTouchEvent *)scaleTouchEventCoordinates:(SDLOnTouchEvent *)onTouchEvent {
    if (self.scale <= SDLDefaultScaleValue) {
        return onTouchEvent;
    }
    for (SDLTouchEvent *touchEvent in onTouchEvent.event) {
        for (SDLTouchCoord *coord in touchEvent.coord) {
            coord.x = @(coord.x.floatValue / self.scale);
            coord.y = @(coord.y.floatValue / self.scale);
        }
    }
    return onTouchEvent;
}

- (SDLHapticRect *)scaleHapticRect:(SDLHapticRect *)hapticRect {
    hapticRect.rect.x = @(hapticRect.rect.x.floatValue * self.scale);
    hapticRect.rect.y = @(hapticRect.rect.y.floatValue * self.scale);
    hapticRect.rect.width = @(hapticRect.rect.width.floatValue * self.scale);
    hapticRect.rect.height = @(hapticRect.rect.height.floatValue * self.scale);
    return hapticRect;
}

#pragma mark - Getters and Setters

- (CGRect)appViewportFrame {
    // Screen capture in the CarWindow API only works if the width and height are integer values
    return CGRectMake(0, 0, roundf((float)self.displayViewportResolution.width / self.scale), roundf((float)self.displayViewportResolution.height / self.scale));
}

- (void)setScale:(float)scale {
    _scale = [self.class validateScale:scale];
}

#pragma mark - Helpers

/**
 Validates the scale value. Returns the default scale value for 1.0 if the scale value is less than 1.0

 @param scale The scale value to be validated.
 @return The validated scale value
 */
+ (float)validateScale:(float)scale {
    return (scale > SDLDefaultScaleValue) ? scale : SDLDefaultScaleValue;
}

@end

NS_ASSUME_NONNULL_END