Skip to content

Instantly share code, notes, and snippets.

@dadon
Created December 25, 2011 14:15
Show Gist options
  • Save dadon/1519332 to your computer and use it in GitHub Desktop.
Save dadon/1519332 to your computer and use it in GitHub Desktop.
//
// VERSION 1.4.3
(function () {
resetProperty = function ( obj ) {
for ( var property in obj ) {
obj[property] = undefined;
}
};
hasProperty = function ( obj, key ) {
for ( var property in obj ) {
if ( property == key )
return true;
}
return false;
};
inArray = function ( array, item ) {
if ( item === undefined )
return false;
for ( var i = 0; i < array.length; i++ ) {
if ( array[i] === item )
return true;
}
return false;
};
getFrames = function ( layer, lookForProperties, dif ) {
var frames = [];
resetProperty( lookForProperties );
if ( dif === undefined )
dif = false;
var frameCount = layer.frameCount;
for ( var i = 0; i < frameCount; i++ ) {
if ( layer.frames[i] == undefined ) continue;
var curElement = layer.frames[i].elements[0];
if ( curElement === undefined ) continue;
frameObj = {};
frameObj.num = i + 1;
if ( dif ) {
var col = 0;
}
for ( var property in curElement ) {
if ( hasProperty( lookForProperties, property ) ) {
if ( !dif ) {
frameObj[property] = curElement[property];
}
else {
if ( lookForProperties[property] != curElement[property] ) {
col++;
lookForProperties[property] = curElement[property];
frameObj[property] = curElement[property];
}
}
}
}
if ( !dif ) {
frames.push( frameObj );
}
else {
if ( col > 0 ) {
frames.push( frameObj );
}
}
}
return frames;
}
getSourceProp = function ( sourceElement ) {
if ( sourceElement !== undefined ) {
sourceElementParent = sourceElement.libraryItem.timeline.layers[0].frames[0].elements[0];
if ( sourceElementParent != undefined ) {
var path_array = sourceElementParent.libraryItem.name.split( "/" );
if ( path_array.length > 1 ) {
source = path_array[path_array.length - 1]
}
else {
source = path_array[0];
}
var res = {
src : source,
x : sourceElementParent.x,
y : sourceElementParent.y,
};
}
}
return res;
};
printXmlTag = function ( element, tag, closed, exclude ) {
var str = '<' + tag + ' ';
if ( closed === undefined )
closed = true;
for ( property in element ) {
if ( element[property] === undefined )
continue;
if ( exclude !== undefined && inArray( exclude, property ) )
continue;
str += property + '="' + element[property] + '" ';
}
if ( closed == false )
str += '>\n';
else
str += '/>\n';
return str
};
fl.outputPanel.clear();
var mainElement, mainTimeLinel;
var rootObj, frameObj, childObj;
var curLayer;
var source, sourceElement, sourceElementParent;
var i, j;
var xmlStr;
var lookForProperties = {
x : undefined,
y : undefined,
rotation : undefined,
scaleX : undefined,
scaleY : undefined,
};
mainElement = fl.getDocumentDOM().selection[0];
if ( mainElement === undefined ) {
mainElement = fl.getDocumentDOM();
mainTimeLinel = mainElement.getTimeline();
}
else {
mainTimeLinel = mainElement.libraryItem.timeline;
}
rootObj = {};
// внешняя анимация главного символа
if ( mainElement.layer !== undefined && mainElement.layer.frameCount > 0 ) {
rootObj.frames = getFrames( mainElement.layer, lookForProperties );
}
// внутренняя анимация
if ( mainTimeLinel.layers.length > 0 ) {
rootObj.children = [];
for ( i = 0; i < mainTimeLinel.layers.length; i++ ) {
curLayer = mainTimeLinel.layers[i];
if ( curLayer.frameCount < 1 )
continue;
childObj = {};
childObj.frames = getFrames( curLayer, lookForProperties );
childObj.layerName = curLayer.name;
if ( mainTimeLinel.layers[i].frames[0] != undefined ) {
for ( j = 0; j < mainTimeLinel.layers[i].frames.length; j++ ) {
sourceElement = mainTimeLinel.layers[i].frames[j].elements[0];
var sourceProp = getSourceProp( sourceElement );
if ( sourceProp !== undefined ) {
childObj.centerOffsetX = sourceProp.x;
childObj.centerOffsetY = sourceProp.y;
childObj.source = sourceProp.src;
break;
}
}
}
rootObj.children.push( childObj );
}
}
// печать xml
xmlStr = '<?xml version="1.0" encoding="UTF-8" ?>\n';
xmlStr += '<root>\n';
if ( rootObj.frames != undefined && rootObj.frames.length > 0 ) {
xmlStr += '\t<frames>\n';
for ( i = 0; i < rootObj.frames.length; i++ ) {
xmlStr += '\t\t' + printXmlTag( rootObj.frames[i], 'frame' );
}
xmlStr += '\t</frames>\n';
}
if ( rootObj.children.length > 0 ) {
xmlStr += '\t<children>\n';
for ( i = 0; i < rootObj.children.length; i++ ) {
child = rootObj.children[i];
if ( child.frames.length < 1 )
continue;
xmlStr += '\t\t' + printXmlTag( child, 'child', false, ["frames"] );
xmlStr += '\t\t\t<frames>\n';
for ( var j = 0; j < child.frames.length; j++ ) {
xmlStr += '\t\t\t\t' + printXmlTag( child.frames[j], 'frame' );
}
xmlStr += '\t\t\t</frames>\n';
xmlStr += '\t\t</child>\n';
}
xmlStr += '\t</children>\n';
}
xmlStr += '</root>';
// сохранение
var xmlURI = flash.browseForFileURL( "save", "Save XML" );
fl.outputPanel.clear();
fl.trace( xmlStr );
if ( xmlURI == null ) {
fl.trace( "File not select" );
}
else {
fl.outputPanel.save( xmlURI, true );
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment