1/* 2 File: MBCAnimation.mm 3 Contains: General animation infrastructure. 4 Copyright: � 2002-2012 by Apple Inc., all rights reserved. 5 6 IMPORTANT: This Apple software is supplied to you by Apple Computer, 7 Inc. ("Apple") in consideration of your agreement to the following 8 terms, and your use, installation, modification or redistribution of 9 this Apple software constitutes acceptance of these terms. If you do 10 not agree with these terms, please do not use, install, modify or 11 redistribute this Apple software. 12 13 In consideration of your agreement to abide by the following terms, 14 and subject to these terms, Apple grants you a personal, non-exclusive 15 license, under Apple's copyrights in this original Apple software (the 16 "Apple Software"), to use, reproduce, modify and redistribute the 17 Apple Software, with or without modifications, in source and/or binary 18 forms; provided that if you redistribute the Apple Software in its 19 entirety and without modifications, you must retain this notice and 20 the following text and disclaimers in all such redistributions of the 21 Apple Software. Neither the name, trademarks, service marks or logos 22 of Apple Inc. may be used to endorse or promote products 23 derived from the Apple Software without specific prior written 24 permission from Apple. Except as expressly stated in this notice, no 25 other rights or licenses, express or implied, are granted by Apple 26 herein, including but not limited to any patent rights that may be 27 infringed by your derivative works or by other works in which the 28 Apple Software may be incorporated. 29 30 The Apple Software is provided by Apple on an "AS IS" basis. APPLE 31 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 32 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND 33 FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS 34 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 35 36 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, 37 INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 38 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 39 PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 40 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, 41 HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING 42 NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN 43 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 44*/ 45 46#import "MBCAnimation.h" 47#import "MBCBoardView.h" 48#import "MBCBoardWin.h" 49 50#include <algorithm> 51 52using std::min; 53 54@implementation MBCAnimation 55 56- (void) scheduleNextStep 57{ 58 [self performSelector:@selector(doStep:) withObject:nil afterDelay:0.010]; 59} 60 61- (void) startState 62{ 63 [fView startAnimation]; 64} 65 66- (void) step: (float)pctDone {} 67 68- (void) endState 69{ 70 [fView animationDone]; 71} 72 73- (void) doStep:(id)arg 74{ 75 struct timeval now; 76 77 gettimeofday(&now, NULL); 78 float elapsedTime = 79 now.tv_sec - fStart.tv_sec 80 + 0.000001f * (now.tv_usec - fStart.tv_usec); 81 float elapsed = min(elapsedTime/fTime, 1.0f); 82 // 83 // Prevent excessive jerks on slow hardware 84 // 85 if (elapsed-fLastElapsed > 0.5f) 86 elapsed = 1.0f; 87 [self step:elapsed]; 88 fLastElapsed = elapsed; 89 if (elapsed >= 1.0f) { 90 [self endState]; 91 [[[fView window] windowController] endAnimation]; 92 [self release]; 93 [fView setNeedsDisplay:YES]; 94 } else { 95 [self scheduleNextStep]; 96 [fView drawNow]; 97 } 98} 99 100- (void) runWithTime:(float)seconds view:(MBCBoardView *)view 101{ 102 gettimeofday(&fStart, NULL); 103 fTime = seconds; 104 fView = view; 105 fLastElapsed = 0.0f; 106 [self startState]; 107 [self doStep:nil]; 108} 109 110- (void)cancel 111{ 112 [NSObject cancelPreviousPerformRequestsWithTarget:self]; 113 fStart.tv_sec = 0; 114 [self doStep:self]; 115} 116 117@end 118 119// Local Variables: 120// mode:ObjC 121// End: 122