In a somehow similar situation (2 triangles in a rect), I handled it with the following design:
- subclass NSView (in your case for the triangle), may be with property to define its orientation
@IBDesignable class SplitSquareView: NSView {
@IBInspectable var orientationNOSE : Bool = true // set on init() or in IB
enum subPart : Int {
case all = 0
case top
case bottom
}
- define the drawRect to draw it
- override the mouse events, as
override func mouseMoved(with theEvent: NSEvent) { }
- set the cursor there from a preset of cursor shapes.
Here an example to illustrate:
override func mouseMoved(with theEvent: NSEvent) {
let point = convert(theEvent.locationInWindow, from: nil)
var s: CGFloat
if orientationNOSE { { // NorthWet to SouthEast separation
s = point.x + point.y
if s < self.frame.size.width {
drawPart = .bottom
triangleSOCursor!.set() // SW pointing triangle cursor
} else {
drawPart = .top
triangleNECursor!.set() // NE pointing triangle cursor
}
} else { // SouthWest to NorthEast separation
s = point.y - point.x
if s < 0 {
drawPart = .bottom
triangleSECursor!.set() // SE pointing triangle cursor
} else {
drawPart = .top
triangleNOCursor!.set() // NW pointing triangle cursor
}
}
setNeedsDisplay(self.bounds)
}
Did the same for
override func mouseEntered(with theEvent: NSEvent) { }
override func mouseExited(with theEvent: NSEvent) { }
override func mouseDown(with theEvent: NSEvent) {
override func cursorUpdate(with event: NSEvent) {
let point = convert(event.locationInWindow, from: nil)
var s: CGFloat
if orientationNOSE {
let s = point.x + point.y
if s < self.frame.size.width {
triangleSOCursor!.set()
} else {
triangleNECursor!.set()
}
} else { // SONE
s = point.y - point.x
if s < 0 {
triangleSECursor!.set()
} else {
triangleNOCursor!.set()
}
}
}
Doing so, you should not reach the limit of 255.
Tell me if that makes sense for you.