0

I am having some trouble figuring out the correct way to determine the bounds of NZBezierPath for redrawing correctly.

As you can see from the picture below the thin border line seems to be falling outside the NSBezierPath.bounds rectangle.

The code for handling the object dragging is as follows:

func offsetItemLocationBy(item: DItem, x: CGFloat, y:CGFloat)
    {
        // tell the display to redraw the old rect
        let oldBounds = item.bounds  // NSBezierPath.bounds

        // since the offset can be generated by both mouse moves
        // and moveUp:, moveDown:, etc.. actions, we'll invert
        // the deltaY amount based on if the view is flipped or
        // not.
        let invertDeltaY: CGFloat = self.isFlipped ? -1.0: 1.0
        let y1=y*invertDeltaY

        item.offsetLocationBy(x: x, y: y1) // Creates a new NSBezierPath

        self.setNeedsDisplay(oldBounds)
        // invalidate the new rect location so that it'll
        // be redrawn
        self.setNeedsDisplay(item.bounds)

    }

Should I be using some other method to calculate the bounds of NSBezierPath?

enter image description here

Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76

1 Answers1

1

OK, I just figured it out - NSBezierPath.bounds does not include the LINE WIDTH.

So to calculate the correct bounds subtract lineWidth/2.0 from x and y and add lineWidth to width and height.

var bounds: NSRect {
        let b = path.bounds
        let sw = path.lineWidth/2.0
        let boundsRect = NSRect(x: b.minX-sw, y: b.minY-sw, width: b.width+2*sw, height: b.height+2*sw)
        return boundsRect
    }
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76