
/* Generated by Interface Builder */

#import "HandTileView.h"
#import	<assert.h>


@implementation HandTileView

+ newFrame:( const NXRect * )frameRect
{

	int		i;
	

	self = [ super newFrame:frameRect ];
	for( i = 0; i < ( sizeof( myHand ) / sizeof( CardView * )); ++i )
		myHand[ i ] = nil;
	
	return self;
}


- addCard:( CardView * )aCard :sender
{


	assert( !myHand[ 0 ] );
	myHand[ 0 ] = aCard;
	[ self addSubview:[ aCard setEventsEnabled:reactiveCardsFlag ]];
	
	return [ self _adjustCards ];
}


- removeCard:( CardView * )aCard :sender
{

	int		i;
	

	assert([ aCard isDescendantOf:self ]);
	[ aCard removeFromSuperview ];
	for( i = 0; i < ( sizeof( myHand ) / sizeof( CardView * )); ++i )
		if( myHand[ i ] == aCard )
			myHand[ i ] = nil;

	return [ self _adjustCards ];
}


- _adjustCards
{

	
	if( myHand[ 0 ] ) {
		int			i,
					placementAdjust = 0;
		BOOL		placeMoved = NO;
		NXRect		cardBounds;
		NXCoord		x_offset;
		
												// Try to move the card from the leftmost position
												//	to another position in the hand.
		for( i = 1; !placeMoved && ( i < ( sizeof( myHand ) / sizeof( CardView * ))); ++i )
			if( !myHand[ i ] ) {
				myHand[ i ] = myHand[ 0 ];
				myHand[ 0 ] = nil;
				placementAdjust = i;
				placeMoved = YES;
			}

												// Wherever the card was moved to then calculate and
												//	adjust its position.
		[ myHand[ placementAdjust ] getBounds:&cardBounds ];
		x_offset = ( NX_WIDTH( &bounds ) / HAND_SIZE ) - NX_WIDTH( &cardBounds );
		[ myHand[ placementAdjust ] moveTo:(( x_offset / 2 ) + (( x_offset + NX_WIDTH( &cardBounds )) * placementAdjust )) :(( NX_HEIGHT( &bounds ) - NX_HEIGHT( &cardBounds )) / 2 )];
	}
	
	return self;
}


- sendCard:( CardView * )aCard to:anObject
{

	assert([ aCard isDescendantOf:self ]);
	[ self removeCard:aCard		:self ];
	[ anObject addCard:aCard	:self ];
	
	return self;
}

	 
- sendAllCardsTo:anObject
{

	int		i;
	
	
	for( i = 0; i < ( sizeof( myHand ) / sizeof( CardView * )); ++i )
		if( myHand[ i ])
			[ self sendCard:myHand[ i ] to:anObject ];
		
	return self;
}



@end
