bitetti

ubunted_image2

Jan 9th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  
  2. {
  3.     import adobe.utils.CustomActions;
  4.     import flash.display.Bitmap;
  5.     import flash.display.BitmapData;
  6.     import flash.display.DisplayObject;
  7.     import flash.display.Graphics;
  8.     import flash.display.Shader;
  9.     import flash.display.Shape;
  10.     import flash.display.Sprite;
  11.     import flash.events.Event;
  12.     import flash.events.MouseEvent;
  13.     import flash.filters.BlurFilter;
  14.     import flash.filters.ColorMatrixFilter;
  15.     import flash.filters.ConvolutionFilter;
  16.     import flash.filters.DropShadowFilter;
  17.     import flash.filters.ShaderFilter;
  18.     import flash.geom.Matrix;
  19.     import flash.geom.Point;
  20.     import flash.geom.Rectangle;
  21.    
  22.     /**
  23.      * ...
  24.      * @author @bitetti
  25.      */
  26.     public class Ubunted extends Sprite
  27.     {
  28.         [@Embed(source = "start-here-ubuntuoriginal.jpg")]
  29.         static private const BASE:Class;
  30.        
  31.         // Biblioteca
  32.         [@Embed(source = "brilho.png")]
  33.         static private const BRILHO:Class;
  34.         // Shader copyright de http://www.dasprinzip.com/prinzipiell/2008/06/06/fisheye/
  35.         [@Embed(source = "shaders/fisheye.pbj", mimeType = 'application/octet-stream')]
  36.         private static const Fisheye: Class;
  37.        
  38.        
  39.         private var canvas:BitmapData = new BitmapData(150, 150, false, 0xffffffff );
  40.         private var image:BitmapData = new BASE().bitmapData.clone();
  41.         private var mbrilho:BitmapData = new BRILHO().bitmapData.clone();
  42.         private var bmp:BitmapData = new BitmapData(canvas.width, canvas.height, false, 0xffffffff);
  43.         private var _brilho:Shape;
  44.        
  45.         private var rectangle:Shape;
  46.         private var mat:Matrix = new Matrix;
  47.         private var sc:Number = 0.31;
  48.         private var rot:Number = 0;
  49.         private var _blur:Number = 0;
  50.         private var fisheyeShader:Shader = new Shader ( new Fisheye );
  51.         private var fisheye:ShaderFilter = new ShaderFilter( fisheyeShader );
  52.         private var _fisheye_x:Number = 0.5;
  53.         private var _fisheye_y:Number = 0.5;
  54.         private var _fisheye_rad:int = 0;
  55.         private var _nitidez:Number;
  56.        
  57.         public function Ubunted()
  58.         {
  59.             addChild( rectangle = new Shape );
  60.             addChild( _brilho = new Shape );
  61.             brilho = 0.4;
  62.             draw();
  63.            
  64.             addEventListener(MouseEvent.MOUSE_DOWN, dow );
  65.         }
  66.        
  67.        
  68.         public function set rotate(v:Number):void
  69.         {
  70.             rot = v;
  71.             draw();
  72.         }
  73.        
  74.         public function set scale(v:Number):void
  75.         {
  76.             sc = v;
  77.             draw();
  78.         }
  79.        
  80.         public function set brilho(v:Number):void
  81.         {
  82.             _brilho.alpha = v;
  83.         }
  84.        
  85.         public function set blur(v:Number):void
  86.         {
  87.             _blur = int( 32 * v );
  88.             draw();
  89.         }
  90.        
  91.         public function set fish_x(v:Number):void
  92.         {
  93.             _fisheye_x = v;
  94.             draw();
  95.         }
  96.        
  97.         public function set fish_y(v:Number):void
  98.         {
  99.             _fisheye_y = v;
  100.             draw();
  101.         }
  102.        
  103.         public function set fish_rad(v:Number):void
  104.         {
  105.             _fisheye_rad = int(400*v);
  106.             draw();
  107.         }
  108.        
  109.         public function set nitidez(v:Number):void
  110.         {
  111.             _nitidez = v;
  112.             draw();
  113.         }
  114.        
  115.         public function setupImage( dt:DisplayObject ):void
  116.         {
  117.             trace(dt.width, dt.height );
  118.             var tBmp:BitmapData = image;
  119.            
  120.             image = new BitmapData( dt.width, dt.height, false, 0xffffffff );
  121.             image.lock();
  122.             image.draw(dt);
  123.             image.unlock();
  124.            
  125.             draw();
  126.         }
  127.        
  128.         public function getCanvas():BitmapData
  129.         {
  130.             canvas.lock();
  131.             canvas.fillRect( canvas.rect, 0xffffffff );
  132.             canvas.draw(this);
  133.             canvas.unlock();
  134.            
  135.             return canvas;
  136.         }
  137.        
  138.         private function draw( e:Event = null ):void
  139.         {
  140.             mat.identity();
  141.             mat.translate( oldPM.x + pt.x, oldPM.y + pt.y);
  142.             curPM.x = mat.tx;
  143.             curPM.y = mat.ty;
  144.             //mat.tx = 81+ image.width / 2;
  145.             //mat.ty = 81+ image.height / 2;
  146.             mat.scale( sc,sc );
  147.             mat.rotate( rot );
  148.            
  149.             // filtros
  150.             bmp.lock();
  151.             bmp.fillRect( new Rectangle(0, 0, canvas.width, canvas.height), 0xffffffff );
  152.             bmp.draw(image, mat, null, null, null, true);
  153.            
  154.             if (_fisheye_rad > 0)
  155.             {
  156.                 var tmp:BitmapData = image.clone();
  157.                 tmp.lock();
  158.                 fisheyeShader.data.radius.value = [ _fisheye_rad ];
  159.                 fisheyeShader.data.center_x.value = [ int(tmp.width * _fisheye_x) ];
  160.                 fisheyeShader.data.center_y.value = [ int(tmp.height * _fisheye_y) ];
  161.                 tmp.applyFilter(tmp,tmp.rect, new Point, fisheye);
  162.                 tmp.unlock();
  163.                 bmp.draw(tmp, mat, null, null, null, true);
  164.                 //bmp.applyFilter( bmp, bmp.rect, new Point(0, 0), fisheye );
  165.             }
  166.            
  167.             if (_blur > 0)
  168.                 bmp.applyFilter( bmp, bmp.rect, new Point(0, 0), new BlurFilter( _blur, _blur, 3 ) );
  169.            
  170.             var mt:Array;
  171.             mt = new Array;
  172.             if (_nitidez > 0)
  173.             {
  174.                 mt=mt.concat([1+8*_nitidez,0,0,0,-40]);// red
  175.                 mt=mt.concat([0,1+8*_nitidez,0,0,-40]);// green
  176.                 mt=mt.concat([0,0,1+8*_nitidez,0,-40]);// blue
  177.                 mt=mt.concat([0,0,0,1,0]);// alpha
  178.                 //bmp.applyFilter(bmp, bmp.rect, new Point, new ConvolutionFilter(3,3,mt) );
  179.                 bmp.applyFilter(bmp, bmp.rect, new Point, new ColorMatrixFilter(mt) );
  180.             }
  181.                
  182.                
  183.             bmp.unlock();
  184.            
  185.             var g:Graphics = rectangle.graphics;
  186.            
  187.            
  188.            
  189.             g.clear();
  190.             g.lineStyle(0, 0xa0a0a0, 0);
  191.             //g.beginFill(0xffffff, 1);
  192.             //g.beginBitmapFill( image, mat, true, true);
  193.             g.beginBitmapFill( bmp, new Matrix, true, true);
  194.             g.drawRoundRectComplex(5, 8, 138, 138,  24, 24, 26, 0);
  195.             g.endFill();
  196.            
  197.             g = _brilho.graphics;
  198.             g.lineStyle(0, 0, 0);
  199.             g.clear();
  200.             var bMat:Matrix = new Matrix;
  201.             bMat.translate( -5, -5);
  202.             g.beginBitmapFill( mbrilho, bMat, true, true);
  203.             g.drawRoundRectComplex(5, 8, 138, 138,  24, 24, 26, 0);
  204.             g.endFill();
  205.            
  206.            
  207.             rectangle.filters = [
  208.                 new DropShadowFilter(
  209.                 0, 45, // dist , angle
  210.                 0x303030, 1, // color, alpha
  211.                 6, 6, //blur
  212.                 1, //strentch
  213.                 3, false,false, false ) // quality
  214.             ];
  215.            
  216.             //bmp.dispose();
  217.         }
  218.        
  219.        
  220.         private var pt:Point = new Point;
  221.         private var oldPM:Point = new Point;
  222.         private var curPM:Point = new Point;
  223.         private var oldX:Number = 0;
  224.         private var oldY:Number = 0;
  225.        
  226.         private function drag( e:MouseEvent ):void
  227.         {
  228.             pt.x = e.localX - oldX;
  229.             pt.y = e.localY - oldY;
  230.            
  231.             //oldX = e.stageX;
  232.             //oldY = e.stageY;
  233.            
  234.             draw();
  235.         }
  236.        
  237.         private function dow(e:MouseEvent):void
  238.         {
  239.             oldPM.x = curPM.x;
  240.             oldPM.y = curPM.y;
  241.             oldX = e.localX;
  242.             oldY = e.localY;
  243.             stage.addEventListener( MouseEvent.MOUSE_UP, up );
  244.             stage.addEventListener( MouseEvent.MOUSE_MOVE, drag );
  245.         }
  246.        
  247.         private function up(e:MouseEvent):void
  248.         {
  249.             stage.removeEventListener( MouseEvent.MOUSE_MOVE, drag );
  250.             stage.removeEventListener( MouseEvent.MOUSE_UP, up );
  251.            
  252.         }
  253.        
  254.     }
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment