Skip to content

Commit

Permalink
Merge pull request #3 from markd2/md-different-drawing
Browse files Browse the repository at this point in the history
Tweaked drawing to remove NS helpers
  • Loading branch information
Mark Dalrymple committed Nov 25, 2014
2 parents 0df4ee1 + 851a64a commit 54e47b3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 49 deletions.
61 changes: 35 additions & 26 deletions GrafDemo/BNRSimpleView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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];
Expand Down
46 changes: 23 additions & 23 deletions GrafDemo/SimpleView.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Cocoa

class SimpleView: NSView {

// Should drawing be sloppy with graphic saves and restores?
var beSloppy : Bool = false {
willSet {
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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);
}
}
Expand All @@ -103,13 +103,13 @@ class SimpleView: NSView {
CGContextStrokeRect (self.currentContext, self.bounds);
}
}


// --------------------------------------------------

override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)

if beSloppy {
drawSloppily()
} else {
Expand Down

0 comments on commit 54e47b3

Please sign in to comment.