add Gem.sprite property
[exterlulz-kokogems.git] / src / TimerView.m
bloba43af904276dd7a9a1dcb6c7afa16dfd011ff523
1 /* ----====----====----====----====----====----====----====----====----====----
2 MyTimerView.m (jeweltoy)
4 JewelToy is a simple game played against the clock.
5 Copyright (C) 2001  Giles Williams
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 ----====----====----====----====----====----====----====----====----====---- */
22 #import "TimerView.h"
24 @implementation TimerView
26 - (id)initWithFrame:(NSRect)frame {
27     [super initWithFrame:frame];
28     
29     meter       = 0.5;
30     color1      = [[NSColor redColor] retain];
31     color2      = [[NSColor yellowColor] retain];
32     colorOK     = [[NSColor greenColor] retain];
33     backColor   = [[NSColor blackColor] retain];
34     isRunning   = NO;
35     
36     return self;
39 // dealloc is the method called when objects are being freed. (Note that "release"
40 // is called to release objects; when the number of release calls reduce the
41 // total reference count on an object to zero, dealloc is called to free
42 // the object.  dealloc should free any memory allocated by the subclass
43 // and then call super to get the superclass to do additional cleanup.
45 - (void)dealloc {
46     [color1 release];
47     [color2 release];
48     [colorOK release];
49     [backColor release];
50     [super dealloc];
53 - (void)drawRect:(NSRect)rect
55   #pragma unused (rect)
56   
57     NSRect dotRect;
59     [backColor set];
60     NSRectFill([self bounds]);   // Equiv to [[NSBezierPath bezierPathWithRect:[self bounds]] fill]
62     dotRect.origin.x = 4;
63     dotRect.origin.y = 4;
64     dotRect.size.width  = meter * ([self bounds].size.width - 8);
65     dotRect.size.height = [self bounds].size.height - 8;
66     
67     [colorOK set];
68     //
69     // another MW change...
70     //
71     if (decrement!=0)
72     {
73         if (meter < 0.3) [color2 set];
74         if (meter < 0.1) [color1 set];
75     }
77     NSRectFill(dotRect);   // Equiv to [[NSBezierPath bezierPathWithRect:dotRect] fill]
80 - (BOOL)isOpaque {
81     return YES;
84 // Utility
85 - (void) setPaused:(BOOL) value
87     isRunning = !value;
90 - (void) incrementMeter:(float) value
92     meter += value;
93     if (meter > 1) meter = 1;
94     [self setNeedsDisplay:YES];
97 - (void) setDecrement:(float) value
99     decrement = value;
102 - (void) decrementMeter:(float) value
104     meter -= value;
105     if (meter < 0) meter = 0;
106     [self setNeedsDisplay:YES];
109 - (void) setTimerRunningEvery:(NSTimeInterval) timeInterval
110             decrement:(float) value
111             withTarget:(id) targ
112             whenRunOut:(SEL) runOutSel
113             whenRunOver:(SEL) runOverSel
115     decrement = value;
116     target = targ;
117     runOutSelector = runOutSel;
118     runOverSelector = runOverSel;
119     if (timer)
120     {
121         [timer invalidate];
122     }
123     timer = [NSTimer    scheduledTimerWithTimeInterval:timeInterval
124                 target:self
125                 selector:@selector(runTimer)
126                 userInfo:self
127                 repeats:YES];
128     isRunning = YES;
131 - (void) runTimer
133     if (isRunning)
134     {
135         if (meter == 1)
136         {
137             isRunning = NO;
138             [target performSelector:runOverSelector];
139             return;
140         }
141         [self decrementMeter:decrement];
142         if (meter == 0 && decrement!=0) // MW change added '&& decrement'
143         {
144             isRunning = NO;
145             [target performSelector:runOutSelector];
146             return;
147         }
148     }
151 - (void) setTimer:(float)value
153     isRunning = NO;
154     meter = value;
155     [self setNeedsDisplay:YES];
158 - (float) meter {
159     return meter;
162 @end