~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to lib/ui/AnimationHolder.hx

  • 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
/* <license>
 
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
 
3
 * For full copyright and license information please refer to doc/license.txt.
 
4
 * </license> 
 
5
 */
 
6
package ui;
 
7
 
 
8
#if neko
 
9
//NEASH: animations completely unsupported for now
 
10
typedef Dictionary = Dynamic;
 
11
#elseif flash
 
12
import flash.utils.Dictionary;
 
13
#end
 
14
 
 
15
/** 
 
16
 * A common/shared implementation of a the core parts of an
 
17
 * AnimationMaster
 
18
 *
 
19
 * TODO: Technically only the AnimationManager needs to use the
 
20
 * dictionary class to hold the items, since in all other cases the
 
21
 * AnimationMaster's will actually be cleaned up and thus could use
 
22
 * a lighter class -- check performance of this first.
 
23
 */
 
24
class AnimationHolder
 
25
{
 
26
        var listeners : Dictionary;
 
27
        var toRemove : Array<Animation>;        
 
28
        var inTick : Bool;
 
29
        
 
30
        public function new()
 
31
        {
 
32
                inTick = false;
 
33
                //listeners = new Array<AMCallback>();
 
34
#if !neko
 
35
                listeners = new Dictionary( true );     //use weak references
 
36
#end
 
37
                
 
38
                toRemove = new Array<Animation>();
 
39
        }
 
40
        
 
41
        public function step( elapsed : Float )
 
42
        {
 
43
                inTick = true;
 
44
                
 
45
                var q : Iterator<Dynamic> = untyped (__keys__(listeners)).iterator();
 
46
                for( i in q )
 
47
                {
 
48
                        if( i == null )
 
49
                                continue;
 
50
                                
 
51
                        var x : Dynamic = untyped i;
 
52
                        var anim : Animation = untyped listeners[x];
 
53
                        //var anim : Animation = i;
 
54
                        if( anim == null )
 
55
                                continue;
 
56
                                
 
57
                        var rem : Bool = anim.animate( elapsed );
 
58
                        if( !rem )
 
59
                                removeListener( anim );
 
60
                }
 
61
 
 
62
                inTick = false;
 
63
                
 
64
                __cleanup();
 
65
        }
 
66
        
 
67
        public function addListener( anim : Animation, ?timeout : Null<Float> ) : Void
 
68
        {
 
69
                //TODO: timeout complete ignored now...
 
70
                untyped listeners[anim] = anim;
 
71
        }
 
72
        
 
73
        public function removeListener( anim : Animation ) : Void
 
74
        {
 
75
                toRemove.push( anim );
 
76
                if( !inTick )
 
77
                        __cleanup();
 
78
        }
 
79
        
 
80
        /*private*/ function __cleanup()
 
81
        {
 
82
                if( toRemove.length == 0 )
 
83
                        return;
 
84
                        
 
85
                while( true ) 
 
86
                {
 
87
                        var anim = toRemove.shift();
 
88
                        if( anim == null )
 
89
                                break;
 
90
                        untyped __delete__(listeners,anim);
 
91
                }
 
92
        }
 
93
#if debug
 
94
        public function traceItems()
 
95
        {
 
96
                var q : Iterator<Dynamic> = untyped (__keys__(listeners)).iterator();
 
97
                for( i in q )
 
98
                        trace( i );
 
99
        }
 
100
#end
 
101
}