1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-07-14 16:30:34 +03:00

Fix stroke+fill color animations

This commit is contained in:
Alisa Mylnikova 2020-08-04 17:55:48 +07:00
parent 7cb4f58764
commit 56645212af
16 changed files with 143 additions and 23 deletions

View File

@ -26,7 +26,9 @@ func addShapeAnimation(_ animation: BasicAnimation, _ context: AnimationContext,
let transactionsDisabled = CATransaction.disableActions()
CATransaction.setDisableActions(true)
let fromShape = shapeAnimation.getVFunc()(0.0)
guard let fromShape = SceneUtils.copyNode(shapeAnimation.getVFunc()(0.0)) as? Shape else {
return
}
let toShape = shapeAnimation.getVFunc()(animation.autoreverses ? 0.5 : 1.0)
let duration = animation.autoreverses ? animation.getDuration() / 2.0 : animation.getDuration()
@ -54,16 +56,28 @@ func addShapeAnimation(_ animation: BasicAnimation, _ context: AnimationContext,
animation.progress = animation.manualStop ? 0.0 : 1.0
if !animation.autoreverses && finished {
shape.form = toShape.form
shape.stroke = toShape.stroke
shape.fill = toShape.fill
if fromShape.form != toShape.form {
shape.form = toShape.form
}
if fromShape.stroke != toShape.stroke {
shape.stroke = toShape.stroke
}
if fromShape.fill != toShape.fill {
shape.fill = toShape.fill
}
}
if !finished {
animation.progress = 0.0
shape.form = fromShape.form
shape.stroke = fromShape.stroke
shape.fill = fromShape.fill
if fromShape.form != toShape.form {
shape.form = toShape.form
}
if fromShape.stroke != toShape.stroke {
shape.stroke = toShape.stroke
}
if fromShape.fill != toShape.fill {
shape.fill = toShape.fill
}
}
renderer.freeLayer()

View File

@ -4,7 +4,7 @@ open class Fill: Equatable {
}
func equals<T>(other: T) -> Bool where T: Fill {
fatalError("Equals can't be realised for Fill")
fatalError("Equals can't be implemented for Fill")
}
}

View File

@ -1,4 +1,4 @@
open class Stroke {
open class Stroke: Equatable {
public let fill: Fill
public let width: Double
@ -18,3 +18,13 @@ open class Stroke {
self.offset = offset
}
}
public func ==<T> (lhs: T, rhs: T) -> Bool where T: Stroke {
return lhs.fill == rhs.fill
&& lhs.width == rhs.width
&& lhs.cap == rhs.cap
&& lhs.join == rhs.join
&& lhs.miterLimit == rhs.miterLimit
&& lhs.dashes == rhs.dashes
&& lhs.offset == rhs.offset
}

View File

@ -45,4 +45,13 @@ open class Arc: Locus {
return PathBuilder(segment: PathSegment(type: .M, data: [x1, y1])).A(rx, ry, 0.0, largeArcFlag, sweepFlag, x2, y2).build()
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? Arc else {
return false
}
return ellipse == other.ellipse
&& shift == other.shift
&& extent == other.extent
}
}

View File

@ -25,4 +25,13 @@ open class Circle: Locus {
override open func toPath() -> Path {
return MoveTo(x: cx, y: cy).m(-r, 0).a(r, r, 0.0, true, false, r * 2.0, 0.0).a(r, r, 0.0, true, false, -(r * 2.0), 0.0).build()
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? Circle else {
return false
}
return cx == other.cx
&& cy == other.cy
&& r == other.r
}
}

View File

@ -23,4 +23,14 @@ open class Ellipse: Locus {
open func arc(shift: Double, extent: Double) -> Arc {
return Arc(ellipse: self, shift: shift, extent: extent)
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? Ellipse else {
return false
}
return cx == other.cx
&& cy == other.cy
&& rx == other.rx
&& ry == other.ry
}
}

View File

@ -26,4 +26,14 @@ open class Line: Locus {
override open func toPath() -> Path {
return MoveTo(x: x1, y: y1).lineTo(x: x2, y: y2).build()
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? Line else {
return false
}
return x1 == other.x1
&& y1 == other.y1
&& x2 == other.x2
&& y2 == other.y2
}
}

View File

@ -1,4 +1,4 @@
open class Locus {
open class Locus: Equatable {
public init() {
}
@ -38,4 +38,13 @@ open class Locus {
open func toPath() -> Path {
fatalError("Unsupported locus: \(self)")
}
func equals<T>(other: T) -> Bool where T: Locus {
fatalError("Equals can't be implemented for Locus")
}
}
public func ==<T> (lhs: T, rhs: T) -> Bool where T: Locus {
return type(of: lhs) == type(of: rhs)
&& lhs.equals(other: rhs)
}

View File

@ -19,4 +19,12 @@ open class Path: Locus {
override open func toPath() -> Path {
return self
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? Path else {
return false
}
return segments == other.segments
&& fillRule == other.fillRule
}
}

View File

@ -1,6 +1,6 @@
import Foundation
open class PathSegment {
open class PathSegment: Equatable {
public let type: PathSegmentType
public let data: [Double]
@ -18,4 +18,9 @@ open class PathSegment {
return false
}
}
public static func == (lhs: PathSegment, rhs: PathSegment) -> Bool {
return lhs.type == rhs.type
&& lhs.data == rhs.data
}
}

View File

@ -38,13 +38,17 @@ open class Point: Locus {
override open func toPath() -> Path {
return MoveTo(x: x, y: y).lineTo(x: x, y: y).build()
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? Point else {
return false
}
return x == other.y
&& y == other.y
}
}
extension Point: Equatable {
public static func == (lhs: Point, rhs: Point) -> Bool {
return lhs.x == rhs.x
&& lhs.y == rhs.y
}
extension Point {
public static func - (lhs: Point, rhs: Point) -> Size {
return Size(w: lhs.x - rhs.x, h: lhs.y - rhs.y)

View File

@ -60,4 +60,11 @@ open class Polygon: Locus {
}
return pb.close().build()
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? Polygon else {
return false
}
return points == other.points
}
}

View File

@ -60,4 +60,11 @@ open class Polyline: Locus {
}
return pb.build()
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? Polyline else {
return false
}
return points == other.points
}
}

View File

@ -73,13 +73,14 @@ open class Rect: Locus {
override open func toPath() -> Path {
return MoveTo(x: x, y: y).lineTo(x: x, y: y + h).lineTo(x: x + w, y: y + h).lineTo(x: x + w, y: y).close().build()
}
}
extension Rect {
public static func == (lhs: Rect, rhs: Rect) -> Bool {
return lhs.x == rhs.x
&& lhs.y == rhs.y
&& lhs.w == rhs.w
&& lhs.h == rhs.h
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? Rect else {
return false
}
return x == other.y
&& y == other.y
&& w == other.w
&& h == other.h
}
}

View File

@ -13,4 +13,13 @@ open class RoundRect: Locus {
override open func bounds() -> Rect {
return rect
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? RoundRect else {
return false
}
return rect == other.rect
&& rx == other.rx
&& ry == other.ry
}
}

View File

@ -18,4 +18,12 @@ open class TransformedLocus: Locus {
open override func bounds() -> Rect {
return locus.bounds().applying(transform)
}
override func equals<T>(other: T) -> Bool where T: Locus {
guard let other = other as? TransformedLocus else {
return false
}
return locus == other.locus
&& transform == other.transform
}
}