From cb28131212a90986d6af66ede21b0896c17c7270 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 21 Jun 2010 22:45:47 -0400 Subject: [PATCH] rendere a grid --- src/main/objc/ZaWorldGrid.h | 4 ++ src/main/objc/ZaWorldGrid.m | 92 +++++++++++++++---------------------- 2 files changed, 42 insertions(+), 54 deletions(-) diff --git a/src/main/objc/ZaWorldGrid.h b/src/main/objc/ZaWorldGrid.h index 2b67413..48c7238 100644 --- a/src/main/objc/ZaWorldGrid.h +++ b/src/main/objc/ZaWorldGrid.h @@ -12,6 +12,10 @@ * This is a generic Grid view that allows for a grid layout of sprites. I * may eventually redo this to support animation so there would most likely be a * "next" view. + * + * A grid will contain the data for an entire floor. The model has its coordinate + * system in the cartetian coordinate system and it it do a tranform so that the bottom left is drawn. + * It will draw the entire area for now. */ @interface ZaWorldGrid : NSView { diff --git a/src/main/objc/ZaWorldGrid.m b/src/main/objc/ZaWorldGrid.m index cb58ad9..135c159 100644 --- a/src/main/objc/ZaWorldGrid.m +++ b/src/main/objc/ZaWorldGrid.m @@ -12,45 +12,10 @@ @implementation ZaWorldGrid @synthesize location, itemColor, backgroundColor, gridColor, gridVisible; -- (void)setItemPropertiesToDefault:sender - -{ - - [self setLocation:NSMakePoint(0.0,0.0)]; - +- (void)setItemPropertiesToDefault:sender { [self setItemColor:[NSColor redColor]]; - [self setBackgroundColor:[NSColor grayColor]]; - -} -- (NSRect)calculatedItemBounds - -{ - - NSRect calculatedRect; - - - - // calculate the bounds of the draggable item - - // relative to the location - - calculatedRect.origin=location; - - - - // the example assumes that the width and height - - // are fixed values - - calculatedRect.size.width=60.0; - - calculatedRect.size.height=200.0; - - - - return calculatedRect; - + [self setGridColor: [NSColor blackColor]]; } - (id)initWithFrame:(NSRect)frame { @@ -58,29 +23,48 @@ - (id)initWithFrame:(NSRect)frame { if (self) { // Initialization code here. [self setItemPropertiesToDefault:self]; + + // Set the size of the object to a large size. + NSSize mapSize; + mapSize.width = 1000; + mapSize.height = 1000; + [self setFrameSize: mapSize]; } return self; } -- (void)drawRect:(NSRect)dirtyRect { +- (void)drawRect:(NSRect)rect { // Drawing code here. - // erase the background by drawing white - - [backgroundColor set]; - - [NSBezierPath fillRect:dirtyRect]; - - - - // set the current color for the draggable item - - [[self itemColor] set]; - - - - // draw the draggable item + int width = [self frame].size.width; + int height = [self frame].size.height ; + + int i = 0 ; + int GRIDSIZE= 10; + + // Set the color in the current graphics context for future draw operations + [gridColor setStroke]; + + + // Create our drawing path + + NSBezierPath* drawingPath = [NSBezierPath bezierPath]; + // [drawingPath setLineWidth:0.1]; + // Draw a grid + // first the vertical lines + for( i = 0 ; i <= width ; i=i+GRIDSIZE ) { + // see http://www.cocoabuilder.com/archive/cocoa/53752-how-to-draw-1-pixel-line.html + // for the reason why I had to add 0.5 + [drawingPath moveToPoint:NSMakePoint(i + 0.5, 0)]; + [drawingPath lineToPoint:NSMakePoint(i + 0.5, height)]; + } // then the horizontal lines + for( i = 0 ; i <= height ; i=i+GRIDSIZE ) { + [drawingPath moveToPoint:NSMakePoint(0, i + 0.5)]; + [drawingPath lineToPoint:NSMakePoint(width, i + 0.5)]; + } + // TODO transform to shift the map if it is smaller than the rect that was passed in. + // actually draw the grid + [drawingPath stroke]; - [NSBezierPath fillRect:[self calculatedItemBounds]]; } @end