From 3e5a63c1bca63e334d8d775b7e2d0a2815b2cf13 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Fri, 8 Aug 2008 02:12:57 +0000 Subject: [PATCH] Command: contains bind() for wiring events up commands, without having to create var-args functions or make your existing functions varargs so that they can react to events. This might turn into more. I already did a bunch of impl and then KISS since we don't use the advanced shit yet. It might be nice to tie this more closely to CommandButton, et al. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5300 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/Command.as | 75 +++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/as/com/threerings/util/Command.as diff --git a/src/as/com/threerings/util/Command.as b/src/as/com/threerings/util/Command.as new file mode 100644 index 000000000..8f1c927f9 --- /dev/null +++ b/src/as/com/threerings/util/Command.as @@ -0,0 +1,75 @@ +// +// $Id$ + +package com.threerings.util { + +import flash.events.Event; +import flash.events.IEventDispatcher; + +/** + * Contains a simple binding function to bind events to commands. + * + * TODO: be able to set up a Command object, and bind it to multiple functions + */ +public class Command +{ + /** + * Bind an event to a command. + */ + public static function bind ( + source :IEventDispatcher, eventType :String, cmdOrFn :Object, arg :Object = null) :void + { + source.addEventListener(eventType, function (... ignored) :void { + CommandEvent.dispatch(source, cmdOrFn, arg); + }); + } + + /** + * Convenience, since otherwise nobody usually needs to ever import CommandEvent. + */ + public static function dispatch ( + source :IEventDispatcher, cmdOrFn :Object, arg :Object = null) :void + { + CommandEvent.dispatch(source, cmdOrFn, arg); + } + +// public static function bind ( +// source :IEventDispatcher, eventType :String, cmdOrFn :Object, arg :Object = null) :Command +// { +// var cmd :Command = new Command(cmdOrFn, arg); +// cmd.addBind(source, eventType); +// return cmd; +// } +// +// public function Command (cmdOrFn :Object, arg :Object = null) +// { +// _cmdOrFn = cmdOrFn; +// _arg = arg; +// } +// +// public function setOverrideSource (disp :IEventDispatcher) :void +// { +// _source = source; +// } +// +// public function addBind (source :IEventDispatcher, eventType :String) :void +// { +// source.addEventListener(eventType, eventHandler); +// } +// +// public function removeBind (source :IEventDispatcher, eventType :String) :void +// { +// source.removeEventListener(eventType, eventHandler); +// } +// +// protected function eventHandler (event :Event) :void +// { +// dispatch(_source || IEventDispatcher(event.source), _cmdOrFn, arg); +// } +// +// /** An override source for the command events, or null to use the triggering dispatcher. */ +// protected var _source :IEventDispatcher; +// protected var _cmdOrFn :Object; +// protected var _arg :Object; +} +}