Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Graphics lib ios

Eonist edited this page Mar 23, 2017 · 16 revisions

Notes one graphics lib for iOS:

  • the truth is that you need a decorator style system to avoid too many concrete classes

  • just clean up v1 when porting to iOS

  • your new code that can reach protocol from shallow super type is cool

  • Decorator isn't too bad for value types as you can always copy vars to a new instance with less decoration if an instance gets too much decoration

Wants:

  • addSubView(Graphic())//draw on init

  • init colors via: .blue,.red,

  • init gradients via: .redBlue

  • init gradients via: NSColor.blue.to(.red)//gradient

  • init RectGraphic via: CGRect(0,0,100,100).graphic.fill(.red).stroke(.blue,10)

  • init CircGraphic via: CGEllipse(0,0,100,100).graphic

  • init RoundRectGraphic via: CGRect(0,0,100,100).graphic.fillet(20)

  • init PolyLineGraphic via: [CGPoint(0,0),CGPoint(50,50)].graphic.stroke(.redOrange,4)

  • init PathGraphic via: Path([.mt,.lt],[0,0,40,40]).graphic.stroke(.blue,10)

  • init DropShadow via: CGRect(100,100).fill(.orange).dropShadow//Blue rect with default dropshadow applied

  • init svg: "~/Desktop/test.svg".svg.graphic.fill(.blue).stroke(.red,10).dropShadow //overrides SVG style properties etc.

//how do we edit the end result?

let graphic:IGraphic = CGRect(50,50).graphic.dropShadow() graphic.dropShadow(.grey)//change dropShadow color to grey

  • you should be able to use graphicslib with a custom calayer that you can set. this way you can avoid too many nsviews etc

NOTES:

  • you call .draw() at the end of graphic chains. draw() returns NSView that you can pass to addSubView

  • a special addSubView(graphic.draw()) -> IGraphic draw() returns graphic. but addSubView adds graphic.view 👌

  • The graphic instance is always a struct with protocol extensions, and draw() returns NSView, also .graphic returns NSView.

  • The NSView does not contain any Graphic data. As that would require multiple NSViews. TO decorate each-other and add each other to eachother. Less NSView's is better. Also Graphic can be a value type and can easily be removed etc.

Struct Graphic:IGraphic {}
protocol IGraphic{
	var view:NSView
	func draw()->IGraphic
}

//a protocol can have template method calls and call other protocols and even reach the class

//you call draw() 
	//Form: drawFill() + drawStroke() 
	//Appearance: beginFill() + applyLineStyle
  • Maybe move the Graphics method into protocol extensions that spans Appearance and Form. So that they can be used in svg and graphic lib. This should be fine as long as you call the right methods in the correct order. And make sure that you add the decorators in the correct order. DropShadow goes after other Appearance decorators etc.

  • How do you mix gradient and color on fill and line? I guess: Gradient extends Graphic and the forward if fill != gradient?

  • DropShadowGraphic is just concerned with doing the correct CGContext stuff to add inner and outer shadows.

struct DropShadowGraphic{
	var graphic:IGraphic
	var dropShadow:DropShadow
	func fill(ctx:CGContext){
		/*...*/
	}
}

DropShadowGraphic(Graphic(),DropShadow())/*<--Regular init*/
CGRect().graphic().dropShadow().draw()/*<--alt init*/
  • GradientGraphic
struct GradientGraphic{
	var graphic:IGraphic
	var fillStyle:IFillStyle
	var strokeStyle:IStrokeStyle
	func fill(ctx;CGContext){
		/*...*/
	}
	func stroke(ctx:CGContext){
		/*...*/
	}
}
GradientGraphic	(Graphic(),Gradient())/*<--Regular init*/
CGRect().graphic().fill(.redBlue).draw()/*<--alt init*/
  • RectGraphic,EllipseGraphic
struct RectGraphic{
	var graphic:IGraphic
	var cgRect:CGRect/*CGSize should also be supported!?!?*/
	func drawFill(ctx;CGContext){
		/*...*/
	}
	func drawStroke(ctx;CGContext){
		/*...*/
	}
}
RectGraphic(Graphic(),CGRect())
CGSize().graphic().draw()/*Alt init*/