Tuesday, April 20, 2010

mx.controls.MenuBar

Recently I was working on a project where someone wanted the menu to open when went over. I thought how hard could this be there should be a hook in there to say open on mouse over but as usual I was wrong.
So I went digging and experimenting, it was interesting to see how many ways people have tried to solve this issue and talk about complicated. I am from the old school where the main design pattern is KISS (keep it simple stupid).
So I decide I could write one myself and hopefully make it less complicated, and surprisingly enough it worked out perfectly (well so far it has not been totally tested out yet).
Basically you extend mx.controls.MenuBar
In the constructor add two event listeners, set the buttomMode to true
Public function myMenuBar()
{
super;
this.buttonMode = true;
this.addEventListener(MouseEvent.MOUSE_OVER,mouseRollOverHandler);
}
The opening of the menu is done by the mouseRollOverHandler, which checks to see if the target is a MenuBarItem and if it is fires off a MouseEvent.MOUSE_DOWN, this will basically make it seem like the user clicked the menu bar.
private function mouseRollOverHandler(event:MouseEvent):void
{
     if (event.target is MenuBarItem)
     {
          event.target.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
     }
}
I hope this was of some help.

No comments:

Post a Comment