Skip to content

Instantly share code, notes, and snippets.

@eonist
Created January 19, 2017 12:50
Show Gist options
  • Save eonist/cfb34e45a1c893d5599090f79941de41 to your computer and use it in GitHub Desktop.
Save eonist/cfb34e45a1c893d5599090f79941de41 to your computer and use it in GitHub Desktop.
GraphicsLib + Playground + Framework
import Cocoa
import XCPlayground
import PlaygroundSupport
@testable import MyFrameWork
class TouchView: NSView {
var (path, currentPath) = (NSBezierPath(), NSBezierPath())
override var isFlipped:Bool {return true}/*Organizes your view from top to bottom*/
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)//<--maybe: MTLSystemCreateDefaultDevice()
self.wantsLayer = true/*if true then view is layer backed*/
layer = CALayer()/*needs to be layer-hosted so that we dont get clipping of children*/
layer?.backgroundColor = NSColor.black.cgColor
layer!.masksToBounds = false
/*Gradients*/
let gradient = LinearGradient(Gradients.deepPurple(),[],π/2)
let lineGradient = LinearGradient(Gradients.blue(0.5),[],π/2)
/*Styles*/
let fill:GradientFillStyle = GradientFillStyle(gradient)
let lineStyle = LineStyle(20,NSColorParser.nsColor(Colors.green()).alpha(0.5),CGLineCap.round)
let line = GradientLineStyle(lineGradient,lineStyle)
/*Rect*/
let rect = RectGraphic(40,40,200,200,fill,line)
_ = addSubView(rect.graphic)
rect.draw()
/*Ellipse*/
let ellipse = EllipseGraphic(300,40,200,200,fill.mix(Gradients.green()),line.mix(Gradients.lightGray(0.5)))
addSubview(ellipse.graphic)
ellipse.draw()
/*RoundRect*/
let roundRect = RoundRectGraphic(40,300,200,200,Fillet(40),fill.mix(Gradients.red()),line.mix(Gradients.blue(0.5)))
addSubview(roundRect.graphic)
roundRect.draw()
/*Line*/
let lineGraphic = LineGraphic(CGPoint(300,300),CGPoint(500,500),line.mix(Gradients.orange()))
addSubview(lineGraphic.graphic)
lineGraphic.draw()
}
required init(coder: NSCoder) {fatalError("init(coder:) has not been implemented") }
}
let touchView: TouchView = TouchView(frame:NSRect(x: 0, y: 0, width: 540, height: 540))
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = touchView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment