rendered paste body#import "XXOView.h"
#import "GridArray.h"
#define DEFAULTGRIDCOLOR [NSColor grayColor]
#define DEFAULTGRIDSIZE 3
@implementation XXOView
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
[self drawGridWithSize:DEFAULTGRIDSIZE inColor:DEFAULTGRIDCOLOR];
gameArray = [[GridArray alloc] initWithWidth:DEFAULTGRIDSIZE];
}
return self;
}
- (void)drawGridWithSize:(int)size inColor:(NSColor*)color
{
NSPoint p;
NSRect r = [self bounds];
float maxHeight = r.size.height, maxWidth = r.size.width;
gridSize = size;
gridLines = [NSBezierPath new];
[gridLines setLineWidth:0.8];
// Horizontal lines
for(int i=1; i<size; ++i) {
p = NSMakePoint(0,(maxHeight/gridSize)*i);
[gridLines moveToPoint:p];
p = NSMakePoint(maxWidth,(maxHeight/gridSize)*i);
[gridLines lineToPoint: p];
}
// Vertical lines
for(int i=1; i<size; ++i) {
p = NSMakePoint((maxWidth/gridSize)*i,0);
[gridLines moveToPoint:p];
p = NSMakePoint((maxWidth/gridSize)*i,maxHeight);
[gridLines lineToPoint: p];
}
// Box
[color set];
[NSBezierPath setDefaultLineWidth:3.0];
[NSBezierPath strokeRect:[self bounds]];
[gridLines closePath];
[gridLines stroke];
}
- (void)drawCurrentGameState
{
for(int x=0; x < gridSize; ++x) {
for(int y=0; y < gridSize; ++y) {
Field currentField = [gameArray getField:GridIndex(x,y)];
NSColor* c;
if ([gameArray isPartOfThree:GridIndex(x,y)]) {
c = [NSColor redColor];
} else {
c = [NSColor darkGrayColor];
}
switch(currentField) {
case one:
[self drawSign:@"X" centeredIn:[self getFieldBoundsForIndex:GridIndex(x,y)] withColor:c];
break;
case two:
[self drawSign:@"O" centeredIn:[self getFieldBoundsForIndex:GridIndex(x,y)] withColor:c];
break;
default:
break;
}
[c release];
}
}
}
- (void)drawSign:(NSString*)s centeredIn:(NSRect)r withColor:(NSColor*)c
{
// NSLog(@"Bounds in DrawSign: %@", NSStringFromRect(r));
NSPoint stringOrigin;
NSSize stringSize;
NSMutableDictionary *attributes = [NSMutableDictionary new];
[attributes setObject:[NSFont fontWithName:@"Helvetica" size:r.size.height]
forKey:NSFontAttributeName];
[attributes setObject:c
forKey:NSForegroundColorAttributeName];
stringSize = [s sizeWithAttributes:attributes];
stringOrigin.x = r.origin.x + (r.size.width - stringSize.width)/2.0;
stringOrigin.y = r.origin.y + (r.size.height - stringSize.height)/2.0;
[s drawAtPoint:stringOrigin withAttributes:attributes];
[attributes release];
[s release];
/*
NSImage *image = [[NSImage alloc] initWithContentsOfFile:@"/Users/max/Documents/workspace/MacXXO/x.tif"];
NSRect imageRect;
imageRect.origin = NSZeroPoint;
imageRect.size = [image size];
NSLog(@"imageRect: %@", NSStringFromRect(imageRect));
[image drawInRect:r
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:1.0];
[image release];
*/
}
- (void)drawRect:(NSRect)rect
{
[self drawGridWithSize:DEFAULTGRIDSIZE inColor:DEFAULTGRIDCOLOR];
[self drawCurrentGameState];
}
- (void)mouseDown:(NSEvent*)event
{
NSPoint p = [event locationInWindow];
p = [self convertPoint:p fromView:nil];
GridIndex index = [self getIndexForPoint:p];
blubb = ((blubb == one) ? two : one);
if ([gameArray getField:index] != one && [gameArray getField:index] != two) {
[gameArray setField:index to:blubb];
[self setNeedsDisplay:YES];
}
}
- (GridIndex)getIndexForPoint:(NSPoint)p
{
GridIndex i;
NSRect r = [self bounds];
i.x = ((p.x * gridSize) / r.size.width);
i.y = ((p.y * gridSize) / r.size.height);
NSLog(@"Clickpoint: x = %f y = %f", p.x, p.y);
return i;
}
- (NSRect)getFieldBoundsForIndex:(GridIndex)i
{
NSRect r = [self bounds];
float maxHeight = r.size.height, maxWidth = r.size.width;
float x = i.x, y = i.y;
x = (maxHeight/gridSize)*x;
y = (maxWidth/gridSize)*y;
NSLog(@"Bounds: %@ gridSize: %f", NSStringFromRect(r), (float)gridSize);
r = NSMakeRect(x,y,maxWidth/(float)gridSize,maxHeight/(float)gridSize);
return r;
}
- (void)resetGameArray
{
[gameArray clearGrid];
}
// Keep the square size of the window when resizing
- (NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize
{
NSRect senderRect = [sender frame];
NSSize newSize = frameSize;
newSize.width = (frameSize.height*(senderRect.size.width/senderRect.size.height));
return newSize;
}
// Make the view resizable
- (void)windowDidResize:(NSNotification*)aNotification
{
[self setNeedsDisplay:YES];
}
- (void)dealloc
{
[gridLines release];
[super dealloc];
}
@end