1.1 --- a/Source/BoardUIView.m Mon Mar 10 17:30:57 2008 -0700
1.2 +++ b/Source/BoardUIView.m Tue Mar 11 09:21:53 2008 -0700
1.3 @@ -14,6 +14,11 @@
1.4 #import "GGBUtils.h"
1.5
1.6
1.7 +@interface BoardUIView ()
1.8 +- (void) _findDropTarget: (CGPoint)pos;
1.9 +@end
1.10 +
1.11 +
1.12 @implementation BoardUIView
1.13
1.14
1.15 @@ -102,17 +107,40 @@
1.16 NSAssert(touches.count==1,@"No multitouch support yet");
1.17 UITouch *touch = touches.anyObject;
1.18
1.19 + BOOL placing = NO;
1.20 _dragStartPos = touch.locationInView;
1.21 _dragBit = (Bit*) [self hitTestPoint: _dragStartPos
1.22 forLayerMatching: layerIsBit
1.23 offset: &_dragOffset];
1.24 - if( _dragBit ) {
1.25 - _dragMoved = NO;
1.26 - _dropTarget = nil;
1.27 - _oldHolder = _dragBit.holder;
1.28 - // Ask holder's and game's permission before dragging:
1.29 - if( _oldHolder )
1.30 - _dragBit = [_oldHolder canDragBit: _dragBit];
1.31 +
1.32 + if( ! _dragBit ) {
1.33 + // If no bit was clicked, see if it's a BitHolder the game will let the user add a Bit to:
1.34 + id<BitHolder> holder = (id<BitHolder>) [self hitTestPoint: _dragStartPos
1.35 + forLayerMatching: layerIsBitHolder
1.36 + offset: NULL];
1.37 + if( holder ) {
1.38 + _dragBit = [_game bitToPlaceInHolder: holder];
1.39 + if( _dragBit ) {
1.40 + _dragOffset.x = _dragOffset.y = 0;
1.41 + if( _dragBit.superlayer==nil )
1.42 + _dragBit.position = _dragStartPos;
1.43 + placing = YES;
1.44 + }
1.45 + }
1.46 + }
1.47 +
1.48 + if( ! _dragBit ) {
1.49 + Beep();
1.50 + return;
1.51 + }
1.52 +
1.53 + // Clicked on a Bit:
1.54 + _dragMoved = NO;
1.55 + _dropTarget = nil;
1.56 + _oldHolder = _dragBit.holder;
1.57 + // Ask holder's and game's permission before dragging:
1.58 + if( _oldHolder ) {
1.59 + _dragBit = [_oldHolder canDragBit: _dragBit];
1.60 if( _dragBit && ! [_game canBit: _dragBit moveFrom: _oldHolder] ) {
1.61 [_oldHolder cancelDragBit: _dragBit];
1.62 _dragBit = nil;
1.63 @@ -122,14 +150,20 @@
1.64 Beep();
1.65 return;
1.66 }
1.67 - // Start dragging:
1.68 - _oldSuperlayer = _dragBit.superlayer;
1.69 - _oldLayerIndex = [_oldSuperlayer.sublayers indexOfObjectIdenticalTo: _dragBit];
1.70 - _oldPos = _dragBit.position;
1.71 - ChangeSuperlayer(_dragBit, self.layer, self.layer.sublayers.count);
1.72 - _dragBit.pickedUp = YES;
1.73 - } else
1.74 - Beep();
1.75 + }
1.76 + // Start dragging:
1.77 + _oldSuperlayer = _dragBit.superlayer;
1.78 + _oldLayerIndex = [_oldSuperlayer.sublayers indexOfObjectIdenticalTo: _dragBit];
1.79 + _oldPos = _dragBit.position;
1.80 + ChangeSuperlayer(_dragBit, self.layer, self.layer.sublayers.count);
1.81 + _dragBit.pickedUp = YES;
1.82 +
1.83 + if( placing ) {
1.84 + if( _oldSuperlayer )
1.85 + _dragBit.position = _dragStartPos; // animate Bit to new position
1.86 + _dragMoved = YES;
1.87 + [self _findDropTarget: _dragStartPos];
1.88 + }
1.89 }
1.90
1.91
1.92 @@ -158,27 +192,33 @@
1.93 [CATransaction commit];
1.94
1.95 // Find what it's over:
1.96 - id<BitHolder> target = (id<BitHolder>) [self hitTestPoint: pos
1.97 - forLayerMatching: layerIsBitHolder
1.98 - offset: NULL];
1.99 - if( target == _oldHolder )
1.100 - target = nil;
1.101 - if( target != _dropTarget ) {
1.102 - [_dropTarget willNotDropBit: _dragBit];
1.103 - _dropTarget.highlighted = NO;
1.104 - _dropTarget = nil;
1.105 - }
1.106 - if( target ) {
1.107 - CGPoint targetPos = [(CALayer*)target convertPoint: _dragBit.position
1.108 - fromLayer: _dragBit.superlayer];
1.109 - if( [target canDropBit: _dragBit atPoint: targetPos]
1.110 - && [_game canBit: _dragBit moveFrom: _oldHolder to: target] ) {
1.111 - _dropTarget = target;
1.112 - _dropTarget.highlighted = YES;
1.113 - }
1.114 + [self _findDropTarget: pos];
1.115 + }
1.116 +}
1.117 +
1.118 +
1.119 +- (void) _findDropTarget: (CGPoint)pos
1.120 +{
1.121 + id<BitHolder> target = (id<BitHolder>) [self hitTestPoint: pos
1.122 + forLayerMatching: layerIsBitHolder
1.123 + offset: NULL];
1.124 + if( target == _oldHolder )
1.125 + target = nil;
1.126 + if( target != _dropTarget ) {
1.127 + [_dropTarget willNotDropBit: _dragBit];
1.128 + _dropTarget.highlighted = NO;
1.129 + _dropTarget = nil;
1.130 + }
1.131 + if( target ) {
1.132 + CGPoint targetPos = [(CALayer*)target convertPoint: _dragBit.position
1.133 + fromLayer: _dragBit.superlayer];
1.134 + if( [target canDropBit: _dragBit atPoint: targetPos]
1.135 + && [_game canBit: _dragBit moveFrom: _oldHolder to: target] ) {
1.136 + _dropTarget = target;
1.137 + _dropTarget.highlighted = YES;
1.138 }
1.139 }
1.140 -}
1.141 +}
1.142
1.143
1.144 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
1.145 @@ -200,9 +240,13 @@
1.146 } else {
1.147 // Nope, cancel:
1.148 [_dropTarget willNotDropBit: _dragBit];
1.149 - ChangeSuperlayer(_dragBit, _oldSuperlayer, _oldLayerIndex);
1.150 - _dragBit.position = _oldPos;
1.151 - [_oldHolder cancelDragBit: _dragBit];
1.152 + if( _oldSuperlayer ) {
1.153 + ChangeSuperlayer(_dragBit, _oldSuperlayer, _oldLayerIndex);
1.154 + _dragBit.position = _oldPos;
1.155 + [_oldHolder cancelDragBit: _dragBit];
1.156 + } else {
1.157 + [_dragBit removeFromSuperlayer];
1.158 + }
1.159 }
1.160 } else {
1.161 // Just a click, without a drag: