package { import flash.display.BitmapData; import flash.display.Shape; import flash.events.Event; import flash.events.EventDispatcher; /** * Applies filter multiple functions to a bitmap as an * asynchronous operation. */ public class ApplyFilters extends EventDispatcher { // ENTER_FRAME events dispatched by a composed DisplayObject // instance; one can be used for all CaesarCipher instances private static var enterFrameDispatcher:Shape = new Shape(); private var bmp:BitmapData; // bitmap to apply filters to private var filters:Array; // list of filters to apply private var savedIndex:int = 0; // current position in sequence public function ApplyFilters() { } public function apply(bmp:BitmapData, filters:Array):void { // initialize values this.bmp = bmp; this.filters = filters.concat(); // copy incase edited externally savedIndex = 0; // set up ENTER_FRAME loop enterFrameDispatcher.addEventListener(Event.ENTER_FRAME, loopHandler, false, 0, true); // call the first iteration immediately loopHandler(null); } // ENTER_FRAME loop for asynchronous processing private function loopHandler(event:Event):void { // try applying the filter at the current // index; silently fail on errors try { filters[savedIndex](bmp); }catch(err:Error){} savedIndex++; // complete condition if (savedIndex >= filters.length){ // cleanup bmp = null; filters = null; // remove ENTER_FRAME event handler enterFrameDispatcher.removeEventListener(Event.ENTER_FRAME, loopHandler, false); // signal to listeners that processing is complete dispatchEvent(new Event(Event.COMPLETE)); } } } }