@@ -5,6 +5,7 @@ var Ellipse = require('../geom/ellipse/Ellipse');
55var EllipseContains = require ( '../geom/ellipse/Contains' ) ;
66var InputEvent = require ( '../input/events' ) ;
77var InteractiveObject = require ( '../input/InteractiveObject' ) ;
8+ var NOOP = require ( '../utils/NOOP' ) ;
89var Rectangle = require ( '../geom/rectangle/Rectangle' ) ;
910var RectangleContains = require ( '../geom/rectangle/Contains' ) ;
1011var Triangle = require ( '../geom/triangle/Triangle' ) ;
@@ -41,7 +42,7 @@ var InputManager = new Class({
4142
4243 this . _size = 0 ;
4344
44- // All list of all Interactive Objects
45+ // All list of all Game Objects that have been input enabled
4546 this . _list = [ ] ;
4647
4748 // Only those which are currently below a pointer (any pointer)
@@ -50,6 +51,8 @@ var InputManager = new Class({
5051 // Objects waiting to be inserted or removed from the active list
5152 this . _pendingInsertion = [ ] ;
5253 this . _pendingRemoval = [ ] ;
54+
55+ this . _validTypes = [ 'onDown' , 'onUp' , 'onOver' , 'onOut' ] ;
5356 } ,
5457
5558 boot : function ( )
@@ -81,6 +84,8 @@ var InputManager = new Class({
8184 if ( index > - 1 )
8285 {
8386 this . _list . splice ( index , 1 ) ;
87+
88+ gameObject . input = null ;
8489 }
8590 }
8691
@@ -217,27 +222,57 @@ var InputManager = new Class({
217222 processPointer : function ( pointer )
218223 {
219224 var i ;
225+ var gameObject ;
220226 var over = this . _over ;
221227
222228 if ( pointer . justDown )
223229 {
224230 for ( i = 0 ; i < over . length ; i ++ )
225231 {
226- this . events . dispatch ( new InputEvent . DOWN ( pointer , over [ i ] ) ) ;
232+ gameObject = over [ i ] ;
233+
234+ // Maybe this should contain ALL game objects it hit in one single event?
235+ this . events . dispatch ( new InputEvent . DOWN ( pointer , gameObject ) ) ;
236+
237+ gameObject . input . onDown ( gameObject , pointer , gameObject . input . localX , gameObject . input . localY ) ;
227238 }
228239 }
229240 else if ( pointer . justUp )
230241 {
231242 for ( i = 0 ; i < over . length ; i ++ )
232243 {
233- this . events . dispatch ( new InputEvent . UP ( pointer , over [ i ] ) ) ;
244+ gameObject = over [ i ] ;
245+
246+ this . events . dispatch ( new InputEvent . UP ( pointer , gameObject ) ) ;
247+
248+ gameObject . input . onUp ( gameObject , pointer , gameObject . input . localX , gameObject . input . localY ) ;
234249 }
235250 }
236251 } ,
237252
238- // Adds a new InteractiveObject to this Input Manager.
239- // Created automatically via methods like setHitArea, or can be created manually.
240- add : function ( child )
253+ enable : function ( gameObject , shape , callback )
254+ {
255+ if ( gameObject . input )
256+ {
257+ // If it is already has an InteractiveObject then just enable it and return
258+ gameObject . input . enabled = true ;
259+ }
260+ else
261+ {
262+ // Create an InteractiveObject and enable it
263+ this . setHitArea ( gameObject , shape , callback ) ;
264+ }
265+
266+ return this ;
267+ } ,
268+
269+ disable : function ( gameObject )
270+ {
271+ gameObject . input . enabled = false ;
272+ } ,
273+
274+ // Queues a Game Object for insertion into this Input Manager on the next update.
275+ queueForInsertion : function ( child )
241276 {
242277 if ( this . _pendingInsertion . indexOf ( child ) === - 1 && this . _list . indexOf ( child ) === - 1 )
243278 {
@@ -247,101 +282,176 @@ var InputManager = new Class({
247282 return this ;
248283 } ,
249284
250- // Removes an InteractiveObject from this Input Manager.
251- remove : function ( child )
285+ // Queues a Game Object for removal from this Input Manager on the next update .
286+ queueForRemoval : function ( child )
252287 {
253288 this . _pendingRemoval . push ( child ) ;
254289
255290 return this ;
256291 } ,
257292
258- setHitArea : function ( gameObject , shape , callback )
293+
294+ // Set Hit Areas
295+
296+ setHitArea : function ( gameObjects , shape , callback )
259297 {
260298 if ( shape === undefined )
261299 {
262- return this . setHitAreaFromTexture ( gameObject ) ;
300+ return this . setHitAreaFromTexture ( gameObjects ) ;
263301 }
264302
265- if ( Array . isArray ( gameObject ) )
303+ if ( ! Array . isArray ( gameObjects ) )
266304 {
267- for ( var i = 0 ; i < gameObject . length ; i ++ )
268- {
269- gameObject [ i ] . hitArea = shape ;
270- gameObject [ i ] . hitAreaCallback = callback ;
271-
272- this . add ( InteractiveObject ( gameObject [ i ] , shape , callback ) ) ;
273- }
305+ gameObjects = [ gameObjects ] ;
274306 }
275- else
307+
308+ for ( var i = 0 ; i < gameObjects . length ; i ++ )
276309 {
277- gameObject . hitArea = shape ;
278- gameObject . hitAreaCallback = callback ;
310+ var gameObject = gameObjects [ i ] ;
311+
312+ gameObject . input = InteractiveObject ( gameObject , shape , callback ) ;
279313
280- this . add ( InteractiveObject ( gameObject , shape , callback ) ) ;
314+ this . queueForInsertion ( gameObject ) ;
281315 }
282316
283317 return this ;
284318 } ,
285319
286- setHitAreaFromTexture : function ( gameObject , callback )
320+ setHitAreaFromTexture : function ( gameObjects , callback )
287321 {
288322 if ( callback === undefined ) { callback = RectangleContains ; }
289323
290- if ( ! Array . isArray ( gameObject ) )
324+ if ( ! Array . isArray ( gameObjects ) )
291325 {
292- gameObject = [ gameObject ] ;
326+ gameObjects = [ gameObjects ] ;
293327 }
294328
295- for ( var i = 0 ; i < gameObject . length ; i ++ )
329+ for ( var i = 0 ; i < gameObjects . length ; i ++ )
296330 {
297- var entity = gameObject [ i ] ;
331+ var gameObject = gameObjects [ i ] ;
298332
299- if ( entity . frame )
333+ if ( gameObject . frame )
300334 {
301- entity . hitArea = new Rectangle ( 0 , 0 , entity . frame . width , entity . frame . height ) ;
302- entity . hitAreaCallback = callback ;
335+ gameObject . input = InteractiveObject ( gameObject , new Rectangle ( 0 , 0 , gameObject . frame . width , gameObject . frame . height ) , callback ) ;
303336
304- this . add ( InteractiveObject ( entity , entity . hitArea , callback ) ) ;
337+ this . queueForInsertion ( gameObject ) ;
305338 }
306339 }
307340
308341 return this ;
309342 } ,
310343
311- setHitAreaRectangle : function ( gameObject , x , y , width , height , callback )
344+ setHitAreaRectangle : function ( gameObjects , x , y , width , height , callback )
312345 {
313346 if ( callback === undefined ) { callback = RectangleContains ; }
314347
315348 var shape = new Rectangle ( x , y , width , height ) ;
316349
317- return this . setHitArea ( gameObject , shape , callback ) ;
350+ return this . setHitArea ( gameObjects , shape , callback ) ;
318351 } ,
319352
320- setHitAreaCircle : function ( gameObject , x , y , radius , callback )
353+ setHitAreaCircle : function ( gameObjects , x , y , radius , callback )
321354 {
322355 if ( callback === undefined ) { callback = CircleContains ; }
323356
324357 var shape = new Circle ( x , y , radius ) ;
325358
326- return this . setHitArea ( gameObject , shape , callback ) ;
359+ return this . setHitArea ( gameObjects , shape , callback ) ;
327360 } ,
328361
329- setHitAreaEllipse : function ( gameObject , x , y , width , height , callback )
362+ setHitAreaEllipse : function ( gameObjects , x , y , width , height , callback )
330363 {
331364 if ( callback === undefined ) { callback = EllipseContains ; }
332365
333366 var shape = new Ellipse ( x , y , width , height ) ;
334367
335- return this . setHitArea ( gameObject , shape , callback ) ;
368+ return this . setHitArea ( gameObjects , shape , callback ) ;
336369 } ,
337370
338- setHitAreaTriangle : function ( gameObject , x1 , y1 , x2 , y2 , x3 , y3 , callback )
371+ setHitAreaTriangle : function ( gameObjects , x1 , y1 , x2 , y2 , x3 , y3 , callback )
339372 {
340373 if ( callback === undefined ) { callback = TriangleContains ; }
341374
342375 var shape = new Triangle ( x1 , y1 , x2 , y2 , x3 , y3 ) ;
343376
344- return this . setHitArea ( gameObject , shape , callback ) ;
377+ return this . setHitArea ( gameObjects , shape , callback ) ;
378+ } ,
379+
380+ // type = onDown, onUp, onOver, onOut
381+
382+ setOnDownCallback : function ( gameObjects , callback , context )
383+ {
384+ return this . setCallback ( gameObjects , 'onDown' , callback , context ) ;
385+ } ,
386+
387+ setOnUpCallback : function ( gameObjects , callback , context )
388+ {
389+ return this . setCallback ( gameObjects , 'onUp' , callback , context ) ;
390+ } ,
391+
392+ setOnOverCallback : function ( gameObjects , callback , context )
393+ {
394+ return this . setCallback ( gameObjects , 'onOver' , callback , context ) ;
395+ } ,
396+
397+ setOnOutCallback : function ( gameObjects , callback , context )
398+ {
399+ return this . setCallback ( gameObjects , 'onOut' , callback , context ) ;
400+ } ,
401+
402+ setCallbacks : function ( gameObjects , onDown , onUp , onOver , onOut , context )
403+ {
404+ if ( onDown )
405+ {
406+ this . setOnDownCallback ( gameObjects , onDown , context ) ;
407+ }
408+
409+ if ( onUp )
410+ {
411+ this . setOnDownCallback ( gameObjects , onUp , context ) ;
412+ }
413+
414+ if ( onOver )
415+ {
416+ this . setOnDownCallback ( gameObjects , onOver , context ) ;
417+ }
418+
419+ if ( onOut )
420+ {
421+ this . setOnDownCallback ( gameObjects , onOut , context ) ;
422+ }
423+
424+ return this ;
425+ } ,
426+
427+ setCallback : function ( gameObjects , type , callback , context )
428+ {
429+ if ( this . _validTypes . indexOf ( type ) === - 1 )
430+ {
431+ return this ;
432+ }
433+
434+ if ( ! Array . isArray ( gameObjects ) )
435+ {
436+ gameObjects = [ gameObjects ] ;
437+ }
438+
439+ for ( var i = 0 ; i < gameObjects . length ; i ++ )
440+ {
441+ var gameObject = gameObjects [ i ] ;
442+
443+ if ( gameObject . input )
444+ {
445+ gameObject . input [ type ] = callback ;
446+
447+ if ( context )
448+ {
449+ gameObject . input . callbackContext = context ;
450+ }
451+ }
452+ }
453+
454+ return this ;
345455 } ,
346456
347457 // Drag Events
0 commit comments