// // PolygonNode.swift // lottie-swift // // Created by Brandon Withrow on 1/21/19. // import Foundation import QuartzCore final class PolygonNodeProperties: NodePropertyMap { init(star: Star) { self.direction = star.direction self.position = NodeProperty(provider: KeyframeInterpolator(keyframes: star.position.keyframes)) self.outerRadius = NodeProperty(provider: KeyframeInterpolator(keyframes: star.outerRadius.keyframes)) self.outerRoundedness = NodeProperty(provider: KeyframeInterpolator(keyframes: star.outerRoundness.keyframes)) self.rotation = NodeProperty(provider: KeyframeInterpolator(keyframes: star.rotation.keyframes)) self.points = NodeProperty(provider: KeyframeInterpolator(keyframes: star.points.keyframes)) let keypathProperties: [String : AnyNodeProperty] = [ "Position" : position, "Outer Radius" : outerRadius, "Outer Roundedness" : outerRoundedness, "Rotation" : rotation, "Points" : points ] self.properties = Array(keypathProperties.values) } let properties: [AnyNodeProperty] let direction: PathDirection let position: NodeProperty let outerRadius: NodeProperty let outerRoundedness: NodeProperty let rotation: NodeProperty let points: NodeProperty } final class PolygonNode: AnimatorNode, PathNode { let properties: PolygonNodeProperties let pathOutput: PathOutputNode init(parentNode: AnimatorNode?, star: Star) { self.pathOutput = PathOutputNode(parent: parentNode?.outputNode) self.properties = PolygonNodeProperties(star: star) self.parentNode = parentNode } // MARK: Animator Node var propertyMap: NodePropertyMap { return properties } let parentNode: AnimatorNode? var hasLocalUpdates: Bool = false var hasUpstreamUpdates: Bool = false var lastUpdateFrame: CGFloat? = nil var isEnabled: Bool = true { didSet{ self.pathOutput.isEnabled = self.isEnabled } } /// Magic number needed for constructing path. static let PolygonConstant: CGFloat = 0.25 func rebuildOutputs(frame: CGFloat) { let outerRadius = properties.outerRadius.value.cgFloatValue let outerRoundedness = properties.outerRoundedness.value.cgFloatValue * 0.01 let numberOfPoints = properties.points.value.cgFloatValue let rotation = properties.rotation.value.cgFloatValue let position = properties.position.value.pointValue var currentAngle = (rotation - 90).toRadians() let anglePerPoint = ((2 * CGFloat.pi) / numberOfPoints) var point = CGPoint(x: (outerRadius * cos(currentAngle)), y: (outerRadius * sin(currentAngle))) var vertices = [CurveVertex(point: point + position, inTangentRelative: .zero, outTangentRelative: .zero)] var previousPoint = point currentAngle += anglePerPoint; for _ in 0..