From 851a64ad8e6771e7185bdb2064bff679a7fa5582 Mon Sep 17 00:00:00 2001 From: Mark Dalrymple Date: Tue, 25 Nov 2014 18:13:27 -0500 Subject: [PATCH] Tweaked drawing to remove NS helpers --- GrafDemo/BNRSimpleView.m | 61 ++++++++++++++++++++++----------------- GrafDemo/SimpleView.swift | 46 ++++++++++++++--------------- 2 files changed, 58 insertions(+), 49 deletions(-) diff --git a/GrafDemo/BNRSimpleView.m b/GrafDemo/BNRSimpleView.m index f650d3b..f4f90a9 100644 --- a/GrafDemo/BNRSimpleView.m +++ b/GrafDemo/BNRSimpleView.m @@ -40,15 +40,16 @@ - (void) drawSloppyBackground { - (void) drawSloppyContents { - CGRect innerRect = CGRectInset (self.bounds, 20, 20); + CGContextRef context = self.currentContext; - CGContextSetLineWidth (self.currentContext, 6.0); + CGRect innerRect = CGRectInset (self.bounds, 20, 20); - [[NSColor greenColor] set]; - CGContextFillEllipseInRect (self.currentContext, innerRect); + CGContextSetRGBFillColor (context, 0.0, 1.0, 0.0, 1.0); // Green + CGContextFillEllipseInRect (context, innerRect); - [[NSColor blueColor] set]; - CGContextStrokeEllipseInRect (self.currentContext, innerRect); + CGContextSetRGBStrokeColor (context, 0.0, 0.0, 1.0, 1.0); // Blue + CGContextSetLineWidth (context, 6.0); + CGContextStrokeEllipseInRect (context, innerRect); } // drawSloppyContents @@ -59,55 +60,63 @@ - (void) drawSloppyBorder { - (void) drawSloppily { - // Set the background and border size attributes. - [[NSColor whiteColor] setFill]; - [[NSColor blackColor] setStroke]; - CGContextSetLineWidth (self.currentContext, 3.0); + CGContextRef context = self.currentContext; + + CGContextSetRGBStrokeColor (context, 0.0, 0.0, 0.0, 1.0); // Black + CGContextSetRGBFillColor (context, 1.0, 1.0, 1.0, 1.0); // White [self drawSloppyBackground]; [self drawSloppyContents]; [self drawSloppyBorder]; - + } // drawSloppily // -------------------------------------------------- - (void) drawNiceBackground { - CGContextSaveGState (self.currentContext); { - CGContextFillRect (self.currentContext, self.bounds); - } CGContextRestoreGState (self.currentContext); + CGContextRef context = self.currentContext; + + CGContextSaveGState (context); { + CGContextFillRect (context, self.bounds); + } CGContextRestoreGState (context); } // drawNiceBackground - (void) drawNiceContents { - CGContextSaveGState (self.currentContext); { + CGContextRef context = self.currentContext; + + CGContextSaveGState (context); { CGRect innerRect = CGRectInset (self.bounds, 20, 20); - CGContextSetLineWidth (self.currentContext, 6.0); + CGContextSetLineWidth (context, 6.0); - [[NSColor greenColor] set]; - CGContextFillEllipseInRect (self.currentContext, innerRect); + CGContextSetRGBFillColor (context, 0.0, 1.0, 0.0, 1.0); // Green + CGContextFillEllipseInRect (context, innerRect); - [[NSColor blueColor] set]; - CGContextStrokeEllipseInRect (self.currentContext, innerRect); + CGContextSetRGBStrokeColor (context, 0.0, 0.0, 1.0, 1.0); // Blue + CGContextStrokeEllipseInRect (context, innerRect); - } CGContextRestoreGState (self.currentContext); + } CGContextRestoreGState (context); } // drawNiceContents - (void) drawNiceBorder { - CGContextSaveGState (self.currentContext); { - CGContextStrokeRect (self.currentContext, self.bounds); - } CGContextRestoreGState (self.currentContext); + CGContextRef context = self.currentContext; + + CGContextSaveGState (context); { + CGContextStrokeRect (context, self.bounds); + } CGContextRestoreGState (context); } // drawNiceBorder - (void) drawNicely { + CGContextRef context = self.currentContext; + // Set the background and border size attributes. - [[NSColor whiteColor] setFill]; - [[NSColor blackColor] setStroke]; + CGContextSetRGBStrokeColor (context, 0.0, 0.0, 0.0, 1.0); // Black + CGContextSetRGBFillColor (context, 1.0, 1.0, 1.0, 1.0); // White CGContextSetLineWidth (self.currentContext, 3.0); [self drawNiceBackground]; diff --git a/GrafDemo/SimpleView.swift b/GrafDemo/SimpleView.swift index 28dc1a9..6e06396 100644 --- a/GrafDemo/SimpleView.swift +++ b/GrafDemo/SimpleView.swift @@ -1,7 +1,7 @@ import Cocoa class SimpleView: NSView { - + // Should drawing be sloppy with graphic saves and restores? var beSloppy : Bool = false { willSet { @@ -15,7 +15,7 @@ class SimpleView: NSView { // that's not available to folks running 10.9, so perform this // violence to get a context via a void*. // iOS can use UIGraphicsGetCurrentContext. - + let unsafeContextPointer = NSGraphicsContext.currentContext()?.graphicsPort if let contextPointer = unsafeContextPointer { @@ -33,12 +33,14 @@ class SimpleView: NSView { drawStuff() CGContextRestoreGState (currentContext) } - + // -------------------------------------------------- - + func drawSloppily () { - NSColor.whiteColor().setFill() - NSColor.blackColor().setStroke() + + CGContextSetRGBStrokeColor (currentContext, 0.0, 0.0, 0.0, 1.0); // Black + CGContextSetRGBFillColor (currentContext, 1.0, 1.0, 1.0, 1.0); // White + CGContextSetLineWidth (currentContext, 3.0); drawSloppyBackground() @@ -53,24 +55,23 @@ class SimpleView: NSView { func drawSloppyContents() { let innerRect = CGRectInset(bounds, 20.0, 20.0) - CGContextSetLineWidth (currentContext, 6.0); - - NSColor.greenColor().set() + CGContextSetRGBFillColor (currentContext, 0.0, 1.0, 0.0, 1.0); // Green CGContextFillEllipseInRect (currentContext, innerRect); - - NSColor.blueColor().set() + + CGContextSetRGBStrokeColor (currentContext, 0.0, 0.0, 1.0, 1.0); // Blue + CGContextSetLineWidth (currentContext, 6.0); CGContextStrokeEllipseInRect (currentContext, innerRect); } func drawSloppyBorder() { CGContextStrokeRect (currentContext, self.bounds); } - + // -------------------------------------------------- - + func drawNicely () { - NSColor.whiteColor().setFill() - NSColor.blackColor().setStroke() + CGContextSetRGBStrokeColor (currentContext, 0.0, 0.0, 0.0, 1.0); // Black + CGContextSetRGBFillColor (currentContext, 1.0, 1.0, 1.0, 1.0); // White CGContextSetLineWidth (currentContext, 3.0); drawNiceBackground() @@ -88,12 +89,11 @@ class SimpleView: NSView { saveGState { let innerRect = CGRectInset(self.bounds, 20.0, 20.0) - CGContextSetLineWidth (self.currentContext, 6.0); - - NSColor.greenColor().set() + CGContextSetRGBFillColor (self.currentContext, 0.0, 1.0, 0.0, 1.0); // Green CGContextFillEllipseInRect (self.currentContext, innerRect); - NSColor.blueColor().set() + CGContextSetRGBStrokeColor (self.currentContext, 0.0, 0.0, 1.0, 1.0); // Blue + CGContextSetLineWidth (self.currentContext, 6.0); CGContextStrokeEllipseInRect (self.currentContext, innerRect); } } @@ -103,13 +103,13 @@ class SimpleView: NSView { CGContextStrokeRect (self.currentContext, self.bounds); } } - - + + // -------------------------------------------------- - + override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) - + if beSloppy { drawSloppily() } else {