| 1 |
/* |
|---|
| 2 |
* BetweenAS3 |
|---|
| 3 |
* |
|---|
| 4 |
* Licensed under the MIT License |
|---|
| 5 |
* |
|---|
| 6 |
* Copyright (c) 2009 BeInteractive! (www.be-interactive.org) and |
|---|
| 7 |
* Spark project (www.libspark.org) |
|---|
| 8 |
* |
|---|
| 9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 10 |
* of this software and associated documentation files (the "Software"), to deal |
|---|
| 11 |
* in the Software without restriction, including without limitation the rights |
|---|
| 12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 13 |
* copies of the Software, and to permit persons to whom the Software is |
|---|
| 14 |
* furnished to do so, subject to the following conditions: |
|---|
| 15 |
* |
|---|
| 16 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 17 |
* all copies or substantial portions of the Software. |
|---|
| 18 |
* |
|---|
| 19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 25 |
* THE SOFTWARE. |
|---|
| 26 |
* |
|---|
| 27 |
*/ |
|---|
| 28 |
package org.libspark.betweenas3.tweens |
|---|
| 29 |
{ |
|---|
| 30 |
import flash.events.EventDispatcher; |
|---|
| 31 |
import flash.events.IEventDispatcher; |
|---|
| 32 |
import flash.events.Event; |
|---|
| 33 |
import org.libspark.betweenas3.events.BetweenEvent; |
|---|
| 34 |
import org.libspark.betweenas3.targets.ITweenTarget; |
|---|
| 35 |
import org.libspark.betweenas3.tickers.ITicker; |
|---|
| 36 |
import org.libspark.betweenas3.tickers.TickerListener; |
|---|
| 37 |
|
|---|
| 38 |
/** |
|---|
| 39 |
* . |
|---|
| 40 |
* |
|---|
| 41 |
* @author yossy:beinteractive |
|---|
| 42 |
*/ |
|---|
| 43 |
public class StandardTween extends TickerListener implements ITween |
|---|
| 44 |
{ |
|---|
| 45 |
public function StandardTween(tweenTarget:ITweenTarget, ticker:ITicker, position:Number) |
|---|
| 46 |
{ |
|---|
| 47 |
_tweenTarget = tweenTarget; |
|---|
| 48 |
_ticker = ticker; |
|---|
| 49 |
_position = position; |
|---|
| 50 |
_duration = tweenTarget.duration; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
private var _tweenTarget:ITweenTarget; |
|---|
| 54 |
private var _ticker:ITicker; |
|---|
| 55 |
private var _position:Number; |
|---|
| 56 |
private var _duration:Number; |
|---|
| 57 |
private var _startTime:Number; |
|---|
| 58 |
private var _isPlaying:Boolean = false; |
|---|
| 59 |
private var _stopOnComplete:Boolean = true; |
|---|
| 60 |
private var _dispatcher:IEventDispatcher; |
|---|
| 61 |
private var _willTriggerFlags:uint = 0; |
|---|
| 62 |
private var _onPlay:Function; |
|---|
| 63 |
private var _onPlayParams:Array; |
|---|
| 64 |
private var _onStop:Function; |
|---|
| 65 |
private var _onStopParams:Array; |
|---|
| 66 |
private var _onUpdate:Function; |
|---|
| 67 |
private var _onUpdateParams:Array; |
|---|
| 68 |
private var _onComplete:Function; |
|---|
| 69 |
private var _onCompleteParams:Array; |
|---|
| 70 |
|
|---|
| 71 |
/** |
|---|
| 72 |
* @inheritDoc |
|---|
| 73 |
*/ |
|---|
| 74 |
public function get tweenTarget():ITweenTarget |
|---|
| 75 |
{ |
|---|
| 76 |
return _tweenTarget; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
/** |
|---|
| 80 |
* このトゥイーンの継続時間 (秒) を返します. |
|---|
| 81 |
*/ |
|---|
| 82 |
public function get duration():Number |
|---|
| 83 |
{ |
|---|
| 84 |
return _duration; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
/** |
|---|
| 88 |
* このトゥイーンの現在位置 (秒) を返します. |
|---|
| 89 |
*/ |
|---|
| 90 |
public function get position():Number |
|---|
| 91 |
{ |
|---|
| 92 |
return _position; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
/** |
|---|
| 96 |
* このトゥイーンが現在再生中であれば true, そうでなければ false を返します. |
|---|
| 97 |
*/ |
|---|
| 98 |
public function get isPlaying():Boolean |
|---|
| 99 |
{ |
|---|
| 100 |
return _isPlaying; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
/** |
|---|
| 104 |
* @inheritDoc |
|---|
| 105 |
*/ |
|---|
| 106 |
public function get stopOnComplete():Boolean |
|---|
| 107 |
{ |
|---|
| 108 |
return _stopOnComplete; |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
/** |
|---|
| 112 |
* @private |
|---|
| 113 |
*/ |
|---|
| 114 |
public function set stopOnComplete(value:Boolean):void |
|---|
| 115 |
{ |
|---|
| 116 |
_stopOnComplete = value; |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
public function get onPlay():Function |
|---|
| 120 |
{ |
|---|
| 121 |
return _onPlay; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
public function set onPlay(value:Function):void |
|---|
| 125 |
{ |
|---|
| 126 |
_onPlay = value; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
public function get onPlayParams():Array |
|---|
| 130 |
{ |
|---|
| 131 |
return _onPlayParams; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
public function set onPlayParams(value:Array):void |
|---|
| 135 |
{ |
|---|
| 136 |
_onPlayParams = value; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
public function get onStop():Function |
|---|
| 140 |
{ |
|---|
| 141 |
return _onStop; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
public function set onStop(value:Function):void |
|---|
| 145 |
{ |
|---|
| 146 |
_onStop = value; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
public function get onStopParams():Array |
|---|
| 150 |
{ |
|---|
| 151 |
return _onStopParams; |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
public function set onStopParams(value:Array):void |
|---|
| 155 |
{ |
|---|
| 156 |
_onStopParams = value; |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
public function get onUpdate():Function |
|---|
| 160 |
{ |
|---|
| 161 |
return _onUpdate; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
public function set onUpdate(value:Function):void |
|---|
| 165 |
{ |
|---|
| 166 |
_onUpdate = value; |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
public function get onUpdateParams():Array |
|---|
| 170 |
{ |
|---|
| 171 |
return _onUpdateParams; |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
public function set onUpdateParams(value:Array):void |
|---|
| 175 |
{ |
|---|
| 176 |
_onUpdateParams = value; |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
public function get onComplete():Function |
|---|
| 180 |
{ |
|---|
| 181 |
return _onComplete; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
public function set onComplete(value:Function):void |
|---|
| 185 |
{ |
|---|
| 186 |
_onComplete = value; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
public function get onCompleteParams():Array |
|---|
| 190 |
{ |
|---|
| 191 |
return _onCompleteParams; |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
public function set onCompleteParams(value:Array):void |
|---|
| 195 |
{ |
|---|
| 196 |
_onCompleteParams = value; |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
/** |
|---|
| 200 |
* このトゥイーンの再生を現在の位置から開始します. |
|---|
| 201 |
*/ |
|---|
| 202 |
public function play():void |
|---|
| 203 |
{ |
|---|
| 204 |
if (!_isPlaying && _position < _tweenTarget.duration) { |
|---|
| 205 |
var t:Number = _ticker.time; |
|---|
| 206 |
_startTime = t - _position; |
|---|
| 207 |
_isPlaying = true; |
|---|
| 208 |
_ticker.addTickerListener(this); |
|---|
| 209 |
if ((_willTriggerFlags & 0x01) != 0) { |
|---|
| 210 |
_dispatcher.dispatchEvent(new BetweenEvent(BetweenEvent.PLAY)); |
|---|
| 211 |
} |
|---|
| 212 |
if (_onPlay != null) { |
|---|
| 213 |
_onPlay.apply(null, _onPlayParams); |
|---|
| 214 |
} |
|---|
| 215 |
tick(t); |
|---|
| 216 |
} |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
/** |
|---|
| 220 |
* このトゥイーンの再生を現在の位置で停止します. |
|---|
| 221 |
*/ |
|---|
| 222 |
public function stop():void |
|---|
| 223 |
{ |
|---|
| 224 |
if (_isPlaying) { |
|---|
| 225 |
_isPlaying = false; |
|---|
| 226 |
_ticker.removeTickerListener(this); |
|---|
| 227 |
if ((_willTriggerFlags & 0x02) != 0) { |
|---|
| 228 |
_dispatcher.dispatchEvent(new BetweenEvent(BetweenEvent.STOP)); |
|---|
| 229 |
} |
|---|
| 230 |
if (_onStop != null) { |
|---|
| 231 |
_onStop.apply(null, _onStopParams); |
|---|
| 232 |
} |
|---|
| 233 |
} |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
/** |
|---|
| 237 |
* このトゥイーンの再生を指定された位置から開始します. |
|---|
| 238 |
* |
|---|
| 239 |
* @param position 再生を開始する位置 (秒) |
|---|
| 240 |
*/ |
|---|
| 241 |
public function gotoAndPlay(position:Number):void |
|---|
| 242 |
{ |
|---|
| 243 |
_position = position; |
|---|
| 244 |
play(); |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
/** |
|---|
| 248 |
* このトゥイーンの再生を指定された位置で停止します. |
|---|
| 249 |
* |
|---|
| 250 |
* @param position 再生を停止する位置 (秒) |
|---|
| 251 |
*/ |
|---|
| 252 |
public function gotoAndStop(position:Number):void |
|---|
| 253 |
{ |
|---|
| 254 |
_position = position; |
|---|
| 255 |
_tweenTarget.update(position); |
|---|
| 256 |
if ((_willTriggerFlags & 0x04) != 0) { |
|---|
| 257 |
_dispatcher.dispatchEvent(new BetweenEvent(BetweenEvent.UPDATE)); |
|---|
| 258 |
} |
|---|
| 259 |
if (_onUpdate != null) { |
|---|
| 260 |
_onUpdate.apply(null, _onUpdateParams); |
|---|
| 261 |
} |
|---|
| 262 |
stop(); |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
override public function tick(time:Number):Boolean |
|---|
| 266 |
{ |
|---|
| 267 |
var t:Number = time - _startTime; |
|---|
| 268 |
|
|---|
| 269 |
_position = t; |
|---|
| 270 |
_tweenTarget.update(t); |
|---|
| 271 |
|
|---|
| 272 |
if ((_willTriggerFlags & 0x04) != 0) { |
|---|
| 273 |
_dispatcher.dispatchEvent(new BetweenEvent(BetweenEvent.UPDATE)); |
|---|
| 274 |
} |
|---|
| 275 |
if (_onUpdate != null) { |
|---|
| 276 |
_onUpdate.apply(null, _onUpdateParams); |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
if (t >= _duration) { |
|---|
| 280 |
_position = _tweenTarget.duration; |
|---|
| 281 |
if (_stopOnComplete) { |
|---|
| 282 |
_isPlaying = false; |
|---|
| 283 |
if ((_willTriggerFlags & 0x08) != 0) { |
|---|
| 284 |
_dispatcher.dispatchEvent(new BetweenEvent(BetweenEvent.COMPLETE)); |
|---|
| 285 |
} |
|---|
| 286 |
if (_onComplete != null) { |
|---|
| 287 |
_onComplete.apply(null, _onCompleteParams); |
|---|
| 288 |
} |
|---|
| 289 |
return true; |
|---|
| 290 |
} |
|---|
| 291 |
else { |
|---|
| 292 |
if ((_willTriggerFlags & 0x08) != 0) { |
|---|
| 293 |
_dispatcher.dispatchEvent(new BetweenEvent(BetweenEvent.COMPLETE)); |
|---|
| 294 |
} |
|---|
| 295 |
if (_onComplete != null) { |
|---|
| 296 |
_onComplete.apply(null, _onCompleteParams); |
|---|
| 297 |
} |
|---|
| 298 |
_position = t - _duration; |
|---|
| 299 |
_startTime = time - _position; |
|---|
| 300 |
tick(time); |
|---|
| 301 |
} |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
return false; |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
/** |
|---|
| 308 |
* @inheritDoc |
|---|
| 309 |
*/ |
|---|
| 310 |
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void |
|---|
| 311 |
{ |
|---|
| 312 |
if (_dispatcher == null) { |
|---|
| 313 |
_dispatcher = new EventDispatcher(this); |
|---|
| 314 |
} |
|---|
| 315 |
_dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); |
|---|
| 316 |
updateWillTriggerFlags(); |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
/** |
|---|
| 320 |
* @inheritDoc |
|---|
| 321 |
*/ |
|---|
| 322 |
public function dispatchEvent(event:Event):Boolean |
|---|
| 323 |
{ |
|---|
| 324 |
if (_dispatcher != null) { |
|---|
| 325 |
return _dispatcher.dispatchEvent(event); |
|---|
| 326 |
} |
|---|
| 327 |
return false; |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
/** |
|---|
| 331 |
* @inheritDoc |
|---|
| 332 |
*/ |
|---|
| 333 |
public function hasEventListener(type:String):Boolean |
|---|
| 334 |
{ |
|---|
| 335 |
if (_dispatcher != null) { |
|---|
| 336 |
return _dispatcher.hasEventListener(type); |
|---|
| 337 |
} |
|---|
| 338 |
return false; |
|---|
| 339 |
} |
|---|
| 340 |
|
|---|
| 341 |
/** |
|---|
| 342 |
* @inheritDoc |
|---|
| 343 |
*/ |
|---|
| 344 |
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void |
|---|
| 345 |
{ |
|---|
| 346 |
if (_dispatcher != null) { |
|---|
| 347 |
_dispatcher.removeEventListener(type, listener, useCapture); |
|---|
| 348 |
updateWillTriggerFlags(); |
|---|
| 349 |
} |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
/** |
|---|
| 353 |
* @inheritDoc |
|---|
| 354 |
*/ |
|---|
| 355 |
public function willTrigger(type:String):Boolean |
|---|
| 356 |
{ |
|---|
| 357 |
if (_dispatcher != null) { |
|---|
| 358 |
return _dispatcher.willTrigger(type); |
|---|
| 359 |
} |
|---|
| 360 |
return false; |
|---|
| 361 |
} |
|---|
| 362 |
|
|---|
| 363 |
private function updateWillTriggerFlags():void |
|---|
| 364 |
{ |
|---|
| 365 |
if (_dispatcher.willTrigger(BetweenEvent.PLAY)) { |
|---|
| 366 |
_willTriggerFlags |= 0x01; |
|---|
| 367 |
} |
|---|
| 368 |
else { |
|---|
| 369 |
_willTriggerFlags &= ~0x01; |
|---|
| 370 |
} |
|---|
| 371 |
if (_dispatcher.willTrigger(BetweenEvent.STOP)) { |
|---|
| 372 |
_willTriggerFlags |= 0x02; |
|---|
| 373 |
} |
|---|
| 374 |
else { |
|---|
| 375 |
_willTriggerFlags &= ~0x02; |
|---|
| 376 |
} |
|---|
| 377 |
if (_dispatcher.willTrigger(BetweenEvent.UPDATE)) { |
|---|
| 378 |
_willTriggerFlags |= 0x04; |
|---|
| 379 |
} |
|---|
| 380 |
else { |
|---|
| 381 |
_willTriggerFlags &= ~0x04; |
|---|
| 382 |
} |
|---|
| 383 |
if (_dispatcher.willTrigger(BetweenEvent.COMPLETE)) { |
|---|
| 384 |
_willTriggerFlags |= 0x08; |
|---|
| 385 |
} |
|---|
| 386 |
else { |
|---|
| 387 |
_willTriggerFlags &= ~0x08; |
|---|
| 388 |
} |
|---|
| 389 |
} |
|---|
| 390 |
} |
|---|
| 391 |
} |
|---|