topical media & game development

talk show tell print

#graphic-flex-image-effects-05-Flex-DesaturateWithProxyTest.ax

#graphic-flex-image-effects-05-Flex-DesaturateWithProxyTest.ax [swf] [flash] flex


  package {
  
          import flash.display.Shader;
          import flash.filters.ShaderFilter;
          import flash.events.Event;
          import flash.events.MouseEvent;
  
          [SWF(width=500, height=500, backgroundColor=0x000000)]
  
          
Demonstrates use of a custom shader as a bitmap filter, showing how to use custom ShaderProxy to either load or embed shader bytecode. In addition, this class responds to user interaction in order to update and reapply the shader with new settings.

  
          public class @ax-graphic-flex-image-effects-05-Flex-DesaturateWithProxyTest extends graphic_flex_image_effects_05_Flex_AbstractImageLoader {
  
  //                [Embed(source='/source/desaturate.pbj', mimeType='application/octet-stream')]
  //                private static const DesaturateKernel:Class;
  
                  private var _shaderProxy:graphic_flex_image_effects_05_Flex_ShaderProxy;
  
                  
Constructor. Passes path of image to load to super class.

  
                  public function @ax-graphic-flex-image-effects-05-Flex-DesaturateWithProxyTest() {
                          super("graphic-flex-image-effects-05-assets-butterflies.jpg");
                  }
  
                  
Method that is called once image loads in super class. This sets up loading of shader, or applies shader if bytes have been embedded.

  
                  override protected function runPostImageLoad():void {
                          // If you are using the Flex SDK, you can uncomment the Embed metatag above 
                          // and the following line, removing the other ShaderWrapper instantiation that follows,
                          // in order to draw with the shader immediately and bypass loading
                          //_shaderProxy = new graphic_flex_image_effects_05_Flex_ShaderProxy(DesaturateKernel);
                          _shaderProxy = new graphic_flex_image_effects_05_Flex_ShaderProxy("graphic-flex-image-effects-05-assets-desaturate.pbj");
                          if (_shaderProxy.shader == null) {
                                  _shaderProxy.addEventListener(Event.COMPLETE, onShaderLoaded);
                          } else {
                                  applyShader();
                          }
                  }
  
                  
Applies shader through call to setDesaturationFilter() based on current mouse position, and adds loaded image to stage. A listener is set up for when the mouse moves.

  
                  private function applyShader():void {
                          addChild(_loadedBitmap);
                          setDesaturationFilter();
                          stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                  }
  
                  
Sets properties on shader based on current mouse position and applies it to the loaded image.

  
                  private function setDesaturationFilter():void {
                          var percent:Number = stage.mouseX/stage.stageWidth;
                          if (_shaderProxy.percent != percent) {
                                  _shaderProxy.percent = percent;
                                  var shader:Shader = _shaderProxy.shader;
                                  var filter:ShaderFilter = new ShaderFilter(shader);
                                  _loadedBitmap.filters = [filter];
                          }
                  }
  
                  
Handler for when the mouse moves. Calls setDesaturationFilter() to reapply shader.
parameter: event Event dispatched by Stage.

  
                  private function onStageMouseMove(event:MouseEvent):void {
                          setDesaturationFilter();
                  }
  
                  
Handler for when the shader completes loading. Calls applyShader().
parameter: event Event dispatched by ShaderProxy.

  
                  private function onShaderLoaded(event:Event):void {
                          applyShader();
                  }
  
          }
  
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.