I have a very simple app that draws rectangles, ovals or rounderrect. I use NSBezierPath to draw the figures. I draw a rectangle and swap between shapes by typing O for oval / not oval, R for roundedRect / not rounded
When drawing roundedRect, I get a strange behavior. If I draw northEast (from lower left to top right), drawing goes OK, I get a blue rounded rectangle.
But drawing in any other direction (such as SouthWest, from top right to bottom left or southEast or NorthWest) leads to no drawing at all.
If I change roundedRect by a rect in the Bezizer call, drawing works in all directions.
XCode Version 7.1.1 (7B1005), OSX 10.11.1
Here is the code (in drawRect)
// DrawingView.swift
import Cocoa
class DrawingView: NSView {
var startPoint: NSPoint
var endPoint: NSPoint
var isOval: Bool // draw an oval
var isRoundedRect : Bool // draw a roundedrect
override init(frame frameRect: NSRect) {
self.startPoint = NSPoint()
self.endPoint = NSPoint()
isOval = false
isRoundedRect = false
super.init(frame: frameRect)
}
required init?(coder: NSCoder) {
self.startPoint = NSPoint()
self.endPoint = NSPoint()
isOval = false
isRoundedRect = false
super.init(coder: coder)
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
NSColor.redColor().set()
NSRectFill(dirtyRect)
NSColor.blueColor().set()
if isOval {
NSBezierPath(ovalInRect: NSMakeRect(startPoint.x, startPoint.y, (endPoint.x - startPoint.x), (endPoint.y - startPoint.y))).fill()
} else {
if isRoundedRect {
NSBezierPath(roundedRect: NSMakeRect(startPoint.x, startPoint.y, (endPoint.x - startPoint.x), (endPoint.y - startPoint.y)), xRadius: 30.0, yRadius: 30.0).fill()
// If changed to NSBezierPath(rect: NSMakeRect(startPoint.x, startPoint.y, (endPoint.x - startPoint.x), (endPoint.y - startPoint.y))).fill() , drawing OK
} else {
NSBezierPath(rect: NSMakeRect(startPoint.x, startPoint.y, (endPoint.x - startPoint.x), (endPoint.y - startPoint.y))).fill()
}
}
}
override var acceptsFirstResponder: Bool {
return true // inutile, sauf si des TextFields peuvent intercepter ces keyDown events.
}
override func keyDown(theEvent: NSEvent) {
if ((theEvent.characters == "o") || (theEvent.characters == "O")) {
isOval = !isOval
self.needsDisplay = true
}
if ((theEvent.characters == "r") || (theEvent.characters == "R")) {
isRoundedRect = !isRoundedRect
isOval = false
self.needsDisplay = true
}
}
override func mouseDown(theEvent: NSEvent) {
let point = theEvent.locationInWindow
startPoint = self.convertPoint(point, fromView: nil) // nil: converts from window coordinates
}
override func mouseDragged(theEvent: NSEvent) {
let point = theEvent.locationInWindow
endPoint = self.convertPoint(point, fromView: nil) // nil: converts from window coordinates
self.needsDisplay = true
}
}