| 1 |
package org.threetendesign.utils |
|---|
| 2 |
{ |
|---|
| 3 |
import flash.events.Event; |
|---|
| 4 |
import flash.events.EventDispatcher; |
|---|
| 5 |
import flash.events.TimerEvent; |
|---|
| 6 |
import caurina.transitions.Tweener; |
|---|
| 7 |
|
|---|
| 8 |
/** |
|---|
| 9 |
* ... |
|---|
| 10 |
* @author Yukio Sato / 310design (http://310design.org) |
|---|
| 11 |
* @version 0.9 |
|---|
| 12 |
*/ |
|---|
| 13 |
public class Timer extends EventDispatcher |
|---|
| 14 |
{ |
|---|
| 15 |
private var _target:Object; |
|---|
| 16 |
private var _delay:Number; |
|---|
| 17 |
private var _repeatCount:int; |
|---|
| 18 |
private var _currentCount:int; |
|---|
| 19 |
|
|---|
| 20 |
/// Constructs a new Timer object with the specified delay and repeatCount states. |
|---|
| 21 |
public function Timer(delay:Number, repeatCount:int = 0) |
|---|
| 22 |
{ |
|---|
| 23 |
_delay = delay; |
|---|
| 24 |
_repeatCount = repeatCount; |
|---|
| 25 |
_target = new Object(); |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
/// The total number of times the timer has fired since it started at zero. |
|---|
| 29 |
public function get currentCount():int |
|---|
| 30 |
{ |
|---|
| 31 |
return _currentCount; |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
/// The delay, in milliseconds, between timer events. |
|---|
| 35 |
public function get delay():Number |
|---|
| 36 |
{ |
|---|
| 37 |
return _delay; |
|---|
| 38 |
} |
|---|
| 39 |
public function set delay(value:Number):void |
|---|
| 40 |
{ |
|---|
| 41 |
if (value < 0) { |
|---|
| 42 |
throw new RangeError("Error #2006: The Timer delay specified is out of range.", 2006); |
|---|
| 43 |
return; |
|---|
| 44 |
} |
|---|
| 45 |
_delay = value; |
|---|
| 46 |
if (running) { |
|---|
| 47 |
stop(); |
|---|
| 48 |
start(); |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
/// The total number of times the timer is set to run. |
|---|
| 53 |
public function get repeatCount():int |
|---|
| 54 |
{ |
|---|
| 55 |
return _repeatCount; |
|---|
| 56 |
} |
|---|
| 57 |
public function set repeatCount(value:int):void |
|---|
| 58 |
{ |
|---|
| 59 |
_repeatCount = value; |
|---|
| 60 |
if (running) { |
|---|
| 61 |
stop(); |
|---|
| 62 |
start(); |
|---|
| 63 |
} |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
/// The timer's current state; true if the timer is running, otherwise false. |
|---|
| 67 |
public function get running():Boolean |
|---|
| 68 |
{ |
|---|
| 69 |
return Tweener.isTweening(_target); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
/// Starts the timer, if it is not already running. |
|---|
| 73 |
public function start():void |
|---|
| 74 |
{ |
|---|
| 75 |
if (running) { |
|---|
| 76 |
trace("The Timer is running."); |
|---|
| 77 |
return; |
|---|
| 78 |
} |
|---|
| 79 |
var count:Number = (_repeatCount != 0) ? _repeatCount - _currentCount : uint.MAX_VALUE; |
|---|
| 80 |
if (count <= 0) { |
|---|
| 81 |
trace("repeatCount is set to a total that is the same or less than currentCount."); |
|---|
| 82 |
return; |
|---|
| 83 |
} |
|---|
| 84 |
Tweener.addCaller(_target, { time:_delay*count/1000, count:count, transition:"linear", onUpdate:countUp, onComplete:onComplete } ); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
/// Stops the timer. |
|---|
| 88 |
public function stop():void |
|---|
| 89 |
{ |
|---|
| 90 |
Tweener.removeTweens(_target); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
/// Stops the timer, if it is running, and sets the currentCount property back to 0, like the reset button of a stopwatch. |
|---|
| 94 |
public function reset():void |
|---|
| 95 |
{ |
|---|
| 96 |
stop(); |
|---|
| 97 |
_currentCount = 0; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
/// Pauses the timer. |
|---|
| 101 |
public function pause():void |
|---|
| 102 |
{ |
|---|
| 103 |
Tweener.pauseTweens(_target); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
/// Resumes the timer. |
|---|
| 107 |
public function resume():void |
|---|
| 108 |
{ |
|---|
| 109 |
Tweener.resumeTweens(_target); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
private function countUp() |
|---|
| 113 |
{ |
|---|
| 114 |
_currentCount++; |
|---|
| 115 |
dispatchEvent(new TimerEvent(TimerEvent.TIMER)); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
private function onComplete() |
|---|
| 119 |
{ |
|---|
| 120 |
dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE)); |
|---|
| 121 |
} |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|