-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
tween-action.ts
242 lines (214 loc) · 8.25 KB
/
tween-action.ts
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
/*
Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
https://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.
*/
/**
* @packageDocumentation
* @module tween
*/
import { warnID, warn, easing } from '../core';
import { ActionInterval } from './actions/action-interval';
import { ITweenOption } from './export-api';
import { legacyCC, VERSION } from '../core/global-exports';
/** adapter */
function TweenEasinAdapter (easingName: string) {
const initialChar = easingName.charAt(0);
if (/[A-Z]/.test(initialChar)) {
easingName = easingName.replace(initialChar, initialChar.toLowerCase());
const arr = easingName.split('-');
if (arr.length == 2) {
const str0 = arr[0];
if (str0 == 'linear') {
easingName = 'linear';
} else {
const str1 = arr[1];
switch (str0) {
case 'quadratic':
easingName = `quad${str1}`;
break;
case 'quartic':
easingName = `quart${str1}`;
break;
case 'quintic':
easingName = `quint${str1}`;
break;
case 'sinusoidal':
easingName = `sine${str1}`;
break;
case 'exponential':
easingName = `expo${str1}`;
break;
case 'circular':
easingName = `circ${str1}`;
break;
default:
easingName = str0 + str1;
break;
}
}
}
}
return easingName;
}
/** checker */
function TweenOptionChecker (opts: ITweenOption) {
const header = ' [Tween:] ';
const message = ` option is not support in v + ${VERSION}`;
if (opts.delay) {
warn(`${header}delay${message}`);
}
if (opts.repeat) {
warn(`${header}repeat${message}`);
}
if (opts.repeatDelay) {
warn(`${header}repeatDelay${message}`);
}
if (opts.interpolation) {
warn(`${header}interpolation${message}`);
}
if (opts.onStop) {
warn(`${header}onStop${message}`);
}
}
export class TweenAction extends ActionInterval {
private _opts: any;
private _props: any;
private _originProps: any;
constructor (duration: number, props: any, opts?: ITweenOption) {
super();
if (opts == null) {
opts = Object.create(null);
} else {
/** checker */
TweenOptionChecker(opts);
/** adapter */
if (opts.easing && typeof opts.easing === 'string') {
opts.easing = TweenEasinAdapter(opts.easing) as any;
}
// global easing or progress used for this action
if (!opts.progress) {
opts.progress = this.progress;
}
if (opts.easing && typeof opts.easing === 'string') {
const easingName = opts.easing as string;
opts.easing = easing[easingName];
if (!opts.easing) { warnID(1031, easingName); }
}
}
this._opts = opts;
this._props = Object.create(null);
for (const name in props) {
// filtering if
// - it was not own property
// - it was a function
// - it was undefined / null
if (!props.hasOwnProperty(name)) continue;
let value = props[name];
if (!value || typeof value === 'function') continue;
// property may have custom easing or progress function
let easing; let progress;
if (value.value !== undefined && (value.easing || value.progress)) {
if (typeof value.easing === 'string') {
easing = easing[value.easing];
!easing && warnID(1031, value.easing);
} else {
easing = value.easing;
}
progress = value.progress;
value = value.value;
}
const prop = Object.create(null);
prop.value = value;
prop.easing = easing;
prop.progress = progress;
this._props[name] = prop;
}
this._originProps = props;
this.initWithDuration(duration);
}
clone () {
const action = new TweenAction(this._duration, this._originProps, this._opts);
this._cloneDecoration(action);
return action;
}
startWithTarget (target: {}) {
ActionInterval.prototype.startWithTarget.call(this, target);
const relative = !!this._opts.relative;
const props = this._props;
for (const property in props) {
const _t: any = target[property];
if (_t === undefined) { continue; }
const prop: any = props[property];
const value = prop.value;
if (typeof _t === 'number') {
prop.start = _t;
prop.current = _t;
prop.end = relative ? _t + value : value;
} else if (typeof _t === 'object') {
if (prop.start == null) {
prop.start = {}; prop.current = {}; prop.end = {};
}
for (const k in value) {
// filtering if it not a number
if (isNaN(_t[k])) continue;
prop.start[k] = _t[k];
prop.current[k] = _t[k];
prop.end[k] = relative ? _t[k] + value[k] : value[k];
}
}
}
if (this._opts.onStart) { this._opts.onStart(this.target); }
}
update (t: number) {
const target = this.target;
if (!target) return;
const props = this._props;
const opts = this._opts;
let easingTime = t;
if (opts.easing) easingTime = opts.easing(t);
const progress = opts.progress;
for (const name in props) {
const prop = props[name];
const time = prop.easing ? prop.easing(t) : easingTime;
const interpolation = prop.progress ? prop.progress : progress;
const start = prop.start;
const end = prop.end;
if (typeof start === 'number') {
prop.current = interpolation(start, end, prop.current, time);
} else if (typeof start === 'object') {
// const value = prop.value;
for (const k in start) {
// if (value[k].easing) {
// time = value[k].easing(t);
// }
// if (value[k].progress) {
// interpolation = value[k].easing(t);
// }
prop.current[k] = interpolation(start[k], end[k], prop.current[k], time);
}
}
target[name] = prop.current;
}
if (opts.onUpdate) { opts.onUpdate(this.target, t); }
if (t == 1 && opts.onComplete) { opts.onComplete(this.target); }
}
progress (start: any, end: any, current: any, t: number) {
return current = start + (end - start) * t;
}
}