- 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
This commit is contained in:
Ray Greenwell
2007-03-16 21:57:44 +00:00
parent 4fb21a01a9
commit 0b8e4cf22a
2 changed files with 30 additions and 72 deletions
+14 -4
View File
@@ -46,7 +46,16 @@ public class CommandButton extends Button
*/ */
public function setCommand (cmd :String, arg :Object = null) :void 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; _arg = arg;
} }
@@ -64,12 +73,13 @@ public class CommandButton extends Button
if (arg == null && toggle) { if (arg == null && toggle) {
arg = selected; arg = selected;
} }
CommandEvent.dispatch(this, _cmd, arg); CommandEvent.dispatch(this, _cmdOrFn, arg);
} }
} }
/** The command to submit when clicked. */ /** The command (String) to submit, or the function (Function) to call
protected var _cmd :String; * when clicked, */
protected var _cmdOrFn :Object;
/** The argument that accompanies our command. */ /** The argument that accompanies our command. */
protected var _arg :Object; protected var _arg :Object;
+16 -68
View File
@@ -32,6 +32,8 @@ import mx.core.Application;
import mx.core.ScrollPolicy; import mx.core.ScrollPolicy;
import mx.events.MenuEvent; import mx.events.MenuEvent;
import mx.utils.ObjectUtil;
import com.dougmccune.controls.ScrollableArrowMenu; import com.dougmccune.controls.ScrollableArrowMenu;
import com.threerings.util.CommandEvent; import com.threerings.util.CommandEvent;
@@ -121,89 +123,35 @@ public class CommandMenu extends ScrollableArrowMenu
*/ */
protected function itemClicked (event :MenuEvent) :void protected function itemClicked (event :MenuEvent) :void
{ {
var arg :Object = getItemArgument(event.item); var arg :Object = getItemProp(event.item, "arg");
var cmd :String = getItemCommand(event.item); var cmdOrFn :Object = getItemProp(event.item, "command");
var fn :Function; if (cmdOrFn == null) {
if (cmd == null) { cmdOrFn = getItemProp(event.item, "callback");
fn = getItemCallback(event.item);
} }
if (cmd != null || fn != null) { if (cmdOrFn != null) {
event.stopImmediatePropagation(); event.stopImmediatePropagation();
if (cmd != null) { CommandEvent.dispatch(mx_internal::parentDisplayObject, cmdOrFn, arg)
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);
}
}
} }
// 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. * Somewhat similar to bits in the DefaultDataDescriptor.
*/ */
protected function getItemCommand (item :Object) :String protected function getItemProp (item :Object, prop :String) :Object
{ {
try { try {
if (item is XML) { if (item is XML) {
return (item.@command as String); return String((item as XML).attribute(prop));
} else if (item is Object) { } else if (prop in item) {
return (item.command as String); return item[prop];
} }
} catch (e :Error) { } 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; return null;
} }
} }