1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-08-15 16:10:39 +03:00

Add compact API for geometry classes and Color

This commit is contained in:
Yuri Strot 2019-05-24 19:41:40 +07:00
parent 4a0027316f
commit 397a4570e0
10 changed files with 126 additions and 22 deletions

View File

@ -20,6 +20,10 @@ open class Color: Fill {
public static let purple: Color = Color( val: 0x800080 )
public static let clear: Color = Color.rgba(r: 0, g: 0, b: 0, a: 0)
public init(_ val: Int = 0) {
self.val = val
}
public init(val: Int = 0) {
self.val = val
}

View File

@ -5,6 +5,13 @@ open class Line: Locus {
public let x2: Double
public let y2: Double
public init(_ x1: Double, _ y1: Double, _ x2: Double, _ y2: Double) {
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
}
public init(x1: Double = 0, y1: Double = 0, x2: Double = 0, y2: Double = 0) {
self.x1 = x1
self.y1 = y1
@ -13,11 +20,7 @@ open class Line: Locus {
}
override open func bounds() -> Rect {
return Rect(
x: min(x1, x2),
y: min(y1, y2),
w: abs(x1 - x2),
h: abs(y1 - y2))
return Rect(x: min(x1, x2), y: min(y1, y2), w: abs(x1 - x2), h: abs(y1 - y2))
}
override open func toPath() -> Path {

View File

@ -15,10 +15,26 @@ open class Locus {
return Shape(form: self, fill: with)
}
open func fill(_ hex: Int) -> Shape {
return Shape(form: self, fill: Color(val: hex))
}
open func fill(_ fill: Fill) -> Shape {
return Shape(form: self, fill: fill)
}
open func stroke(fill: Fill = Color.black, width: Double = 1, cap: LineCap = .butt, join: LineJoin = .miter, dashes: [Double] = []) -> Shape {
return Shape(form: self, stroke: Stroke(fill: fill, width: width, cap: cap, join: join, dashes: dashes))
}
open func stroke(color: Color, width: Double = 1, cap: LineCap = .butt, join: LineJoin = .miter, dashes: [Double] = []) -> Shape {
return Shape(form: self, stroke: Stroke(fill: color, width: width, cap: cap, join: join, dashes: dashes))
}
open func stroke(color: Int, width: Double = 1, cap: LineCap = .butt, join: LineJoin = .miter, dashes: [Double] = []) -> Shape {
return Shape(form: self, stroke: Stroke(fill: Color(color), width: width, cap: cap, join: join, dashes: dashes))
}
open func toPath() -> Path {
fatalError("Unsupported locus: \(self)")
}

View File

@ -1,5 +1,9 @@
open class MoveTo: PathBuilder {
public init(_ x: Double, _ y: Double) {
super.init(segment: PathSegment(type: .M, data: [x, y]))
}
public init(x: Double, y: Double) {
super.init(segment: PathSegment(type: .M, data: [x, y]))
}

View File

@ -5,7 +5,12 @@ open class Point: Locus {
public let x: Double
public let y: Double
public static let origin: Point = Point( x: 0, y: 0 )
public static let origin = Point(0, 0)
public init(_ x: Double, _ y: Double) {
self.x = x
self.y = y
}
public init(x: Double = 0, y: Double = 0) {
self.x = x

View File

@ -4,6 +4,10 @@ open class Polygon: Locus {
public let points: [Double]
public init(_ points: [Double]) {
self.points = points
}
public init(points: [Double] = []) {
self.points = points
}

View File

@ -4,6 +4,10 @@ open class Polyline: Locus {
public let points: [Double]
public init(_ points: [Double]) {
self.points = points
}
public init(points: [Double] = []) {
self.points = points
}

View File

@ -5,6 +5,13 @@ open class Rect: Locus {
public let w: Double
public let h: Double
public init(_ x: Double, _ y: Double, _ w: Double, _ h: Double) {
self.x = x
self.y = y
self.w = w
self.h = h
}
public init(x: Double = 0, y: Double = 0, w: Double = 0, h: Double = 0) {
self.x = x
self.y = y

View File

@ -5,7 +5,12 @@ open class Size {
public let w: Double
public let h: Double
public static let zero: Size = Size(w: 0, h: 0)
public static let zero = Size(0, 0)
public init(_ w: Double, _ h: Double) {
self.w = w
self.h = h
}
public init(w: Double = 0, h: Double = 0) {
self.w = w

View File

@ -9,7 +9,16 @@ public final class Transform {
public let dx: Double
public let dy: Double
public static let identity: Transform = Transform()
public static let identity = Transform()
public init(_ m11: Double, _ m12: Double, _ m21: Double, _ m22: Double, _ dx: Double, _ dy: Double) {
self.m11 = m11
self.m12 = m12
self.m21 = m21
self.m22 = m22
self.dx = dx
self.dy = dy
}
public init(m11: Double = 1, m12: Double = 0, m21: Double = 0, m22: Double = 1, dx: Double = 0, dy: Double = 0) {
self.m11 = m11
@ -20,39 +29,74 @@ public final class Transform {
self.dy = dy
}
public func move(dx: Double, dy: Double) -> Transform {
return Transform(m11: m11, m12: m12, m21: m21, m22: m22,
dx: dx * m11 + dy * m21 + self.dx, dy: dx * m12 + dy * m22 + self.dy)
public func move(_ dx: Double, _ dy: Double) -> Transform {
return move(dx: dx, dy: dy)
}
public func scale(sx: Double, sy: Double) -> Transform {
public func move(dx: Double = 0, dy: Double = 0) -> Transform {
return Transform(m11: m11, m12: m12, m21: m21, m22: m22,
dx: dx * m11 + dy * m21 + self.dx,
dy: dx * m12 + dy * m22 + self.dy)
}
public func scale(sx: Double = 0, sy: Double = 0) -> Transform {
return Transform(m11: m11 * sx, m12: m12 * sx, m21: m21 * sy, m22: m22 * sy, dx: dx, dy: dy)
}
public func shear(shx: Double, shy: Double) -> Transform {
public func scale(_ sx: Double, _ sy: Double) -> Transform {
return scale(sx: sx, sy: sy)
}
public func shear(shx: Double = 0, shy: Double = 0) -> Transform {
return Transform(m11: m11 + m21 * shy, m12: m12 + m22 * shy,
m21: m11 * shx + m21, m22: m12 * shx + m22, dx: dx, dy: dy)
}
public func rotate(angle: Double) -> Transform {
let asin = sin(angle); let acos = cos(angle)
return Transform(m11: acos * m11 + asin * m21, m12: acos * m12 + asin * m22,
m21: -asin * m11 + acos * m21, m22: -asin * m12 + acos * m22, dx: dx, dy: dy)
public func shear(_ shx: Double, _ shy: Double) -> Transform {
return shear(shx: shx, shy: shy)
}
public func rotate(angle: Double, x: Double, y: Double) -> Transform {
public func rotate(angle: Double) -> Transform {
let asin = sin(angle)
let acos = cos(angle)
return Transform(m11: acos * m11 + asin * m21, m12: acos * m12 + asin * m22,
m21: -asin * m11 + acos * m21, m22: -asin * m12 + acos * m22,
dx: dx, dy: dy)
}
public func rotate(_ angle: Double) -> Transform {
return rotate(angle: angle)
}
public func rotate(angle: Double, x: Double = 0, y: Double = 0) -> Transform {
return move(dx: x, dy: y).rotate(angle: angle).move(dx: -x, dy: -y)
}
public class func move(dx: Double, dy: Double) -> Transform {
public func rotate(_ angle: Double, _ x: Double, _ y: Double) -> Transform {
return rotate(angle: angle, x: x, y: y)
}
public class func move(dx: Double = 0, dy: Double = 0) -> Transform {
return Transform(dx: dx, dy: dy)
}
public class func scale(sx: Double, sy: Double) -> Transform {
public class func move(_ dx: Double, _ dy: Double) -> Transform {
return Transform(dx: dx, dy: dy)
}
public class func scale(sx: Double = 0, sy: Double = 0) -> Transform {
return Transform(m11: sx, m22: sy)
}
public class func shear(shx: Double, shy: Double) -> Transform {
public class func scale(_ sx: Double, _ sy: Double) -> Transform {
return Transform(m11: sx, m22: sy)
}
public class func shear(shx: Double = 0, shy: Double = 0) -> Transform {
return Transform(m12: shy, m21: shx)
}
public class func shear(_ shx: Double, _ shy: Double) -> Transform {
return Transform(m12: shy, m21: shx)
}
@ -61,10 +105,18 @@ public final class Transform {
return Transform(m11: acos, m12: asin, m21: -asin, m22: acos)
}
public class func rotate(angle: Double, x: Double, y: Double) -> Transform {
public class func rotate(_ angle: Double) -> Transform {
return rotate(angle: angle)
}
public class func rotate(angle: Double, x: Double = 0, y: Double = 0) -> Transform {
return Transform.move(dx: x, dy: y).rotate(angle: angle).move(dx: -x, dy: -y)
}
public class func rotate(_ angle: Double, _ x: Double, _ y: Double) -> Transform {
return rotate(angle: angle, x: x, y: y)
}
public func concat(with: Transform) -> Transform {
let nm11 = with.m11 * m11 + with.m12 * m21
let nm21 = with.m21 * m11 + with.m22 * m21