Challenge: NSBezierPath-base Hit Testing

Managed to get this one working.

  1. Added a variable for the die path:

var diePath: NSBezierPath?

  1. Altered the draw code to update the variable in func drawDieWithSize()

    self.diePath = NSBezierPath(roundedRect: dieFrame, xRadius: cornerRadius, yRadius: cornerRadius)
    self.diePath!.fill()

  2. Changed the hit test to use the path

    override func mouseDown(theEvent: NSEvent) {
    Swift.print(“mouseDown”)
    let pointInView = convertPoint(theEvent.locationInWindow, fromView: nil)
    pressed = (self.diePath?.containsPoint(pointInView))!
    }

One must also modify mouseUp to prevent changing the number of dots when clicking outside the dice bounds :

    override func mouseUp(with event: NSEvent) {
    print("mouseUp clic count: \(event.clickCount)")
    
    let pointInView = convert(event.locationInWindow, from: nil)
    if event.clickCount == 2 && (self.diePath?.contains(pointInView))! {
        randomize()
    }
    pressed = false
}