From 0b8e4cf22a6f77b3d2ca7b0fc1eed5ee91010489 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Fri, 16 Mar 2007 21:57:44 +0000 Subject: [PATCH] - Moved CommandMenu's callback handling to CommandEvent. - Allow CommandButtons to be set up with callback too. That's much easier than adding a mouse listener, bla bla. - Cleaned up CommandMenu's grabbing of menu properties. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@172 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/as/com/threerings/flex/CommandButton.as | 18 ++++- src/as/com/threerings/flex/CommandMenu.as | 84 ++++----------------- 2 files changed, 30 insertions(+), 72 deletions(-) diff --git a/src/as/com/threerings/flex/CommandButton.as b/src/as/com/threerings/flex/CommandButton.as index 32f2afcd..eff0dd97 100644 --- a/src/as/com/threerings/flex/CommandButton.as +++ b/src/as/com/threerings/flex/CommandButton.as @@ -46,7 +46,16 @@ public class CommandButton extends Button */ public function setCommand (cmd :String, arg :Object = null) :void { - _cmd = cmd; + _cmdOrFn = cmd; + _arg = arg; + } + + /** + * Set a function to call when the button is pressed. + */ + public function setFunction (fn :Function, arg :Object = null) :void + { + _cmdOrFn = fn; _arg = arg; } @@ -64,12 +73,13 @@ public class CommandButton extends Button if (arg == null && toggle) { arg = selected; } - CommandEvent.dispatch(this, _cmd, arg); + CommandEvent.dispatch(this, _cmdOrFn, arg); } } - /** The command to submit when clicked. */ - protected var _cmd :String; + /** The command (String) to submit, or the function (Function) to call + * when clicked, */ + protected var _cmdOrFn :Object; /** The argument that accompanies our command. */ protected var _arg :Object; diff --git a/src/as/com/threerings/flex/CommandMenu.as b/src/as/com/threerings/flex/CommandMenu.as index 4f7a7a00..05d9b8d7 100644 --- a/src/as/com/threerings/flex/CommandMenu.as +++ b/src/as/com/threerings/flex/CommandMenu.as @@ -32,6 +32,8 @@ import mx.core.Application; import mx.core.ScrollPolicy; import mx.events.MenuEvent; +import mx.utils.ObjectUtil; + import com.dougmccune.controls.ScrollableArrowMenu; import com.threerings.util.CommandEvent; @@ -121,89 +123,35 @@ public class CommandMenu extends ScrollableArrowMenu */ protected function itemClicked (event :MenuEvent) :void { - var arg :Object = getItemArgument(event.item); - var cmd :String = getItemCommand(event.item); - var fn :Function; - if (cmd == null) { - fn = getItemCallback(event.item); + var arg :Object = getItemProp(event.item, "arg"); + var cmdOrFn :Object = getItemProp(event.item, "command"); + if (cmdOrFn == null) { + cmdOrFn = getItemProp(event.item, "callback"); } - if (cmd != null || fn != null) { + if (cmdOrFn != null) { event.stopImmediatePropagation(); - if (cmd != null) { - CommandEvent.dispatch(mx_internal::parentDisplayObject, cmd, arg); - - } else { - try { - var args :Array = (arg as Array); - if (args == null && arg != null) { - args = [ arg ]; - } - fn.apply(null, args); - - } catch (err :Error) { - Log.getLog(this).warning("Unable to call menu callback: " + - event.item); - } - } + CommandEvent.dispatch(mx_internal::parentDisplayObject, cmdOrFn, arg) } + // else: no warning. There may be non-command menu items mixed in. } /** - * Get the command for the specified item, if any. + * Get the specified property for the specified item, if any. * Somewhat similar to bits in the DefaultDataDescriptor. */ - protected function getItemCommand (item :Object) :String + protected function getItemProp (item :Object, prop :String) :Object { try { if (item is XML) { - return (item.@command as String); + return String((item as XML).attribute(prop)); - } else if (item is Object) { - return (item.command as String); + } else if (prop in item) { + return item[prop]; } + } catch (e :Error) { - // fall through + // alas; fall through } - - return null; - } - - /** - * Get the callback function for the specified item, if any. - */ - protected function getItemCallback (item :Object) :Function - { - try { - if (item is XML) { - return (item.@callback as Function); - - } else if (item is Object) { - return (item.callback as Function); - } - } catch (e :Error) { - // fall through - } - - return null; - } - - /** - * Get the command for the specified item, if any. - * Somewhat similar to bits in the DefaultDataDescriptor. - */ - protected function getItemArgument (item :Object) :Object - { - try { - if (item is XML) { - return item.@arg; - - } else if (item is Object) { - return item.arg; - } - } catch (e :Error) { - // fall through - } - return null; } }