Source/Stack.m
author snej@snej.local
Tue Mar 10 22:36:23 2009 -0700 (2009-03-10)
changeset 27 b0affce7beb1
parent 1 3eb7be1dd7b6
permissions -rw-r--r--
Fixed some problems reported by the CLANG static analyzer.
jens@0
     1
/*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
jens@0
     2
    http://developer.apple.com/samplecode/GeekGameBoard/
jens@0
     3
    Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
jens@0
     4
jens@0
     5
    Redistribution and use in source and binary forms, with or without modification, are permitted
jens@0
     6
    provided that the following conditions are met:
jens@0
     7
jens@0
     8
    * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@0
     9
      and the following disclaimer.
jens@0
    10
    * Redistributions in binary form must reproduce the above copyright notice, this list of
jens@0
    11
      conditions and the following disclaimer in the documentation and/or other materials provided
jens@0
    12
      with the distribution.
jens@0
    13
jens@0
    14
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@0
    15
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@0
    16
    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@0
    17
    BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@0
    18
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@0
    19
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@0
    20
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@0
    21
    THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@0
    22
*/
jens@0
    23
#import "Stack.h"
jens@0
    24
#import "QuartzUtils.h"
jens@0
    25
jens@0
    26
jens@0
    27
@implementation Stack
jens@0
    28
jens@0
    29
jens@0
    30
- (id) initWithStartPos: (CGPoint)startPos spacing: (CGSize)spacing
jens@0
    31
           wrapInterval: (int)wrapInterval wrapSpacing: (CGSize)wrapSpacing
jens@0
    32
{
jens@0
    33
    self = [super init];
jens@0
    34
    if (self != nil) {
jens@0
    35
        _startPos = startPos;
jens@0
    36
        _spacing = spacing;
jens@0
    37
        _wrapInterval = wrapInterval;
jens@0
    38
        _wrapSpacing = wrapSpacing;
jens@0
    39
        self.cornerRadius = 8;
jens@0
    40
        self.backgroundColor = kAlmostInvisibleWhiteColor;
jens@0
    41
        self.borderColor = kHighlightColor;
jens@0
    42
        _bits = [[NSMutableArray alloc] init];
jens@0
    43
    }
jens@0
    44
    return self;
jens@0
    45
}
jens@0
    46
jens@0
    47
- (id) initWithStartPos: (CGPoint)startPos spacing: (CGSize)spacing;
jens@0
    48
{
jens@0
    49
    return [self initWithStartPos: startPos spacing: spacing 
jens@0
    50
                     wrapInterval: INT_MAX wrapSpacing: CGSizeZero];
jens@0
    51
}
jens@0
    52
jens@0
    53
jens@0
    54
- (void) dealloc
jens@0
    55
{
jens@0
    56
    [_bits release];
jens@0
    57
    [super dealloc];
jens@0
    58
}
jens@0
    59
jens@0
    60
jens@0
    61
@synthesize spacing=_spacing, wrapSpacing=_wrapSpacing, startPos=_startPos, wrapInterval=_wrapInterval;
jens@0
    62
@synthesize dragAsStacks=_dragAsStacks;
jens@0
    63
@synthesize bits=_bits;
jens@0
    64
jens@0
    65
jens@21
    66
- (NSUInteger) numberOfBits
jens@21
    67
{
jens@21
    68
    return _bits.count;
jens@21
    69
}
jens@21
    70
jens@21
    71
- (void) setNumberOfBits: (NSUInteger)n
jens@21
    72
{
jens@21
    73
    NSAssert2(n<=_bits.count,@"Cannot increase numberOfBits (from %u to %u)", _bits.count,n);
jens@21
    74
    while( _bits.count > n )
jens@21
    75
        [self removeBit: self.topBit];
jens@21
    76
}
jens@21
    77
jens@0
    78
- (Bit*) topBit
jens@0
    79
{
jens@0
    80
    return [_bits lastObject];
jens@0
    81
}
jens@0
    82
jens@0
    83
jens@0
    84
- (void) dump
jens@0
    85
{
jens@0
    86
    printf("Stack = ");
jens@1
    87
    for( GGBLayer *layer in self.sublayers )
jens@0
    88
        printf("%s @z=%g   ", [[layer description] UTF8String],layer.zPosition);
jens@0
    89
    printf("\n");
jens@0
    90
}
jens@0
    91
jens@0
    92
jens@0
    93
- (void) x_repositionBit: (Bit*)bit forIndex: (int)i
jens@0
    94
{
jens@0
    95
    bit.position = CGPointMake(_startPos.x + _spacing.width *(i%_wrapInterval) + _wrapSpacing.width *(i/_wrapInterval),
jens@0
    96
                               _startPos.y + _spacing.height*(i%_wrapInterval) + _wrapSpacing.height*(i/_wrapInterval));
jens@0
    97
}
jens@0
    98
jens@0
    99
- (void) addBit: (Bit*)bit
jens@0
   100
{
jens@0
   101
    if( [bit isKindOfClass: [DraggedStack class]] ) {
jens@0
   102
        for( Bit *subBit in [(DraggedStack*)bit bits] )
jens@0
   103
            [self addBit: subBit];
jens@0
   104
    } else {
jens@0
   105
        int n = _bits.count;
jens@0
   106
        [_bits addObject: bit];
jens@0
   107
        ChangeSuperlayer(bit, self, n);
jens@0
   108
        [self x_repositionBit: bit forIndex: n];
jens@0
   109
    }
jens@0
   110
}
jens@0
   111
jens@0
   112
jens@21
   113
- (void) removeBit: (Bit*)bit
jens@21
   114
{
jens@21
   115
    NSUInteger index = [_bits indexOfObjectIdenticalTo: bit];
jens@21
   116
    if( index != NSNotFound ) {
jens@21
   117
        [bit removeFromSuperlayer];
jens@21
   118
        [_bits removeObjectAtIndex: index];
jens@21
   119
    }
jens@21
   120
}
jens@21
   121
jens@21
   122
jens@0
   123
- (void) setHighlighted: (BOOL)highlighted
jens@0
   124
{
jens@0
   125
    [super setHighlighted: highlighted];
jens@0
   126
    self.borderWidth = (highlighted ?6 :0);
jens@0
   127
}
jens@0
   128
jens@0
   129
jens@0
   130
- (Bit*) canDragBit: (Bit*)bit
jens@0
   131
{
jens@0
   132
    NSInteger index = [_bits indexOfObjectIdenticalTo: bit];
jens@0
   133
    if( index==NSNotFound )
jens@0
   134
        return nil;
jens@0
   135
    if( _dragAsStacks && index < _bits.count-1 ) {
jens@0
   136
        // Move bit and those above it into a temporary DraggedStack:
jens@0
   137
        NSRange r = NSMakeRange(index,_bits.count-index);
jens@0
   138
        NSArray *bitsToDrag = [_bits subarrayWithRange: r];
jens@0
   139
        [_bits removeObjectsInRange: r];
jens@0
   140
        DraggedStack *stack = [[DraggedStack alloc] initWithBits: bitsToDrag];
jens@0
   141
        [self addSublayer: stack];
jens@0
   142
        [stack release];
jens@0
   143
        stack.anchorPoint = CGPointMake( bit.position.x/stack.bounds.size.width,
jens@0
   144
                                         bit.position.y/stack.bounds.size.height );
jens@0
   145
        stack.position = bit.position;
jens@0
   146
        return stack;
jens@0
   147
    } else {
jens@0
   148
        [bit retain];
jens@0
   149
        [_bits removeObjectIdenticalTo: bit];
jens@0
   150
        return [bit autorelease];
jens@0
   151
    }
jens@0
   152
}
jens@0
   153
jens@0
   154
jens@0
   155
- (void) cancelDragBit: (Bit*)bit
jens@0
   156
{
jens@0
   157
    [self addBit: bit];
jens@0
   158
    if( [bit isKindOfClass: [DraggedStack class]] ) {
jens@0
   159
        [bit removeFromSuperlayer];
jens@0
   160
    }
jens@0
   161
}
jens@0
   162
jens@0
   163
jens@0
   164
- (void) draggedBit: (Bit*)bit to: (id<BitHolder>)dst
jens@0
   165
{
jens@0
   166
    int i=0;
jens@0
   167
    for( Bit *bit in self.sublayers )
jens@0
   168
        [self x_repositionBit: bit forIndex: i++];
jens@0
   169
}
jens@0
   170
jens@0
   171
jens@0
   172
- (BOOL) dropBit: (Bit*)bit atPoint: (CGPoint)point
jens@0
   173
{
jens@0
   174
    [self addBit: bit];
jens@0
   175
    return YES;
jens@0
   176
}
jens@0
   177
jens@0
   178
@end
jens@0
   179
jens@0
   180
jens@0
   181
jens@0
   182
jens@0
   183
@implementation DraggedStack
jens@0
   184
jens@0
   185
jens@0
   186
- (id) initWithBits: (NSArray*)bits
jens@0
   187
{
jens@0
   188
    self = [super init];
jens@0
   189
    if( self ) {
jens@0
   190
        CGRect bounds = CGRectZero;
jens@0
   191
        for( Bit *bit in bits ) {
jens@0
   192
            bounds = CGRectUnion(bounds, bit.frame);
jens@0
   193
            [self addSublayer: bit];
jens@0
   194
        }
jens@0
   195
        self.bounds = bounds;
jens@0
   196
        self.anchorPoint = CGPointZero;
jens@0
   197
        self.position = CGPointZero;
jens@0
   198
    }
jens@0
   199
    return self;
jens@0
   200
}
jens@0
   201
jens@0
   202
- (NSArray*) bits
jens@0
   203
{
jens@0
   204
    return [self.sublayers.copy autorelease];
jens@0
   205
}
jens@0
   206
jens@0
   207
@end