ーさかさっさdrobpilCな議思不

Clipbordを試してみた。


とるすーピコてしトスーペをトスキテ
。よるなにさ逆がび並の字文
☆利便に時いたし出演を性個☆る光

▼Wonderfl
http://wonderfl.net/code/da4dafc4b4f39e786bf2cb20dd8996480e891239

▼ActionScript AS3(FP9)
[sourcecode language="as3"]
/*
* 。トステ方い使のりわまdrobpilC*
*
* とるすーピコてしトスーペをトスキテ
* 。よるなにさ逆がび並の字文
* ☆利便に時いたし出演を性個☆る光
*
* 参考
* http://log.xingxx.com/2009/03/as3eventpaste-clipboardgeneral.html
* */
package {
import flash.desktop.ClipboardFormats;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;

[SWF(backgroundColor="0xDDDDDD",width=465,height=465)]
public class Main extends Sprite {

private var _tf:TextField;
private var _bitmapData:BitmapData;
private var _sp:Sprite;
public function Main():void {
var clipboardWrapper:ClipboardWrapper = new ClipboardWrapper(this);
clipboardWrapper.onGetClipbord = onGetClipbord;
clipboardWrapper.onSetClipbord = onSetClipbord;

_tf = new TextField();
var textFormat:TextFormat = new TextFormat("_sans",24);
textFormat.align = "right";
_tf.defaultTextFormat = textFormat;
_tf.width = 454;
_tf.height = 454;
_tf.border = true;
_tf.background = true;
_tf.wordWrap = true;
_tf.backgroundColor = 0xF5F5F5;
_tf.text = "とるすーピコてしトスーペをトスキテ\r。よるなにさ逆がび並の字文\r☆利便に時いたし出演を性個☆る光";
_tf.selectable = false;

_bitmapData = new BitmapData(455,455,true,0x000000);
_bitmapData.draw(_tf);
_sp = new Sprite();
_sp.graphics.beginBitmapFill(_bitmapData,new Matrix(-1,0,0,1,455));
_sp.graphics.drawRect(0,0,455,455);
_sp.graphics.endFill();
_sp.x = 5;
_sp.y = 5;

this.addChild(_sp)
}

private function reverse(string:String):String {
var array:Array = string.split("\n");
string = "";
var n:int = array.length;
for (var i:int = 0; i < n; i++) {
var m:int = array[i].length;
var str:String = "";
for (var j:int = 0; j < m; j++) {
str += array[i].substr(m-j-1,1);
}
string += str + "\r";
}
return string;
}

private function onSetClipbord():Object {
var object:Object = { };
object[ClipboardFormats.TEXT_FORMAT] = _tf.text;
return object;
}

private function onGetClipbord(object:Object):void {
for (var str:String in object) {
trace(str+":"+object[str]);
}
if (object[ClipboardFormats.TEXT_FORMAT]) {
_tf.text = reverse(object[ClipboardFormats.TEXT_FORMAT]);
}
_bitmapData.draw(_tf);
_sp.graphics.clear();
_sp.graphics.beginBitmapFill(_bitmapData,new Matrix(-1,0,0,1,455));
_sp.graphics.drawRect(0,0,455,455);
_sp.graphics.endFill();
}
}
}

import flash.desktop.Clipboard;
import flash.events.ContextMenuEvent;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.ContextMenu;
class ClipboardWrapper {
private var _target:Object;
public var onGetClipbord:Function = function(object:Object):void { };
public var onSetClipbord:Function = function():Object { return {}};
public function ClipboardWrapper(target:Object) {
_target = target;
var contextMenu:ContextMenu = new ContextMenu();
contextMenu.clipboardMenu = contextMenu.clipboardItems.paste = true;
contextMenu.clipboardItems.copy = true;
contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, setStageFocus);
target.contextMenu = contextMenu;
target.stage.addEventListener(Event.PASTE, pasteHandler);
target.stage.addEventListener(Event.COPY, copyHandler);
target.stage.addEventListener(KeyboardEvent.KEY_DOWN, setStageFocus);
}
private function copyHandler(event:Event):void {
Clipboard.generalClipboard.clear();
var object:Object = onSetClipbord();
for (var str:String in object) {
Clipboard.generalClipboard.setData(str, object[str]);
}
}
private function pasteHandler(event:Event):void {
var n:int = Clipboard.generalClipboard.formats.length;
var result:Object = { };
for (var i:int = 0; i < n; i++) {
result[Clipboard.generalClipboard.formats[i]] = Clipboard.generalClipboard.getData(Clipboard.generalClipboard.formats[i]);
}
onGetClipbord(result);
}
private function setStageFocus(event:Event):void {
if (_target.stage.focus == _target.stage) { return };
_target.stage.focus = _target.stage;
}
}
[/sourcecode]