var strlis = new Array();var timeLine = new Array();
var fileUrl = fl.browseForFileURL("open", "选择要解析的FLA文件", "FLA Document (*.fla)", "fla");
var doc = fl.openDocument(fileUrl);
var rootElements = doc.getTimeline().layers[0].frames[0].elements.reverse();
var needBase = false;
fl.trace("start anlysis11");
anysisElements(rootElements); //至第二部分
// 逐层记录元素信息// 如果是Shape组就递归调用自身,获取数组替换。是其他元素就获取属性Object进行替换。function anysisElements(elements){for (var index = 0; index<elements.length; index++){var element = elements[index];if (element instanceof SymbolInstance && element.symbolType == "graphic"){var groName = element.libraryItem.name;var subElements = element.libraryItem.timeline.layers[0].frames[0].elements.reverse();anysisElements(subElements);subElements.push(groName);elements[index] = subElements;}else{var args = anlysisElement(element);elements[index] = args;}}}
/*** 获得单个实例或文本信息* 添加[name, textureType, label, scale9Grid(对于img), color, size, font, bold, align(这五项对于lb), x, y, width, height, changeWH, isImport, importUrl]*/function anlysisElement(element){// 基础属性var args = new Object();args.name = element.name;args.x = element.x;args.y = element.y;args.width = element.width;args.height = element.height;if (element.scaleX !=1 || element.scaleY != 1){args.changeWH = 1;} else {args.changeWH = 0;}// 分情况考虑获取其他属性switch (element.elementType){case "instance":instanceHandler(element, args);break;case "text":textHandler(element, args);break;default:fl.trace("unsupported element type" + " " + element.elementType);break;}return args;}
/*** 从库中解析元件* 添加[textureType, isImport, importUrl, hasScale9, scale9grid, changeWH]*/function instanceHandler(instance, args){// 公共属性var item = instance.libraryItem;args.isImport = item.linkageImportForRS;args.importUrl = item.linkageUrl;// 这是个资源名(可能携带库路径)args.textureType = item.name;if (item instanceof SymbolItem){anysisGrid9(item, args);} else {fl.trace("unsupported instance type");}if (args.hasScale9 && !args.isImport){needBase = true;}}
/*** 解析 scale9* 添加 [hasScale9, scale9Grid]*/function anysisGrid9(symbolItem, args){args.hasScale9 = symbolItem.scalingGrid;if (args.hasScale9){var rect1 = symbolItem.scalingGridRect;var elements = symbolItem.timeline.layers[0].frames[0].elements;var element = elements[0];var rect2 = new Object();var hw = element.width * 0.5;var hh = element.height * 0.5;rect2.left = element.x - hw; rect2.right = element.x + hw; rect2.top = element.y - hh; var rect3 = new Object();rect2.bottom = element.y + hh;rect3.left = rect1.left - rect2.left;rect3.top = rect1.top - rect2.top;rect3.right = rect3.left + (rect1.right-rect1.left);rect3.bottom = rect3.top + (rect1.right-rect1.left);rect3.rightGap = rect2.right - rect1.right;rect3.bottomGap = rect2.bottom - rect1.bottom;args.scale9Grid = rect3;}}
/*** 解析文本* 添加[label, color, size, font, bold, align]*/function textHandler(text, args){args.label = text.getTextString();args.color = text.getTextAttr("fillColor");args.size = text.getTextAttr("size");args.font = "宋体";args.bold = text.getTextAttr("bold");args.align = text.getTextAttr("alignment");}
// ------------------------------------------------------- 第二部分:后续处理和写入XML --------------------------------
// 公共库名var commonLibName = "UIResGommon";
// 设置保存路径var fullPath = fl.browseForFileURL("save", "请选择保存目录和文件名");var fileName = fullPath.match(/\w+\.\w+$/);var path = fullPath.replace(fileName, "");var mainName = ""+fileName;mainName = mainName.replace("UI", "");mainName = mainName.replace(".xml", "");fl.trace(fileName);fl.trace(fullPath);fl.trace(mainName);
// 创建文件头var xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";xmlText += getBlank(0) + "<UICood uiName = \""+"UI"+mainName+"\" "+"uiWidth=\"\" "+"uiHeight=\"\" "+">\r\n";xmlText += getBlank(1) + "<texture>\r\n";xmlText += getBlank(2) + "<t name=\"UIResCommon_t2\" pngsrc=\"./assets/images/UIResCommon.png\" xmlsrc=\"./assets/images/UIResCommon.xml\" basesrc=\"./assets/images/UIResCommonBase.xml\"/>\r\n"var resName = "UIRes" + mainName;xmlText += getBlank(2) + "<t name=\"" + resName + "_t4" + "\" pngsrc=\"./assets/images/"+resName+".png\" xmlsrc=\"./assets/images/"+resName+".xml\"";if (needBase){xmlText += " basesrc=\"./assets/images/"+resName+"Base"+".xml\"/>\r\n"}else{xmlText += "/>\r\n"}xmlText += getBlank(1) + "</texture>\r\n";
xmlText += getBlank(1) + "<UI>\r\n";
writeElements(rootElements, 0);
xmlText += getBlank(1) + "</UI>\r\n";
xmlText += "</UICood>"
FLfile.write(fullPath, xmlText);
function writeElements(elements, lv){var len = elements.length;if (lv==0){len ++;}else{addGroupNotify(elements[elements.length-1], lv);}for (var i=0; i<len -1;i++){if (elements[i] instanceof Array){writeElements(elements[i], lv+1);}else if (lv != 0){writeElement(elements[i], lv);}}}
// 属性集合var props = ["name", "textureId", "textureType", "label", "scale9Grid", "color", "size", "font", "bold", "align", "x", "y", "width", "height", "numberLayer", "changeWH"]
function writeElement(element, lv){xmlText += getBlank(lv+2);xmlText += "<ui";anlysisName(element);anlysisTextureId(element);anlysisNumberLayer(element);if (element.hasScale9){anlysisScale9(element);}if (element.label){fillLabelProps(element);}fillLabel(element);for (var i=0; i<props.length; i++){if (element.hasOwnProperty(props[i])){xmlText += " ";xmlText += props[i];xmlText += "=\"";xmlText += element[props[i]];xmlText += "\"";}}xmlText += "/>\r\n";}
var resNameT = resName + "_t4";var resCommonNameT = "UIResCommon"+"_t2";function anlysisTextureId(element){if (element.isImport){element.textureId = resCommonNameT;} else {element.textureId = resNameT;}}
//设定层次function anlysisNumberLayer(element){if (element.hasOwnProperty("size")){element.numberLayer = 3;}if (element.hasOwnProperty("scale9Grid")){element.numberLayer = 1;}else{element.numberLayer = 2;}}
// 防重复命名var usedNames = new Array();function anlysisName(element){if (!element.name){if (element.textureType){element.name = element.textureType;}else{element.name = "lb_text";}}var i=+1;while (indexof(usedNames, element.name) != -1){if (i==1){element.name += ""+i;}else{element.name = element.name.slice(0, -1);element.name += ""+i;}i++;}usedNames.push(element.name);}
function fillLabel(element){if (!element.hasOwnProperty("label")){element.label = "";}}
function anlysisScale9(element){var obj = element.scale9Grid;var str = obj.left+","+obj.top+","+obj.right+","+obj.bottom;element.scale9Grid = str;}
var labelProps = ["color","size","font","bold","align"];var defaultLabelProps = ["0xffffff", "12", "宋体", "false", "left"];
function fillLabelProps(element){for (var i=0; i<labelProps.length; i++){if (typeof element[labelProps[i]] == "undefined"){element[labelProps[i]] = defaultLabelProps[i];}}}
// ---------------------- tools -----------------
function indexof(arr, obj){for (var i=0; i<arr.length; i++){if (arr[i] == obj){fl.trace("arr has obj:"+obj);return i;}}return -1;}
function addGroupNotify(gouName, lv){xmlText += getBlank(lv+2) + "<!-- 以下内容来自" + gouName + "-->\r\n";}
function getBlank(level){var str = "";var blank = " ";for (var i=0;i<level;i++){str+=blank;}return str;}









