forked from Wizcorp/tina.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestedTween.js
187 lines (152 loc) · 5.61 KB
/
NestedTween.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
var BriefPlayable = require('./BriefPlayable');
var AbstractTween = require('./AbstractTween');
/**
*
* @classdesc
* Manages transition of properties of an object
*
* @param {object} object - Object to tween
* @param {array} properties - Properties of the object to tween
*
*/
function NestedTween(object, properties) {
if ((this instanceof NestedTween) === false) {
return new NestedTween(object, properties);
}
BriefPlayable.call(this);
// Map if tween per object for fast access
this._tweensPerObject = {};
// Array of tween for fast iteration when udpating
this._tweens = [];
// Property chains per object
this._propertyChains = {};
// Array of object chains
this._propertyChainStrings = [];
var objects = {};
var propertiesPerObject = {};
var property, propertyChainString;
for (var p = 0; p < properties.length; p += 1) {
var propertyString = properties[p];
propertyChainString = propertyString.substring(0, propertyString.lastIndexOf('.'));
if (propertiesPerObject[propertyChainString] === undefined) {
// Fetching object and property
var propertyChain = propertyString.split('.');
var propertyIndex = propertyChain.length - 1;
var propertyObject = object;
// Following the chain to get the object
for (var c = 0; c < propertyIndex; c += 1) {
propertyObject = propertyObject[propertyChain[c]];
}
property = propertyChain[propertyIndex];
if (propertyObject[property] instanceof Array) {
propertiesPerObject[propertyString] = null;
objects[propertyString] = propertyObject[property];
this._propertyChainStrings.push(propertyString);
this._propertyChains[propertyString] = propertyChain;
} else {
propertiesPerObject[propertyChainString] = [property];
objects[propertyChainString] = propertyObject;
this._propertyChainStrings.push(propertyChainString);
// Removing last element of the property chain
propertyChain.pop();
this._propertyChains[propertyChainString] = propertyChain;
}
} else {
// Object was already fetched
property = propertyString.substring(propertyString.lastIndexOf('.') + 1);
propertiesPerObject[propertyChainString].push(property);
}
}
// Creating the tweens
for (propertyChainString in objects) {
var tweenObject = objects[propertyChainString];
var tweenProperties = propertiesPerObject[propertyChainString];
var tween = new AbstractTween(tweenObject, tweenProperties);
this._tweens.push(tween);
this._tweensPerObject[propertyChainString] = tween;
}
}
NestedTween.prototype = Object.create(BriefPlayable.prototype);
NestedTween.prototype.constructor = NestedTween;
module.exports = NestedTween;
NestedTween.prototype.relative = function (relative) {
// Dispatching relative
for (var t = 0; t < this._tweens.length; t += 1) {
this._tweens[t].relative(relative);
}
return this;
};
NestedTween.prototype.reset = function () {
// Dispatching reset
for (var t = 0; t < this._tweens.length; t += 1) {
this._tweens[t].reset();
}
this._duration = 0;
return this;
};
NestedTween.prototype.interpolations = function (interpolations) {
// Dispatching interpolations
for (var o = 0; o < this._propertyChainStrings.length; o += 1) {
var propertyChainString = this._propertyChainStrings[o];
var propertyChain = this._propertyChains[propertyChainString];
var chainLength = propertyChain.length;
var objectInterpolations = interpolations;
for (var c = 0; c < chainLength && objectInterpolations !== undefined; c += 1) {
objectInterpolations = objectInterpolations[propertyChain[c]];
}
if (objectInterpolations !== undefined) {
this._tweensPerObject[propertyChainString].interpolations(objectInterpolations);
}
}
return this;
};
NestedTween.prototype.from = function (fromObject) {
// Dispatching from
for (var o = 0; o < this._propertyChainStrings.length; o += 1) {
var propertyChainString = this._propertyChainStrings[o];
var propertyChain = this._propertyChains[propertyChainString];
var chainLength = propertyChain.length;
var object = fromObject;
for (var c = 0; c < chainLength && object !== undefined; c += 1) {
object = object[propertyChain[c]];
}
if (object !== undefined) {
this._tweensPerObject[propertyChainString].from(object);
}
}
return this;
};
NestedTween.prototype.to = function (toObject, duration, easing, easingParam, interpolationParams) {
// Dispatching to
for (var o = 0; o < this._propertyChainStrings.length; o += 1) {
var propertyChainString = this._propertyChainStrings[o];
var propertyChain = this._propertyChains[propertyChainString];
var chainLength = propertyChain.length;
var object = toObject;
for (var c = 0; c < chainLength; c += 1) {
object = object[propertyChain[c]];
}
var objectInterpolationParams = interpolationParams;
for (c = 0; c < chainLength && objectInterpolationParams !== undefined; c += 1) {
objectInterpolationParams = objectInterpolationParams[propertyChain[c]];
}
this._tweensPerObject[propertyChainString].to(object, duration, easing, easingParam, objectInterpolationParams);
}
this._extendDuration(duration);
return this;
};
NestedTween.prototype.wait = function (duration) {
// Dispatching wait
for (var t = 0; t < this._tweens.length; t += 1) {
this._tweens[t].wait(duration);
}
this._extendDuration(duration);
return this;
};
NestedTween.prototype._update = function () {
for (var t = 0; t < this._tweens.length; t += 1) {
var tween = this._tweens[t];
tween._time = this._time;
tween._update();
}
};