This is a program I'm writing (myself as opposed to copying someone else's and thus not learning) as part of the ObjectiveC and Cocoa learning curve. I want to draw simple shapes on a NSView (limiting it to ovals and rectangles for now). The idea is that I record each NSBezierPath to an NSMutableArray so I can also investigate/implement saving/loading, undo/redo. I have a canvas, can draw on it as well as 2 buttons that I use to select the tool. To handle the path I created another object that can hold a NSBezierPath, color values and size value for each object drawn. This is what I want to store in the array. I use mouseDown/Dragged/Up to get coordinates for the drawing path. However, this is where things go wonky. I can instantiate the object that is supposed to hold the path/color/etc. info but, when I try to change an instance variable, the app crashes with no useful message in the debugger. I'll try to keep my code snippets short but tell me if I need to include more. The code has also degenerated a little from me trying so many things to make it work.
Project: Cocoa document based app
I have the following .m/.h files
MyDocument:NSDocument
- generated by XCodeDrawnObject:NSObject
- deals with the drawn object i.e. path, color, type (oval/rect) and sizeCanvas:NSView
- well, shows the drawing, deals with the mouse and buttons
Canvas is also responsible for maintaining a NSMutableArray
of DrawnObject
objects.
DrawnObject.h
looks like this:
#import <Foundation/Foundation.h> //The drawn object must know what tool it was created with etc as this needs to be used for generating the drawing @interface DrawnObject : NSObject { NSBezierPath * aPath; NSNumber * toolType;//0 for oval, 1 for rectangular etc.... float toolSize; struct myCol{ float rd; float grn; float blu; float alp; } toolColor; } -(void)setAPath:(NSBezierPath *) path; -(NSBezierPath *)aPath; @property (readwrite,assign) NSNumber * toolType; -(float)toolSize; -(void)setToolSize:(float) size; -(struct myCol *)toolColor; -(void)setCurrentColor:(float)ref:(float)green:(float)blue:(float)alpha; @end
Canvas.h
looks like this
#import #import "drawnObject.h" @interface Canvas : NSView { NSMutableArray * myDrawing; NSPoint downPoint; NSPoint currentPoint; NSBezierPath * viewPath;//to show the path as the user drags the mouse NSNumber * currentToolType; BOOL mouseUpFlag;//trying a diff way to make it work BOOL mouseDrag; } -(IBAction)useOval:(id)sender; -(IBAction)useRect:(id)sender; -(IBAction)showTool:(id)sender; -(NSRect)currentRect; -(NSBezierPath *)createPath:(NSRect) aRect; -(void)setCurrentToolType:(NSNumber *) t; -(NSNumber *)currentToolType; @end
In the Canvas.m
file there are several functions to deal with the mouse and NSView/XCode also dropped in
and
-(id)initWithFrame:(NSRect)frame-(void)drawRect:(NSRect)rect
Originally I use mouseUp
to try to insert the new DrawnObject
into the array but that caused a crash. So, now I use two BOOL
flags to see when the mouse was released (clunky but I'm trying....)in drawRect
to insert into the array. I've included the method below and indicated where it causes the app to fail:
- (void)drawRect:(NSRect)rect { //This is called automatically // Drawing code here. //NSLog(@"Within drawRect tool type is %d", [self currentTool]); NSRect bounds = [self bounds]; NSRect aRect = [self currentRect]; viewPath = [self createPath:aRect]; //the createPath method uses the tool type to switch between oval and rect bezier curves if(mouseUpFlag==YES && mouseDrag==YES){ mouseDrag=NO; //Create a new drawnObject here DrawnObject * anObject = [[DrawnObject alloc]init];//- WORKS FINE UP TO HERE NSLog(@"CREATED NEW drawnObject"); [anObject setAPath:viewPath]; //- INSTANT APP DEATH!!!! NSLog(@"Set a path in drawnObject"); [anObject setToolType:[[NSNumber alloc]initWithInt:5]]; NSLog(@"Set toolType in DrawnObject"); [anObject setToolType:currentToolType]; [myDrawing addObject:anObject]; NSLog(@"Added Object"); } [[NSColor colorWithCalibratedRed:0.0 green:0.9 blue:0.0 alpha:0.5]set]; [NSBezierPath fillRect:bounds]; [[NSColor lightGrayColor]set]; [viewPath stroke]; //This is so the user can see where the drawing is being done //Now, draw the paths in the array [[NSColor blueColor]set]; for(DrawnObject * indexedObject in myDrawing){ [[indexedObject aPath] stroke];//This will do the actual drawing of ALL objects } }
I guess this has something to do with object scope or something but I just can not figure it out. As I said, as I've tried things the code has sort of undergone an metamorphosis, sadly not for the better. Like those BOOLS etc.
HELP! Any clever people out there, point me in the right direction please!
ADDED THIS ON:
-(NSBezierPath *)createPath:(NSRect) aRect
{
NSBezierPath * tempPath;
//I need to know what tool
switch(0){ //temporary - this would use the toolType as a selector
case 0:
tempPath = [NSBezierPath bezierPathWithOvalInRect:aRect];
break;
case 1:
tempPath = [NSBezierPath bezierPathWithRect:aRect];
break;
default:
tempPath = [NSBezierPath bezierPathWithOvalInRect:aRect];
break;
}
return tempPath;
}