Source/Stack.m
author Jens Alfke <jens@mooseyard.com>
Fri Mar 07 11:43:02 2008 -0800 (2008-03-07)
changeset 0 e9f7ba4718e1
child 1 3eb7be1dd7b6
permissions -rw-r--r--
Initial check-in into Mercurial. Branched from 1.0 release of Apple's sample code. No longer requires garbage collection. Fixed some memory leaks of CG objects. Fixed a bug when advancing to the 8th row in the Checkers game.
     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.
     4 
     5     Redistribution and use in source and binary forms, with or without modification, are permitted
     6     provided that the following conditions are met:
     7 
     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.
    13 
    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.
    22 */
    23 #import "Stack.h"
    24 #import "QuartzUtils.h"
    25 
    26 
    27 @implementation Stack
    28 
    29 
    30 - (id) initWithStartPos: (CGPoint)startPos spacing: (CGSize)spacing
    31            wrapInterval: (int)wrapInterval wrapSpacing: (CGSize)wrapSpacing
    32 {
    33     self = [super init];
    34     if (self != nil) {
    35         _startPos = startPos;
    36         _spacing = spacing;
    37         _wrapInterval = wrapInterval;
    38         _wrapSpacing = wrapSpacing;
    39         self.cornerRadius = 8;
    40         self.backgroundColor = kAlmostInvisibleWhiteColor;
    41         self.borderColor = kHighlightColor;
    42         _bits = [[NSMutableArray alloc] init];
    43     }
    44     return self;
    45 }
    46 
    47 - (id) initWithStartPos: (CGPoint)startPos spacing: (CGSize)spacing;
    48 {
    49     return [self initWithStartPos: startPos spacing: spacing 
    50                      wrapInterval: INT_MAX wrapSpacing: CGSizeZero];
    51 }
    52 
    53 
    54 - (void) dealloc
    55 {
    56     [_bits release];
    57     [super dealloc];
    58 }
    59 
    60 
    61 @synthesize spacing=_spacing, wrapSpacing=_wrapSpacing, startPos=_startPos, wrapInterval=_wrapInterval;
    62 @synthesize dragAsStacks=_dragAsStacks;
    63 @synthesize bits=_bits;
    64 
    65 
    66 - (Bit*) topBit
    67 {
    68     return [_bits lastObject];
    69 }
    70 
    71 
    72 - (void) dump
    73 {
    74     printf("Stack = ");
    75     for( CALayer *layer in self.sublayers )
    76         printf("%s @z=%g   ", [[layer description] UTF8String],layer.zPosition);
    77     printf("\n");
    78 }
    79 
    80 
    81 - (void) x_repositionBit: (Bit*)bit forIndex: (int)i
    82 {
    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));
    85 }
    86 
    87 - (void) addBit: (Bit*)bit
    88 {
    89     if( [bit isKindOfClass: [DraggedStack class]] ) {
    90         for( Bit *subBit in [(DraggedStack*)bit bits] )
    91             [self addBit: subBit];
    92     } else {
    93         int n = _bits.count;
    94         [_bits addObject: bit];
    95         ChangeSuperlayer(bit, self, n);
    96         [self x_repositionBit: bit forIndex: n];
    97     }
    98 }
    99 
   100 
   101 - (void) setHighlighted: (BOOL)highlighted
   102 {
   103     [super setHighlighted: highlighted];
   104     self.borderWidth = (highlighted ?6 :0);
   105 }
   106 
   107 
   108 - (Bit*) canDragBit: (Bit*)bit
   109 {
   110     NSInteger index = [_bits indexOfObjectIdenticalTo: bit];
   111     if( index==NSNotFound )
   112         return nil;
   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];
   120         [stack release];
   121         stack.anchorPoint = CGPointMake( bit.position.x/stack.bounds.size.width,
   122                                          bit.position.y/stack.bounds.size.height );
   123         stack.position = bit.position;
   124         return stack;
   125     } else {
   126         [bit retain];
   127         [_bits removeObjectIdenticalTo: bit];
   128         return [bit autorelease];
   129     }
   130 }
   131 
   132 
   133 - (void) cancelDragBit: (Bit*)bit
   134 {
   135     [self addBit: bit];
   136     if( [bit isKindOfClass: [DraggedStack class]] ) {
   137         [bit removeFromSuperlayer];
   138     }
   139 }
   140 
   141 
   142 - (void) draggedBit: (Bit*)bit to: (id<BitHolder>)dst
   143 {
   144     int i=0;
   145     for( Bit *bit in self.sublayers )
   146         [self x_repositionBit: bit forIndex: i++];
   147 }
   148 
   149 
   150 - (BOOL) dropBit: (Bit*)bit atPoint: (CGPoint)point
   151 {
   152     [self addBit: bit];
   153     return YES;
   154 }
   155 
   156 @end
   157 
   158 
   159 
   160 
   161 @implementation DraggedStack
   162 
   163 
   164 - (id) initWithBits: (NSArray*)bits
   165 {
   166     self = [super init];
   167     if( self ) {
   168         CGRect bounds = CGRectZero;
   169         for( Bit *bit in bits ) {
   170             bounds = CGRectUnion(bounds, bit.frame);
   171             [self addSublayer: bit];
   172         }
   173         self.bounds = bounds;
   174         self.anchorPoint = CGPointZero;
   175         self.position = CGPointZero;
   176     }
   177     return self;
   178 }
   179 
   180 - (NSArray*) bits
   181 {
   182     return [self.sublayers.copy autorelease];
   183 }
   184 
   185 @end