Made my CommandButton factory method the constructor.

I had initially been hesitant to do this because of the parameter that
can be one of two things, and the Java side of me wanted to hold
on to compile-time checking, but the convenience is too damn convenient.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@408 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Ray Greenwell
2008-02-06 01:03:55 +00:00
parent 1fb449b30b
commit a6e1fbe468
3 changed files with 29 additions and 30 deletions
+11 -25
View File
@@ -34,37 +34,23 @@ import com.threerings.util.CommandEvent;
public class CommandButton extends Button
{
/**
* A convenient factory method for creating CommandButtons.
* Create a command button.
*
* Usage: addChild(CommandButton.create("OK", function () :void {
* trace("ok clicked");
* }));
* @param label the label text for the button.
* @param cmdOrFn either a String, which will be the CommandEvent command to dispatch,
* or a function, which will be called when clicked.
* @param arg the argument for the CommentEvent or the function. If the arg is an Array
* then those parameters are used for calling the function.
*/
public static function create (
label :String, cmdOrFn :* = null, arg :Object = null) :CommandButton
public function CommandButton (label :String = null, cmdOrFn :* = null, arg :Object = null)
{
var cb :CommandButton = new CommandButton();
cb.label = label;
if (cmdOrFn is Function) {
cb.setCallback(cmdOrFn as Function, arg)
} else if (cmdOrFn is String) {
cb.setCommand(String(cmdOrFn), arg);
} else if (cmdOrFn != null) {
if (cmdOrFn != null && !(cmdOrFn is String) && !(cmdOrFn is Function)) {
// runtime errors suck, but this is actionscript
throw new Error("cmdOrFn must be a String or Function.");
}
return cb;
}
/**
* Create a command button.
*/
public function CommandButton (cmd :String = null, arg :Object = null)
{
setCommand(cmd, arg);
this.label = label;
_cmdOrFn = cmdOrFn;
_arg = arg;
buttonMode = true;
}