Stripped stroke on NSBezierPath - objective-c

I am trying to draw a simple diagram. I have squares that are connected by lines. The lines are drawn with NSBezierPath's. I am using a random color for the lines so I can follow them. My problem is that the lines change color.
The output -
Code - I removed the lines that draw the squares so it only draws the lines:
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
CGFloat height = 70.0f;
CGFloat yOffset = 20.0f;
NSRect rect = NSMakeRect(50.0f, 50.0f, 200.0f, height);
NSEnumerator *reverseEnumerator = [steps reverseObjectEnumerator];
NSMutableDictionary *rectsByStep = [NSMutableDictionary dictionaryWithCapacity:10];
for( WFGJobStep *step in reverseEnumerator )
{
[[NSColor redColor] setFill];
[rectsByStep setObject:[NSValue valueWithRect:rect] forKey:[NSNumber numberWithInteger:step.step]];
rect.origin.y += height + yOffset;
}
reverseEnumerator = [steps reverseObjectEnumerator];
for( WFGJobStep *step in reverseEnumerator )
{
NSRect stepRect = [[rectsByStep objectForKey:[NSNumber numberWithInteger:step.step]] rectValue];
NSPoint startPoint = NSMakePoint( stepRect.origin.x + stepRect.size.width, stepRect.origin.y);
// draw lines
for( NSNumber *stepNumber in step.nextSteps )
{
//
//
// Line drawing code here
//
//
NSBezierPath * path = [NSBezierPath bezierPath];
[path setLineWidth: 4];
NSRect targetStepRect = [[rectsByStep objectForKey:stepNumber] rectValue];
NSPoint endPoint = NSMakePoint( targetStepRect.origin.x + targetStepRect.size.width, targetStepRect.origin.y + targetStepRect.size.height);
[path moveToPoint:startPoint];
CGFloat controlX = ( startPoint.y - endPoint.y ) * .2 + stepRect.origin.x + stepRect.size.width + 20;
[path curveToPoint:endPoint controlPoint1:NSMakePoint(controlX, startPoint.y) controlPoint2:NSMakePoint(controlX, endPoint.y)];
NSRect square = NSMakeRect( endPoint.x, endPoint.y, 9, 9 );
[path appendBezierPathWithOvalInRect: square];
[[self randomColor] set];
[path stroke];
}
}
}
- (NSColor *)randomColor {
float c[4];
c[0] = (arc4random() / RAND_MAX) * 1;
c[1] = (arc4random() / RAND_MAX) * 1;
c[2] = (arc4random() / RAND_MAX) * 1;
c[3] = 1.0f;
return [NSColor colorWithDeviceRed:c[0] green:c[1] blue:c[2] alpha:c[3]];
}

My issue is that this is in a scoll view and this is my first NSScrollView. I was not aware that as the view scrolls it redraws its subviews. The issue was that I was using a random color for my stroke so that as I scrolled it would randomly change the colors of the lines.

Related

Make BG of macOS screensaver black?

I'm new to Obj-C so apologies if my code is messy.
I've made a screensaver of a DVD logo that changes colour on each bounce. My only problem is the background--it's dark grey instead of black. I'm hoping somebody could help me out.
Here is some of my code:
NSString * dvdPath = [[NSBundle bundleForClass:[self class]] pathForResource:#"dvdlogo" ofType:#"png"];
self.dvdLogo = [[NSImage alloc] initWithContentsOfFile:dvdPath];[self hitWall];}return self;}
- (void)startAnimation
{[super startAnimation];}
- (void)stopAnimation
{[super stopAnimation];}
- (void)drawRect:(NSRect)rectParam
{const float g = 15.0f/255.0f;
[[NSColor colorWithRed:g green:g blue:g alpha:1] setFill];
NSRectFill(rectParam);
NSRect rect;
rect.size = NSMakeSize(self.dvdWidth, self.dvdHeight);
self.x += self.xSpeed;
self.y += self.ySpeed;
rect.origin = CGPointMake(self.x, self.y);
self.dirtyRect = rect;
[self.dvdLogo drawInRect:rect];
CGPoint centre = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
if (centre.x + self.dvdWidth / 2 >= WIDTH || centre.x - self.dvdWidth / 2 <= 0) {
self.xSpeed *= -1;
[self hitWall];}
if (centre.y + self.dvdHeight / 2 >= HEIGHT || centre.y - self.dvdHeight / 2 <= 0) {
self.ySpeed *= -1;
[self hitWall];}}
- (void)hitWall
{NSArray * colours = #[[NSColor redColor],
[NSColor blueColor],
[NSColor yellowColor],
[NSColor cyanColor],
[NSColor orangeColor],
[NSColor magentaColor],
[NSColor greenColor]];
self.dvdColor = colours[arc4random() % [colours count]];
[self.dvdLogo lockFocus];
[self.dvdColor set];
NSRect imageRect = {NSZeroPoint, [self.dvdLogo size]};
NSRectFillUsingOperation(imageRect, NSCompositingOperationSourceAtop);
[self.dvdLogo unlockFocus];}
- (void)animateOneFrame
{//[self setNeedsDisplay:true];
[self setNeedsDisplayInRect:self.dirtyRect];
return;}
Here's a download of the saver, if you wanna see what I'm talking about.
If I follow your code fragment and problem correctly your drawRect starts with:
const float g = 15.0f/255.0f;
[[NSColor colorWithRed:g green:g blue:g alpha:1] setFill];
NSRectFill(rectParam);
This fills the area defined by rectParam with dark gray. Why did you choose 15.0f/255.0f for the RGB components? Black is just 0 but more easily obtained using blackColor:
[[NSColor blackColor] setFill];
HTH

appendBezierPath with style of second path

I have 2 bezierPath. On a first path I draw grid. I handle mouse event and draw new path on first path. I use appendBezierPath, that's all ok, but style of second path don't copy. Cocoa use first path style. How can I append new bezier path with new style.
As example, I draw black grid and I want draw red circle in second path.
UPD:
- (void)drawRect:(NSRect)dirtyRect {
NSRect bounds = [self bounds];
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
[backgroundColor set];
NSRectFill(bounds);
[lineColor set];
for(NSUInteger i = 30; i < NSMaxX(bounds); i += 60) {
[path moveToPoint: (NSPoint) {i, 0}];
[path lineToPoint: (NSPoint) {i, NSMaxY(bounds)}];
for(NSUInteger j = 30; j < NSMaxY(bounds); j += 60) {
[path moveToPoint: (NSPoint) {0, j}];
[path lineToPoint: (NSPoint) {NSMaxX(bounds), j}];
}
}
[NSGraphicsContext saveGraphicsState];
[path stroke];
[currentContext restoreGraphicsState];
}
- (void)setDeniedPoint:(NSPoint)point {
NSBezierPath *newPath = [NSBezierPath bezierPath];
[deniedPointColor set];
[newPath setLineWidth:3.0f];
[newPath moveToPoint:(NSPoint) {point.x-4, point.y-4}];
[newPath lineToPoint:(NSPoint) {point.x+4, point.y+4}];
[newPath moveToPoint:(NSPoint) {point.x-4, point.y+4}];
[newPath lineToPoint:(NSPoint) {point.x+4, point.y-4}];
[path appendBezierPath:newPath];
[self setNeedsDisplay:YES];
}
- (void)mouseDown:(NSEvent *)event {
NSPoint currentLocationCursor = [self convertPoint:[event locationInWindow] fromView:nil];
[self setDeniedPoint:currentLocationCursor];
}

Drawing leaves trails when resizing window

I am trying to draw a box in custom view. I have the points for the corners of the box and they are scaled. If I put a small rectangle at each vertex on the box and the resize the window it works perfectly. If I use [path stroke] to draw the lines between the vertices and then resize the window, I end up with a mess as the box is redrawn as the window gets resized, so instead of a single box in a resized window I get all the boxes that were redrawn. I have not found a way to clear the custom view of all the intermediary drawings. Any help would be appreciated. Also, I am really new at this.
#import <Cocoa/Cocoa.h>
#interface PointDisplay : NSView
{
NSBezierPath *pathForLine;
NSMutableArray *pointList;
float originalWidth;
float originalHeight;
}
#end
import "PointDisplay.h"
#implementation PointDisplay
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
int opts = (NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:opts
owner:self
userInfo:nil];
[self addTrackingArea:area];
pathForLine = [[NSBezierPath alloc] init];
pointList = [[NSMutableArray alloc] init];
NSRect originalRect = [self bounds];
originalWidth = originalRect.size.width;
originalHeight = originalRect.size.height;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
NSRect myRect = [self bounds];
float widthNow = myRect.size.width;
float heightNow = myRect.size.height;
[[NSColor greenColor] set];
[NSBezierPath fillRect:myRect];
int rectWidth = 10, rectHeight = 10;
float lineWidth = 3.0;
if (pointList.count != 0)
{
NSPoint myPoint = [pointList[0] locationInWindow];
myPoint = [self convertPoint:myPoint fromView:nil];
myPoint.x = (myPoint.x - rectWidth / 2) * widthNow / originalWidth;
myPoint.y = (myPoint.y - rectHeight / 2) * heightNow / originalHeight;
[pathForLine moveToPoint:myPoint];
NSRect anotherRect = NSMakeRect(myPoint.x, myPoint.y, rectWidth, rectHeight);
[[NSColor redColor] set];
[pathForLine setLineWidth:lineWidth + .5 * ((int)lineWidth % 2)];
//[[NSBezierPath bezierPathWithRect:anotherRect] stroke];
for (int i = 1; i < pointList.count; i++)
{
myPoint = [pointList[i] locationInWindow];
myPoint = [self convertPoint:myPoint fromView:nil];
myPoint.x = (myPoint.x - 5) * widthNow / originalWidth;
myPoint.y = (myPoint.y - 5) * heightNow / originalHeight;
anotherRect = NSMakeRect(myPoint.x, myPoint.y, 10, 10);
[pathForLine lineToPoint:myPoint];
[pathForLine stroke];
//[[NSBezierPath bezierPathWithRect:anotherRect] fill];
}
}
}
#pragma mark Mouse events
- (void)mouseUp:(NSEvent *)theEvent
{
[pointList addObject:theEvent];
[self setNeedsDisplay:YES];
}
- (void)mouseExited:(NSEvent *)theEvent
{
[pathForLine closePath];
[pathForLine stroke];
[self setNeedsDisplay:YES];
}
#end

drawWithFrame NSOutlineView Flickr

I have a custom cell class for NSOutlineView
In the cell class I have implemented the drawWithFrame.
The rect provided(cellFrame) I divide into 3 parts
(a) Image
(b) Text
(c) Darwing (ellipse/circle / rectangle)
The image is drawn using [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
The ellipse is drawn using [[NSBezierPath bezierPathWithRoundedRect:ellipseRect xRadius:10 yRadius:10] fill];
The text rect is given to the super class to draw the text
[super drawInteriorWithFrame:newFrame inView:controlView];
Now my problem is that when any cell of the outline view expands, all the drawing (ellipse etc) flickr and appear to be redrawn, even if the cell was not expanded.
Can anyone help me to resolve this..
Here is the code
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{
//cellFrame.size.height -=16;
Option *ol = [self representedObject];
uint64_t sz;
int fontSize=10;
NSString *sizeText;
MyFile *tmpf;
//NSImage *image = [ol getImage];
if (image != nil)
{
// the cell has an image: draw the normal item cell
NSSize imageSize;
NSRect imageFrame;
imageSize = [image size];
NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
imageFrame.origin.x += kImageOriginXOffset;
imageFrame.origin.y -= kImageOriginYOffset;
imageFrame.size = NSMakeSize(12,12);
if ([controlView isFlipped])
imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
else
imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
[image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
imageFrame.origin.y+=18;
imageFrame.size.width = cellFrame.size.width - 18;
imageFrame.origin.x+=18;
sz = [ol getsize];
/////////////////////////////////
NSRect newFrame = cellFrame;
newFrame.origin.x += kTextOriginXOffset;
newFrame.origin.y += kTextOriginYOffset;
newFrame.size.height -= kTextHeightAdjust;
newFrame.size.width -= 65;
if(sz)
{
//newFrame.origin.x += 65;
NSRect tmpframe = newFrame;
NSRect ellipseRect = NSMakeRect(tmpframe.origin.x+tmpframe.size.width+1,
tmpframe.origin.y+ kTextOriginYOffset,
60,16);
//////// ****ALLOC ********
tmpf = [[MyFile alloc] init];
[tmpf setfsize:sz];
sizeText = [tmpf getFormattedfize];
// [NSShadow setShadowWithOffset:NSMakeSize(0, -8 * 1) blurRadius:12 * 1
// color:[NSColor colorWithCalibratedWhite:0 alpha:0.75]];
[[NSColor colorWithCalibratedWhite:0.9 alpha:1.0] set];
[[NSBezierPath bezierPathWithRoundedRect:ellipseRect xRadius:10 yRadius:10] fill];
// [NSShadow clearShadow];
TextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSColor textColor],
NSForegroundColorAttributeName,
[NSFont systemFontOfSize:10],
NSFontAttributeName, nil];
[sizeText drawAtPoint:NSMakePoint(ellipseRect.origin.x+3, ellipseRect.origin.y+2)
withAttributes:TextAttributes];
//////// ****RELEASE *******
[tmpf release];
}
[super drawInteriorWithFrame:newFrame inView:controlView];
}
}

Draw a stroke of a path with gradient

I'm trying to draw a stroke of Bezier Curve with linear gradient going from red to green. What I do at the moment is:
NSBezierPath* path = [NSBezierPath bezierPath];
[path setLineWidth: 1];
NSPoint startPoint = { 10, 100 };
NSPoint endPoint = { 590, 500 };
int r1 = arc4random() % 1000;
int r2 = arc4random() % 1000;
NSPoint cp1 = { 700, -500 + r1 };
NSPoint cp2 = { -500 + r2, 700 };
[path moveToPoint: startPoint];
[path curveToPoint: endPoint
controlPoint1: cp1
controlPoint2: cp2];
if (curves.count == 50) {
[curves removeObjectAtIndex:0];
}
[curves addObject:path];
int i = 0;
for (NSBezierPath * p in curves) {
[[redColors objectAtIndex:i++] set];
[p stroke];
}
And this works great, but when I convert NSBezierPath path to CGPathRef myPath = [path quartzPath] and iterate over 'CGPathRef' instead of 'NSBezierPath':
CGPathRef myPath = [path quartzPath];
if (curves.count == size) {
[paths removeObjectAtIndex:0];
}
[paths addObject:myPath];
CGContextRef c = [[NSGraphicsContext currentContext]graphicsPort];
for (int i = 0; i < paths.count; i++) {
[self drawPath:c :[paths objectAtIndex:i]:i];
}
My performance drops from about 30 FPS to 5 FPS!
Here is my code for drawPath:
-(void) drawPath:(CGContextRef) c: (CGPathRef) myPath: (int) i {
CGContextSaveGState(c);
CGContextAddPath(c, myPath);
CGContextReplacePathWithStrokedPath(c);
CGContextClip(c);
// Draw a linear gradient from top to bottom
CGContextDrawLinearGradient(c, cfGradients[i], start, end, 0);
CGContextRestoreGState(c);
}
redColors and cfGradients are arrays storing elements with alpha from 0-1/0-255, so they don't need to be recreated at each iteration.
This performance is even much worse than in Java. Surely there must be a way to draw a stroke more efficiently without all this transitions from NSBezierPath to CGPathRef, etc.
Please help.