Re: Hillegass Book (3rd Edition): Chapter 18 Challenge
Re: Hillegass Book (3rd Edition): Chapter 18 Challenge
- Subject: Re: Hillegass Book (3rd Edition): Chapter 18 Challenge
- From: Erik Buck <email@hidden>
- Date: Mon, 26 May 2008 21:10:24 -0400
Here you go. Written in Mail. I sometimes enjoy other people's home
work.
-(void)drawRect:(NSRect)rect {
NSLog(@"drawRect:");
NSRect bounds = [self bounds];
[[NSColor whiteColor] set];
[NSBezierPath fillRect:bounds];
//[currentPath stroke];
[NSBezierPath strokeRect:[self currentRect]];
for(NSBezierPath *oval in ovals) {
[[NSColor blueColor] set];
[oval stroke];
}
// Add these lines
if(!NSIsEmptyRect([self currentRect])) {
[[NSBezierPath bezierPathWithOvalInRect:[self currentRect]]
stroke];
}
}
-(void)addOval:(NSRect)rect {
NSBezierPath *thePath =currentPath;
[ovals addObject:thePath];
[self setNeedsDisplay:YES];
}
#pragma mark Events
-(void)mouseDown:(NSEvent*)event {
NSLog(@"mouseDown: %d", [event clickCount]);
NSPoint p = [event locationInWindow];
downPoint = [self convertPoint:p fromView:nil];
currentPoint = downPoint;
// next line is not needed
//currentPath = [[NSBezierPath alloc] init];
// Next line is not needed
//[self setNeedsDisplay:YES];
}
-(void)mouseDragged:(NSEvent*)event {
NSPoint p = [event locationInWindow];
NSLog(@"mouseDragged: %@", NSStringFromPoint(p));
currentPoint = [self convertPoint:p fromView:nil];
// next line is not needed
//currentPath = [NSBezierPath bezierPathWithOvalInRect:[self
currentRect]];
[self autoscroll:event];
[self setNeedsDisplay:YES];
}
-(void)mouseUp:(NSEvent*)event {
NSLog(@"mouseUp:");
NSPoint p = [event locationInWindow];
currentPoint = [self convertPoint:p fromView:nil];
[self addOval:[self currentRect]];
[self setNeedsDisplay:YES];
// Add line
currentPoint = downPoint;
}
-(NSRect)currentRect {
float minX = MIN(downPoint.x, currentPoint.x);
float maxX = MAX(downPoint.x, currentPoint.x);
float minY = MIN(downPoint.y, currentPoint.y);
float maxY = MAX(downPoint.y, currentPoint.y);
return NSMakeRect(minX, minY, maxX-minX, maxY-minY);
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden