| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A class which automatically plays through a queue of |
| 17 |
* animations. AnimationParallelQueue and AnimationSerialQueue provide |
| 18 |
* specific implementations of the abstract class AnimationQueue. |
| 19 |
* |
| 20 |
* @see ../demos/animationqueue.html |
| 21 |
*/ |
| 22 |
|
| 23 |
goog.provide('goog.fx.AnimationParallelQueue'); |
| 24 |
goog.provide('goog.fx.AnimationQueue'); |
| 25 |
goog.provide('goog.fx.AnimationSerialQueue'); |
| 26 |
|
| 27 |
goog.require('goog.array'); |
| 28 |
goog.require('goog.asserts'); |
| 29 |
goog.require('goog.events'); |
| 30 |
goog.require('goog.fx.Animation'); |
| 31 |
goog.require('goog.fx.Transition'); |
| 32 |
goog.require('goog.fx.TransitionBase'); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor for AnimationQueue object. |
| 38 |
* |
| 39 |
* @constructor |
| 40 |
* @struct |
| 41 |
* @extends {goog.fx.TransitionBase} |
| 42 |
*/ |
| 43 |
goog.fx.AnimationQueue = function() { |
| 44 |
goog.fx.AnimationQueue.base(this, 'constructor'); |
| 45 |
|
| 46 |
/** |
| 47 |
* An array holding all animations in the queue. |
| 48 |
* @type {Array<goog.fx.TransitionBase>} |
| 49 |
* @protected |
| 50 |
*/ |
| 51 |
this.queue = []; |
| 52 |
}; |
| 53 |
goog.inherits(goog.fx.AnimationQueue, goog.fx.TransitionBase); |
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* Pushes an Animation to the end of the queue. |
| 58 |
* @param {goog.fx.TransitionBase} animation The animation to add to the queue. |
| 59 |
*/ |
| 60 |
goog.fx.AnimationQueue.prototype.add = function(animation) { |
| 61 |
goog.asserts.assert( |
| 62 |
this.isStopped(), |
| 63 |
'Not allowed to add animations to a running animation queue.'); |
| 64 |
|
| 65 |
if (goog.array.contains(this.queue, animation)) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
this.queue.push(animation); |
| 70 |
goog.events.listen( |
| 71 |
animation, goog.fx.Transition.EventType.FINISH, this.onAnimationFinish, |
| 72 |
false, this); |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Removes an Animation from the queue. |
| 78 |
* @param {goog.fx.Animation} animation The animation to remove. |
| 79 |
*/ |
| 80 |
goog.fx.AnimationQueue.prototype.remove = function(animation) { |
| 81 |
goog.asserts.assert( |
| 82 |
this.isStopped(), |
| 83 |
'Not allowed to remove animations from a running animation queue.'); |
| 84 |
|
| 85 |
if (goog.array.remove(this.queue, animation)) { |
| 86 |
goog.events.unlisten( |
| 87 |
animation, goog.fx.Transition.EventType.FINISH, this.onAnimationFinish, |
| 88 |
false, this); |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Handles the event that an animation has finished. |
| 95 |
* @param {goog.events.Event} e The finishing event. |
| 96 |
* @protected |
| 97 |
*/ |
| 98 |
goog.fx.AnimationQueue.prototype.onAnimationFinish = goog.abstractMethod; |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Disposes of the animations. |
| 103 |
* @override |
| 104 |
*/ |
| 105 |
goog.fx.AnimationQueue.prototype.disposeInternal = function() { |
| 106 |
goog.array.forEach(this.queue, function(animation) { animation.dispose(); }); |
| 107 |
this.queue.length = 0; |
| 108 |
|
| 109 |
goog.fx.AnimationQueue.base(this, 'disposeInternal'); |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Constructor for AnimationParallelQueue object. |
| 116 |
* @constructor |
| 117 |
* @struct |
| 118 |
* @extends {goog.fx.AnimationQueue} |
| 119 |
*/ |
| 120 |
goog.fx.AnimationParallelQueue = function() { |
| 121 |
goog.fx.AnimationParallelQueue.base(this, 'constructor'); |
| 122 |
|
| 123 |
/** |
| 124 |
* Number of finished animations. |
| 125 |
* @type {number} |
| 126 |
* @private |
| 127 |
*/ |
| 128 |
this.finishedCounter_ = 0; |
| 129 |
}; |
| 130 |
goog.inherits(goog.fx.AnimationParallelQueue, goog.fx.AnimationQueue); |
| 131 |
|
| 132 |
|
| 133 |
/** @override */ |
| 134 |
goog.fx.AnimationParallelQueue.prototype.play = function(opt_restart) { |
| 135 |
if (this.queue.length == 0) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
if (opt_restart || this.isStopped()) { |
| 140 |
this.finishedCounter_ = 0; |
| 141 |
this.onBegin(); |
| 142 |
} else if (this.isPlaying()) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
this.onPlay(); |
| 147 |
if (this.isPaused()) { |
| 148 |
this.onResume(); |
| 149 |
} |
| 150 |
var resuming = this.isPaused() && !opt_restart; |
| 151 |
|
| 152 |
this.startTime = goog.now(); |
| 153 |
this.endTime = null; |
| 154 |
this.setStatePlaying(); |
| 155 |
|
| 156 |
goog.array.forEach(this.queue, function(anim) { |
| 157 |
if (!resuming || anim.isPaused()) { |
| 158 |
anim.play(opt_restart); |
| 159 |
} |
| 160 |
}); |
| 161 |
|
| 162 |
return true; |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
/** @override */ |
| 167 |
goog.fx.AnimationParallelQueue.prototype.pause = function() { |
| 168 |
if (this.isPlaying()) { |
| 169 |
goog.array.forEach(this.queue, function(anim) { |
| 170 |
if (anim.isPlaying()) { |
| 171 |
anim.pause(); |
| 172 |
} |
| 173 |
}); |
| 174 |
|
| 175 |
this.setStatePaused(); |
| 176 |
this.onPause(); |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
/** @override */ |
| 182 |
goog.fx.AnimationParallelQueue.prototype.stop = function(opt_gotoEnd) { |
| 183 |
goog.array.forEach(this.queue, function(anim) { |
| 184 |
if (!anim.isStopped()) { |
| 185 |
anim.stop(opt_gotoEnd); |
| 186 |
} |
| 187 |
}); |
| 188 |
|
| 189 |
this.setStateStopped(); |
| 190 |
this.endTime = goog.now(); |
| 191 |
|
| 192 |
this.onStop(); |
| 193 |
this.onEnd(); |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
/** @override */ |
| 198 |
goog.fx.AnimationParallelQueue.prototype.onAnimationFinish = function(e) { |
| 199 |
this.finishedCounter_++; |
| 200 |
if (this.finishedCounter_ == this.queue.length) { |
| 201 |
this.endTime = goog.now(); |
| 202 |
|
| 203 |
this.setStateStopped(); |
| 204 |
|
| 205 |
this.onFinish(); |
| 206 |
this.onEnd(); |
| 207 |
} |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
/** |
| 213 |
* Constructor for AnimationSerialQueue object. |
| 214 |
* @constructor |
| 215 |
* @struct |
| 216 |
* @extends {goog.fx.AnimationQueue} |
| 217 |
*/ |
| 218 |
goog.fx.AnimationSerialQueue = function() { |
| 219 |
goog.fx.AnimationSerialQueue.base(this, 'constructor'); |
| 220 |
|
| 221 |
/** |
| 222 |
* Current animation in queue currently active. |
| 223 |
* @type {number} |
| 224 |
* @private |
| 225 |
*/ |
| 226 |
this.current_ = 0; |
| 227 |
}; |
| 228 |
goog.inherits(goog.fx.AnimationSerialQueue, goog.fx.AnimationQueue); |
| 229 |
|
| 230 |
|
| 231 |
/** @override */ |
| 232 |
goog.fx.AnimationSerialQueue.prototype.play = function(opt_restart) { |
| 233 |
if (this.queue.length == 0) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
if (opt_restart || this.isStopped()) { |
| 238 |
if (this.current_ < this.queue.length && |
| 239 |
!this.queue[this.current_].isStopped()) { |
| 240 |
this.queue[this.current_].stop(false); |
| 241 |
} |
| 242 |
|
| 243 |
this.current_ = 0; |
| 244 |
this.onBegin(); |
| 245 |
} else if (this.isPlaying()) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
this.onPlay(); |
| 250 |
if (this.isPaused()) { |
| 251 |
this.onResume(); |
| 252 |
} |
| 253 |
|
| 254 |
this.startTime = goog.now(); |
| 255 |
this.endTime = null; |
| 256 |
this.setStatePlaying(); |
| 257 |
|
| 258 |
this.queue[this.current_].play(opt_restart); |
| 259 |
|
| 260 |
return true; |
| 261 |
}; |
| 262 |
|
| 263 |
|
| 264 |
/** @override */ |
| 265 |
goog.fx.AnimationSerialQueue.prototype.pause = function() { |
| 266 |
if (this.isPlaying()) { |
| 267 |
this.queue[this.current_].pause(); |
| 268 |
this.setStatePaused(); |
| 269 |
this.onPause(); |
| 270 |
} |
| 271 |
}; |
| 272 |
|
| 273 |
|
| 274 |
/** @override */ |
| 275 |
goog.fx.AnimationSerialQueue.prototype.stop = function(opt_gotoEnd) { |
| 276 |
this.setStateStopped(); |
| 277 |
this.endTime = goog.now(); |
| 278 |
|
| 279 |
if (opt_gotoEnd) { |
| 280 |
for (var i = this.current_; i < this.queue.length; ++i) { |
| 281 |
var anim = this.queue[i]; |
| 282 |
// If the animation is stopped, start it to initiate rendering. This |
| 283 |
// might be needed to make the next line work. |
| 284 |
if (anim.isStopped()) anim.play(); |
| 285 |
// If the animation is not done, stop it and go to the end state of the |
| 286 |
// animation. |
| 287 |
if (!anim.isStopped()) anim.stop(true); |
| 288 |
} |
| 289 |
} else if (this.current_ < this.queue.length) { |
| 290 |
this.queue[this.current_].stop(false); |
| 291 |
} |
| 292 |
|
| 293 |
this.onStop(); |
| 294 |
this.onEnd(); |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
/** @override */ |
| 299 |
goog.fx.AnimationSerialQueue.prototype.onAnimationFinish = function(e) { |
| 300 |
if (this.isPlaying()) { |
| 301 |
this.current_++; |
| 302 |
if (this.current_ < this.queue.length) { |
| 303 |
this.queue[this.current_].play(); |
| 304 |
} else { |
| 305 |
this.endTime = goog.now(); |
| 306 |
this.setStateStopped(); |
| 307 |
|
| 308 |
this.onFinish(); |
| 309 |
this.onEnd(); |
| 310 |
} |
| 311 |
} |
| 312 |
}; |