home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / Rhapsody / Graphics / Morph-2.0a / MovieView.m < prev    next >
Encoding:
Text File  |  1998-01-25  |  2.4 KB  |  120 lines

  1. #import "MovieView.h"
  2.  
  3. @implementation MovieView
  4.  
  5. - initWithFrame:(NSRect)r
  6. {
  7.     self = [super initWithFrame:r];
  8.     if (self == nil)
  9.         return nil;
  10.  
  11.     framesPerSecond = 10;
  12.     direction = 1;
  13.     [self updatePositionControl];
  14.     
  15.     return self;
  16. }
  17.  
  18. - (void)play:(id)sender
  19. {
  20.     [timer invalidate];
  21.     [timer autorelease];
  22.     timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 / framesPerSecond
  23.                                     target:self
  24.                                   selector:@selector(animate:)
  25.                                     userInfo:nil
  26.                                    repeats:YES] retain]; 
  27. }
  28.  
  29. - (void)positionControlChanged:(id)sender
  30. {
  31.     currentFrameIndex = [sender floatValue] * [frames count];
  32.     [self setNeedsDisplay:YES];
  33. }
  34.  
  35. - (void)setFrames:(id)obj
  36. {
  37.     [frames autorelease];
  38.     frames = [obj retain];
  39. }
  40.  
  41. - (void)speedControlChanged:(id)sender
  42. {
  43.     BOOL wasRunning = [timer isValid];
  44.     
  45.     framesPerSecond = [speedControl floatValue];
  46.     [self stop:nil];
  47.     if (wasRunning)
  48.         [self play:nil];
  49. }
  50.  
  51. - (void)rewind:sender
  52. {
  53.     currentFrameIndex -= 5;
  54.     [self updatePositionControl];
  55.     [self setNeedsDisplay:YES];
  56. }
  57.  
  58. - (void)fastForward:sender
  59. {
  60.     currentFrameIndex += 5;
  61.     [self updatePositionControl];
  62.     [self setNeedsDisplay:YES];
  63. }
  64.  
  65. - (void)stepBackward:(id)sender
  66. {
  67.     --currentFrameIndex;
  68.     [self updatePositionControl];
  69.     [self setNeedsDisplay:YES];
  70. }
  71.  
  72. - (void)stepForward:(id)sender
  73. {
  74.     ++currentFrameIndex;
  75.     [self updatePositionControl];
  76.     [self setNeedsDisplay:YES];
  77. }
  78.  
  79. - (void)stop:(id)sender
  80. {
  81.     [timer invalidate];
  82.     [self setNeedsDisplay:YES];
  83. }
  84.  
  85. - (void)drawRect:(NSRect)rect
  86. {
  87.     currentFrameIndex = MIN (currentFrameIndex, [frames count] - 1);
  88.     currentFrameIndex = MAX (currentFrameIndex, 0);
  89.  
  90.     if ([frames count] > 0)
  91.     {
  92.         NSImage *image = [frames objectAtIndex:currentFrameIndex];
  93.         [image compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
  94.     }
  95. }
  96.  
  97. - (void)animate:(id)userInfo
  98. {
  99.     currentFrameIndex += direction;
  100.     if (currentFrameIndex < 0)
  101.     {
  102.         direction = 1;
  103.         currentFrameIndex = 1;
  104.     }
  105.     else if (currentFrameIndex >= [frames count])
  106.     {
  107.         direction = -1;
  108.         currentFrameIndex = [frames count] - 2;
  109.     }
  110.     
  111.     [self updatePositionControl];
  112.     [self setNeedsDisplay:YES];
  113. }
  114.  
  115. -(void)updatePositionControl
  116. {
  117.     [positionControl setFloatValue:currentFrameIndex / (float)([frames count] - 1.0)];
  118. }
  119. @end
  120.