bitetti

ubunted_image1

Jan 9th, 2012
127
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 com.adobe.images.JPGEncoder;
  5.     import com.adobe.images.PNGEncoder;
  6.     import flash.display.DisplayObject;
  7.     import flash.display.Sprite;
  8.     import flash.events.Event;
  9.     import flash.filters.DropShadowFilter;
  10.     import flash.utils.ByteArray;
  11.     import flash.events.IOErrorEvent;
  12.     import flash.net.FileReference;
  13.     import flash.net.FileFilter;
  14.     import flash.display.Loader;
  15.     import flash.display.LoaderInfo;
  16.     import flash.text.TextField;
  17.     import sliz.miniui.Button;
  18.     import sliz.miniui.Label;
  19.     import sliz.miniui.Silder;
  20.     import sliz.miniui.skin.ButtonCoreSkin;
  21.  
  22.     /**
  23.      * ...
  24.      * @author @bitetti
  25.      */
  26.     [Frame(factoryClass="Preloader")]
  27.     public class Main extends Sprite
  28.     {
  29.         // ui
  30.         //private var loadBot:Button;
  31.         //private var saveBot:Button;
  32.         //private var zoomBot:Silder;
  33.         //private var brilhoBot:Silder;
  34.        
  35.         private var file:FileReference;
  36.        
  37.         // tela
  38.         private var ubunted:Ubunted;
  39.        
  40.        
  41.         public function Main():void
  42.         {
  43.             if (stage) init();
  44.             else addEventListener(Event.ADDED_TO_STAGE, init);
  45.         }
  46.  
  47.         private function init(e:Event = null):void
  48.         {
  49.             removeEventListener(Event.ADDED_TO_STAGE, init);
  50.             // entry point
  51.            
  52.             file = new FileReference;
  53.             file.addEventListener(Event.SELECT, selectHandler);
  54.            
  55.             addChild( ubunted = new Ubunted );
  56.            
  57.             addChild( new Button("Carregar", 4, 154, null, preparaLoad ) );
  58.             addChild( new Button("JPEG", 70, 154, null, preparaSalvar ) );
  59.             addChild( new Button("PNG", 118, 154, null, preparaSalvar2 ) );
  60.             addChild( new Silder(148, 142, null, "", null, 100, 270, silderFunc ));
  61.             Silder(getChildAt(numChildren - 1)).value = 0.5;
  62.             addChild( new Silder(168, 142, null, "", null, 100, 270, silder2Func ));
  63.             Silder(getChildAt(numChildren - 1)).value = 0.4;
  64.            
  65.             /*addChild( new Label("Contraste", null, 4, 174) );
  66.             addChild( new Silder(64, 180, null, "", null, 100, 0, silder7Func) );*/
  67.            
  68.             addChild( new Label("Desfoque", null, 4, 174) );
  69.             addChild( new Silder(64, 180, null, "", null, 100, 0, silder3Func) );
  70.            
  71.             addChild( new Label("Fish Eye", null, 4, 194) );
  72.             addChild( new Silder(64, 200, null, "", null, 100, 0, silder4Func) );
  73.             addChild( new Silder(64, 220, null, "", null, 100, 0, silder5Func) );
  74.             Silder(getChildAt(numChildren - 1)).value = 0.5;
  75.             addChild( new Silder(64, 240, null, "", null, 100, 0, silder6Func) );
  76.             Silder(getChildAt(numChildren - 1)).value = 0.5;
  77.            
  78.             addChild( new Label("@powered by", null, 120, 256 ) );
  79.             getChildAt(numChildren - 1).scaleX = getChildAt(numChildren - 1).scaleY = 0.7;
  80.             TextField( getChildAt(numChildren - 1) ).htmlText = "<font color=\"#4040ff\"><a href=\"https://github.com/marcosbitetti/Parallax-Scroll-Web-Objects\" target=\"_new\">@powered by</a></font>";
  81.            
  82.             //zoomBot.value = 0.5;
  83.             //brilhoBot.value = 0.4;
  84.         }
  85.        
  86.         private function silderFunc( e:Event ):void
  87.         {
  88.             var sl:Silder = e.target as Silder;
  89.             ubunted.scale = 2*sl.value;
  90.         }
  91.        
  92.         private function silder2Func( e:Event ):void
  93.         {
  94.             var sl:Silder = e.target as Silder;
  95.             ubunted.brilho = sl.value;
  96.         }
  97.        
  98.         private function silder3Func( e:Event ):void
  99.         {
  100.             var sl:Silder = e.target as Silder;
  101.             ubunted.blur = sl.value;
  102.         }
  103.        
  104.         private function silder4Func( e:Event ):void
  105.         {
  106.             var sl:Silder = e.target as Silder;
  107.             ubunted.fish_rad = sl.value;
  108.         }
  109.        
  110.         private function silder5Func( e:Event ):void
  111.         {
  112.             var sl:Silder = e.target as Silder;
  113.             ubunted.fish_x = sl.value;
  114.         }
  115.        
  116.         private function silder6Func( e:Event ):void
  117.         {
  118.             var sl:Silder = e.target as Silder;
  119.             ubunted.fish_y = sl.value;
  120.         }
  121.        
  122.         private function silder7Func( e:Event ):void
  123.         {
  124.             var sl:Silder = e.target as Silder;
  125.             ubunted.nitidez = sl.value;
  126.         }
  127.        
  128.        
  129.         private function addMessage(msg:String):void
  130.         {
  131.             removeMessage();
  132.            
  133.             var shp:Sprite = new Sprite;
  134.             shp.name = "mensagem";
  135.            
  136.             shp.graphics.clear();
  137.             shp.graphics.beginFill(0xffffff, 0.83);
  138.             shp.graphics.lineStyle(1, 0x303030, 0.5);
  139.             shp.graphics.drawRoundRect(0, 0, 120, 100, 12, 12);
  140.             shp.graphics.endFill();
  141.             shp.filters = [ new DropShadowFilter(2, 2, 0x000000, 0.8, 8, 8, 1, 3, false, false, false) ];
  142.             shp.x = root.width / 2 - shp.width / 2;
  143.             shp.y = root.height / 2 - shp.height / 2;
  144.            
  145.             var tx:TextField = new TextField;
  146.             tx.wordWrap = tx.multiline = true;
  147.             tx.width = 100;
  148.             tx.height = 90;
  149.             tx.x = 10;
  150.             tx.y = 5;
  151.             tx.text = msg;
  152.             shp.addChild(tx);
  153.            
  154.             addChild(shp);
  155.         }
  156.        
  157.         private function removeMessage():void
  158.         {
  159.             var d:DisplayObject = getChildByName("mensagem");
  160.             if (d) removeChild(d);
  161.             d = null;
  162.         }
  163.        
  164.         private function preparaSalvar(e:Event):void
  165.         {
  166.             var finalData:ByteArray;
  167.            
  168.             for (var i:int = 95; i > 50; i++)
  169.             {
  170.                 addMessage("Preparando optimização...\nTeste com " + i.toString());
  171.                 var encoder:JPGEncoder = new JPGEncoder(85);
  172.                 var encodedImage:ByteArray = encoder.encode(ubunted.getCanvas() );
  173.                
  174.                 if (encodedImage.bytesAvailable < 9700)
  175.                 {
  176.                     finalData = encodedImage;
  177.                     break;
  178.                 }
  179.             }
  180.             if (finalData == null) finalData = encodedImage;
  181.            
  182.             var saveFile:FileReference = new FileReference();
  183.             saveFile.addEventListener(Event.COMPLETE, saveCompleteHandler);
  184.             saveFile.addEventListener(IOErrorEvent.IO_ERROR, saveIOErrorHandler);
  185.             saveFile.save(finalData, "avatar.jpg");
  186.             removeMessage();
  187.         }
  188.        
  189.         private function preparaSalvar2(e:Event):void
  190.         {
  191.             var encodedImage:ByteArray = PNGEncoder.encode(ubunted.getCanvas() );
  192.            
  193.             var saveFile:FileReference = new FileReference();
  194.             saveFile.addEventListener(Event.COMPLETE, saveCompleteHandler);
  195.             saveFile.addEventListener(IOErrorEvent.IO_ERROR, saveIOErrorHandler);
  196.             saveFile.save(encodedImage, "avatar.png");
  197.             removeMessage();
  198.         }
  199.        
  200.         private function saveCompleteHandler(e:Event):void
  201.         {
  202.            
  203.         }
  204.        
  205.         private function saveIOErrorHandler(e:IOErrorEvent):void
  206.         {
  207.            
  208.         }
  209.        
  210.         private function preparaLoad(e:Event):void
  211.         {
  212.             file.browse([new FileFilter("Images: (*.jpeg, *.jpg, *.gif, *.png)", "*.jpeg; *.jpg; *.gif; *.png")]);
  213.         }
  214.        
  215.         private function selectHandler( e:Event ):void
  216.         {
  217.             file.addEventListener(Event.COMPLETE, loadHandler);
  218.             file.load();
  219.         }
  220.        
  221.         private function loadHandler( e:Event ):void
  222.         {
  223.             file.removeEventListener(Event.COMPLETE, loadHandler);
  224.             var loader:Loader = new Loader();
  225.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadBytesHandler);
  226.             loader.loadBytes(file.data);
  227.         }
  228.        
  229.         private function loadBytesHandler( e:Event ):void
  230.         {
  231.             var loaderInfo:LoaderInfo = (e.target as LoaderInfo);
  232.             loaderInfo.removeEventListener(Event.COMPLETE, loadBytesHandler);
  233.            
  234.             ubunted.setupImage(loaderInfo.content);
  235.         }
  236.  
  237.     }
  238.  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment