views:

101

answers:

1

I have a project that involves polling a hardware switch each update and converting its states to a custom events:

ToggleSwitchEvent.BackWardButtonDown
ToggleSwitchEvent.BackWardButtonUp
ToggleSwitchEvent.Click
etc ....

The hardware switch acts similar to a two button mouse. So I would like to be able to "inject" the custom ToggleSwitchEvent events into the Display List and have it act just as a MouseEvent does, bubbling up from the InteractionObject that the click intersects with. This would allow me to addEventListener to the display list just as I do with MouseEvents.

The trouble, as far as I understand it, is that I would need a reference to the leaf nodes of the DisplayList to insert my ToggleSwitchEvent manually. I would like to say that the switch was clicked at a certain position on screen, and have the DisplayList figure out what InteractionObject the click intersects and dispatch my ToggleSwitchEvent instead of a MouseEvent.

Is there a method to emulate the MouseEvent in AS3 but using a custom event?

+2  A: 

i do not quite understand ... do you want a custom event to bubble, or to fire MouseEvents on your own?

  1. any event will bubble as expected, if its bubbles property is set to true ...
  2. if you dispatch mouse events manually, they will also bubble, and even their stageX and stageY is calculated appropriately ...

so good luck then ... ;)


edit:

ok, well the display list will not really take care of this for you ... actually, you have two options ... start at the stage, and make your way through it, always keeping under the mouse ... or, start at the object under the mouse, get its path in the tree, and find the right InteractiveObject in its parent chain ... the latter can be done using DisplayObjectContainer::getObjectsUnderPoint ... here's the code ...

public static function dispatchEventUnderMouse(event:Event, stage:Stage, point:Point):Boolean {
 //return if stage is disabled
 if (!stage.mouseEnabled) return false;
 //find the topmost object
 var cur:DisplayObject = stage.getObjectsUnderPoint(point).pop();
 if (cur == null) cur = stage;
 //build the path
 var path:Array = [];
 while (cur != null) {
  path.push(cur);
  cur = cur.parent;
 }
 //traverse the path from the root to find the right InteractiveObject
 while (path.length > 0) {
  cur = path.pop();
  if (cur is InteractiveObject) {
   if (!(cur as InteractiveObject).mouseEnabled) {
    cur = cur.parent;
    break;
   }
   if (cur is DisplayObjectContainer) {
    if (!(cur as DisplayObjectContainer).mouseChildren) break;
   }
  }
  else {
   cur = cur.parent;
   break;     
  }
 }
 return cur.dispatchEvent(event);
}

you might actually want to inject the right coordinates into the event, but i guess you'll figure that out ... ;)


back2dos
I've updated the question
Brian Heylin
The bubbling is not the problem, more, I would like to not have to figure out what InteractionObject the ToggleButtonEvent should be dispatched from. I would like it if the DisplayList could do that for me
Brian Heylin
Thanks back2Dos, this is what I thought I would have to do. Thanks for the detailed response.
Brian Heylin