/*
 *  The View that displays the next piece that will drop.
 *
 */
#import <appkit/Application.h>
#import <appkit/NXImage.h>
#import <appkit/Button.h>
#import "NextMatrix.h"
#import "Piece.h"

@implementation NextMatrix

- initFrame:(const NXRect *)frameRect
{
    NXSize size;
	 extern resize(NXSize *);

    [super initFrame:frameRect numRows:0 numCols:0];

    thePiece = [[Piece alloc] init];
    [thePiece newPiece];

    [thePiece getBlockSize:&size];
	 resize(&size);
    [super setElementSize:&size]; // Remember the image's size.
	 pieceVisible = showNext = NO;
    size.width = size.height = 1.0;
    [super setIntercell:&size];

#ifdef DEBUG
	 fprintf(stderr, "NextMatrix initialized\n");
#endif
    return self;
}


- drawSelf:(const NXRect *)rects :(int)rectCount
{
    [super drawSelf:rects :rectCount];
	 fprintf(stderr, "Thinking about showing you the next piece\n");
    if (pieceVisible && showNext) {
		 fprintf(stderr, "Here's the next piece.\n");
		 [thePiece draw:self];	  // Show the next piece
	 }
    return self;
}


- (struct pieceInfo *)pieceInfo
{
    struct pieceInfo *info;
#ifdef DEBUG
	 puts("NextMatrix instance creating a new piece");
#endif
    info = [thePiece pieceInfo];
    [thePiece newPiece];		  // Generate the new piece

    [self display];
    return info;
}

/*
 * TetApp will inform us when the game has started.
 */
- start:sender
{
	[thePiece newPiece];
	pieceVisible = YES;
	return self;
}

/*
 * TetApp will inform us when the game has been stopped.
 */
- end:sender
{
	pieceVisible = NO;
	[self display];
	return self;
}

- showNext:sender
{
	showNext = [showNextSwitch state];
	if (pieceVisible)
	  [self display];
	return self;
}


- free
{
	[thePiece free];
	return [super free];
}

@end
