forked from samiamwork/Movist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMovieViewLayer.m
193 lines (169 loc) · 5 KB
/
MMovieViewLayer.m
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
//
// MMovieViewLayer.m
// Movist
//
// Created by Nur Monson on 2/18/13.
//
//
#import "MMovieViewLayer.h"
#import "MMovieLayer.h"
#import "MMovie.h"
#import "MMovieOSDLayer.h"
@implementation MMovieViewLayer
- (id)init
{
if ((self = [super init]))
{
CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
CGColorRef lightBlue = CGColorCreateGenericRGB(0.0, 0.5, 1.0, 1.0);
self.backgroundColor = black;
self.borderColor = lightBlue;
CGColorRelease(lightBlue);
CGColorRelease(black);
_subtitle = [MMovieOSDLayer layer];
_subtitle.name = @"subtitle";
_subtitle.hidden = NO;
_subtitle.zPosition = 1.0;
_subtitle.horizontalPlacement = OSD_HPOSITION_CENTER;
_subtitle.verticalPlacement = OSD_VPOSITION_LBOX;
[self replaceOldOSD:nil withNew:self.subtitle];
}
return self;
}
- (void)setMovie:(CALayer<MMovieLayer>*)newMovie
{
if(newMovie == _movie)
return;
[_movie removeFromSuperlayer];
[self addSublayer:newMovie];
_movie = newMovie;
}
- (void)setIcon:(CALayer*)newIcon
{
if(newIcon == _icon)
return;
[_icon removeFromSuperlayer];
[self addSublayer:newIcon];
_icon = newIcon;
}
- (void)replaceOldOSD:(MMovieOSDLayer*)oldOSD withNew:(MMovieOSDLayer*)newOSD
{
[oldOSD removeObserver:self forKeyPath:@"horizontalPlacement"];
[oldOSD removeObserver:self forKeyPath:@"verticalPlacement"];
[oldOSD removeFromSuperlayer];
newOSD.zPosition = 1.0;
[self addSublayer:newOSD];
[newOSD addObserver:self forKeyPath:@"horizontalPlacement" options:NSKeyValueObservingOptionNew context:NULL];
[newOSD addObserver:self forKeyPath:@"verticalPlacement" options:NSKeyValueObservingOptionNew context:NULL];
}
- (void)setMessage:(MMovieOSDLayer*)newMessage
{
if(newMessage == _message)
return;
[self replaceOldOSD:_message withNew:newMessage];
_message = newMessage;
}
- (void)setError:(MMovieOSDLayer*)newError
{
if(newError == _error)
return;
[self replaceOldOSD:_error withNew:newError];
_error = newError;
}
- (void)setSubtitleEnabled:(BOOL)isEnabled
{
if(_subtitleEnabled == isEnabled)
return;
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithBool:YES] forKey:kCATransactionDisableActions];
{
self.subtitle.hidden = !isEnabled;
}
[CATransaction commit];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(object == _message)
{
[self setNeedsLayout];
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)layoutIcon
{
if(!_icon)
return;
_icon.position = CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
CGImageRef iconImageRef = (CGImageRef)_icon.contents;
_icon.bounds = (CGRect){
.origin = CGPointZero,
.size = (CGSize){.width = CGImageGetWidth(iconImageRef), .height = CGImageGetHeight(iconImageRef)}
};
}
- (void)layoutMovie
{
NSSize movieSize = [_movie.movie adjustedSizeByAspectRatio];
if(!_movie || movieSize.width*movieSize.height <= 0.0)
{
_movie.frame = self.bounds;
return;
}
// TODO: to match the old way, the y-offset should be at least the height of
// one (to three) line(s) of the first subtitle that's in the letterbox.
CGFloat movieAspectRatio = movieSize.width/movieSize.height;
CGFloat boundsAspectRation = self.bounds.size.width/self.bounds.size.height;
if(movieAspectRatio > boundsAspectRation)
{
CGSize size = (CGSize){.width = self.bounds.size.width, .height = self.bounds.size.width/movieAspectRatio};
_movie.frame = (CGRect){
.origin = (CGPoint){.x = 0.0, .y = (self.bounds.size.height-size.height)/2.0},
.size = size,
};
}
else
{
CGSize size = (CGSize){.width = self.bounds.size.height*movieAspectRatio, .height = self.bounds.size.height};
_movie.frame = (CGRect){
.origin = (CGPoint){.x = (self.bounds.size.width-size.width)/2.0, .y = 0.0},
.size = size,
};
}
}
- (void)layoutOSD:(MMovieOSDLayer*)osd
{
CGPoint newPosition = CGPointZero;
switch(osd.horizontalPlacement)
{
case OSD_HPOSITION_LEFT: newPosition.x = 0.0; break;
case OSD_HPOSITION_CENTER: newPosition.x = self.bounds.size.width/2.0; break;
case OSD_HPOSITION_RIGHT: newPosition.x = self.bounds.size.width; break;
default: break;
}
// Assumes that the movie layer has been layed-out already
switch(osd.verticalPlacement)
{
case OSD_VPOSITION_LBOX: newPosition.y = 0.0; break;
case OSD_VPOSITION_BOTTOM: newPosition.y = _movie.frame.origin.y; break;
case OSD_VPOSITION_CENTER: newPosition.y = self.bounds.size.height/2.0; break;
case OSD_VPOSITION_UBOX: newPosition.y = self.bounds.size.height; break;
case OSD_VPOSITION_TOP: newPosition.y = CGRectGetMaxY(_movie.frame); break;
default: break;
}
osd.position = newPosition;
}
- (void)layoutSublayers
{
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithBool:YES] forKey:kCATransactionDisableActions];
{
[self layoutIcon];
[self layoutMovie];
[self layoutOSD:_message];
[self layoutOSD:_subtitle];
}
[CATransaction commit];
}
@end