clean whitespace
[exterlulz-kokogems.git] / src / TimerView.m
blob3aa70c7f08426f478a5ef01ff39578525b4ce5fc
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 // drawRect: should be overridden in subclassers of NSView to do necessary
54 // drawing in order to recreate the the look of the view. It will be called
55 // to draw the whole view or parts of it (pay attention the rect argument);
57 - (void)drawRect:(NSRect)rect {
58     NSRect dotRect;
60     [backColor set];
61     NSRectFill([self bounds]);   // Equiv to [[NSBezierPath bezierPathWithRect:[self bounds]] fill]
63     dotRect.origin.x = 4;
64     dotRect.origin.y = 4;
65     dotRect.size.width  = meter * ([self bounds].size.width - 8);
66     dotRect.size.height = [self bounds].size.height - 8;
67     
68     [colorOK set];
69     //
70     // another MW change...
71     //
72     if (decrement!=0)
73     {
74         if (meter < 0.3) [color2 set];
75         if (meter < 0.1) [color1 set];
76     }
78     NSRectFill(dotRect);   // Equiv to [[NSBezierPath bezierPathWithRect:dotRect] fill]
81 - (BOOL)isOpaque {
82     return YES;
85 // Utility
86 - (void) setPaused:(BOOL) value
88     isRunning = !value;
91 - (void) incrementMeter:(float) value
93     meter += value;
94     if (meter > 1) meter = 1;
95     [self setNeedsDisplay:YES];
98 - (void) setDecrement:(float) value
100     decrement = value;
103 - (void) decrementMeter:(float) value
105     meter -= value;
106     if (meter < 0) meter = 0;
107     [self setNeedsDisplay:YES];
110 - (void) setTimerRunningEvery:(NSTimeInterval) timeInterval
111             decrement:(float) value
112             withTarget:(id) targ
113             whenRunOut:(SEL) runOutSel
114             whenRunOver:(SEL) runOverSel
116     decrement = value;
117     target = targ;
118     runOutSelector = runOutSel;
119     runOverSelector = runOverSel;
120     if (timer)
121     {
122         [timer invalidate];
123     }
124     timer = [NSTimer    scheduledTimerWithTimeInterval:timeInterval
125                 target:self
126                 selector:@selector(runTimer)
127                 userInfo:self
128                 repeats:YES];
129     isRunning = YES;
132 - (void) runTimer
134     if (isRunning)
135     {
136         if (meter == 1)
137         {
138             isRunning = NO;
139             [target performSelector:runOverSelector];
140             return;
141         }
142         [self decrementMeter:decrement];
143         if (meter == 0 && decrement!=0) // MW change added '&& decrement'
144         {
145             isRunning = NO;
146             [target performSelector:runOutSelector];
147             return;
148         }
149     }
152 - (void) setTimer:(float)value
154     isRunning = NO;
155     meter = value;
156     [self setNeedsDisplay:YES];
159 - (float) meter {
160     return meter;
163 @end