first commit
[exterlulz-kokomonds.git] / src / TimerView.m
blob11e128bb0cd66044153b369e3cd748a7bf008c24
1 /* ----====----====----====----====----====----====----====----====----====----
2  MyTimerView.m (jeweltoy)
3  
4  JewelToy is a simple game played against the clock.
5  Copyright (C) 2001  Giles Williams
6  
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 /* kokomonds is a fork of JewelToy.
23  * repository: http://github.com/exterlulz/kokomonds
24  */
26 // TODO: clean
28 #import "TimerView.h"
30 @implementation TimerView
32 - (id)initWithFrame:(NSRect)frame
34   self = [super initWithFrame:frame];
35   
36   meter = 0.5;
37   
38   // TODO: replace color1 with [NSColor redColor], same thing for the rest...
39   color1        = [[NSColor redColor] retain];
40   color2        = [[NSColor yellowColor] retain];
41   colorOK       = [[NSColor greenColor] retain];
42   backColor     = [[NSColor blackColor] retain];
43   isRunning     = NO;
44   
45   return self;
48 // dealloc is the method called when objects are being freed. (Note that "release"
49 // is called to release objects; when the number of release calls reduce the
50 // total reference count on an object to zero, dealloc is called to free
51 // the object.  dealloc should free any memory allocated by the subclass
52 // and then call super to get the superclass to do additional cleanup.
54 - (void)dealloc
56   [color1 release];
57   [color2 release];
58   [colorOK release];
59   [backColor release];
60   [super dealloc];
63 // drawRect: should be overridden in subclassers of NSView to do necessary
64 // drawing in order to recreate the the look of the view. It will be called
65 // to draw the whole view or parts of it (pay attention the rect argument);
67 - (void)drawRect:(NSRect)rect {
68   NSRect dotRect;
69   
70   [backColor set];
71   NSRectFill([self bounds]);   // Equiv to [[NSBezierPath bezierPathWithRect:[self bounds]] fill]
72   
73   dotRect.origin.x = 4;
74   dotRect.origin.y = 4;
75   dotRect.size.width  = meter * ([self bounds].size.width - 8);
76   dotRect.size.height = [self bounds].size.height - 8;
77   
78   [colorOK set];
79   //
80   // another MW change...
81   //
82   if (decrement!=0)
83   {
84     if (meter < 0.3) [color2 set];
85     if (meter < 0.1) [color1 set];
86   }
87   
88   NSRectFill(dotRect);   // Equiv to [[NSBezierPath bezierPathWithRect:dotRect] fill]
91 - (BOOL)isOpaque {
92   return YES;
95 // Utility
96 - (void) setPaused:(BOOL) value
98   isRunning = !value;
101 - (void) incrementMeter:(float) value
103   meter += value;
104   if (meter > 1) meter = 1;
105   [self setNeedsDisplay:YES];
108 - (void) setDecrement:(float) value
110   decrement = value;
113 - (void) decrementMeter:(float) value
115   meter -= value;
116   if (meter < 0) meter = 0;
117   [self setNeedsDisplay:YES];
120 - (void) setTimerRunningEvery:(NSTimeInterval) timeInterval
121                     decrement:(float) value
122                    withTarget:(id) targ
123                    whenRunOut:(SEL) runOutSel
124                   whenRunOver:(SEL) runOverSel
126   decrement = value;
127   target = targ;
128   runOutSelector = runOutSel;
129   runOverSelector = runOverSel;
130   if (timer)
131   {
132     [timer invalidate];
133   }
134   timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval
135                                            target:self
136                                          selector:@selector(runTimer)
137                                          userInfo:self
138                                           repeats:YES];
139   isRunning = YES;
142 - (void) runTimer
144   if (isRunning)
145   {
146     if (meter == 1)
147     {
148       isRunning = NO;
149       [target performSelector:runOverSelector];
150       return;
151     }
152     
153     [self decrementMeter:decrement];
154     if (meter == 0 && decrement!=0)     // MW change added '&& decrement'
155     {
156       isRunning = NO;
157       [target performSelector:runOutSelector];
158       return;
159     }
160   }
163 - (void) setTimer:(float)value
165   isRunning = NO;
166   meter = value;
167   
168   [self setNeedsDisplay:YES];
171 - (float) meter {
172   return meter;
175 @end