Lots of reworking. Completed support for game history, including Turn class. Changed Game API around quite a bit.
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.
24 #import "QuartzUtils.h"
30 - (id) initWithStartPos: (CGPoint)startPos spacing: (CGSize)spacing
31 wrapInterval: (int)wrapInterval wrapSpacing: (CGSize)wrapSpacing
37 _wrapInterval = wrapInterval;
38 _wrapSpacing = wrapSpacing;
39 self.cornerRadius = 8;
40 self.backgroundColor = kAlmostInvisibleWhiteColor;
41 self.borderColor = kHighlightColor;
42 _bits = [[NSMutableArray alloc] init];
47 - (id) initWithStartPos: (CGPoint)startPos spacing: (CGSize)spacing;
49 return [self initWithStartPos: startPos spacing: spacing
50 wrapInterval: INT_MAX wrapSpacing: CGSizeZero];
61 @synthesize spacing=_spacing, wrapSpacing=_wrapSpacing, startPos=_startPos, wrapInterval=_wrapInterval;
62 @synthesize dragAsStacks=_dragAsStacks;
63 @synthesize bits=_bits;
68 return [_bits lastObject];
75 for( GGBLayer *layer in self.sublayers )
76 printf("%s @z=%g ", [[layer description] UTF8String],layer.zPosition);
81 - (void) x_repositionBit: (Bit*)bit forIndex: (int)i
83 bit.position = CGPointMake(_startPos.x + _spacing.width *(i%_wrapInterval) + _wrapSpacing.width *(i/_wrapInterval),
84 _startPos.y + _spacing.height*(i%_wrapInterval) + _wrapSpacing.height*(i/_wrapInterval));
87 - (void) addBit: (Bit*)bit
89 if( [bit isKindOfClass: [DraggedStack class]] ) {
90 for( Bit *subBit in [(DraggedStack*)bit bits] )
91 [self addBit: subBit];
94 [_bits addObject: bit];
95 ChangeSuperlayer(bit, self, n);
96 [self x_repositionBit: bit forIndex: n];
101 - (void) setHighlighted: (BOOL)highlighted
103 [super setHighlighted: highlighted];
104 self.borderWidth = (highlighted ?6 :0);
108 - (Bit*) canDragBit: (Bit*)bit
110 NSInteger index = [_bits indexOfObjectIdenticalTo: bit];
111 if( index==NSNotFound )
113 if( _dragAsStacks && index < _bits.count-1 ) {
114 // Move bit and those above it into a temporary DraggedStack:
115 NSRange r = NSMakeRange(index,_bits.count-index);
116 NSArray *bitsToDrag = [_bits subarrayWithRange: r];
117 [_bits removeObjectsInRange: r];
118 DraggedStack *stack = [[DraggedStack alloc] initWithBits: bitsToDrag];
119 [self addSublayer: stack];
121 stack.anchorPoint = CGPointMake( bit.position.x/stack.bounds.size.width,
122 bit.position.y/stack.bounds.size.height );
123 stack.position = bit.position;
127 [_bits removeObjectIdenticalTo: bit];
128 return [bit autorelease];
133 - (void) cancelDragBit: (Bit*)bit
136 if( [bit isKindOfClass: [DraggedStack class]] ) {
137 [bit removeFromSuperlayer];
142 - (void) draggedBit: (Bit*)bit to: (id<BitHolder>)dst
145 for( Bit *bit in self.sublayers )
146 [self x_repositionBit: bit forIndex: i++];
150 - (BOOL) dropBit: (Bit*)bit atPoint: (CGPoint)point
161 @implementation DraggedStack
164 - (id) initWithBits: (NSArray*)bits
168 CGRect bounds = CGRectZero;
169 for( Bit *bit in bits ) {
170 bounds = CGRectUnion(bounds, bit.frame);
171 [self addSublayer: bit];
173 self.bounds = bounds;
174 self.anchorPoint = CGPointZero;
175 self.position = CGPointZero;
182 return [self.sublayers.copy autorelease];