
/* Skeleton generated by Interface Builder */

#import "PileOfChips.h"
#import <appkit/graphics.h>
#import <appkit/Cell.h>
#import <appkit/NXImage.h>
#import <dpsclient/wraps.h>

@implementation PileOfChips

- setChips:(int)number
{
   totalValue=number;
   [myTextField setIntValue:totalValue];
   [self display]; 
   
   return self;
}

// * 
// * TakeChipValueFrom is written mainly so that pileOfChips can be a target
// * for sliders and the like.
// *

- takeChipValueFrom:sender
{
   totalValue=[sender intValue];
   [myTextField setIntValue:totalValue];
   
   [self display];
   
   return self;
}

- testTakeChipValueFrom:sender
{
   totalValue=[sender intValue];
   [myTextField setIntValue:totalValue];
   [otherPile setChips:(30-totalValue)];
   [self display];
   
   return self;
}


// redefined methods

- initFrame:(const NXRect *)frameRect
{
     [super initFrame:frameRect];
     
     // set up chipImageOne
     
     chipImageOne=[[NXImage alloc] init];
     [chipImageOne initFromSection:"one-1"];
     
     // find boundaries
     
     height=bounds.size.height;
     width=bounds.size.width;
     
     return self;
}

// *
// * drawSelf is where all the real work gets done.
// * 

- drawSelf:(const NXRect *)rects :(int)rectCount
{
    int chipsLeft;
    int numberOfRows;
    NXPoint currentPoint={0.0, 0.0};
		   
    // clear screen
    
    PSsetgray(NX_LTGRAY);
    NXRectFill(&bounds);
    
    PSsetgray(NX_WHITE);
    NXFrameRectWithWidth(&bounds, 2.0);
    
    // begin drawing chips
    
    // see if we have room
    
    if ((height < (CLEARANCE + STEPUP)) || (width <(STEPRIGHT+STARTRIGHT)))
        return self;
    
    // Otherwise, draw it.
     
    currentPoint.x=STARTRIGHT;
    currentPoint.y=STARTUP;
    numberOfRows=0;

    // draw all the chips
    
    for (chipsLeft=totalValue;chipsLeft>0;chipsLeft--)
       {
           // while there is still space left in this pile, draw a chip and move
	   
	   if ((height-currentPoint.y>CLEARANCE))
	   {
	       [chipImageOne composite:NX_SOVER toPoint:&currentPoint];
	       currentPoint.y+=STEPUP;
	   } else
	   {
	       // otherwise, reset y and move x
	       
	       // reset
	       
	       currentPoint.y=STARTUP;
	       currentPoint.x+=STEPRIGHT;
	       
	       // check to see if we still have room
	       
	       while ((width<(currentPoint.x+CLEARANCE)))
	       {
	           // if not, reset again
		   
		   numberOfRows++;
		   currentPoint.x=STARTRIGHT+((numberOfRows%2)*STEPRIGHT/2);
	       }
	       
	       // draw the thing and move up a notch
	       
	       [chipImageOne composite:NX_SOVER toPoint:&currentPoint];
	       currentPoint.y+=STEPUP;   
	   }
       } 
    
    
    return self;
}

@end
