pbjをAS3に変換する for wonderfl
23時くらい
修正
未だにpbjをAS3に変換する事をしてます。
前よりは効率的になってきたかなぁ…
効率的に、無駄を少なく出来たらいいです。
でも、この出力はwonderfl用です。
>>asファイルにする必要ないな・・・
変換したasを試してみた例
http://wonderfl.net/code/3aee585fe20741a9501b84d5f1f8a7216bac5949
/**
* Copyright zahir ( http://wonderfl.net/user/zahir )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2ZjS
*/
/*
23時くらい
修正
未だにpbjをAS3に変換する事をしてます。
前よりは効率的になってきたかなぁ…
効率的に、無駄を少なく出来たらいいです。
でも、この出力はwonderfl用です。
>>asファイルにする必要ないな・・・
変換したasを試してみた例
http://wonderfl.net/code/3aee585fe20741a9501b84d5f1f8a7216bac5949
*/
package{
import flash.display.Sprite;
import flash.events.*;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.utils.ByteArray;
[SWF(width="465", height="465", backgroundColor="0xEEEEEE")]
public class Pbj2AS3 extends Sprite{
// pbjファイルのロード ・ ASファイルの保存のボタン
private var loadBtn:TxtBtn;
private var saveBtn:TxtBtn;
private var textArea:TxtArea;
private var fileRef:FileReference;
private var conv:Shader2AS;
private var loaded_flg:Boolean = false;
public function Pbj2AS3(){
loadBtn = new TxtBtn( this, "<a href='event:load'>load</a>" );
saveBtn = new TxtBtn( this, "<a href='event:save'>save</a>" );
loadBtn.addEventListener( TextEvent.LINK, onLink );
saveBtn.addEventListener( TextEvent.LINK, onLink );
textArea = new TxtArea( this, 450, 400);
textArea.x = loadBtn.x = (465 - textArea.width) *0.5;
textArea.y = 40;
saveBtn.x = loadBtn.x + loadBtn.width + 5;
loadBtn.y = saveBtn.y = 8;
textArea.text = "" +
"pbjファイルをロードしてください。\n" +
"ここにAS3に変換して表示されます(多分。\n" +
"\n";
}
private function load():void{
fileRef = new FileReference();
fileRef.addEventListener( Event.SELECT, loadSelect );
fileRef.browse( [ new FileFilter("load pbj file", "*.pbj")] );
}
private function loadSelect(e:Event):void{
fileRef.removeEventListener( Event.SELECT, loadSelect );
fileRef.addEventListener( Event.COMPLETE, loaded );
fileRef.load();
}
private function loaded(e:Event):void{
loaded_flg = true;
fileRef.removeEventListener( Event.COMPLETE, loaded );
conv = new Shader2AS(fileRef.data as ByteArray, false, true);
textArea.text = conv.toString();
}
private function save():void{
if(loaded_flg ){
fileRef = new FileReference();
fileRef.addEventListener( Event.SELECT, saveSelect );
fileRef.save( textArea.text, conv.name() + ".as" );
}
}
private function saveSelect( e:Event ):void{
fileRef.removeEventListener( Event.SELECT, saveSelect );
}
private function onLink(e:TextEvent):void{
switch(e.text){
case "load": load(); break;
case "save": save(); break;
}
}
}
}
import flash.text.TextField;
import flash.events.*;
import flash.utils.ByteArray;
import flash.display.*;
import flash.filters.BlurFilter;
import mx.utils.Base64Encoder;
import flash.filters.GlowFilter;
import flash.geom.Point;
import org.papervision3d.core.render.sort.NullSorter;
class ShaderReader{
static public const SORT_INDEX:String = "index";
static public const SORT_NAME:String = "name";
private var sd:ShaderData;
private var _bytes:ByteArray;
private var _input:Array = [];
private var _param:Array = [];
private var _name:String = "";
private var _namespace:String = "";
private var _vendor:String = "";
private var _version:String = "";
private var _description:String = "";
public function ShaderReader( bytes:ByteArray ){
sd = new Shader( bytes ).data;
_bytes = bytes;
for( var p:String in sd){
var d:* = sd[p];
if(d is ShaderParameter){
var sp:ShaderParameter = d as ShaderParameter;
var _values:Object = {};
for(var _d:String in sp) _values[_d] = (sp[_d]) ? sp[_d] : "";
_param[_param.length] = {"name":p, "index":sp.indexm, "type":sp.type, "values":_values};
}else if(d is ShaderInput){
var si:ShaderInput = d as ShaderInput;
_input[_input.length] = {"name":p, "index":si.index, "channels":si.channels};
}else{
meta(p.toLowerCase(), d);
}
}
}
private function meta( paramName:String, paramData:String):void{
switch( paramName ){
case "name": _name = paramData; break;
case "namespace": _namespace = paramData; break;
case "vendor": _vendor = paramData; break;
case "version": _version = paramData; break;
case "description": _description = paramData; break;
}
}
public function get bytes():ByteArray{ return _bytes; }
// metaData
public function get name():String{ return _name; }
public function get nameSpace():String{ return _namespace; }
public function get vendor():String{ return _vendor; }
public function get version():String{ return _version; }
public function get description():String{ return _description;}
public function getInputData( sort:String = "index" ):Array{
_input.sortOn( sort , Array.CASEINSENSITIVE);
return _input;
}
public function getParamData( sort:String = "index" ):Array{
_param.sortOn( sort , Array.CASEINSENSITIVE);
return _param;
}
}
class Shader2AS{
private var base:String = "" +
"import flash.display.Shader;\n" +
"import flash.utils.ByteArray;\n" +
"import mx.utils.Base64Decoder;\n" +
"\n" +
"class $CLASS_NAME$ extends Shader$BREAK${\n" +
"\t" + "static private var code:ByteArray;\n" +
"\n" +
"\t" + "// static initializer\n" +
"\t" + "{\n" +
"\t\t" + "static var dec:Base64Decoder = new Base64Decoder() ;\n" +
"\t\t" + "dec.decode(\n" +
"\t\t" + "\t\t\t\"$BASE64$\" );\n" +
"\t\t" + "code = dec.toByteArray();\n" +
"\t\t" + "dec = null;\n" +
"\t" + "}\n" +
"\n" +
"\t" + "public function $CLASS_NAME$()$BREAK2${\n" +
"\t\t" + "super( code );\n" +
"\t" + "}\n" +
"\n" +
"$PROPERTY$" +
"}";
private const metaString:String = "\n" +
"\t" + "public function get $META$():String$BREAK2${\n" +
"\t\t" + "return data[ \"$META$\" ];\n" +
"\t" + "}\n";
private const paramString:String = "\n" +
"\t" + "public function get $PARAM$():Array$BREAK2${\n" +
"\t\t" + "return data[ \"$PARAM$\" ].value;\n" +
"\t" + "}\n" +
"\t" + "public function set $PARAM$( value:Array ):void$BREAK2${\n" +
"\t\t" + "data[ \"$PARAM$\" ].value = value;\n" +
"\t" + "}\n";
private const inputString:String = "\n" +
"\t" + "public function $INPUT$( input:*, width:int, height:int):void$BREAK2${\n" +
"\t\t" + "data[ \"$INPUT$\" ].input = input;\n" +
"\t\t" + "data[ \"$INPUT$\" ].width = width;\n" +
"\t\t" + "data[ \"$INPUT$\" ].height = height;\n" +
"\t" + "}\n" +
"\t" + "public function get $INPUT$Channels():int$Break2${\n" +
"\t\t" + "return data[ \"$INPUT$\" ].channels;\n" +
"\t}\n"
private var _property:String = "";
private var _name:String = "";
public function Shader2AS( bytes:ByteArray ,
useBreak:Boolean = true,
useProperty:Boolean = true ){
//
var reader:ShaderReader = new ShaderReader( bytes );
_name = reader.name;
var enc:Base64Encoder = new Base64Encoder();
enc.encodeBytes( bytes );
var code:String = enc.toString();
code = code.replace( /\n/g, "\" +\n\t\t\t\t\t\"");
base = base.replace( /\$BASE64\$/ig, code);
base = base.replace( /\$CLASS_NAME\$/ig, reader.name);
if(useProperty) base = base.replace(/\$PROPERTY\$/ig, propertys( reader ) );
else base = base.replace(/\$PROPERTY\$/ig, "");
if(useBreak){
base = base.replace(/\$BREAK\$/ig, "\n");
base = base.replace(/\$BREAK2\$/ig, "\n\t");
}
else{
base = base.replace(/\$BREAK\$/ig, "");
base = base.replace(/\$BREAK[0-9]\$/ig, "");
}
}
private function propertys( reader:ShaderReader ):String{
var str:String = "";
str += metaString.replace(/\$META\$/g, "name" );
str += metaString.replace(/\$META\$/g, "nameSpace" );
str += metaString.replace(/\$META\$/g, "vendor" );
str += metaString.replace(/\$META\$/g, "version" );
str += metaString.replace(/\$META\$/g, "description" );
var arr:Array = reader.getParamData();
for(var i:int = 0, len:int = arr.length; i<len; i++)
str += paramString.replace(/\$PARAM\$/g, arr[i].name);
arr = reader.getInputData();
for(i=0,len=arr.length;i<len;i++)
str += inputString.replace(/\$INPUT\$/g, arr[i].name);
return str;
}
public function toString():String{ return base; }
public function name():String{ return _name; }
}
//簡易的なテキストエリア
class TxtArea extends Sprite{
private const SCROLL_BAR_WIDTH:int = 20;
private var t:TextField;
private var s:Shape;
private var g:Graphics;
private var gf:GlowFilter;
private var _w:int;
private var _h:int;
private var vTickness:Number;
private var vScrollBar:VScrollBar;
public function get w():int{ return _w; }
public function get h():int{ return _h; }
public function TxtArea( parent:DisplayObjectContainer, width:int, height:int ){
parent.addChild( this );
gf = new GlowFilter( 0x0099FF, 0.8, 4,4 , 2, 2);
addChild( (s = new Shape() ) );
addChild( (t = new TextField() ) );
_w = t.width = width;
_h = t.height = height;
drawFream();
addEventListener( FocusEvent.FOCUS_IN, onFocus);
t.addEventListener( Event.SCROLL, onScroll );
setBar();
}
private function setBar():void{
addChild( (vScrollBar = new VScrollBar(this, SCROLL_BAR_WIDTH, height) ));
}
public function get text():String{
return t.text;
}
public function set text(value:String):void{
t.text = value;
checkScroll();
}
public function set htmlText(value:String):void{
t.htmlText = value;
checkScroll();
}
private function checkScroll():void{
vScrollBar.thickness = vTickness =( t.height - t.height * (t.height / t.textHeight) )/ (t.maxScrollV - 1);
if( t.textHeight > t.height ){
t.width -= SCROLL_BAR_WIDTH;
vScrollBar.resize( SCROLL_BAR_WIDTH, t.height, SCROLL_BAR_WIDTH, t.textHeight );
vScrollBar.visible = true;
}else{
t.width = _w;
vScrollBar.visible = false;
}
}
private function onScroll( e:Event ):void{
vScrollBar.setSliderPos( vTickness * ( t.scrollV - 1 ) );
}
public function get scrollV():int{
return t.scrollV;
}
public function set scrollV( value:int ):void{
t.scrollV = (value < 1) ? 1: value;
}
private function onFocus( e:FocusEvent ):void{
addEventListener(FocusEvent.FOCUS_OUT, outFocus );
drawFream( 0xFFFFEE , 1, 0x0099FF);
filters = [gf];
}
private function outFocus( e:FocusEvent ):void{
removeEventListener(FocusEvent.FOCUS_OUT, outFocus );
drawFream();
filters = [];
}
private function drawFream( bgColor:uint = 0xFFFFFF , bold:int = 1, color:uint = 0x333333 ):void{
g = graphics;
g.clear();
(bold == 0) ? g.lineStyle() : g.lineStyle( bold, color );
g.beginFill( bgColor );
g.drawRect( 0,0, _w, _h );
g.endFill();
}
}
class ScrollBar extends Sprite{
protected var g:Graphics;
protected var _w:int, _h:int;
protected var bgColor:uint = 0xCCCCCC;
protected var strokeColor:uint = 0x333333;
protected var fieldWidth:int;
protected var fieldHeight:int;
protected var worldWidth:int;
protected var worldHeight:int;
protected var slider:Slider;
protected var oldPoint:Point;
public var thickness:Number = 0;
public function ScrollBar(parent:DisplayObjectContainer, w:int, h:int){
(slider = new Slider( this, w, h)).addEventListener( MouseEvent.MOUSE_DOWN, onSliderDown);
parent.addChild( this );
g = this.graphics;
resize( w, h, w,h);
}
protected function onSliderDown(e:MouseEvent):void{
oldPoint = new Point(slider.mouseX, slider.mouseY);
slider.stage.addEventListener(MouseEvent.MOUSE_MOVE, onSliderMove);
slider.stage.addEventListener(MouseEvent.MOUSE_UP, onSliderUp);
}
protected function onSliderMove(e:MouseEvent):void{
}
protected function onSliderUp(e:MouseEvent):void{
slider.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onSliderMove);
slider.stage.removeEventListener(MouseEvent.MOUSE_UP, onSliderUp);
}
public function resize( w:int, h:int, maxWidth:int, maxHeight:int):void{
setSize(w,h, maxWidth, maxHeight);
}
protected function setSize( w:int, h:int, maxWidth:int, maxHeight:int):void{
fieldWidth = _w = w;
fieldHeight = _h = h;
worldWidth = maxWidth;
worldHeight = maxHeight;
setSliderSize();
draw();
}
protected function setSliderSize():void{
var w:int, h:int;
w = _w * ( fieldWidth / worldWidth );
h = _h * ( fieldHeight / worldHeight );
slider.resize(w,h);
}
public function setSliderPos( position:Number ):void{
}
private function draw():void{
g.clear();
g.beginFill( strokeColor );
g.drawRect( 0,0, _w, _h);
g.endFill();
g.beginFill( bgColor );
g.drawRect( 1,1, _w-2, _h-2);
g.endFill();
}
}
class VScrollBar extends ScrollBar{
public function VScrollBar( parent:DisplayObjectContainer, w:int, h:int){
super(parent, w, h );
}
override public function setSliderPos(position:Number):void{
slider.y = position;
}
override protected function onSliderMove(e:MouseEvent):void{
var yy:Number = mouseY - oldPoint.y;
var __h:Number = _h - slider.height - thickness ;
yy = (yy > __h ) ? __h : (yy < 0) ? 0:yy;
var ta:TxtArea = parent as TxtArea;
slider.y = yy;
ta.scrollV = yy / thickness;
}
override protected function setSize( w:int, h:int, maxWidth:int, maxHeight:int ):void{
super.setSize( w,h, maxWidth, maxHeight);
var p:TxtArea = parent as TxtArea;
x = p.w - w;
}
}
class SpriteUI extends Sprite{
protected var mode:String = "none";
protected var g:Graphics;
protected var _w:int, _h:int;
public function SpriteUI( parent:DisplayObjectContainer ){
parent.addChild( this );
g = this.graphics;
addEventListener( MouseEvent.MOUSE_DOWN, onDown );
}
public function resize( w:int, h:int):void{
setSize(w,h);
}
protected function setSize( w:int, h:int):void{
_w = w;
_h = h;
draw();
}
protected function onDown( e:MouseEvent ):void{
stage.addEventListener( MouseEvent.MOUSE_UP, onUp );
mode = "down";
draw();
}
protected function onUp( e:MouseEvent ):void{
stage.addEventListener( MouseEvent.MOUSE_UP, onUp );
mode = "none";
draw();
}
protected function draw():void{}
}
class Slider extends SpriteUI{
public function Slider( parent:DisplayObjectContainer , w:int, h:int){
super( parent );
setSize(w,h);
}
override protected function draw():void{
var color:uint = (mode == "none") ? 0x999999 : 0x666666;
g.clear();
g.lineStyle( 0, 0x333333 );
g.beginFill( color );
g.drawRoundRect( 2,2, _w-5,_h-5, 3,3);
g.endFill()
}
}
class TxtBtn extends TextField{
public function TxtBtn(parent:DisplayObjectContainer, text:String, color:uint = 0xCCCCCC, bgColor:uint = 0x0){
autoSize = "left";
background = border = true;
backgroundColor = bgColor;
textColor = borderColor = color;
htmlText = " " + text + " ";
parent.addChild( this );
}
}