~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to lib/ui/Events.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
/* <license>
 
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) 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
import neash.events.Event;
 
10
#elseif flash
 
11
import flash.events.Event;
 
12
import flash.events.MouseEvent;
 
13
#end
 
14
 
 
15
class Events
 
16
{
 
17
        /**
 
18
         * Use this to add global handlers since otherwise it is annoyting to try
 
19
         * and wait for the stage in an added function to add these handlers.
 
20
         */
 
21
        static public function addListener<T>( evt : String, func : T -> Void, ?priority : Null<Int> )
 
22
        {
 
23
                if( priority == null )
 
24
                        priority = 0;
 
25
#if neko
 
26
                neash.Lib.current.stage.addEventListener( evt, func,false, priority, true /*weak*/ );
 
27
#elseif flash
 
28
                flash.Lib.current.stage.addEventListener( evt, func,false, priority, true /*weak*/ );
 
29
#end
 
30
        }
 
31
        
 
32
        static public function removeListener<T>( evt : String, func : T -> Void )
 
33
        {
 
34
#if neko
 
35
                neash.Lib.current.stage.removeEventListener( evt, func );
 
36
#elseif flash
 
37
                flash.Lib.current.stage.removeEventListener( evt, func );
 
38
#end
 
39
        }
 
40
        
 
41
        static public function dispatch( evt : Event )
 
42
        {
 
43
#if neko
 
44
                neash.Lib.current.stage.dispatchEvent( evt );
 
45
#elseif flash
 
46
                flash.Lib.current.stage.dispatchEvent( evt );
 
47
#end
 
48
        }
 
49
        
 
50
        /**
 
51
         * Does the current VM support the ADDED_TO_STAGE and
 
52
         * REMOVED_FROM_STAGE events.
 
53
         */
 
54
        static public function supportsAddedToStage() : Bool
 
55
        {
 
56
#if neko
 
57
                //NEASH: unknown
 
58
                return null;
 
59
#elseif flash   
 
60
                if( _supportsAddedToStage == null )
 
61
                        _supportsAddedToStage = system.Version.checkFlashVersion( "9.0.28" );
 
62
                return _supportsAddedToStage;
 
63
#end
 
64
        }
 
65
        //cache the result since it is called often by AnimatedWidget, and perhaps others
 
66
        static var _supportsAddedToStage : Null<Bool>;
 
67
        
 
68
#if flash       
 
69
        static var captureMouseEvents = [
 
70
                MouseEvent.MOUSE_DOWN, MouseEvent.MOUSE_UP, 
 
71
                MouseEvent.CLICK, MouseEvent.DOUBLE_CLICK,
 
72
                MouseEvent.MOUSE_WHEEL,
 
73
                MouseEvent.MOUSE_MOVE, MouseEvent.MOUSE_OVER, MouseEvent.MOUSE_OUT,
 
74
                MouseEvent.ROLL_OUT, MouseEvent.ROLL_OVER
 
75
                ];
 
76
                
 
77
        /**
 
78
         * Captures all mouse events and redispatches them to the provided object.
 
79
         * This is a true capture, dispatching is then stopped in the main flow.
 
80
         *
 
81
         * NOTE: there are some potential caveats to how this works, to avoid them the
 
82
         * receiver should follow these rules:
 
83
         *              * Not dispatch an event subject to the capture
 
84
         *              * call endCapture as soon as possible
 
85
         *              * not change the display tree
 
86
         *
 
87
         * TODO: make Modal use this system, though heed warning about somethign wrong
 
88
         * with capturing some mouse events
 
89
         */
 
90
        static public function captureMouse( no : NativeObject )
 
91
        {
 
92
                capture( no, captureMouseEvents );
 
93
        }
 
94
        
 
95
        static var captureNO : NativeObject;
 
96
        static var captureEvents : Array<String>;
 
97
        static public var captureContProp : Bool; //continue propogation of the current event
 
98
        
 
99
        static function capture( no : NativeObject, events : Array<String> )
 
100
        {
 
101
                ASSERT_NULL( captureNO );
 
102
                captureNO = no;
 
103
                captureEvents = events;
 
104
                
 
105
                for( evt in captureEvents )
 
106
                        flash.Lib.current.stage.addEventListener( evt, propagateEvent, true );
 
107
                        
 
108
                //TODO: watch for object being removed and stop the capture
 
109
        }
 
110
        
 
111
        static public function endCapture( no : NativeObject )
 
112
        {
 
113
                ASSERT( no == captureNO );
 
114
                captureNO = null;
 
115
                
 
116
                for( evt in captureEvents )
 
117
                        flash.Lib.current.stage.removeEventListener( evt, propagateEvent, true );
 
118
        }
 
119
        
 
120
        //used to stop the infinite recursion caused by the redispatch of the event
 
121
        static var inCapture : Bool = false;
 
122
        static function propagateEvent( evt : Event )
 
123
        {
 
124
                if( inCapture || !captureNO.hasEventListener( evt.type ) )
 
125
                        return;
 
126
                        
 
127
                inCapture = true;
 
128
                captureContProp = false;
 
129
                captureNO.dispatchEvent( evt.clone() );
 
130
                inCapture = false;
 
131
                
 
132
                if( !captureContProp )
 
133
                        evt.stopPropagation();
 
134
        }
 
135
#end
 
136
}