Skip to content

Instantly share code, notes, and snippets.

@butihuzi
Last active December 20, 2015 20:48
Show Gist options
  • Save butihuzi/6192811 to your computer and use it in GitHub Desktop.
Save butihuzi/6192811 to your computer and use it in GitHub Desktop.
Flash 扩展开发
// from http://www.cnblogs.com/GameDeveloper/articles/2827583.html
// Flash 扩展开发
//var dom = fl.getDocumentDOM();
//var fileName = dom.name.split(".fla").join(".swf");
//var swfFile = fl.configURI + "WindowSWF/" + fileName;
//dom.exportSWF(swfFile, true);
cls();
fl.reloadTools();
fl.getDocumentDOM().save();
trace("重新记载工具栏OK");
trace(fl.getDocumentDOM().library);
/**********************************************************************************************/
//功能性函数
/*给函数原型增加一个extend函数,实现继承*/
Function.prototype.extend = function(superClass){
if(typeof superClass !== 'function'){
throw new Error('fatal error:Function.prototype.extend expects a constructor of class');
}
var F = function(){}; //创建一个中间函数对象以获取父类的原型对象
F.prototype = superClass.prototype; //设置原型对象
this.prototype = new F(); //实例化F, 继承父类的原型中的属性和方法,而无需调用父类的构造函数实例化无关的父类成员
this.prototype.constructor = this; //设置构造函数指向自己
this.superClass = superClass; //同时,添加一个指向父类构造函数的引用,方便调用父类方法或者调用父类构造函数
return this;
};
/*打印输出*/
function trace(str){
fl.outputPanel.trace(str);
}
/*清屏*/
function cls(){
fl.outputPanel.clear();
}
/** 保留小数 */
function formatNumber( num, retain){
retain = retain || 100;
return Math.round(num * retain) / 100;
}
/** 把一个库目录里的资源转换为资源的绝对目录 */
function getFilePathByLibraryPath( path ){
if( path == null || path == "" || path == undefined ){
alert( "getFilePathByLibraryPath 无效的参数" );
}
var lib = fl.getDocumentDOM().library;
var item_index = lib.findItemIndex( path );
if( item_index < 0 ){
alert( "此路径不存在: " + path );
}
var item = lib.items[item_index];
var url = item.sourceFilePath;
url = url.replace( "file:///", "" );
url = url.replace( "|", ":" );
//for ( var i in item ){
//trace( " " + i + " " + ":" + item[i] );
//}
//trace( "转换路径 " + path + "===>>" + url );
return url;
}
/**
@brief 获取当前文档的时间轴
@return Timeline object
*/
getCurrentTimeline = function(){
try{
return fl.getDocumentDOM().getTimeline();
}catch(e){
alert( "打开 Fla 文件失败" );
}
return null;
}
/**
@brief 判断一个帧是否是关键帧, 原理是每个关键帧总是一序列帧的第一帧
@param frame:Frame object 帧对象
@param frameIndex:int 该帧的索引号
@return boolean
*/
isKeyFrame = function( frame, frameIndex ){
if( !frame ) return false;
if( frame.startFrame == frameIndex ){
return true;
}
return false;
}
/**
@brief 把某个图层的关键帧枚举出来
@param layer: Layer object 图层对象
@param startFrameIndex:int 起始帧数,不一定是从0开始
@param endFrameIndex:int 结束帧数
@return vector<int>: 范围[startFrameIndex,endFrameIndex)之间的关键帧索引号数组
*/
getKeyframeIndices = function(layer, startFrameIndex, endFrameIndex){
if( !layer ) return [];
var list = [];
for (var frameIndex=startFrameIndex; frameIndex<endFrameIndex; frameIndex++){
var frame = layer.frames[frameIndex];
var isFirstFrame = (frameIndex == startFrameIndex);
var isKeyframe = (isFirstFrame || frame.startFrame == frameIndex);
// if (isKeyframe){
// list[frameIndex] = true;
// }else{
// list[frameIndex] = false;
// }
if( isKeyframe ){
list.push( frameIndex );
}
}
return list;
}
/**
@brief 获取某个图层的所有关键帧
@param layer:Layer object 图层对象
@return vector<Frame object>
*/
getKeyframeObjects = function( layer ){
if( !layer ) return [];
var list = [];
var index = 0;
for each( frame in layer.frames ){
if( frame.startFrame == index ){
list.push( frame );
}
index++;
}
return list;
}
/**
@brief 获取一个帧上的所有元素
@param frame:Frame object 帧对象
@return vector<Element object> 该帧上的所有元素
*/
getElementObjects = function( frame ){
if( !frame ) return [];
return frame.elements;
}
/**
@brief 获取元素的类型
@param element:Element object 元素对象
@return string 类型
*/
getElementType = function(element){
if (!element) return '';
var elementType = '';
var libraryItem = element.libraryItem;
// element.elementType: "shape", "text", "instance", or "shapeObj"
// item.itemType: "undefined", "component", "movie clip", "graphic", "button", "folder", "font", "sound", "bitmap", "compiled clip", "screen", and "video".
switch (element.elementType){
case 'shape' :
{
//NOTE: a drawing object is both a group and a drawing object, so check it first
elementType = element.isRectangleObject ? 'rectangle object'
: element.isOvalObject ? 'oval object'
: element.isDrawingObject ? 'drawing object'
: element.isGroup ? 'group'
: 'shape';
}break;
case 'shapeObj' :
{
elementType = 'shape object';
}break;
case 'text' :
{
elementType = 'text';
}break;
case 'instance' :
{
if (element.symbolType)
elementType = element.symbolType;
else if (libraryItem.itemType && libraryItem.itemType != 'undefined')
elementType = libraryItem.itemType;
}break;
}
return elementType;
}
/** 精减小数 */
roundToTwip = function(value){
return Math.round(value*20) / 20;
}
/** 获取元素的x坐标 */
getX = function(element){
return roundToTwip(element.transformX);
}
/** 获取元素的y坐标 */
getY = function(element){
return roundToTwip(element.transformY);
}
/** 设置元素的x坐标 */
setX = function(element, x){
element.transformX = x;
}
/** 设置元素的y坐标 */
setY = function(element, y){
element.transformY = y;
}
/**********************************************************************************************/
/**********************************************************************************************/
//Class HSTexture
HSTexture = function( oElement ){
this.imageName = oElement.libraryItem.name;
trace("HSTexture @ imageName : "+ this.imageName);
}
//Class HSNode
HSNode = function( oElement ){
if(!oElement){
return;
}
this.fX = oElement.x;
this.fY = oElement.y;
this.fZ = oElement.depth;
this.fW = oElement.width;
this.fH = oElement.height;
this.fScaleX = oElement.scaleX;
this.fScaleY = oElement.scaleY;
this.fSkewX = oElement.skewX;
this.fSkewY = oElement.skewY;
this.fRotation = oElement.rotation;
}
printList = function( infoList , strDescribe ){
strDescribe += "Info: ";
var info = strDescribe;
for(var i = 0; i< infoList.length;++i){
info += infoList[i];
}
trace(info);
}
printNode = function( oHSNode ){
trace("obectName : " + oHSNode.obectName);
trace("fX : " + oHSNode.fX);
trace("fY : " + oHSNode.fY);
trace("fZ : " + oHSNode.fZ);
trace("fW : " + oHSNode.fW);
trace("fH : " + oHSNode.fH);
trace("fScaleX : " + oHSNode.fScaleX);
trace("fScaleY : " + oHSNode.fScaleY);
trace("fSkewX : " + oHSNode.fSkewX);
trace("fSkewY : " + oHSNode.fSkewY);
trace("fRotation : " + oHSNode.fRotation);
}
//Class HSDevice
HSDevice = function(strDeviceName ,strShowType, iScreenW , iScreenH){
this.sName = strDeviceName;
this.sShowType = strShowType;
this.iWidth = iScreenW;
this.iHeight = iScreenH;
}
//Class HSSprite
HSSprite = function( oElement ){
if(!oElement){
return;
}
this.obectName = oElement.name;
this.oHSTexture = new HSTexture(oElement); //纹理对象
this.nodeData = new HSNode(oElement);
}
HSSprite.prototype.printInfo = function(){
trace("SpriteName : " + this.obectName);
trace("SpriteTextureName : " + this.oHSTexture.imageName);
}
//Class HSMenu
HSMenu = function(){
this.obectName = ""; //代表图层的名字
this.frameCount = 4;
this.normalList = []; //HSSprite
this.passList = []; //HSSprite
this.pressdownList = []; //HSSprite
this.upspringList = []; //HSSprite
this.nodeData = null; //HSNodeData
}
HSMenu.prototype.printInfo = function(){
trace("MenuName : " + this.obectName);
printList(this.normalList,"normalList");
printList(this.passList,"passList");
printList(this.pressdownList,"pressdownList");
printList(this.upspringList,"upspringList");
printNode(this.nodeData);
}
//Class HSLayer
HSLayer = function( oLayer ){
this.type = oLayer.layerType;
this.name = oLayer.name;
this.hsSpriteList = [];
this.hsMenuList = []; //HSMenu
var frameCount = oLayer.frameCount;
for(var frameIndex = 0;frameIndex<frameCount;++frameIndex){
var oFrame = oLayer.frames[frameIndex];
var elementCount = oFrame.elements.length;
for(var elementIndex = 0;elementIndex<elementCount;++elementIndex){
var oElement = oFrame.elements[elementIndex];
var elementLibrartItem = oElement.libraryItem;
//trace("元素名字 : " + oElement.libraryItem.name);
//trace("元素类型 : " + getElementType(oElement));
trace("/*********************************/");
switch(elementLibrartItem.itemType)
{
case "button":
{
trace("case \"button\":");
trace(oElement.name);
this.nodeData = new HSNode(oElement);
//trace(elementLibrartItem.timeline.layers[0].frames[2].elements[0].libraryItem.name);
var oHSMenu = this.handleMenu(elementLibrartItem.timeline ,oElement.name );
oHSMenu.nodeData = new HSNode(oElement);
this.hsMenuList.push(oHSMenu);
for(var i = 0; i<this.hsMenuList.length;++i){
this.hsMenuList[i].printInfo();
}
break;
}
case "movie clip":
{
trace("case \"movie clip\":");
break;
}
case "bitmap":
{
var oHSSprite = new HSSprite(oElement);
this.hsSpriteList.push(oHSSprite);
oHSSprite.printInfo();
break;
}
case "graphic":
{
trace("case graphic:");
//需要Shape 对象
trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0]);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].getHalfEdge().getVertex().x);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].getHalfEdge().getVertex().y);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[1].getHalfEdge().getVertex().x);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[1].getHalfEdge().getVertex().y);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.color);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.b);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.c);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.d);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.tx);
//trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.ty);
//trace(typeof oElement.symbolType);
break;
}
default:
{
alert( elementLibrartItem.itemType + " 不识别");
break;
}
}
trace("/*********************************/");
//var symbolItemTimeline = oElement.libraryItem.timeline;
//var layerList = symbolItemTimeline.layers;
//var layerListCount = symbolItemTimeline.layerCount;
//for(var symIndex = 0;symIndex < layerListCount;++symIndex){
//trace("symbol :" + layerList[symIndex].name);
//}
}
}
}
HSLayer.prototype.handleMenu = function( oTimeline , strMenuName){
trace("handleMenu");
//var layerCount = oTimeline.layerCount;
var oLayer = oTimeline.layers[0];
var oMenu = new HSMenu();
oMenu.obectName = strMenuName;
var frameCount = oLayer.frameCount;
//
trace("frameCount : " + frameCount);
for(var frameIndex = 0; frameIndex < 4;++frameIndex){
var elementList = oLayer.frames[frameIndex].elements;
var elementCount = elementList.length;
var frameSprite = [];
for(var elementIndex = 0;elementIndex < elementCount;++elementIndex){
var oElement = elementList[elementIndex];
var oElementType = oElement.libraryItem.itemType;
//trace(oElementType);
if(elementIndex == 0){
oMenu.nodeData = new HSNode(oElement);
}
if(oElementType == "bitmap"){
trace(oElement.libraryItem.name + " " + elementIndex);
frameSprite.push(oElement.libraryItem.name);
}else{
alert(oLayer.name + " 包含了图元,如有问题请联系:Ambition");
}
}
switch(frameIndex){
case 0:
oMenu.normalList = frameSprite;
break;
case 1:
oMenu.passList = frameSprite;
break;
case 2:
oMenu.pressdownList = frameSprite;
break;
case 3:
oMenu.upspringList = frameSprite;
break;
}
}
return oMenu;
}
HSLayer.prototype.handleBitmap = function(){
}
HSLayer.prototype.handleMC = function(){
}
HSLayer.prototype.handleGraphic = function(){
}
SaveXml = function(strDeviceName,strShowType,iScreenW,iScreenH){
trace(strShowType);
var device = new HSDevice(strDeviceName,strShowType,iScreenW,iScreenH);
//得到时间轴
var timeLine = getCurrentTimeline();
var layerList = [];
switch(device.sShowType)
{
case "All":
{
trace(device.sShowType);
var layerCount = timeLine.layerCount;
for(var layerIndex = 0;layerIndex < layerCount;++layerIndex){
var oLayer = timeLine.layers[layerIndex];
var hsLayer = new HSLayer(oLayer);
//图层类型包括:guide folder normal mask normal
if("normal" != oLayer.layerType){
alert("[ " + oLayer.name + " ] 类型不为normal类型,因此跳过,有疑问请联系: Ambition");
continue;
}
//layerList.push(hsLayer);
}
break;
}
default:
{
alert("不支持显示类型 : " + device.sShowType);
break;
}
}
trace(layerList);
trace(timeLine);
return "";
}
/**********************************************************************************************/
SaveXml("android","All",480,800);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment