Fixed some memory leaks, and took the address-related properties out of Player.
1 /* This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
2 http://developer.apple.com/samplecode/GeekGameBoard/
3 Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
5 Redistribution and use in source and binary forms, with or without modification, are permitted
6 provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice, this list of conditions
9 and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this list of
11 conditions and the following disclaimer in the documentation and/or other materials provided
12 with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
17 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
21 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #import "QuartzUtils.h"
29 @implementation Dispenser
32 - (id) initWithPrototype: (Bit*)prototype quantity: (unsigned)quantity frame: (CGRect)frame
36 self.backgroundColor = kTranslucentLightGrayColor;
37 self.borderColor = kTranslucentGrayColor;
39 self.cornerRadius = 16;
40 self.zPosition = kBoardZ;
41 self.masksToBounds = YES;
43 self.prototype = prototype;
44 self.quantity = quantity;
63 Bit *bit = [_prototype copy];
64 CGRect bounds = self.bounds;
65 bit.position = GetCGRectCenter(bounds);
66 return [bit autorelease];
71 - (void) x_regenerateCurrentBit
73 NSAssert(_bit==nil,@"Already have a currentBit");
75 BeginDisableAnimations();
76 self.bit = [self createBit];
77 CGPoint pos = _bit.position;
78 _bit.position = CGPointMake(pos.x, pos.y+70);
79 [self addSublayer: _bit];
80 EndDisableAnimations();
91 - (void) setPrototype: (Bit*)prototype
93 setObj(&_prototype, prototype);
95 [_bit removeFromSuperlayer];
98 [self x_regenerateCurrentBit];
103 - (unsigned) quantity
108 - (void) setQuantity: (unsigned)quantity
110 _quantity = quantity;
111 if( quantity > 0 && !_bit )
112 [self x_regenerateCurrentBit];
113 else if( quantity==0 && _bit ) {
114 [_bit removeFromSuperlayer];
121 #pragma mark DRAGGING BITS:
124 - (Bit*) canDragBit: (Bit*)bit
126 bit = [super canDragBit: bit];
128 [[bit retain] autorelease];
134 - (void) cancelDragBit: (Bit*)bit
139 [bit removeFromSuperlayer];
142 - (void) draggedBit: (Bit*)bit to: (id<BitHolder>)dst
144 if( --_quantity > 0 )
145 [self performSelector: @selector(x_regenerateCurrentBit) withObject: nil afterDelay: 0.0];
148 - (BOOL) canDropBit: (Bit*)bit atPoint: (CGPoint)point
150 return [bit isEqual: _bit];
153 - (BOOL) dropBit: (Bit*)bit atPoint: (CGPoint)point
155 [bit removeFromSuperlayer];
161 #pragma mark DRAG-AND-DROP:
164 #if ! TARGET_OS_IPHONE
166 // An image from another app can be dragged onto a Dispenser to change the Piece's appearance.
169 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
171 if( ! [_prototype isKindOfClass: [Piece class]] )
172 return NSDragOperationNone;
173 NSPasteboard *pb = [sender draggingPasteboard];
174 if( [NSImage canInitWithPasteboard: pb] )
175 return NSDragOperationCopy;
177 return NSDragOperationNone;
180 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
182 if( ! [_prototype isKindOfClass: [Piece class]] )
184 CGImageRef image = GetCGImageFromPasteboard([sender draggingPasteboard],sender);
186 [(Piece*)_prototype setImage: image];
187 self.prototype = _prototype; // recreates _bit