11var Class = require ( '../utils/Class' ) ;
2+ var InputEvent = require ( '../input/events' ) ;
23
34var InputManager = new Class ( {
45
@@ -9,7 +10,7 @@ var InputManager = new Class({
910 // The State that owns this plugin
1011 this . state = state ;
1112
12- this . cameras = state . cameras . cameras ;
13+ this . cameras ;
1314
1415 // GlobalInputManager
1516 this . manager = game . input ;
@@ -21,11 +22,23 @@ var InputManager = new Class({
2122 this . mouse = this . manager . mouse ;
2223
2324 this . _size = 0 ;
25+
26+ // All interactive objects
2427 this . _list = [ ] ;
28+
29+ // Only those which are currently below a pointer (any pointer)
30+ this . _over = [ ] ;
31+
32+ // Objects waiting to be inserted or removed from the active list
2533 this . _pendingInsertion = [ ] ;
2634 this . _pendingRemoval = [ ] ;
2735 } ,
2836
37+ boot : function ( )
38+ {
39+ this . cameras = this . state . sys . cameras . cameras ;
40+ } ,
41+
2942 begin : function ( )
3043 {
3144 var toRemove = this . _pendingRemoval . length ;
@@ -53,13 +66,14 @@ var InputManager = new Class({
5366 }
5467 }
5568
56- // Move pending to active
69+ // Move pending to active (can swap for concat splice if we don't need anything extra here)
70+
5771 for ( i = 0 ; i < toInsert ; i ++ )
5872 {
5973 gameObject = this . _pendingInsertion [ i ] ;
6074
6175 // Swap for Input Enabled Object
62- this . _list . push ( { gameObject : gameObject , over : false , down : false , localX : 0 , localY : 0 } ) ;
76+ this . _list . push ( gameObject ) ;
6377 }
6478
6579 this . _size = this . _list . length ;
@@ -85,20 +99,64 @@ var InputManager = new Class({
8599
86100 processPointer : function ( pointer )
87101 {
88- var over = [ ] ;
102+ var i ;
103+ var tested = [ ] ;
104+ var justOut = [ ] ;
105+ var justOver = [ ] ;
106+ var stillOver = [ ] ;
89107
90108 // Returns an array of objects the pointer is over
91109
92- for ( var i = 0 ; i < this . cameras . length ; i ++ )
110+ for ( i = 0 ; i < this . cameras . length ; i ++ )
93111 {
94112 var camera = this . cameras [ i ] ;
95113
96114 if ( camera . inputEnabled )
97115 {
98- over = over . concat ( this . manager . hitTest ( this . _list , pointer . x , pointer . y , camera ) ) ;
116+ tested = tested . concat ( this . manager . hitTest ( this . _list , pointer . x , pointer . y , camera ) ) ;
117+ }
118+ }
119+
120+ for ( i = 0 ; i < tested . length ; i ++ )
121+ {
122+ var item = tested [ i ] ;
123+
124+ if ( this . _over . indexOf ( item ) !== - 1 )
125+ {
126+ stillOver . push ( item ) ;
99127 }
128+ else
129+ {
130+ justOver . push ( item ) ;
131+ }
132+ }
133+
134+ this . _over . forEach ( function ( item ) {
135+
136+ if ( tested . indexOf ( item ) === - 1 )
137+ {
138+ justOut . push ( item ) ;
139+ }
140+
141+ } ) ;
142+
143+ // Now we can process what has happened
144+ for ( i = 0 ; i < justOut . length ; i ++ )
145+ {
146+ // Dispatch event? (include the pointer)
147+ this . events . dispatch ( new InputEvent . OUT ( pointer , justOut [ i ] ) ) ;
100148 }
101149
150+ for ( i = 0 ; i < justOver . length ; i ++ )
151+ {
152+ // Dispatch event? (include the pointer)
153+ this . events . dispatch ( new InputEvent . OVER ( pointer , justOver [ i ] ) ) ;
154+ }
155+
156+ // Store everything that is currently over
157+ this . _over = stillOver . concat ( justOver ) ;
158+
159+ // console.log('tested', tested.length, 'justOver', justOver.length, 'justOut', justOut.length, 'stillOver', stillOver.length, '_over', this._over.length);
102160 } ,
103161
104162 add : function ( child )
0 commit comments