-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Animation implementation using the Web Animations API (#427)
- Loading branch information
1 parent
a064956
commit ab5e564
Showing
45 changed files
with
1,920 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright 2020 Tokamak contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// Created by Carson Katri on 7/11/21. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol Animatable { | ||
associatedtype AnimatableData: VectorArithmetic | ||
var animatableData: Self.AnimatableData { get set } | ||
} | ||
|
||
public protocol _PrimitiveAnimatable {} | ||
|
||
public extension Animatable where Self: VectorArithmetic { | ||
var animatableData: Self { | ||
get { self } | ||
// swiftlint:disable:next unused_setter_value | ||
set {} | ||
} | ||
} | ||
|
||
public extension Animatable where Self.AnimatableData == EmptyAnimatableData { | ||
var animatableData: EmptyAnimatableData { | ||
@inlinable get { EmptyAnimatableData() } | ||
// swiftlint:disable:next unused_setter_value | ||
@inlinable set {} | ||
} | ||
} | ||
|
||
@frozen public struct EmptyAnimatableData: VectorArithmetic { | ||
@inlinable | ||
public init() {} | ||
@inlinable public static var zero: Self { .init() } | ||
@inlinable | ||
public static func += (lhs: inout Self, rhs: Self) {} | ||
@inlinable | ||
public static func -= (lhs: inout Self, rhs: Self) {} | ||
@inlinable | ||
public static func + (lhs: Self, rhs: Self) -> Self { | ||
.zero | ||
} | ||
|
||
@inlinable | ||
public static func - (lhs: Self, rhs: Self) -> Self { | ||
.zero | ||
} | ||
|
||
@inlinable | ||
public mutating func scale(by rhs: Double) {} | ||
@inlinable public var magnitudeSquared: Double { .zero } | ||
public static func == (a: Self, b: Self) -> Bool { true } | ||
} | ||
|
||
@frozen public struct AnimatablePair<First, Second>: VectorArithmetic | ||
where First: VectorArithmetic, Second: VectorArithmetic | ||
{ | ||
public var first: First | ||
public var second: Second | ||
@inlinable | ||
public init(_ first: First, _ second: Second) { | ||
self.first = first | ||
self.second = second | ||
} | ||
|
||
@inlinable | ||
internal subscript() -> (First, Second) { | ||
get { (first, second) } | ||
set { (first, second) = newValue } | ||
} | ||
|
||
@_transparent public static var zero: Self { | ||
@_transparent get { | ||
.init(First.zero, Second.zero) | ||
} | ||
} | ||
|
||
@_transparent | ||
public static func += (lhs: inout Self, rhs: Self) { | ||
lhs.first += rhs.first | ||
lhs.second += rhs.second | ||
} | ||
|
||
@_transparent | ||
public static func -= (lhs: inout Self, rhs: Self) { | ||
lhs.first -= rhs.first | ||
lhs.second -= rhs.second | ||
} | ||
|
||
@_transparent | ||
public static func + (lhs: Self, rhs: Self) -> Self { | ||
.init(lhs.first + rhs.first, lhs.second + rhs.second) | ||
} | ||
|
||
@_transparent | ||
public static func - (lhs: Self, rhs: Self) -> Self { | ||
.init(lhs.first - rhs.first, lhs.second - rhs.second) | ||
} | ||
|
||
@_transparent | ||
public mutating func scale(by rhs: Double) { | ||
first.scale(by: rhs) | ||
second.scale(by: rhs) | ||
} | ||
|
||
@_transparent public var magnitudeSquared: Double { | ||
@_transparent get { | ||
first.magnitudeSquared + second.magnitudeSquared | ||
} | ||
} | ||
|
||
public static func == (a: Self, b: Self) -> Bool { | ||
a.first == b.first | ||
&& a.second == b.second | ||
} | ||
} | ||
|
||
extension CGPoint: Animatable { | ||
public var animatableData: AnimatablePair<CGFloat, CGFloat> { | ||
@inlinable get { .init(x, y) } | ||
@inlinable set { (x, y) = newValue[] } | ||
} | ||
} | ||
|
||
extension CGSize: Animatable { | ||
public var animatableData: AnimatablePair<CGFloat, CGFloat> { | ||
@inlinable get { .init(width, height) } | ||
@inlinable set { (width, height) = newValue[] } | ||
} | ||
} | ||
|
||
extension CGRect: Animatable { | ||
public var animatableData: AnimatablePair<CGPoint.AnimatableData, CGSize.AnimatableData> { | ||
@inlinable get { | ||
.init(origin.animatableData, size.animatableData) | ||
} | ||
@inlinable set { | ||
(origin.animatableData, size.animatableData) = newValue[] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2020 Tokamak contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// Created by Carson Katri on 7/11/21. | ||
// | ||
|
||
public protocol AnimatableModifier: Animatable, ViewModifier {} |
Oops, something went wrong.