forked from cocos/cocos-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation-curves.js
450 lines (359 loc) · 12.7 KB
/
animation-curves.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var bezierByTime = require('./bezier').bezierByTime;
var binarySearch = require('../core/utils/binary-search').binarySearchEpsilon;
var WrapModeMask = require('./types').WrapModeMask;
var WrappedInfo = require('./types').WrappedInfo;
/**
* Compute a new ratio by curve type
* @param {Number} ratio - The origin ratio
* @param {Array|String} type - If it's Array, then ratio will be computed with bezierByTime. If it's string, then ratio will be computed with cc.Easing function
*/
function computeRatioByType (ratio, type) {
if (typeof type === 'string') {
var func = cc.Easing[type];
if (func) {
ratio = func(ratio);
}
else {
cc.errorID(3906, type);
}
}
else if (Array.isArray(type)) {
// bezier curve
ratio = bezierByTime(type, ratio);
}
return ratio;
}
//
// 动画数据类,相当于 AnimationClip。
// 虽然叫做 AnimCurve,但除了曲线,可以保存任何类型的值。
//
// @class AnimCurve
//
//
var AnimCurve = cc.Class({
name: 'cc.AnimCurve',
//
// @method sample
// @param {number} time
// @param {number} ratio - The normalized time specified as a number between 0.0 and 1.0 inclusive.
// @param {AnimationState} state
//
sample: function (time, ratio, state) {},
onTimeChangedManually: undefined
});
/**
* 当每两帧之前的间隔都一样的时候可以使用此函数快速查找 index
*/
function quickFindIndex (ratios, ratio) {
var length = ratios.length - 1;
if (length === 0) return 0;
var start = ratios[0];
if (ratio < start) return 0;
var end = ratios[length];
if (ratio > end) return length;
ratio = (ratio - start) / (end - start);
var eachLength = 1 / length;
var index = ratio / eachLength;
var floorIndex = index | 0;
var EPSILON = 1e-6;
if ((index - floorIndex) < EPSILON) {
return floorIndex;
}
return ~(floorIndex + 1);
}
//
//
// @class DynamicAnimCurve
//
// @extends AnimCurve
//
var DynamicAnimCurve = cc.Class({
name: 'cc.DynamicAnimCurve',
extends: AnimCurve,
properties: {
// The object being animated.
// @property target
// @type {object}
target: null,
// The name of the property being animated.
// @property prop
// @type {string}
prop: '',
// The values of the keyframes. (y)
// @property values
// @type {any[]}
values: [],
// The keyframe ratio of the keyframe specified as a number between 0.0 and 1.0 inclusive. (x)
// @property ratios
// @type {number[]}
ratios: [],
// @property types
// @param {object[]}
// Each array item maybe type:
// - [x, x, x, x]: Four control points for bezier
// - null: linear
types: [],
// @property {string[]} subProps - The path of sub property being animated.
subProps: null
},
_findFrameIndex: binarySearch,
sample: function (time, ratio, state) {
var values = this.values;
var ratios = this.ratios;
var frameCount = ratios.length;
if (frameCount === 0) {
return;
}
// evaluate value
var value;
var index = this._findFrameIndex(ratios, ratio);
if (index < 0) {
index = ~index;
if (index <= 0) {
value = values[0];
}
else if (index >= frameCount) {
value = values[frameCount - 1];
}
else {
var fromVal = values[index - 1];
var isNumber = typeof fromVal === 'number';
var canLerp = fromVal && fromVal.lerp;
if (!isNumber && !canLerp) {
value = fromVal;
}
else {
var fromRatio = ratios[index - 1];
var toRatio = ratios[index];
var type = this.types[index - 1];
var ratioBetweenFrames = (ratio - fromRatio) / (toRatio - fromRatio);
if (type) {
ratioBetweenFrames = computeRatioByType(ratioBetweenFrames, type);
}
// calculate value
var toVal = values[index];
// lerp
if (isNumber) {
value = fromVal + (toVal - fromVal) * ratioBetweenFrames;
}
else if (canLerp) {
value = fromVal.lerp(toVal, ratioBetweenFrames);
}
}
}
}
else {
value = values[index];
}
var subProps = this.subProps;
if (subProps) {
// create batched value dynamically
var mainProp = this.target[this.prop];
var subProp = mainProp;
for (var i = 0; i < subProps.length - 1; i++) {
var subPropName = subProps[i];
if (subProp) {
subProp = subProp[subPropName];
}
else {
return;
}
}
var propName = subProps[subProps.length - 1];
if (subProp) {
subProp[propName] = value;
}
else {
return;
}
value = mainProp;
}
// apply value
this.target[this.prop] = value;
}
});
DynamicAnimCurve.Linear = null;
DynamicAnimCurve.Bezier = function (controlPoints) {
return controlPoints;
};
/**
* Event information,
* @class EventInfo
*
*/
var EventInfo = function () {
this.events = [];
};
/**
* @param {Function} [func] event function
* @param {Object[]} [params] event params
*/
EventInfo.prototype.add = function (func, params) {
this.events.push({
func: func || '',
params: params || []
});
};
/**
*
* @class EventAnimCurve
*
* @extends AnimCurve
*/
var EventAnimCurve = cc.Class({
name: 'cc.EventAnimCurve',
extends: AnimCurve,
properties: {
/**
* The object being animated.
* @property target
* @type {object}
*/
target: null,
/** The keyframe ratio of the keyframe specified as a number between 0.0 and 1.0 inclusive. (x)
* @property ratios
* @type {number[]}
*/
ratios: [],
/**
* @property events
* @type {EventInfo[]}
*/
events: [],
_wrappedInfo: {
default: function () {
return new WrappedInfo();
}
},
_lastWrappedInfo: null,
_ignoreIndex: NaN
},
_wrapIterations: function (iterations) {
if (iterations - (iterations | 0) === 0) iterations -= 1;
return iterations | 0;
},
sample: function (time, ratio, state) {
var length = this.ratios.length;
var currentWrappedInfo = state.getWrappedInfo(state.time, this._wrappedInfo);
var direction = currentWrappedInfo.direction;
var currentIndex = binarySearch(this.ratios, currentWrappedInfo.ratio);
if (currentIndex < 0) {
currentIndex = ~currentIndex - 1;
// if direction is inverse, then increase index
if (direction < 0) currentIndex += 1;
}
if (this._ignoreIndex !== currentIndex) {
this._ignoreIndex = NaN;
}
currentWrappedInfo.frameIndex = currentIndex;
if (!this._lastWrappedInfo) {
this._fireEvent(currentIndex);
this._lastWrappedInfo = new WrappedInfo(currentWrappedInfo);
return;
}
var wrapMode = state.wrapMode;
var currentIterations = this._wrapIterations(currentWrappedInfo.iterations);
var lastWrappedInfo = this._lastWrappedInfo;
var lastIterations = this._wrapIterations(lastWrappedInfo.iterations);
var lastIndex = lastWrappedInfo.frameIndex;
var lastDirection = lastWrappedInfo.direction;
var interationsChanged = lastIterations !== -1 && currentIterations !== lastIterations;
if (lastIndex === currentIndex && interationsChanged && length === 1) {
this._fireEvent(0);
}
else if (lastIndex !== currentIndex || interationsChanged) {
direction = lastDirection;
do {
if (lastIndex !== currentIndex) {
if (direction === -1 && lastIndex === 0 && currentIndex > 0) {
if ((wrapMode & WrapModeMask.PingPong) === WrapModeMask.PingPong) {
direction *= -1;
}
else {
lastIndex = length;
}
lastIterations ++;
}
else if (direction === 1 && lastIndex === length - 1 && currentIndex < length - 1) {
if ((wrapMode & WrapModeMask.PingPong) === WrapModeMask.PingPong) {
direction *= -1;
}
else {
lastIndex = -1;
}
lastIterations ++;
}
if (lastIndex === currentIndex) break;
if (lastIterations > currentIterations) break;
}
lastIndex += direction;
cc.director.getAnimationManager().pushDelayEvent(this, '_fireEvent', [lastIndex]);
} while (lastIndex !== currentIndex && lastIndex > -1 && lastIndex < length);
}
this._lastWrappedInfo.set(currentWrappedInfo);
},
_fireEvent: function (index) {
if (index < 0 || index >= this.events.length || this._ignoreIndex === index) return;
var eventInfo = this.events[index];
var events = eventInfo.events;
if ( !this.target.isValid ) {
return;
}
var components = this.target._components;
for (var i = 0; i < events.length; i++) {
var event = events[i];
var funcName = event.func;
for (var j = 0; j < components.length; j++) {
var component = components[j];
var func = component[funcName];
if (func) func.apply(component, event.params);
}
}
},
onTimeChangedManually: function (time, state) {
this._lastWrappedInfo = null;
this._ignoreIndex = NaN;
var info = state.getWrappedInfo(time, this._wrappedInfo);
var direction = info.direction;
var frameIndex = binarySearch(this.ratios, info.ratio);
// only ignore when time not on a frame index
if (frameIndex < 0) {
frameIndex = ~frameIndex - 1;
// if direction is inverse, then increase index
if (direction < 0) frameIndex += 1;
this._ignoreIndex = frameIndex;
}
}
});
if (CC_TEST) {
cc._Test.DynamicAnimCurve = DynamicAnimCurve;
cc._Test.EventAnimCurve = EventAnimCurve;
}
module.exports = {
AnimCurve: AnimCurve,
DynamicAnimCurve: DynamicAnimCurve,
EventAnimCurve: EventAnimCurve,
EventInfo: EventInfo,
computeRatioByType: computeRatioByType,
quickFindIndex: quickFindIndex
};