
#import "KenoCardView.h"
#import "Keno.h"
#import "KenoCard.h"

@implementation KenoCardView

- awakeFromNib
{
	cellNumberFont = [Font newFont:"Helvetica-Bold" size:24];
	
	[self setFlipped:YES];
	
	[self adjustMatrix];
	
	return self;
}

- adjustMatrix
{
	float width = NX_WIDTH(&bounds);
	float height = NX_HEIGHT(&bounds);
	float textWidth;
	int r, c, i=0;
	
	cellWidth = width/KENO_NUMCOLS;
	cellHeight = height/KENO_NUMROWS;

	for(r=0; r<KENO_NUMROWS; r++)
	{
		for(c=0; c<KENO_NUMCOLS; c++)
		{
			kcell[i].number = i+1;
			sprintf(kcell[i].text, "%d", i+1);
			kcell[i].x = c*cellWidth;
			kcell[i].y = r*cellHeight;
			
			NX_X(&kcell[i].cellRect) = kcell[i].x;
			NX_Y(&kcell[i].cellRect) = kcell[i].y;
			NX_WIDTH(&kcell[i].cellRect) = cellWidth;
			NX_HEIGHT(&kcell[i].cellRect) = cellHeight;
			
			textWidth = [cellNumberFont getWidthOf:kcell[i].text];
			kcell[i].textX = kcell[i].x+(cellWidth - textWidth)/2;
			kcell[i].textY = kcell[i].y + cellHeight - ((cellHeight - 24.0)/2.0) - 2.0;

			i++;
		}
	}
	
	
	return self;
}

- clearCells
{
	int i;
	
	for(i=0; i<KENO_NUMCELLS; i++)
	{
		kcell[i].isSelected = NO;
	}
	
	[self display];
	
	return self;
}

- (int)getCellForPoint:(NXPoint *)mousePoint
{
	int i;
	
	for(i=0; i<KENO_NUMCELLS; i++)
	{
		if(NXPointInRect(mousePoint, &kcell[i].cellRect))
		{
			return i;
		}
	}
	
	return KENO_NOCELL;
}

- drawSelf:(const NXRect *)rects :(int)rectCount
{
	int i;
	
    PSsetgray(NX_WHITE);
	PSsetlinewidth(2.0);
	NXRectFill(&bounds);
	[cellNumberFont set];
	
	PSsetgray(NX_BLACK);
	PSmoveto(NX_X(&bounds)+1.0, NX_Y(&bounds)+1.0);
	PSlineto(NX_X(&bounds)+1.0, NX_Y(&bounds)+NX_HEIGHT(&bounds)-1.0);
	PSlineto(NX_X(&bounds)+NX_WIDTH(&bounds)-1.0, NX_Y(&bounds)+NX_HEIGHT(&bounds)-1.0);
	PSlineto(NX_X(&bounds)+NX_WIDTH(&bounds)-1.0, NX_Y(&bounds)+1.0);
	PSclosepath();
	PSstroke();
	
	for(i=0; i<KENO_NUMROWS-1; i++)
	{
		PSmoveto(NX_X(&bounds), i*cellHeight+cellHeight);
		PSlineto(NX_WIDTH(&bounds), i*cellHeight+cellHeight);
		PSstroke();
	}
	
	for(i=0; i<KENO_NUMCOLS-1; i++)
	{
		PSmoveto(i*cellWidth+cellWidth, NX_Y(&bounds));
		PSlineto(i*cellWidth+cellWidth, NX_HEIGHT(&bounds));
		PSstroke();
	}

	for(i=0; i<KENO_NUMCELLS; i++)
	{
		PSsetgray(NX_BLACK);
		//PSmoveto(kcell[i].x, kcell[i].y);
		//PSlineto(kcell[i].x, kcell[i].y+cellHeight);
		//PSlineto(kcell[i].x+cellWidth, kcell[i].y+cellHeight);
		//PSlineto(kcell[i].x+cellWidth, kcell[i].y);
		//PSstroke();
		if(kcell[i].isSelected)
			PSsetgray(NX_DKGRAY);
		else
			PSsetgray(NX_LTGRAY);
		PSmoveto(kcell[i].textX, kcell[i].textY);
		PSshow(kcell[i].text);
	}
	
	return self;
}

- (BOOL)acceptsFirstMouse
{
    return YES;
}

- mouseDown:(NXEvent *)theEvent
{
    NXPoint mousePoint = theEvent->location;
	int cellTouched;
	
	[self convertPoint:&mousePoint fromView:nil];
	cellTouched = [self getCellForPoint:&mousePoint];
	
	if(cellTouched != KENO_NOCELL)
	{
		kcell[cellTouched].isSelected = !kcell[cellTouched].isSelected;
	}
	
	[self display];
	NXPing();
	
	return self;
}

@end
