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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. 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.*/

define.class(function(require, exports){

        // internal, instance of checkable end object
        this.End = function(){
        }

        this.delay = 0
        this.interrupt = false
        this.repeat = 1
        this.bounce = false
        this.type = float32
        this.motion = float.ease.linear
        this.speed = 1
        this.interpolator = mix

        this.atConstructor = function(config, obj, key, track, first_value, last_value){
                this.config = config
                this.obj = obj
                this.key = key
                // the internal track construct
                this.track = track
                //else this.track = config

                if (config.motion) this.motion = config.motion

                if (track) {
                        if (track.bounce) this.bounce = track.bounce
                        if (track.repeat) this.repeat = track.repeat
                        if (track.speed) this.speed = track.speed
                }

                if(typeof this.motion === 'string') this.motion = float.ease[this.motion] || float.ease.linear
                if(!config) debugger
                if(config.type) this.type = config.type
                this.first_value = first_value
                this.last_value = last_value
        }

        this.compute = function(local_time, time_skew){
                // lazily initialize order
                if(!this.order){
                        this.order = [0]
                        this.values = {}
                        this.end_time = 0
                        for(var key in this.track){
                                var time = parseFloat(key)
                                if(parseFloat(key) == time){
                                        var desc = {}
                                        var value = this.track[key]
                                        if(value === undefined) continue
                                        //check if we have a descriptor object in the key
                                        if(value && typeof value == 'object' && value.value !== undefined){
                                                desc.value = this.type(value.value)        // parse it
                                                if(value.motion) desc.motion = (typeof value.motion === 'string'? float.ease[value.motion]: value.motion) || this.motion
                                        }
                                        else{
                                                desc.value = this.type(value)
                                        }

                                        this.values[key] = desc
                                        this.order.push(key)
                                        if(time > this.end_time) this.end_time = time
                                }
                        }
                        if(!this.track && this.config.duration !== undefined){
                                var duration = this.config.duration
                                this.order.push(duration)
                                this.values[duration] = {value:this.type(this.last_value)}
                                if(duration > this.end_time) this.end_time = duration
                        }
                        this.order.sort()
                }
                // alright lets process the tracks.
                /*if(this.reset_time){
                        this.last_time_stamp = time_stamp

                        this.start_time_stamp = time_stamp - (time_skew || 0)
                        // initialize slot 0
                        if(this.values[0] === undefined){
                                this.first_value = first_value !== undefined? first_value: (this.first_value || 0)
                        }
                        this.reset_time = false
                }
                if(this.pause_time !== undefined){
                        // just skid the start_time for as long as it is stopped
                        this.start_time_stamp = time_stamp - this.pause_time
                        return this.value
                }
                //var local_time = (time_stamp - this.start_time_stamp )
                */

                // alright now, we have to compute the right time.

                if(this.delay !== undefined){
                        if(local_time - this.delay <= 0) local_time = 0
                        else local_time -= this.delay
                }

                if(this.reverse_time !== undefined){
                        // essentially we are going to count backwards starting this.reverse_time
                        local_time = this.reverse_time - (local_time - this.reverse_time)
                }

                // norm time goes from 0 to 1 over the length of the track
                var norm_time = (local_time / this.end_time) * this.speed
                // lets check the looping
                var end_gap = undefined

                if (this.bounce){
                        if(norm_time >= this.repeat) end_gap = norm_time - this.repeat, norm_time = this.repeat
                        norm_time = norm_time % 2
                        if(norm_time > 1) norm_time = 2 - norm_time
                }
                else if (this.repeat){
                        if(norm_time >= this.repeat) end_gap = norm_time - this.repeat, norm_time = this.repeat - 0.000001
                        norm_time = norm_time % 1
                }

                if(norm_time < 0){
                        end_gap = -norm_time
                }

                if(end_gap !== undefined){ // alright we are stopping
                        var end = new this.End()
                        // fetch the last key of our track and pass it in as the new first for the next one

                        end.last_value = norm_time >= 0.99? this.values[this.order[this.order.length - 1]].value: this.values[this.order[0]].value
                        end.skew = end_gap * this.end_time
                        return end
                }

                if(norm_time > 1) norm_time = 1
                if(norm_time < 0) norm_time = 0
                var motion_time = this.motion(norm_time)
                if(motion_time < 0.001 ) motion_time = 0
                if(motion_time > 0.999 ) motion_time = 1
                // lets convert it back to keyspace time
                var track_time = motion_time * this.end_time
                // the key pair to interpolate between
                var key1, key2
                // find the right keys
                if(!this.clamped && (track_time <= this.order[0] || track_time >= this.order[this.order.length-1])){
                        if(track_time <= this.order[0]){
                                key1 = this.order[0]
                                key2 = this.order[1]
                        }
                        else{
                                key1 = this.order[this.order.length - 2] || 0
                                key2 = this.order[this.order.length - 1]
                        }
                }
                else{ // scan for it
                        for(var i = 0, len = this.order.length; i<len; i++){
                                var key = this.order[i]
                                if(track_time <= key){
                                        key1 = this.order[i-1] || 0
                                        key2 = key
                                        break
                                }
                        }
                }
                // if has atleast 1 key, do something
                if(key1 !== undefined){
                        // only key1, the check is to deal with the 'computed' 0 key
                        if(key2 === undefined) return key1 in this.values? this.values[key1].value: this.first_value
                        else{ // interpolate
                                var inter_key_time
                                if(Math.abs(key2 - key1) <0.0001) inter_key_time = 0
                                else inter_key_time = (track_time - key1) / (key2 - key1)

                                var value1 = key1 in this.values? this.values[key1]: {value:this.first_value}
                                var value2 = this.values[key2]

                                if(value2.motion) inter_key_time = value2.motion(inter_key_time)
                                var out = mix(value1.value, value2.value, inter_key_time)

                                return out
                        }
                }
        }
        /*
        // stops an animation, untill resumed
        this.pause = function(){
                this.pause_time = this.last_time_stamp - this.start_time_stamp
        }

        // resume a paused track
        this.resume = function(){
                this.pause_time = undefined
        }

        // this reverses the playback. it will just play the current trackbackwards starting now
        this.reverse = function(){
                // alright if we have reverse time, calculate the right start_time_stamp
                if(this.reverse_time !== undefined){
                        // calculate the current time
                        this.start_time_stamp = 2*(this.last_time_stamp - this.reverse_time) - this.start_time_stamp
                        //this.start_time_stamp = this.last_time_stamp-this.reverse_time
                        this.reverse_time = undefined
                }
                else{
                        this.reverse_time = this.last_time_stamp - this.start_time_stamp
                }
        }*/
})