Advertisement
Guest User

Copy

a guest
Dec 5th, 2011
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package scraps
  2. {
  3.     import flash.desktop.Clipboard;
  4.     import flash.desktop.ClipboardFormats;
  5.     import flash.desktop.ClipboardTransferMode;
  6.     import flash.desktop.NativeDragActions;
  7.     import flash.desktop.NativeDragManager;
  8.     import flash.desktop.NativeDragOptions;
  9.     import flash.display.BitmapData;
  10.     import flash.display.Sprite;
  11.     import flash.events.Event;
  12.     import flash.events.FocusEvent;
  13.     import flash.events.KeyboardEvent;
  14.     import flash.events.MouseEvent;
  15.     import flash.events.NativeDragEvent;
  16.     import flash.filesystem.File;
  17.     import flash.geom.Matrix;
  18.     import flash.geom.Point;
  19.     import flash.ui.Keyboard;
  20.    
  21.     /**
  22.      * The base class for all Scrap objects.
  23.      *
  24.      * Event handlers, UI interactions, and the Scrap factory are defined here.
  25.      * Subclasses add variables to hold the transfered data and to add their
  26.      * specific data type to the Clipboard when a scrap is dragged out.
  27.      */
  28.     public class Scrap extends Sprite
  29.     {
  30.         public static const SCRAP_FORMAT:String = "SCRAP";
  31.         private static const highlightOffset:int = 6;
  32.        
  33.         //Used to manage focus
  34.         protected static var focusedScrap:Scrap;
  35.         protected static var offset:Point = new Point();
  36.        
  37.         //Calls super class constructor, then adds event listeners
  38.         public function Scrap():void{
  39.             trace("Creating scrap");
  40.             super();
  41.             this.focusRect = false;
  42.             this.mouseChildren = false;
  43.             addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown,false,0,true);
  44.             addEventListener(NativeDragEvent.NATIVE_DRAG_START,onDragStart,false,0,true);
  45.             addEventListener(NativeDragEvent.NATIVE_DRAG_COMPLETE,onComplete,false,0,true);
  46.             addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown,false,0,true);
  47.             addEventListener(FocusEvent.FOCUS_IN, grabFocus);
  48.         }
  49.  
  50.         /**
  51.          * Selects the scrap and starts the drag. The mouse must
  52.          *  move for the drag to begin.
  53.          */        
  54.         private function onMouseDown(event:MouseEvent):void{
  55.             var transferObject:Clipboard = addTransferableData();
  56.  
  57.             var allowedActions:NativeDragOptions = new NativeDragOptions();
  58.             allowedActions.allowLink = false;
  59.  
  60.             var proxyImage:BitmapData = getImage();
  61.             Scrap.offset.x = -mouseX;
  62.             Scrap.offset.y = -mouseY;
  63.  
  64.             NativeDragManager.doDrag(this, transferObject, getImage(), offset, allowedActions);
  65.  
  66.             grabFocus();
  67.             event.stopPropagation();
  68.         }        
  69.        
  70.        /**
  71.          * Fades the sprite object being dragged.
  72.          */
  73.         private function onDragStart(event:NativeDragEvent):void{
  74.             trace("Drag start event");
  75.             alpha = .25;
  76.         }
  77.        
  78.         /**
  79.          * Restores the sprite that was dragged. If the
  80.          * action was a copy, the focus is cleared from the
  81.          * original scrap.
  82.          */
  83.         private function onComplete(event:NativeDragEvent):void{
  84.             trace("Drag complete. Action set by target: " + event.dropAction);
  85.             alpha = 1;
  86.             if(event.dropAction == NativeDragActions.COPY){
  87.                 Scrap.clearFocus();
  88.             }
  89.         }
  90.        
  91.         /**
  92.          * Creates a Clipboard object and adds a flash reference.
  93.          * Subclasses override this method to add data in one of the standard
  94.          * formats.
  95.          */
  96.         protected function addTransferableData():Clipboard{
  97.             var transfer:Clipboard = new Clipboard;
  98.             transfer.setData(Scrap.SCRAP_FORMAT, this, true);
  99.             return transfer;          
  100.         }
  101.        
  102.         /**
  103.          * Creates a drag image by drawing the scrap into a BitmapData object,
  104.          * if possible.
  105.          */
  106.         public function getImage():BitmapData{
  107.             var image:BitmapData;
  108.             try {
  109.                 image = new BitmapData(width + 2*highlightOffset, height + 2*highlightOffset, true, 0x00ffffff);
  110.                 image.draw(this, new Matrix(1,0,0,1,highlightOffset,highlightOffset));
  111.             } catch (e:ArgumentError){
  112.                 image = null;
  113.             }
  114.             return image;
  115.         }
  116.  
  117.         /**
  118.          * Handles keystrokes when this scrap has focus.
  119.          * Note: shortcut keys also handled through menu key equivalents
  120.          */
  121.         private function onKeyDown(event:KeyboardEvent):void{
  122.             if(event.keyCode == Keyboard.DELETE){doDelete();}  
  123.         }
  124.                
  125.         /**
  126.          * Copies the scrap data to the clipboard.
  127.          */
  128.         public function doCopy():void{
  129.             var transferObject:Clipboard = addTransferableData();
  130.             for each( var format:String in transferObject.formats){
  131.                 Clipboard.generalClipboard.setData(format, transferObject.getData(format),true);
  132.             }
  133.         }
  134.  
  135.         /**
  136.          * Combines the copy and delete commands.
  137.          */
  138.         public function doCut():void{
  139.             doCopy();
  140.             doDelete();
  141.         }
  142.        
  143.         /**
  144.          * Replaces this scrap with the data on the clipboard.
  145.          */
  146.         public function doReplace():void{
  147.             Scrap.offset.x = -Scrap.highlightOffset;
  148.             Scrap.offset.y = -Scrap.highlightOffset;
  149.              var scrap:Scrap = Scrap.createScrap(Clipboard.generalClipboard, x, y, NativeDragActions.COPY);
  150.              stage.addChild(scrap);
  151.              doDelete();
  152.         }
  153.        
  154.         /**
  155.          * Removes the focus from this scrap, then removes the scrap from its parent.
  156.          */
  157.         public function doDelete():void{
  158.             stage.focus = stage;
  159.             parent.removeChild(this);
  160.         }
  161.        
  162.          /**
  163.           * Creates a new scrap from a Clipboard object.
  164.           *
  165.           * @param data The Clipboard object
  166.           * @param placeX The new scrap x coordinate.
  167.           * @param placeY The new scrap y coordinate.
  168.           * @param action The copy or move actions
  169.           * @return The new Scrap object.
  170.           *
  171.           */
  172.          public static function createScrap(data:Clipboard, placeX:int, placeY:int, action:String):Scrap{
  173.             var scrap:Scrap;
  174.             trace(data.formats.length + " formats found: " + " " + data.formats);
  175.             if(data.hasFormat(Scrap.SCRAP_FORMAT)){
  176.                     scrap = data.getData(Scrap.SCRAP_FORMAT, ClipboardTransferMode.ORIGINAL_ONLY) as Scrap;
  177.                 if((action == NativeDragActions.COPY) || (scrap == null)){
  178.                     var clipping:Clipboard = new Clipboard();
  179.                     for each (var format:String in data.formats){
  180.                         if(format != Scrap.SCRAP_FORMAT){
  181.                             clipping.setData(format, data.getData(format), false);
  182.                         }
  183.                     }
  184.                     scrap = Scrap.createScrap(clipping, placeX, placeY, NativeDragActions.COPY);
  185.                 }
  186.             } else if(data.hasFormat(ClipboardFormats.BITMAP_FORMAT)){
  187.                 scrap = new BitmapScrap(BitmapData(data.getData(ClipboardFormats.BITMAP_FORMAT)));
  188.                        
  189.             } else if(data.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)){
  190.                 var dropfiles:Array = data.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
  191.                 for each (var file:File in dropfiles)
  192.                 {
  193.                     scrap = new FileScrap(file);
  194.                 }
  195.             } else if(data.hasFormat(ClipboardFormats.TEXT_FORMAT)){
  196.                 scrap = new TextScrap(String(data.getData(ClipboardFormats.TEXT_FORMAT)));
  197.  
  198.             }  else if(data.hasFormat(ClipboardFormats.URL_FORMAT)){
  199.                 scrap = new HTMLScrap(String(data.getData(ClipboardFormats.URL_FORMAT)));
  200.             }
  201.             scrap.x = placeX + offset.x + highlightOffset;
  202.             scrap.y = placeY + offset.y + highlightOffset;
  203.             offset.x = 0;
  204.             offset.y = 0;
  205.             return scrap;
  206.          }
  207.          
  208.          /**
  209.           * Simple focus management.
  210.           *
  211.           * The Scrap class keeps a class variable, focusedScrap, that tracks
  212.           * which Scrap, if any, has the current focus. When a Scrap gets focus,
  213.           * it first removes focus from the current Scrap.
  214.           *
  215.           */
  216.          public function grabFocus(event:FocusEvent = null):void{
  217.             if(focusedScrap != null){
  218.                 Scrap.clearFocus();
  219.             }
  220.            
  221.             //Brings this Scrap to the top
  222.             parent.addChild(this);
  223.            
  224.             //Highlight this scrap
  225.             with (graphics){
  226.                 clear();
  227.                lineStyle(3,0,1);
  228.                beginFill(0x777777,.2);
  229.                     drawRect(-highlightOffset, -highlightOffset, width+2*highlightOffset, height+2*highlightOffset)
  230.                endFill();
  231.                
  232.             }
  233.                 focusedScrap = this;
  234.                 stage.focus = this;
  235.          }
  236.          
  237.          public static function clearFocus():void{
  238.             if(focusedScrap != null){
  239.                 focusedScrap.graphics.clear();
  240.             }
  241.          }
  242.      }
  243. }
  244.  
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement