VLCMovieViewControlPanelView.m 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*****************************************************************************
 * VLCMovieViewControlPanelView.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Tobias Conradi <videolan@tobias-conradi.de>
 *          Carola Nitz <nitz.carola@googlemail.com>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCMovieViewControlPanelView.h"
#import "VLCPlaybackController.h"
16
#import "VLCMetadata.h"
17 18 19 20

@interface VLCMovieViewControlPanelView ()

@property (nonatomic, strong) UIView *playbackControls;
21 22
@property (nonatomic, strong) UIView *spacer1;
@property (nonatomic, strong) UIView *spacer2;
23 24 25 26 27 28 29
@property (nonatomic, strong) NSMutableArray *constraints;
@property (nonatomic, assign) BOOL compactMode;
@property (nonatomic, strong) VLCPlaybackController *playbackController;
@end

@implementation VLCMovieViewControlPanelView

30
static const CGFloat maxControlsWidth = 474.0;
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

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
        [self setupConstraints];
        _compactMode = YES;
        [self setupConstraints:_compactMode];
    }
    return self;
}

- (void)setupSubviews
{
    _playbackSpeedButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [_playbackSpeedButton setImage:[UIImage imageNamed:@"speedIcon"] forState:UIControlStateNormal];
    _playbackSpeedButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_playbackSpeedButton];

    _trackSwitcherButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [_trackSwitcherButton setImage:[UIImage imageNamed:@"audioTrackIcon"] forState:UIControlStateNormal];
    _trackSwitcherButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_trackSwitcherButton];

    _playbackControls = [[UIView alloc] initWithFrame:CGRectZero];
    _playbackControls.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_playbackControls];

    _bwdButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [_bwdButton setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
    _bwdButton.translatesAutoresizingMaskIntoConstraints = NO;
    [_playbackControls addSubview:_bwdButton];

65 66 67 68 69 70 71 72 73 74
    _spacer1 = [UIView new];
    _spacer1.backgroundColor = [UIColor clearColor];
    _spacer1.translatesAutoresizingMaskIntoConstraints = NO;
    [_playbackControls addSubview:_spacer1];

    _spacer2 = [UIView new];
    _spacer2.backgroundColor = [UIColor clearColor];
    _spacer2.translatesAutoresizingMaskIntoConstraints = NO;
    [_playbackControls addSubview:_spacer2];

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
    _playPauseButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [_playPauseButton setImage:[UIImage imageNamed:@"playIcon"] forState:UIControlStateNormal];
    _playPauseButton.translatesAutoresizingMaskIntoConstraints = NO;
    [_playbackControls addSubview:_playPauseButton];

    _fwdButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [_fwdButton setImage:[UIImage imageNamed:@"forwardIcon"] forState:UIControlStateNormal];
    _fwdButton.translatesAutoresizingMaskIntoConstraints = NO;
    [_playbackControls addSubview:_fwdButton];

    _videoFilterButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [_videoFilterButton setImage:[UIImage imageNamed:@"videoEffectsIcon"] forState:UIControlStateNormal];
    _videoFilterButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_videoFilterButton];

    _moreActionsButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [_moreActionsButton setImage:[UIImage imageNamed:@"More"] forState:UIControlStateNormal];
    _moreActionsButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_moreActionsButton];

    _volumeView = [[VLCVolumeView alloc] initWithFrame:CGRectZero];
    _volumeView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_volumeView];

    _playbackSpeedButton.accessibilityLabel = NSLocalizedString(@"PLAYBACK_SPEED", nil);
    _trackSwitcherButton.accessibilityLabel = NSLocalizedString(@"OPEN_TRACK_PANEL", nil);
    _bwdButton.accessibilityLabel = NSLocalizedString(@"BWD_BUTTON", nil);
    _playPauseButton.accessibilityLabel = NSLocalizedString(@"PLAY_PAUSE_BUTTON", nil);
    _playPauseButton.accessibilityHint = NSLocalizedString(@"LONGPRESS_TO_STOP", nil);
    _fwdButton.accessibilityLabel = NSLocalizedString(@"FWD_BUTTON", nil);
    _videoFilterButton.accessibilityLabel = NSLocalizedString(@"VIDEO_FILTER", nil);

    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(playPauseLongPress:)];
    [_playPauseButton addGestureRecognizer:longPressRecognizer];

    [self.volumeView setVolumeThumbImage:[UIImage imageNamed:@"sliderKnob"] forState:UIControlStateNormal];
}

- (void)setupConstraints
{
    NSDictionary *viewsDict = @{
                                @"forward" : self.fwdButton,
                                @"backward" : self.bwdButton,
                                @"playpause" : self.playPauseButton,
                                @"speed" : self.playbackSpeedButton,
                                @"track" : self.trackSwitcherButton,
                                @"more" : self.moreActionsButton,
                                @"filter" : self.videoFilterButton,
                                @"volume" : self.volumeView,
124 125
                                @"spacer1" : _spacer1,
                                @"spacer2" : _spacer2,
126 127
                                };
    NSMutableArray *staticConstraints = [NSMutableArray new];
128 129

    [staticConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[backward(40)][spacer1(margin@750)][playpause(40)][spacer2(==spacer1)][forward(40)]|"
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
                                                                              options:NSLayoutFormatAlignAllCenterY
                                                                              metrics:@{@"margin": @15.0}
                                                                                views:viewsDict]];

    [staticConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[backward(40)]|"
                                                                                   options:NSLayoutFormatAlignAllCenterX
                                                                                   metrics:nil
                                                                                     views:viewsDict]];
    for (NSString *object in viewsDict) {
        NSString *format = [NSString stringWithFormat:@"V:[%@(40)]", object];

        [staticConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:format
                                                                                       options:NSLayoutFormatAlignAllCenterX
                                                                                       metrics:nil
                                                                                         views:viewsDict]];
    }
146
    [self addConstraints:staticConstraints];
147 148 149 150
}

- (void)updateViewConstraints
{
151
    BOOL compactMode = CGRectGetWidth(self.frame) <= maxControlsWidth;
152 153 154 155 156 157 158 159
    if (self.compactMode != compactMode) {
        self.compactMode = compactMode;
        [self setupConstraints:compactMode];
    }
}

- (void)setupConstraints:(BOOL)compactMode
{
160 161 162 163
    if (_constraints != nil) {
        [self removeConstraints:_constraints];
    }

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
    NSDictionary *viewsDict = @{@"speed" : self.playbackSpeedButton,
                                @"track" : self.trackSwitcherButton,
                                @"playback" : self.playbackControls,
                                @"filter" : self.videoFilterButton,
                                @"actions" : self.moreActionsButton,
                                @"volume" : self.volumeView,
                                };

    _constraints = [NSMutableArray array];
    if (compactMode) {

        [_constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[speed(35)]-[track(35)]-(>=8)-[playback]-(>=8)-[filter(35)]-[actions(35)]-|"
                                                                                 options:NSLayoutFormatAlignAllCenterY
                                                                                 metrics:nil
                                                                                   views:viewsDict]];
        [_constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=10)-[volume(==300)]-(>=10)-|"
                                                                                 options:0
                                                                                 metrics:nil
                                                                                   views:viewsDict]];

        [_constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[playback]-[volume]-|"
                                                                                 options:NSLayoutFormatAlignAllCenterX
                                                                                 metrics:nil
                                                                                   views:viewsDict]];
    } else {
        [_constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[volume(>=150)]-(>=8)-[playback]-(>=8)-[speed(35)]-[track(35)]-[filter(35)]-[actions(35)]-|"
                                                                                 options:NSLayoutFormatAlignAllCenterY
                                                                                 metrics:nil
                                                                                   views:viewsDict]];

        [_constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[playback]-|"
                                                                                 options:0
                                                                                 metrics:nil
                                                                                   views:viewsDict]];
    }

    [_constraints addObject:[NSLayoutConstraint constraintWithItem:self.playbackControls
                                                        attribute:NSLayoutAttributeCenterX
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:self
                                                        attribute:NSLayoutAttributeCenterX
                                                       multiplier:1.0
                                                         constant:0]];
    
    [self addConstraints:_constraints];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self updateViewConstraints];
}

- (VLCPlaybackController *)playbackController
{
    if (!_playbackController) {
        _playbackController = [VLCPlaybackController sharedInstance];
    }
    return _playbackController;
}

- (void)playPauseLongPress:(UILongPressGestureRecognizer *)recognizer
{
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
        {   UIImage *image = [UIImage imageNamed:@"stopIcon"];
            [_playPauseButton setImage:image forState:UIControlStateNormal];
        }
            break;
        case UIGestureRecognizerStateEnded:
            [self.playbackController stopPlayback];
            break;
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            [self updatePlayPauseButton];
            break;
        default:
            break;
    }
}

- (void)updateButtons
{
    [self updatePlayPauseButton];

    self.trackSwitcherButton.hidden = !self.playbackController.currentMediaHasTrackToChooseFrom;
250
    self.videoFilterButton.hidden = self.playbackController.metadata.isAudioOnly;
251 252 253 254 255 256 257 258 259 260
}

- (void)updatePlayPauseButton
{
    const BOOL isPlaying = self.playbackController.isPlaying;
    UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
    [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
}

@end