Whoops. Haha. :D Lemme just slip this fix in there.
[ITKit.git] / Deprecated / ITChasingArrowsView.m
1 #import "ITChasingArrowsView.h"
2
3
4 @implementation ITChasingArrowsView
5
6 - (id)initWithCoder:(NSCoder *)decoder
7 {
8     self = [super initWithCoder:decoder];
9
10     running = NO;
11
12     images = [[decoder decodeObject] retain];
13
14     curIndex = 0;
15     timer = nil;
16     
17     return self;
18 }
19
20 - (void)encodeWithCoder:(NSCoder *)coder
21 {
22     [super encodeWithCoder:coder];
23     
24     [coder encodeObject:images];
25 }
26
27 - (id)initWithFrame:(NSRect)frame {
28     self = [super initWithFrame:frame];
29     if (self) {
30         NSBundle *bund = [NSBundle bundleForClass:[self class]];
31         running = NO;
32         images = [[NSArray alloc] initWithObjects:
33             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow1.tiff"]] autorelease],
34             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow2.tiff"]] autorelease],
35             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow3.tiff"]] autorelease],
36             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow4.tiff"]] autorelease],
37             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow5.tiff"]] autorelease],
38             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow6.tiff"]] autorelease],
39             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow7.tiff"]] autorelease],
40             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow8.tiff"]] autorelease],
41             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow9.tiff"]] autorelease],
42             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow10.tiff"]] autorelease],
43             nil];
44         
45         curIndex = 0;
46         timer = nil;
47     }
48     return self;
49 }
50
51 - (void)dealloc
52 {
53     if (timer)
54     {
55         [timer invalidate];
56         [timer release];
57         timer = nil;
58     }
59     [images release];
60     
61     [super dealloc];
62 }
63 - (void)drawRect:(NSRect)rect {
64
65     if (running)
66     {
67         NSImage *curImage = [images objectAtIndex:curIndex];
68         float amt;
69         if (inForeground)
70         {
71             amt = 1.0;
72         }
73         else
74         {
75             amt = 0.5;
76         }
77         [curImage compositeToPoint:NSMakePoint(0,0) operation:NSCompositeSourceOver fraction:amt];
78     }
79     else
80     {
81         // draw nothing.
82     }
83 }
84
85 - (IBAction)stop:(id)sender
86 {
87     running = NO;
88
89     if (timer)
90     {
91         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
92         [nc removeObserver:self];
93         [timer invalidate];
94         [timer release];
95         timer = nil;
96     }
97     
98     [self setNeedsDisplay:YES];
99 }
100     
101 - (IBAction)start:(id)sender
102 {
103     if (!timer)
104     {
105         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
106         [nc addObserver:self selector:@selector(appWentToBackground:) name:NSApplicationWillResignActiveNotification object:nil];
107         [nc addObserver:self selector:@selector(appWentToForeground:) name:NSApplicationWillBecomeActiveNotification object:nil];
108         [nc addObserver:self selector:@selector(windowWentToBackground:) name:NSWindowDidResignMainNotification object:nil];
109         [nc addObserver:self selector:@selector(windowWentToForeground:) name:NSWindowDidBecomeMainNotification object:nil];
110         inForeground = ([NSApp isActive] && [[self window] isMainWindow]);
111         curIndex = 0;
112         running = YES;
113         timer = [[NSTimer scheduledTimerWithTimeInterval:0.05
114                                                   target:self
115                                                 selector:@selector(showNewImage:)
116                                                 userInfo:nil
117                                                  repeats:YES] retain];
118         [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
119     }
120 }
121
122 - (void)showNewImage:(NSTimer *)t
123 {
124     [self setNeedsDisplay:YES];
125
126     if (curIndex == 9)
127     {
128         curIndex = 0;
129     }
130     else
131     {
132         curIndex++;
133     }
134 }
135
136 - (void)appWentToBackground:(NSNotification *)note
137 {
138     inForeground = NO;
139 }
140
141 - (void)appWentToForeground:(NSNotification *)note
142 {
143     inForeground = YES;
144 }
145
146 - (void)windowWentToBackground:(NSNotification *)note
147 {
148     NSWindow *window = [note object];
149     if (window == [self window])
150     {
151         inForeground = NO;
152     }
153 }
154
155 - (void)windowWentToForeground:(NSNotification *)note
156 {
157     NSWindow *window = [note object];
158     if (window == [self window])
159     {
160         inForeground = YES;
161     }
162 }
163
164
165 @end