~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to lib/ui/anim/Blinker.mhx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package ui.anim;
 
2
 
 
3
/**
 
4
 * Encapsulates the logic to manage blinking or simple multi-step logic.
 
5
 */
 
6
class Blinker
 
7
{
 
8
        static public function timed( timeout : Float )
 
9
        {
 
10
                return new Blinker( timeout );
 
11
        }
 
12
        
 
13
        public function addState( func : Void -> Void )
 
14
        {
 
15
                states.push( func );
 
16
        }
 
17
        
 
18
        /**
 
19
         * Called to indicate that time has elapsed.  IF the state has
 
20
         * changed then the appropriate state switcher will be called.
 
21
         */
 
22
        public function step( elapsed : Float )
 
23
        {
 
24
                timeout += elapsed;
 
25
                
 
26
                var newState = Math.floor( timeout / timeoutFull );
 
27
                if( newState >= states.length )
 
28
                {
 
29
                        newState = 0;
 
30
                        timeout -= states.length * timeoutFull; //ensure equal timing (overlap goes into next bit)
 
31
                }
 
32
                
 
33
                //check if we've changed and call the state changer
 
34
                if( newState != curState )
 
35
                {
 
36
                        states[newState]();
 
37
                        curState = newState;
 
38
                }
 
39
        }
 
40
        
 
41
        var timeoutFull : Float;
 
42
        var timeout : Float;
 
43
        var curState : Int;
 
44
        var states : Array<Void->Void>;
 
45
        
 
46
        function new( timeout : Float )
 
47
        {
 
48
                this.timeoutFull = timeout;
 
49
                this.timeout = 0;
 
50
                curState = -1;
 
51
                states = new Array<Void->Void>();
 
52
        }
 
53
        
 
54
}