jens@0: /* This code is based on Apple's "GeekGameBoard" sample code, version 1.0. jens@0: http://developer.apple.com/samplecode/GeekGameBoard/ jens@0: Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved. jens@0: jens@0: Redistribution and use in source and binary forms, with or without modification, are permitted jens@0: provided that the following conditions are met: jens@0: jens@0: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@0: and the following disclaimer. jens@0: * Redistributions in binary form must reproduce the above copyright notice, this list of jens@0: conditions and the following disclaimer in the documentation and/or other materials provided jens@0: with the distribution. jens@0: jens@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@0: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@0: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@0: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@0: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@0: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@0: */ jens@0: #import "Stack.h" jens@0: #import "QuartzUtils.h" jens@0: jens@0: jens@0: @implementation Stack jens@0: jens@0: jens@0: - (id) initWithStartPos: (CGPoint)startPos spacing: (CGSize)spacing jens@0: wrapInterval: (int)wrapInterval wrapSpacing: (CGSize)wrapSpacing jens@0: { jens@0: self = [super init]; jens@0: if (self != nil) { jens@0: _startPos = startPos; jens@0: _spacing = spacing; jens@0: _wrapInterval = wrapInterval; jens@0: _wrapSpacing = wrapSpacing; jens@0: self.cornerRadius = 8; jens@0: self.backgroundColor = kAlmostInvisibleWhiteColor; jens@0: self.borderColor = kHighlightColor; jens@0: _bits = [[NSMutableArray alloc] init]; jens@0: } jens@0: return self; jens@0: } jens@0: jens@0: - (id) initWithStartPos: (CGPoint)startPos spacing: (CGSize)spacing; jens@0: { jens@0: return [self initWithStartPos: startPos spacing: spacing jens@0: wrapInterval: INT_MAX wrapSpacing: CGSizeZero]; jens@0: } jens@0: jens@0: jens@0: - (void) dealloc jens@0: { jens@0: [_bits release]; jens@0: [super dealloc]; jens@0: } jens@0: jens@0: jens@0: @synthesize spacing=_spacing, wrapSpacing=_wrapSpacing, startPos=_startPos, wrapInterval=_wrapInterval; jens@0: @synthesize dragAsStacks=_dragAsStacks; jens@0: @synthesize bits=_bits; jens@0: jens@0: jens@0: - (Bit*) topBit jens@0: { jens@0: return [_bits lastObject]; jens@0: } jens@0: jens@0: jens@0: - (void) dump jens@0: { jens@0: printf("Stack = "); jens@1: for( GGBLayer *layer in self.sublayers ) jens@0: printf("%s @z=%g ", [[layer description] UTF8String],layer.zPosition); jens@0: printf("\n"); jens@0: } jens@0: jens@0: jens@0: - (void) x_repositionBit: (Bit*)bit forIndex: (int)i jens@0: { jens@0: bit.position = CGPointMake(_startPos.x + _spacing.width *(i%_wrapInterval) + _wrapSpacing.width *(i/_wrapInterval), jens@0: _startPos.y + _spacing.height*(i%_wrapInterval) + _wrapSpacing.height*(i/_wrapInterval)); jens@0: } jens@0: jens@0: - (void) addBit: (Bit*)bit jens@0: { jens@0: if( [bit isKindOfClass: [DraggedStack class]] ) { jens@0: for( Bit *subBit in [(DraggedStack*)bit bits] ) jens@0: [self addBit: subBit]; jens@0: } else { jens@0: int n = _bits.count; jens@0: [_bits addObject: bit]; jens@0: ChangeSuperlayer(bit, self, n); jens@0: [self x_repositionBit: bit forIndex: n]; jens@0: } jens@0: } jens@0: jens@0: jens@0: - (void) setHighlighted: (BOOL)highlighted jens@0: { jens@0: [super setHighlighted: highlighted]; jens@0: self.borderWidth = (highlighted ?6 :0); jens@0: } jens@0: jens@0: jens@0: - (Bit*) canDragBit: (Bit*)bit jens@0: { jens@0: NSInteger index = [_bits indexOfObjectIdenticalTo: bit]; jens@0: if( index==NSNotFound ) jens@0: return nil; jens@0: if( _dragAsStacks && index < _bits.count-1 ) { jens@0: // Move bit and those above it into a temporary DraggedStack: jens@0: NSRange r = NSMakeRange(index,_bits.count-index); jens@0: NSArray *bitsToDrag = [_bits subarrayWithRange: r]; jens@0: [_bits removeObjectsInRange: r]; jens@0: DraggedStack *stack = [[DraggedStack alloc] initWithBits: bitsToDrag]; jens@0: [self addSublayer: stack]; jens@0: [stack release]; jens@0: stack.anchorPoint = CGPointMake( bit.position.x/stack.bounds.size.width, jens@0: bit.position.y/stack.bounds.size.height ); jens@0: stack.position = bit.position; jens@0: return stack; jens@0: } else { jens@0: [bit retain]; jens@0: [_bits removeObjectIdenticalTo: bit]; jens@0: return [bit autorelease]; jens@0: } jens@0: } jens@0: jens@0: jens@0: - (void) cancelDragBit: (Bit*)bit jens@0: { jens@0: [self addBit: bit]; jens@0: if( [bit isKindOfClass: [DraggedStack class]] ) { jens@0: [bit removeFromSuperlayer]; jens@0: } jens@0: } jens@0: jens@0: jens@0: - (void) draggedBit: (Bit*)bit to: (id)dst jens@0: { jens@0: int i=0; jens@0: for( Bit *bit in self.sublayers ) jens@0: [self x_repositionBit: bit forIndex: i++]; jens@0: } jens@0: jens@0: jens@0: - (BOOL) dropBit: (Bit*)bit atPoint: (CGPoint)point jens@0: { jens@0: [self addBit: bit]; jens@0: return YES; jens@0: } jens@0: jens@0: @end jens@0: jens@0: jens@0: jens@0: jens@0: @implementation DraggedStack jens@0: jens@0: jens@0: - (id) initWithBits: (NSArray*)bits jens@0: { jens@0: self = [super init]; jens@0: if( self ) { jens@0: CGRect bounds = CGRectZero; jens@0: for( Bit *bit in bits ) { jens@0: bounds = CGRectUnion(bounds, bit.frame); jens@0: [self addSublayer: bit]; jens@0: } jens@0: self.bounds = bounds; jens@0: self.anchorPoint = CGPointZero; jens@0: self.position = CGPointZero; jens@0: } jens@0: return self; jens@0: } jens@0: jens@0: - (NSArray*) bits jens@0: { jens@0: return [self.sublayers.copy autorelease]; jens@0: } jens@0: jens@0: @end