Skip to content

Commit

Permalink
Merge pull request #12 from amiantos/animation-improvements
Browse files Browse the repository at this point in the history
Animation Improvements
  • Loading branch information
amiantos authored Jan 29, 2022
2 parents 12e997c + a040f72 commit aa2432c
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 183 deletions.
8 changes: 8 additions & 0 deletions MultiClock.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
4413B0E127A5089100FBCCA7 /* Patterns.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4413B0E027A5089100FBCCA7 /* Patterns.swift */; };
4413B0E227A5089100FBCCA7 /* Patterns.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4413B0E027A5089100FBCCA7 /* Patterns.swift */; };
4413B0E327A5089100FBCCA7 /* Patterns.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4413B0E027A5089100FBCCA7 /* Patterns.swift */; };
442F730627753B8F006D0991 /* Animations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 442F730527753B8F006D0991 /* Animations.swift */; };
442F730827759C01006D0991 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 442F730727759C00006D0991 /* Logging.swift */; };
442F730A2775CF92006D0991 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 442F73092775CF92006D0991 /* README.md */; };
Expand Down Expand Up @@ -60,6 +63,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
4413B0E027A5089100FBCCA7 /* Patterns.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Patterns.swift; sourceTree = "<group>"; };
442F730527753B8F006D0991 /* Animations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Animations.swift; sourceTree = "<group>"; };
442F730727759C00006D0991 /* Logging.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = "<group>"; };
442F73092775CF92006D0991 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
Expand Down Expand Up @@ -181,6 +185,7 @@
442F730527753B8F006D0991 /* Animations.swift */,
44C17D572773C947005E171A /* ClockController.swift */,
44E5F5042772BFA4007249E2 /* ClockScene.swift */,
4413B0E027A5089100FBCCA7 /* Patterns.swift */,
);
path = Scene;
sourceTree = "<group>";
Expand Down Expand Up @@ -384,6 +389,7 @@
442F7322277681DF006D0991 /* ClockController.swift in Sources */,
442F7320277681DF006D0991 /* Extensions.swift in Sources */,
442F732A27768669006D0991 /* FileGrabber.swift in Sources */,
4413B0E227A5089100FBCCA7 /* Patterns.swift in Sources */,
442F7325277681DF006D0991 /* Textures.swift in Sources */,
442F7326277681DF006D0991 /* Logging.swift in Sources */,
44884C19278A3E5F006C8FBD /* Manager.swift in Sources */,
Expand All @@ -402,6 +408,7 @@
444CD5AF2776AD0E00F51D7A /* ClusterNode.swift in Sources */,
444CD5B32776AD0E00F51D7A /* ClockNode.swift in Sources */,
444CD5B62776AD0E00F51D7A /* FileGrabber.swift in Sources */,
4413B0E327A5089100FBCCA7 /* Patterns.swift in Sources */,
444CD5B52776AD0E00F51D7A /* Textures.swift in Sources */,
44884C1A278A3E5F006C8FBD /* Manager.swift in Sources */,
444CD59A2776A8D800F51D7A /* AppDelegate.swift in Sources */,
Expand All @@ -422,6 +429,7 @@
44E5F5072772BFA4007249E2 /* ViewController.swift in Sources */,
442F730627753B8F006D0991 /* Animations.swift in Sources */,
44C17D5B2773D45C005E171A /* Extensions.swift in Sources */,
4413B0E127A5089100FBCCA7 /* Patterns.swift in Sources */,
44E5F5182772D315007249E2 /* ClusterNode.swift in Sources */,
44884C18278A3E5F006C8FBD /* Manager.swift in Sources */,
44C17D582773C947005E171A /* ClockController.swift in Sources */,
Expand Down
181 changes: 51 additions & 130 deletions Shared/Scene/Animations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,134 +320,55 @@ class Animation {
animation.pattern = pattern
return animation
}

static func randomizedPattern() -> [Int: [(CGFloat, CGFloat)]] {
return [
0: [
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
],
1: [
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
],
2: [
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
],
3: [
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
(CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))), (CGFloat(Int.random(in: -359...0)), CGFloat(Int.random(in: -359...0))),
],
]
}

static func randomizedRightAnglePattern() -> [Int: [(CGFloat, CGFloat)]] {
let options: [CGFloat] = [0, -90, -180, -270]
return [
0: [
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
],
1: [
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
],
2: [
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
],
3: [
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
(options.randomElement()!, options.randomElement()!), (options.randomElement()!, options.randomElement()!),
],
]
}
}

let inwardPointPattern: [Int: [(CGFloat, CGFloat)]] = [
0: [
(-105, -105), (-115, -115),
(-90, -90), (-90, -90),
(-75, -75), (-65, -65),
],
1: [
(-125, -125), (-150, -150),
(-90, -90), (-90, -90),
(-55, -55), (-30, -30),
],
2: [
(-210, -210), (-235, -235),
(-270, -270), (-270, -270),
(-330, -330), (-305, -305),
],
3: [
(-245, -245), (-255, -255),
(-270, -270), (-270, -270),
(-295, -295), (-285, -285),
],
]

let horizontalLinesPattern: [Int: [(CGFloat, CGFloat)]] = [
0: [
(-90, -90), (-270, -90),
(-90, -90), (-270, -90),
(-90, -90), (-270, -90),
],
1: [
(-270, -90), (-270, -90),
(-270, -90), (-270, -90),
(-270, -90), (-270, -90),
],
2: [
(-270, -90), (-270, -90),
(-270, -90), (-270, -90),
(-270, -90), (-270, -90),
],
3: [
(-270, -90), (-270, -270),
(-270, -90), (-270, -270),
(-270, -90), (-270, -270),
],
]

let numberConfigs: [Int: [(CGFloat, CGFloat)]] = [
// Tuple format is (hour, minute)
0: [
(-90, -180),
(-270, -180),
(-180, 0),
(-180, 0),
(-90, 0),
(-270, 0),
],
1: [
(-225, -225),
(-180, -180),
(-225, -225),
(-180, 0),
(-225, -225),
(0, 0)
],
2: [
(-90, -90),
(-270, -180),
(-180, -90),
(-270, 0),
(0, -90),
(-270, -270)
],
3: [
(-90, -90),
(-270, -180),
(-90, -90),
(0, -270),
(-90, -90),
(-270, 0)
],
4: [
(-180, -180),
(-180, -180),
(0, -90),
(0, -180),
(-225, -225),
(0, 0)
],
5: [
(-180, -90),
(-270, -270),
(0, -90),
(-270, -180),
(-90, -90),
(-270, 0)
],
6: [
(-180, -90),
(-270, -270),
(0, -180),
(-270, -180),
(0, -90),
(0, -270)
],
7: [
(-90, -90),
(-270, -180),
(-225, -225),
(-180, 0),
(-225, -225),
(0, 0)
],
8: [
(-90, -180),
(-270, -180),
(-90, 0),
(-270, 0),
(0, -90),
(-270, 0)
],
9: [
(-90, -180),
(-270, -180),
(-90, 0),
(0, -180),
(-90, -90),
(-270, 0)
],
]
92 changes: 46 additions & 46 deletions Shared/Scene/ClockController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class ClockController {

private var timeSinceLastAnimation: Int = 30

private var allAnimations: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9].shuffled()

private var availableAnimations: [Int] = []

init(size: CGSize) {
for _ in 1...4 {
let cluster = ClusterNode(size: CGSize(width: size.width/4, height: (size.width/4/2)*3))
Expand All @@ -59,6 +63,11 @@ class ClockController {
startTimer()
}
}

public func queue(animations: [Animation]) {
animationQueue.append(contentsOf: animations)
startAnimationQueueIfNeeded()
}

// MARK: - Timer

Expand Down Expand Up @@ -96,9 +105,13 @@ class ClockController {
])
}
} else if timeSinceLastAnimation >= 20 && !isAnimating {
Log.debug("Displaying random animation...")
if availableAnimations.isEmpty {
availableAnimations = allAnimations
}

let number = Int.random(in: 1...6)
let number = availableAnimations.popLast()!
Log.debug("Displaying random animation \(number)...")

switch number {
case 1:
// this one is pretty cool imho
Expand All @@ -125,11 +138,14 @@ class ClockController {
Animation.display(pattern: inwardPointPattern),
Animation.wait(duration: 5),
Animation.spinBothHands(by: 360),
Animation.positionBothHands(minuteDegrees: 0, hourDegrees: 0),
Animation.display(pattern: halfDownHalfUp),
Animation.spinBothHands(by: 360),
Animation.positionBothHands(minuteDegrees: -180, hourDegrees: 0),
Animation.spinBothHands(by: 180),
Animation.currentTimePrint(),
])
case 5:
// horizontal lines pattern
queue(animations: [
Animation.display(pattern: horizontalLinesPattern),
Animation.wait(duration: 10),
Expand All @@ -138,6 +154,31 @@ class ClockController {
Animation.positionBothHands(minuteDegrees: -180, hourDegrees: -180),
Animation.currentTimePrint(),
])
case 6:
// display randomized clock hand positions
queue(animations: [
Animation.display(pattern: Animation.randomizedPattern()),
Animation.wait(duration: 5),
Animation.spinBothHands(by: 180),
Animation.currentTimePrint(),
])
case 7:
// display randomized clock hand positions (right angles only)
queue(animations: [
Animation.display(pattern: Animation.randomizedRightAnglePattern()),
Animation.wait(duration: 5),
Animation.spinBothHands(by: 180),
Animation.currentTimePrint(),
])
case 8:
// display box pattern
queue(animations: [
Animation.display(pattern: boxPattern),
Animation.wait(duration: 5),
Animation.display(pattern: Animation.randomizedRightAnglePattern()),
Animation.spinBothHands(by: 180),
Animation.currentTimePrint(),
])
default:
// delay spin with current time as clock
queue(animations: [
Expand All @@ -161,7 +202,7 @@ class ClockController {
])

let newTimer = Timer(timeInterval: updateInterval, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
newTimer.tolerance = 0.2
newTimer.tolerance = 5
RunLoop.main.add(newTimer, forMode: .common)

timer = newTimer
Expand Down Expand Up @@ -202,11 +243,6 @@ class ClockController {
scene?.run(actionGroup)
}

public func queue(animations: [Animation]) {
animationQueue.append(contentsOf: animations)
startAnimationQueueIfNeeded()
}

private func runNextAnimation() {
let animation = animationQueue.removeFirst()
run(animation)
Expand All @@ -217,41 +253,5 @@ class ClockController {
runNextAnimation()
}
}

// Helper functions for manually triggering animations

public func showCurrentTime() {
queue(animations: [Animation.currentTimePrint()])
}

public func showTime(string: String) {
queue(animations: [Animation.printString(string: string)])
}

public func returnToMidnight() {
queue(animations: [Animation.positionBothHands(minuteDegrees: 0, hourDegrees: 0)])
}

public func moveAll(degrees: CGFloat) {
queue(animations: [Animation.positionBothHands(minuteDegrees: degrees, hourDegrees: degrees)])
}

public func moveAll(minuteDegrees: CGFloat, hourDegrees: CGFloat) {
queue(animations: [Animation.positionBothHands(minuteDegrees: minuteDegrees, hourDegrees: hourDegrees)])
}

public func setAllToCurrentTime() {
queue(animations: [Animation.currentTimeClock()])
}

public func rotateAll(by degrees: CGFloat) {
queue(animations: [Animation.spinBothHands(by: degrees)])
}

public func testQueue() {
queue(animations: [
Animation.spinBothHands(by: 360),
Animation.currentTimePrint(),
])
}

}
Loading

0 comments on commit aa2432c

Please sign in to comment.