Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scraps
- {
- import flash.desktop.Clipboard;
- import flash.desktop.ClipboardFormats;
- import flash.desktop.ClipboardTransferMode;
- import flash.desktop.NativeDragActions;
- import flash.desktop.NativeDragManager;
- import flash.desktop.NativeDragOptions;
- import flash.display.BitmapData;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.FocusEvent;
- import flash.events.KeyboardEvent;
- import flash.events.MouseEvent;
- import flash.events.NativeDragEvent;
- import flash.filesystem.File;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import flash.ui.Keyboard;
- /**
- * The base class for all Scrap objects.
- *
- * Event handlers, UI interactions, and the Scrap factory are defined here.
- * Subclasses add variables to hold the transfered data and to add their
- * specific data type to the Clipboard when a scrap is dragged out.
- */
- public class Scrap extends Sprite
- {
- public static const SCRAP_FORMAT:String = "SCRAP";
- private static const highlightOffset:int = 6;
- //Used to manage focus
- protected static var focusedScrap:Scrap;
- protected static var offset:Point = new Point();
- //Calls super class constructor, then adds event listeners
- public function Scrap():void{
- trace("Creating scrap");
- super();
- this.focusRect = false;
- this.mouseChildren = false;
- addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown,false,0,true);
- addEventListener(NativeDragEvent.NATIVE_DRAG_START,onDragStart,false,0,true);
- addEventListener(NativeDragEvent.NATIVE_DRAG_COMPLETE,onComplete,false,0,true);
- addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown,false,0,true);
- addEventListener(FocusEvent.FOCUS_IN, grabFocus);
- }
- /**
- * Selects the scrap and starts the drag. The mouse must
- * move for the drag to begin.
- */
- private function onMouseDown(event:MouseEvent):void{
- var transferObject:Clipboard = addTransferableData();
- var allowedActions:NativeDragOptions = new NativeDragOptions();
- allowedActions.allowLink = false;
- var proxyImage:BitmapData = getImage();
- Scrap.offset.x = -mouseX;
- Scrap.offset.y = -mouseY;
- NativeDragManager.doDrag(this, transferObject, getImage(), offset, allowedActions);
- grabFocus();
- event.stopPropagation();
- }
- /**
- * Fades the sprite object being dragged.
- */
- private function onDragStart(event:NativeDragEvent):void{
- trace("Drag start event");
- alpha = .25;
- }
- /**
- * Restores the sprite that was dragged. If the
- * action was a copy, the focus is cleared from the
- * original scrap.
- */
- private function onComplete(event:NativeDragEvent):void{
- trace("Drag complete. Action set by target: " + event.dropAction);
- alpha = 1;
- if(event.dropAction == NativeDragActions.COPY){
- Scrap.clearFocus();
- }
- }
- /**
- * Creates a Clipboard object and adds a flash reference.
- * Subclasses override this method to add data in one of the standard
- * formats.
- */
- protected function addTransferableData():Clipboard{
- var transfer:Clipboard = new Clipboard;
- transfer.setData(Scrap.SCRAP_FORMAT, this, true);
- return transfer;
- }
- /**
- * Creates a drag image by drawing the scrap into a BitmapData object,
- * if possible.
- */
- public function getImage():BitmapData{
- var image:BitmapData;
- try {
- image = new BitmapData(width + 2*highlightOffset, height + 2*highlightOffset, true, 0x00ffffff);
- image.draw(this, new Matrix(1,0,0,1,highlightOffset,highlightOffset));
- } catch (e:ArgumentError){
- image = null;
- }
- return image;
- }
- /**
- * Handles keystrokes when this scrap has focus.
- * Note: shortcut keys also handled through menu key equivalents
- */
- private function onKeyDown(event:KeyboardEvent):void{
- if(event.keyCode == Keyboard.DELETE){doDelete();}
- }
- /**
- * Copies the scrap data to the clipboard.
- */
- public function doCopy():void{
- var transferObject:Clipboard = addTransferableData();
- for each( var format:String in transferObject.formats){
- Clipboard.generalClipboard.setData(format, transferObject.getData(format),true);
- }
- }
- /**
- * Combines the copy and delete commands.
- */
- public function doCut():void{
- doCopy();
- doDelete();
- }
- /**
- * Replaces this scrap with the data on the clipboard.
- */
- public function doReplace():void{
- Scrap.offset.x = -Scrap.highlightOffset;
- Scrap.offset.y = -Scrap.highlightOffset;
- var scrap:Scrap = Scrap.createScrap(Clipboard.generalClipboard, x, y, NativeDragActions.COPY);
- stage.addChild(scrap);
- doDelete();
- }
- /**
- * Removes the focus from this scrap, then removes the scrap from its parent.
- */
- public function doDelete():void{
- stage.focus = stage;
- parent.removeChild(this);
- }
- /**
- * Creates a new scrap from a Clipboard object.
- *
- * @param data The Clipboard object
- * @param placeX The new scrap x coordinate.
- * @param placeY The new scrap y coordinate.
- * @param action The copy or move actions
- * @return The new Scrap object.
- *
- */
- public static function createScrap(data:Clipboard, placeX:int, placeY:int, action:String):Scrap{
- var scrap:Scrap;
- trace(data.formats.length + " formats found: " + " " + data.formats);
- if(data.hasFormat(Scrap.SCRAP_FORMAT)){
- scrap = data.getData(Scrap.SCRAP_FORMAT, ClipboardTransferMode.ORIGINAL_ONLY) as Scrap;
- if((action == NativeDragActions.COPY) || (scrap == null)){
- var clipping:Clipboard = new Clipboard();
- for each (var format:String in data.formats){
- if(format != Scrap.SCRAP_FORMAT){
- clipping.setData(format, data.getData(format), false);
- }
- }
- scrap = Scrap.createScrap(clipping, placeX, placeY, NativeDragActions.COPY);
- }
- } else if(data.hasFormat(ClipboardFormats.BITMAP_FORMAT)){
- scrap = new BitmapScrap(BitmapData(data.getData(ClipboardFormats.BITMAP_FORMAT)));
- } else if(data.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)){
- var dropfiles:Array = data.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
- for each (var file:File in dropfiles)
- {
- scrap = new FileScrap(file);
- }
- } else if(data.hasFormat(ClipboardFormats.TEXT_FORMAT)){
- scrap = new TextScrap(String(data.getData(ClipboardFormats.TEXT_FORMAT)));
- } else if(data.hasFormat(ClipboardFormats.URL_FORMAT)){
- scrap = new HTMLScrap(String(data.getData(ClipboardFormats.URL_FORMAT)));
- }
- scrap.x = placeX + offset.x + highlightOffset;
- scrap.y = placeY + offset.y + highlightOffset;
- offset.x = 0;
- offset.y = 0;
- return scrap;
- }
- /**
- * Simple focus management.
- *
- * The Scrap class keeps a class variable, focusedScrap, that tracks
- * which Scrap, if any, has the current focus. When a Scrap gets focus,
- * it first removes focus from the current Scrap.
- *
- */
- public function grabFocus(event:FocusEvent = null):void{
- if(focusedScrap != null){
- Scrap.clearFocus();
- }
- //Brings this Scrap to the top
- parent.addChild(this);
- //Highlight this scrap
- with (graphics){
- clear();
- lineStyle(3,0,1);
- beginFill(0x777777,.2);
- drawRect(-highlightOffset, -highlightOffset, width+2*highlightOffset, height+2*highlightOffset)
- endFill();
- }
- focusedScrap = this;
- stage.focus = this;
- }
- public static function clearFocus():void{
- if(focusedScrap != null){
- focusedScrap.graphics.clear();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement