From 85052535b4e1d16f855fedeb43739ec07820739d Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Thu, 20 Jul 2006 02:14:53 +0000 Subject: [PATCH] Added a CommandMenu, which is a pop-up menu that submits configured CommandEvents for selected items. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4282 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/mx/controls/CommandMenu.as | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/as/com/threerings/mx/controls/CommandMenu.as diff --git a/src/as/com/threerings/mx/controls/CommandMenu.as b/src/as/com/threerings/mx/controls/CommandMenu.as new file mode 100644 index 000000000..2d8708246 --- /dev/null +++ b/src/as/com/threerings/mx/controls/CommandMenu.as @@ -0,0 +1,66 @@ +package com.threerings.mx.controls { + +import flash.display.DisplayObjectContainer; + +import mx.controls.Menu; +import mx.core.mx_internal; +import mx.events.MenuEvent; + +import com.threerings.mx.events.CommandEvent; + +public class CommandMenu extends Menu +{ + public function CommandMenu () + { + super(); + + iconFunction = getIconFor; + labelFunction = getLabelFor; + + addEventListener(MenuEvent.ITEM_CLICK, itemClicked); + } + + /** + * Factory method to create a command menu. + * + * @param parent The parent of this menu. + * @param items an array of menu items, where each item is another array + * with the following format: [ text, iconClass, command, arg ]. Arg is + * optional. + */ + public static function createMenu ( + parent :DisplayObjectContainer, items :Array) :CommandMenu + { + var menu :CommandMenu = new CommandMenu(); + menu.tabEnabled = false; + menu.showRoot = true; + Menu.popUpMenu(menu, parent, items); + return menu; + } + + /** + * Callback for MenuEvent.ITEM_CLICK. + */ + protected function itemClicked (event :MenuEvent) :void + { + event.stopImmediatePropagation(); + var element :Array = (event.item as Array); + mx_internal::parentDisplayObject.dispatchEvent( + new CommandEvent(String(element[2]), element[3])); + } + + // our function for retrieving a label for a menu entry + protected function getLabelFor (obj :Object) :String + { + var element :Array = (obj as Array); + return String(element[0]); + } + + // our function for retrieving an icon for a menu entry + protected function getIconFor (obj :Object) :Class + { + var element :Array = (obj as Array); + return Class(element[1]); + } +} +}