UV付き3DモデルをBase64で埋め込んだasソースの動的生成&ローカル保存
package
{
import flash.net.*;
import flash.system.*;
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import org.papervision3d.view.*;
import org.papervision3d.objects.*;
import org.papervision3d.materials.*;
import flash.text.*;
import org.papervision3d.events.FileLoadEvent;
import caurina.transitions.Tweener;
import flash.utils.ByteArray;
import mx.utils.*;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="25")]
public class Main extends Sprite
{
private var pos:int = 0;
private var src:TextField;
private var in_arr:Array=new Array();
private var sel_arr:Array = new Array();
public var ss:Array = ["",""];
public function Main()
{
var carr:Array = [ { text:"Text", val:"3D文字", width:90,max:7,rest:null }, { text:"Depth", val:2, width:35,max:3,rest:"0-9." }, { text:"Bevel", val:0, width:35,max:3,rest:"0-9." } ];
var barr:Array = [{text:"Encode",val:Btn_Press,width:80},{text:"Save",val:save_Press,width:50}];
var shp:Shape = new Shape();
shp.graphics.beginFill(0xeeeeee);
shp.graphics.drawRoundRect(0,0,stage.stageWidth,30,9);
shp.graphics.endFill();
addChild(shp);
var left:int = 0;
for (var k:int = 0; k < carr.length; k++ ) {
var label:TextField = new TextField();
label.y = 5; label.x = left + 5;
label.selectable = false;
label.autoSize = TextFieldAutoSize.LEFT;
label.htmlText = "<b>"+carr[k].text+"</b>";
addChild(label);
var inp:TextField= new TextField();
inp.y = 5;
inp.x = label.textWidth+label.x+8;
inp.width =carr[k].width;
inp.text = carr[k].val;
inp.border = true;
inp.height = 20;
left = inp.x + carr[k].width;
inp.type = TextFieldType.INPUT;
inp.selectable = true;
inp.maxChars = carr[k].max;
inp.restrict = carr[k].rest;
addChild(inp);
in_arr.push(inp);
}
//テキスト入力
src = new TextField();
src.width = 455
src.height = 400;
src.multiline = true;
var fmt:TextFormat = new TextFormat();
fmt.size = 11;
src.setTextFormat(fmt);
src.defaultTextFormat = fmt;
src.border = src.background = true;
src.backgroundColor = 0xffffff;
src.type = TextFieldType.INPUT;
src.wordWrap=true
src.x = 1;
src.y = 60;
addChild(src);
left += 15;
for (var l:int = 0; l < barr.length; l++ ) {
var btn:Sprite = new Sprite();
btn.buttonMode = true;
btn.y = 5;btn.x = left;
btn.graphics.beginFill(0xcccccc);
btn.graphics.lineStyle(1, 0x888888);
btn.graphics.drawRect(0,0,barr[l].width,20);
btn.graphics.endFill();
btn.mouseChildren = false;
var label2:TextField = new TextField();
label2.width = barr[l].width;
label2.htmlText = "<p align='center'>"+barr[l].text+"</p>";
btn.addChild(label2);
left = btn.x + barr[l].width+7;left+=(l==0)?10:0
addChild(btn);
btn.addEventListener(MouseEvent.CLICK,barr[l].val);
}
var pl:Sprite = new Sprite();pl.x = 5;pl.y = 32;
pl.buttonMode = true;
addChild(pl);
var ld:Loader = new Loader();
ld.load(new URLRequest("http://www.3dtext.in/image/tool.png"));
pl.addChild(ld);
sel_arr.push(new Sprite());sel_arr.push(new Sprite());
pl.addChild(sel_arr[0]); pl.addChild(sel_arr[1]);
pl.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
pl.addEventListener(MouseEvent.ROLL_OUT, function():void{sel_arr[0].graphics.clear();});
pl.addEventListener(MouseEvent.CLICK, mouseClickHandler);
select_image(sel_arr[1],pos,true);
code_create(0);
}
private function code_create(a:int):void
{
var d:Number = Number(in_arr[1].text); if (d > 5) { d = 5; in_arr[1].text = d;}
var b:Number = Number(in_arr[2].text); if (b > 5){b = 5; in_arr[2].text = b;}
var val:URLVariables = new URLVariables();
val.text=in_arr[0].text;//作成する立体文字
val.depth=d; //文字の厚さ
val.bevel=b; //文字べベル
val.font = "ipagp"; //フォント名
for (var k:int = a; k < 2;k++ ){
var req:URLRequest = new URLRequest((k==0)?"http://www.3dtext.in/api/for_wonderfl_demo_key":"http://www.3dtext.in/image/"+pos+".jpg");
if(k==0)req.data=val;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, (k==0)?txt_load_complete:bmp_load_complete);
loader.load(req);
}
}
private function txt_load_complete(event:Event):void { set_str(event.target.data, 0);}
private function bmp_load_complete(event:Event):void { set_str(event.target.data, 1); }
private function set_str(data:ByteArray,a:int):void {
var base64encoder:Base64Encoder = new Base64Encoder();
base64encoder.encodeBytes(data);
ss[a]= "";
var str2:String = base64encoder.toString();
var len:uint = str2.length;
for (var k:uint = 0; k < len; k++)
{
var sub:String = str2.substr(k, 1);
if (sub == "\n")
continue;
ss[a] += sub;
}
set_src(ss);
}
private function set_src(ss:Array):void {
var script:String = "cGFja2FnZSB7CmltcG9ydCBmbGFzaC5ldmVudHMuKjsKaW1wb3J0IGZsYXNoLmdlb20uKjsKaW1wb3J0IGZsYXNoLmRpc3BsYXkuKjsKaW1wb3J0IGZsYXNoLnV0aWxzLio7CmltcG9ydCBteC51dGlscy4qOwppbXBvcnQgb3JnLnBhcGVydmlzaW9uM2QuY29yZS5nZW9tLiogCmltcG9ydCBvcmcucGFwZXJ2aXNpb24zZC5jb3JlLmdlb20ucmVuZGVyYWJsZXMuKjsKaW1wb3J0IG9yZy5wYXBlcnZpc2lvbjNkLmNvcmUubWF0aC4qOwppbXBvcnQgb3JnLnBhcGVydmlzaW9uM2QuY29yZS5wcm90by5NYXRlcmlhbE9iamVjdDNECmltcG9ydCBvcmcucGFwZXJ2aXNpb24zZC5vYmplY3RzLkRpc3BsYXlPYmplY3QzRDsKaW1wb3J0IG9yZy5wYXBlcnZpc2lvbjNkLm1hdGVyaWFscy4qOwppbXBvcnQgb3JnLnBhcGVydmlzaW9uM2Qudmlldy4qOwoKW1NXRih3aWR0aCA9ICI0NjUiLCBoZWlnaHQgPSAiNDY1IiwgZnJhbWVSYXRlID0gIjYwIiwgYmFja2dyb3VuZENvbG9yID0gIjB4MDAwMDAwIildCnB1YmxpYyBjbGFzcyBNYWluIGV4dGVuZHMgQmFzaWNWaWV3IHsKCXByaXZhdGUgdmFyIG1vdXNlRG93bjogQm9vbGVhbiA9IGZhbHNlOwoJcHJpdmF0ZSB2YXIgcHQ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CiAgICAgICAgdmFyIGJ5dGVBcnJheTpCeXRlQXJyYXk7CiAgICAgICAgdmFyIGJhc2U2NERlY29kZXI6QmFzZTY0RGVjb2RlcjsKICAgICAgICAKICAgICAgICBiYXNlNjREZWNvZGVyID0gbmV3IEJhc2U2NERlY29kZXIoKTsKICAgICAgICBiYXNlNjREZWNvZGVyLmRlY29kZShkYXRhKTsKICAgICAgICAKICAgICAgICB0cnkgewogICAgICAgICAgICBieXRlQXJyYXkgPSBiYXNlNjREZWNvZGVyLnRvQnl0ZUFycmF5KCk7CiAgICAgICAgICAgIGJ5dGVBcnJheS5wb3NpdGlvbiA9IDA7CiAgICAgICAgfSBjYXRjaCAoZTpFcnJvcikgewogICAgICAgICAgICByZXR1cm4gbnVsbDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ5dGVBcnJheTsKICAgIH0KfQ==";
var dec:Base64Decoder = new Base64Decoder();
dec.decode(script);
var byteArray:ByteArray = dec.toByteArray();
byteArray.position = 0;
var e:String = byteArray.readUTFBytes(byteArray.length)
src.text = e.substring(0, e.indexOf('_DATA_')) + ss[0] + e.substring(e.indexOf('_DATA_') + 6, e.indexOf('_BMP_')) +ss[1]+ e.substring(e.indexOf('_BMP_') + 5);
}
private function mouseClickHandler(e:MouseEvent):void {
pos = e.localX / 24;
select_image(sel_arr[1], pos, true);
code_create(1);
}
private function select_image(s:Sprite,a:int,b:Boolean):void {
s.graphics.clear();
if(b){s.graphics.lineStyle(1,0xff0000);}else{s.graphics.beginFill(0xcccccc,0.5);}
s.graphics.drawRect(a * 24-1, 0, 24, 24);
s.graphics.endFill();
}
private function mouseMoveHandler(e:MouseEvent):void {
var x:int = e.localX / 24;
if(x!=pos)select_image(sel_arr[0],x,false);
}
private function Btn_Press(e:MouseEvent):void {code_create(0);}
private function save_Press(e:MouseEvent):void {(new FileReference()).save(src.text,'Main.as');}
}
}