package sketchbook.generators { /** * Generatorの値をターゲットのプロパティに注入するクラス * *
以下のサンプルは、Spriteのx,y座標と2つのSineGeneratorの値をバインドします。
* * @example指定した複数の対象のプロパティに対し、Generatorの値を自動で注入します。
* * @param targets Generatorと結びつけるオブジェクトを配列で指定します。 * @param propGeneratorPair Generatorを対応するプロパティ名で格納した無名オブジェクト */ public function GeneratorBinder(targets:Array, propGeneratorPair:Object) { _propGeneratorPair = propGeneratorPair _targets = targets updateValue() } public function set propGeneratorPair(value:Object):void { _propGeneratorPair = value; updateValue() } /** Generatorを対応プロパティ名で格納した無名オブジェクト */ public function get propGeneratorPair():Object { return _propGeneratorPair } public function set targets(targest:Array):void { _targets = targets.concat() updateValue() } /** Generatorの値を注入する対象のオブジェクトの配列 */ public function get targets():Array { return _targets.concat(); } /** * Generatorの値を更新して、対象のオブジェクトに注入します。 */ public function update():void { for each(var gen:IGenerator in propGeneratorPair) gen.update(); updateValue(); } private function updateValue():void { for each(var obj:Object in targets) { for (var prop:String in propGeneratorPair) obj[prop] = IGenerator(propGeneratorPair[prop]).value } } } }