-
Notifications
You must be signed in to change notification settings - Fork 13
/
CustomController.swift
153 lines (119 loc) · 4.38 KB
/
CustomController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// CustomController.swift
// BasicExample
//
// Created by Mandy Lowry on 11/11/19.
// Copyright © 2019 2Dimensions. All rights reserved.
//
import Foundation
/// To use on a physical device.
//import FlareSwift
/// To use on a Simulator.
import FlareSwiftDev
class CustomController: FlareSkViewController {
internal let mixSeconds: Double = 0.1
internal var controlLayers = [FlareAnimationLayer]()
var animation: ActorAnimation? = nil
var animTime: Double = 0.00;
var currentAnimTime: Double = 0;
override open var isPlaying: Bool {
return !isPaused &&
(!controlLayers.isEmpty || super.isPlaying)
}
open func onCompleted(name: String) {
if(name == "Mustache_New"){
play(name: "Idle")
}
}
public func play(name: String?, mix: Double = 1.0, mixSeconds: Double = 0.2) {
animationName = name
guard
let aName = animationName,
let fView = view as? FlareSkView,
let artboard = fView.artboard
else { return }
if let animation = artboard.getAnimation(name: aName) {
controlLayers.append(
FlareAnimationLayer(
animation,
name: aName,
mix: mix,
mixSeconds: mixSeconds
)
)
updatePlayState()
}
}
override open func advanceControls(by elapsed: Double) -> Bool {
guard
let fView = view as? FlareSkView,
let artboard = fView.artboard
else { return false }
/** CustomProperties:
let myNode = artboard.getNode(name: "Scale Node_Special Property")
for cpNodes in myNode.CustomProperties {
print(cpNodes)
}
*/
/**
let animation = artboard.getAnimation(name: "Idle")
currentAnimTime += (animTime - currentAnimTime) * min(1, elapsed * 5)
animation?.apply(
time: currentAnimTime * animation!.duration,
artboard: artboard,
mix: 1
)
*/
var lastFullyMixed = -1
var lastMix = 0.0
var completed = [FlareAnimationLayer]()
var arrayEvent = Array<AnimationEventArgs>()
for i in 0..<controlLayers.count {
let layer = controlLayers[i]
currentAnimTime = layer.time;
layer.mix += elapsed
layer.time += elapsed
lastMix = mixSeconds == 0.0
? 1.0
: min(1.0, layer.mix / mixSeconds)
if layer.isLooping {
layer.time = layer.time.truncatingRemainder(dividingBy: layer.duration)
}
layer.animation.apply(time: layer.time, artboard: artboard, mix: Float(lastMix))
if lastMix == 1.0 {
lastFullyMixed = i
}
if layer.time > layer.animation.duration {
completed.append(layer)
}
// // EVENT TEST:
// if(animation == nil)
// {
// animation = artboard.getAnimation(name: "Mustache_New")
// }
animation?.triggerEvents(components: artboard.components! as! Array<ActorComponent>, fromTime: currentAnimTime, toTime: layer.time, triggerEvents: &arrayEvent)
for i in 0 ..< arrayEvent.count {
print(arrayEvent[i].name)
if(arrayEvent[i].name == "Event"){
//do something
}
}
}
if lastFullyMixed != -1 {
controlLayers.removeSubrange(0..<lastFullyMixed)
}
if
animationName == nil,
controlLayers.count == 1,
lastMix == 1.0
{
controlLayers.remove(at: 0)
}
for completedAnimation in completed {
controlLayers.removeAll { $0 === completedAnimation }
onCompleted(name: completedAnimation.name)
}
return !controlLayers.isEmpty
}
override open func setViewTransform(viewTransform: Mat2D) {}
}