[60] | 1 | //
|
---|
| 2 | // NJRCenteringMovieView.m
|
---|
| 3 | // Pester
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on Fri Jan 03 2003.
|
---|
| 6 | // Copyright (c) 2003 Nicholas Riley. All rights reserved.
|
---|
| 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "NJRCenteringMovieView.h"
|
---|
| 10 | #import <QuickTime/Movies.h>
|
---|
| 11 |
|
---|
| 12 | @implementation NJRCenteringMovieView
|
---|
| 13 |
|
---|
| 14 | // from _MacTech_ December 2002, p. 76
|
---|
| 15 |
|
---|
| 16 | - (NSRect)movieRect;
|
---|
| 17 | {
|
---|
| 18 | NSRect viewRect = [super movieRect];
|
---|
| 19 | Movie qtMovie = [[self movie] QTMovie];
|
---|
| 20 | Rect movieRect = {0, 0, 0, 0};
|
---|
| 21 |
|
---|
| 22 | GetMovieNaturalBoundsRect(qtMovie, &movieRect);
|
---|
| 23 |
|
---|
| 24 | float movieWidth = movieRect.right - movieRect.left;
|
---|
| 25 | float movieHeight = movieRect.bottom - movieRect.top;
|
---|
| 26 |
|
---|
| 27 | if ( (movieWidth <= viewRect.size.width) &&
|
---|
| 28 | (movieHeight <= viewRect.size.height) ) {
|
---|
| 29 | // Movie is smaller than or equal to the view size; just center the movie
|
---|
| 30 | viewRect.origin.y += (int)((viewRect.size.height - movieHeight) / 2.);
|
---|
| 31 | viewRect.size.height = movieHeight;
|
---|
| 32 |
|
---|
| 33 | viewRect.origin.x += (int)((viewRect.size.width - movieWidth) / 2.);
|
---|
| 34 | viewRect.size.width = movieWidth;
|
---|
| 35 | } else {
|
---|
| 36 | // We need to scale down movie, centering horizontally and vertically
|
---|
| 37 | float movieRatio = movieWidth / (float)movieHeight;
|
---|
| 38 | float viewRatio = viewRect.size.width / viewRect.size.height;
|
---|
| 39 |
|
---|
| 40 | if (movieRatio > viewRatio) {
|
---|
| 41 | // Movie is wider than will fit; rescale.
|
---|
| 42 | float newHeight = viewRect.size.width / movieRatio;
|
---|
| 43 |
|
---|
| 44 | viewRect.origin.y += (int)((viewRect.size.height - newHeight) / 2.);
|
---|
| 45 | viewRect.size.height = newHeight;
|
---|
| 46 | } else {
|
---|
| 47 | // Movie is taller than will fit (or has the ideal aspect ratio); rescale.
|
---|
| 48 | float newWidth = viewRect.size.height * movieRatio;
|
---|
| 49 |
|
---|
| 50 | viewRect.origin.x += (int) ((viewRect.size.width - newWidth) / 2.);
|
---|
| 51 | viewRect.size.width = newWidth;
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | return viewRect;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | @end
|
---|