Switching line properties while using NSBezierPath - cocoa

I'm having some very basic problems changing line color/width when drawing different lines (intended for a chart) with NSBezierPath. The following code should make it clear what I'm trying to do:
#import "DrawTest.h"
#implementation DrawTest
- (id)initWithFrame:(NSRect)frameRect
{
NSLog(#"in 'initWithFrame'...");
if ((self = [super initWithFrame:frameRect]) != nil)
{
[self drawBaseline];
[self display]; // ...NO!
[self drawPlotline];
}
return self;
}
- (void)drawBaseline
{
NSRect windowRect;
int width, height;
windowRect = [self bounds];
width = round(windowRect.size.width);
height = round(windowRect.size.height);
theLineWidth=1.0;
[[NSColor grayColor] set];
// allocate an instance of 'NSBezierPath'...
path = [[NSBezierPath alloc]init];
// draw a HORIZONTAL line...
[path moveToPoint:NSMakePoint(0,height/2)];
[path lineToPoint:NSMakePoint(width,height/2)];
}
- (void)drawPlotline
{
theLineWidth=10.0;
[[NSColor redColor] set];
// draw a VERTICAL line...
[path moveToPoint:NSMakePoint(100,125)]; // x,y
[path lineToPoint:NSMakePoint(100,500)];
}
- (void)drawRect:(NSRect)rect
{
NSLog(#"in 'drawRect'...");
// draw the path line(s)
// [[NSColor redColor] set];
[path setLineWidth:theLineWidth];
[path stroke];
}
- (void)dealloc
{
[path release];
[super dealloc];
}
#end
The problem obviously lies in the fact that 'drawRect' only gets called ONCE (after both methods have run), with the result that all lines appear in the last color and line width set. I've tried calling [self display] etc. in the hope of forcing 'drawRect' to redraw the NSView contents between the two method calls, but to no avail.
Can anyone suggest a basic strategy to achieve what I'm trying to do here, please? Any help would be much appreciated.

Quick answer is: move [self drawBaseline] and [self drawPlotline] inside drawRect.
Also, you need to call [path stroke] once per color, before changing the color. So, the pseudo-code is something like
-(void)drawRect:(NSRect)rect
{
NSBezierPath*path1=...
construct the path for color red...
[[NSColor redColor] set];
[path1 stroke];
NSBezierPath*path2=...
construct the path for color blue...
[[NSColor blueColor] set];
[path2 stroke];
}
Remember:
[path moveToPoint:] etc. does not draw the path. It just constructs the path inside the object. Once it's constructed, you stroke the path using [path stroke].
Also, the color is not the property of the path constructed inside the NSBezierPath instance. So, [color set] is not recorded inside your NSBezierPath instance! It's the property of the graphic context. Conceptually, 1. you construct the path. 2. you set the color to the context. 3. you stroke the path.
In Cocoa, it's not that you tell the system when to draw, when to refresh, etc. Cocoa tells you when to draw, by calling your drawRect:. The graphic context on which you draw is not available outside of drawRect:, unless you create off-screen context yourself. So, don't bother drawing beforehand. Just draw everything inside drawRect .

Related

NSBezierPath appendBezierPathWithArcFromPoint Wont Draw

I am attempting to draw and arc using "appendBezierPathWithArcFromPoint" but when run, it doesn't draw the arc. The "lineToPont" works and other "NSBezierPath" commands work. Can anyone please help by telling me what I am doing wrong. I have done quite a bit of serching with no clear answer. I am using MAC OS 10.9.2 and XCode 5.1. Below is the snipet of code. Thank You.
#import "MyView.h"
#implementation MyView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
NSRect bounds = [self bounds];
NSBezierPath *thePath = [NSBezierPath bezierPath]; // all drawing instruction goes to thePath.
[NSBezierPath setDefaultLineWidth:2.0];
[[NSColor blackColor] set];
[thePath moveToPoint:NSMakePoint(0, 0)];
[thePath lineToPoint:NSMakePoint(100, 30)];
[thePath appendBezierPathWithArcFromPoint:NSMakePoint(100,30) toPoint:NSMakePoint(130,0) radius:30];
[thePath stroke];
}
#end
Looking at the docs, it seems like the problem is that your fromPoint is equal to the current point. The docs say:
The created arc is defined by a circle inscribed inside the angle specified by three points: the current point, the fromPoint parameter, and the toPoint parameter (in that order).
So I think that means that the fromPoint must be different from the current point.

Artifacts when using custom view inside NSScrollView

I have a scroll view that shows a number of custom views.
In that custom view I use my own class so I can use the drawRect method to draw a line at the bottom of the view.
#interface MyListItemView : NSView
Here is my implementation of drawRect
#implementation MyListItemView
-(void)drawRect:(NSRect)rect
{
[NSGraphicsContext saveGraphicsState];
NSBezierPath * path;
path = [NSBezierPath bezierPath];
[path moveToPoint: rect.origin];
[path lineToPoint: NSMakePoint(rect.size.width, 0)];
[path setLineWidth:1];
[[NSColor grayColor] set];
[path stroke];
[NSGraphicsContext restoreGraphicsState];
}
#end
This draws the line as expected, but when I scroll the container I get artifacts appearing, like shown on the right hand side of the image below:
I tried saving and restoring the graphics context, but this made no difference and it doesn't look like that code is required. What am I missing?
Inspiration hit me while on a walk.
I'm drawing a line on the very edge of my view. It looks like some of that is getting drawn just outside the view (possibly due to anti aliasing) and isn't getting cleaned up properly.
All I needed to do was move my line up by half a pixel, changing my line code to this:
[path moveToPoint: NSMakePoint(0, 0.5)];
[path lineToPoint: NSMakePoint(rect.size.width, 0.5)];

Prevent NSButtonCell image from drawing outside of its containing NSScrollView

I have asked (and answered) a very similar question before. That question was able to be solved because I knew the dirtyRect, and thus, knew where I should draw the image. Now, I am seeing the same behavior with an subclassed NSButtonCell:
In my subclass, I am simply overriding the drawImage:withFrame:inView method to add a shadow.
- (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView {
NSGraphicsContext *ctx = [NSGraphicsContext currentContext];
[ctx saveGraphicsState];
// Rounded Rect
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame cornerRadius:kDefaultCornerRadius];
NSBezierPath *shadowPath = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 0.5, 0.5) cornerRadius:kDefaultCornerRadius]; // prevents the baground showing through the image corner
// Shadow
NSColor *shadowColor = [UAColor colorWithCalibratedWhite:0 alpha:0.8];
[NSShadow setShadowWithColor:shadowColor
blurRadius:3.0
offset:NSMakeSize(0, -1)];
[[NSColor colorWithCalibratedWhite:0.635 alpha:1.0] set];
[shadowPath fill];
[NSShadow clearShadow];
// Background Gradient in case image is nil
NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[UAColor grayColor] endingColor:[UAColor lightGrayColor]];
[gradient drawInBezierPath:shadowPath angle:90.0];
[gradient release];
// Image
if (image) {
[path setClip];
[image drawInRect:frame fromRect:CGRectZero operation:NSCompositeSourceAtop fraction:1.0];
}
[ctx restoreGraphicsState];
}
It seems all pretty standard, but the image is still drawing outside of the containing scrollview bounds. How can I avoid this?
You shouldn't call -setClip on an NSBezierPath unless you really mean it. Generally you want ‑addClip, which adds your clipping path to the set of current clipping regions.
NSScrollView works by using its own clipping path and you're blowing that path away before drawing your image.
This tripped me up for a long time too.

Cocoa NSView: Making circles but they are getting cropped

the outer edges of my circle at each point of the compass are getting cropped (presumably by the rect frame). How do I get the circle to display within the frame? (This is getting created from a button click):
In my AppController.m
#import "AppController.h"
#import "MakeCircle.h"
#implementation AppController
- (IBAction)makeCircle:(id)sender {
MakeCircle* newCircle = [[MakeCircle alloc] initWithFrame:NSMakeRect(100.0, 100.0, 30.0, 30.0)];
[[[[NSApplication sharedApplication] mainWindow] contentView] addSubview:newCircle];
[newCircle release];
}
#end
In my MakeCircle.m
- (void)drawRect:(NSRect)rect {
[self setNeedsDisplay:YES];
[[NSColor blackColor] setStroke];
// Create our circle path
NSBezierPath* circlePath = [NSBezierPath bezierPath];
[circlePath appendBezierPathWithOvalInRect: rect];
//give the line some thickness
[circlePath setLineWidth:4];
// Outline and fill the path
[circlePath stroke];
}
Thanks.
I think you see only half of the edge, right? You can calculate the half of the thickness of the edge and subtract that from the rectangle:
#define STROKE_COLOR ([NSColor blackColor])
#define STROKE_WIDTH (4.0)
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *path;
NSRect rectangle;
/* Calculate rectangle */
rectangle = [self bounds];
rectangle.origin.x += STROKE_WIDTH / 2.0;
rectangle.origin.y += STROKE_WIDTH / 2.0;
rectangle.size.width -= STROKE_WIDTH / 2.0;
rectangle.size.height -= STROKE_WIDTH / 2.0;
path = [NSBezierPath path];
[path appendBezierPathWithOvalInRect:rectangle];
[path setLineWidth:STROKE_WIDTH];
[STROKE_COLOR setStroke];
[path stroke];
}
I have no Mac at the moment, so I can't test it, but I think it should solve your problem.
Als don't call [self setNeedsDisplay:YES]. The method is used when you want to redraw your whole NSView, and calling it from the drawing method is a little bit recursive. That's why I'm surprised your code actually draws something.
And I have another tip: [[NSApplication sharedApplication] mainWindow] is actually the same as [NSApp mainWindow]. NSApp is a global variable containing the main application.
Hope it helps,
ief2

NSView Not updating?

Im working on a drag n' drop view and found some handlers for drag and drop actions on the web. I want to make it so it turns blue when the user drags a file over the drag and drop area and gray again when they exit the drag and drop area. The issues is its not updating when you drag your mouse over it or exit it. Heres some of the code:
- (void)drawRect:(NSRect)rect
{
NSRect bounds = [self bounds];
[[NSColor grayColor] set];
[NSBezierPath fillRect:bounds];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSRect bounds = [self bounds];
[[NSColor blueColor] set];
[NSBezierPath fillRect:bounds];
return NSDragOperationCopy;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender {
NSRect bounds = [self bounds];
[[NSColor grayColor] set];
[NSBezierPath fillRect:bounds];
}
Thanks for any help.
Are you calling [yourView: setNeedsDisplay] anywhere?
This is how you let the drawing framework know it needs to message your UIView subclass with drawRect:, so you should do it whenever things have changed. In your case, this probably means when the mouse enters or exits the drop area.
Drawing only works when a context (like a canvas for painting) is set up for you to draw into. When the framework calls -drawRect: it has set up a drawing context for you, so drawing commands like -[NSColor set] and -[NSBezierPath fillRect:] work as you expect.
Outside of -drawRect: there is usually no drawing context set up. Using drawing commands outside of -drawRect: is like waving a paintbrush in the air; there's no canvas, so no painting happens.
In 99.99% of cases, all view drawing should be kept within -drawRect: because NSView does a lot of work that you don't want to do to get the drawing context set up correctly and efficiently.
So, how do you change your view's drawing within your -draggingEntered: and -draggingExited: methods? By side effects.
You're doing the same thing in all three cases: 1) Setting a color and 2) Drawing a rectangle. The only difference is the color changes in each method. So, why not control which color you use in -drawRect: with an ivar, like so:
- (void)draggingEntered:(id <NSDraggingInfo>)sender {
drawBlueColorIvar = YES;
// ...
}
Then in -drawRect: you do this:
- (void)drawRect:(NSRect)rect {
NSColor *color = drawBlueColorIvar ? [NSColor blueColor] : [NSColor grayColor];
[color set];
[NSBezierPath fillRect:rect];
}
(Notice I didn't use [self bounds]. It is more efficient to just draw into the "dirty" rect, when possible.)
Finally, you need some way to tell the framework that your view needs to redraw when drawBlueColorIvar changes. The framework won't draw anything unless it's told it needs to. As Chris Cooper said, you do this with [self setNeedsDisplay:YES]. This should go after any place you change drawBlueColorIvar.

Resources