diff --git a/Assets/FlashTools/Examples/Animations/anim00.fla b/Assets/FlashTools/Examples/Animations/anim00.fla index bd84a65..8c50df3 100644 Binary files a/Assets/FlashTools/Examples/Animations/anim00.fla and b/Assets/FlashTools/Examples/Animations/anim00.fla differ diff --git a/Assets/FlashTools/Examples/Animations/sssssss.fla b/Assets/FlashTools/Examples/Animations/sssssss.fla new file mode 100644 index 0000000..9abd766 Binary files /dev/null and b/Assets/FlashTools/Examples/Animations/sssssss.fla differ diff --git a/Assets/FlashTools/Examples/Animations/test00.fla b/Assets/FlashTools/Examples/Animations/test00.fla index 3595930..7f74d33 100644 Binary files a/Assets/FlashTools/Examples/Animations/test00.fla and b/Assets/FlashTools/Examples/Animations/test00.fla differ diff --git a/Tools/FlashExport2.jsfl b/Tools/FlashExport2.jsfl index 7b7e8ed..acd4343 100644 --- a/Tools/FlashExport2.jsfl +++ b/Tools/FlashExport2.jsfl @@ -4,26 +4,26 @@ // // ---------------------------------------------------------------------------- -if ( !String.prototype.format ) { - String.prototype.format = function() { +if (!String.prototype.format) { + String.prototype.format = function () { var args = arguments; - return this.replace(/{(\d+)}/g, function(match, number) { + return this.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; }); }; } -if ( !Function.prototype.bind ) { - Function.prototype.bind = function(oThis) { - if ( typeof this !== 'function' ) { +if (!Function.prototype.bind) { + Function.prototype.bind = function (oThis) { + if (typeof this !== 'function') { throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); } - var aArgs = Array.prototype.slice.call(arguments, 1); + var aArgs = Array.prototype.slice.call(arguments, 1); var fToBind = this; - var fNOP = function() {}; - var fBound = function() { - return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, - aArgs.concat(Array.prototype.slice.call(arguments))); + var fNOP = function () {}; + var fBound = function () { + return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); }; fNOP.prototype = this.prototype; fBound.prototype = new fNOP(); @@ -31,8 +31,8 @@ if ( !Function.prototype.bind ) { }; } -if ( !Array.prototype.find ) { - Array.prototype.find = function(predicate) { +if (!Array.prototype.find) { + Array.prototype.find = function (predicate) { if (this === null) { throw new TypeError('Array.prototype.find called on null or undefined'); } @@ -53,12 +53,12 @@ if ( !Array.prototype.find ) { }; } -if ( typeof Object.create != 'function' ) { - Object.create = (function() { +if (typeof Object.create != 'function') { + Object.create = (function () { function Temp() {} var hasOwn = Object.prototype.hasOwnProperty; return function (O) { - if (typeof O != 'object') { + if (typeof O != 'object') { throw TypeError('Object prototype may only be an Object or null'); } Temp.prototype = O; @@ -73,14 +73,14 @@ if ( typeof Object.create != 'function' ) { } } return obj; - }; + }; })(); } -(function() { +(function () { "use strict"; - + // ---------------------------------------------------------------------------- // // ft @@ -89,23 +89,23 @@ if ( typeof Object.create != 'function' ) { var ft = {}; - ft.trace = function() { + ft.trace = function () { fl.outputPanel.trace( Array.prototype.join.call(arguments, " ")); }; - ft.trace_fmt = function(format) { + ft.trace_fmt = function (format) { var args = Array.prototype.slice.call(arguments, 1); ft.trace(format.format.apply(format, args)); }; - ft.clear_output = function() { + ft.clear_output = function () { fl.outputPanel.clear(); }; - ft.assert = function(expr, format) { - if ( !expr ) { - if ( format === undefined ) { + ft.assert = function (expr, format) { + if (!expr) { + if (format === undefined) { throw "!!!Assert!!!"; } else { var args = Array.prototype.slice.call(arguments, 2); @@ -114,63 +114,63 @@ if ( typeof Object.create != 'function' ) { } }; - ft.type_assert = function(item, type) { + ft.type_assert = function (item, type) { var type_is_string = (typeof type === 'string'); ft.assert( - ( type_is_string && typeof item === type) || + (type_is_string && typeof item === type) || (!type_is_string && item instanceof type), "Type error: {0} != {1}", - typeof item, - type_is_string ? type : type.constructor.name); + typeof item, + type_is_string ? type : type.constructor.name); }; - ft.type_assert_if_defined = function(item, type) { - if ( item !== undefined ) { + ft.type_assert_if_defined = function (item, type) { + if (item !== undefined) { ft.type_assert(item, type); } }; - ft.escape_path = function(path) { + ft.escape_path = function (path) { ft.type_assert(path, 'string'); return path.replace(/ /g, '%20'); }; - ft.escape_string = function(str) { + ft.escape_string = function (str) { ft.type_assert(str, 'string'); return str .replace(/\&/g, '&') .replace(/\"/g, '"') .replace(/\'/g, ''') - .replace(//g , '>'); + .replace(//g, '>'); }; - ft.combine_path = function(lhs, rhs) { + ft.combine_path = function (lhs, rhs) { ft.type_assert(lhs, 'string'); ft.type_assert(rhs, 'string'); return ft.escape_path(lhs) + ft.escape_path(rhs); }; - ft.array_foreach = function(arr, func, filter) { + ft.array_foreach = function (arr, func, filter) { ft.type_assert(arr, Array); ft.type_assert(func, Function); ft.type_assert_if_defined(filter, Function); - for ( var index = 0; index < arr.length; ++index ) { + for (var index = 0; index < arr.length; ++index) { var value = arr[index]; - if ( filter === undefined || filter(value, index) ) { + if (filter === undefined || filter(value, index)) { func(value, index); } } }; - ft.object_foreach = function(obj, func, filter) { + ft.object_foreach = function (obj, func, filter) { ft.type_assert(obj, 'object'); ft.type_assert(func, Function); ft.type_assert_if_defined(filter, Function); - for ( var key in obj ) { - if ( obj.hasOwnProperty(key) ) { + for (var key in obj) { + if (obj.hasOwnProperty(key)) { var value = obj[key]; - if ( filter === undefined || filter(key, value) ) { + if (filter === undefined || filter(key, value)) { func(key, value); } } @@ -183,18 +183,18 @@ if ( typeof Object.create != 'function' ) { // // ---------------------------------------------------------------------------- - var UniqueIds = function() { + var UniqueIds = function () { this.clear(); }; - UniqueIds.prototype.clear = function() { - this.stringIds = Object.create(null); + UniqueIds.prototype.clear = function () { + this.stringIds = Object.create(null); this.lastStringId = 0; }; - UniqueIds.prototype.get_string_id = function(str) { + UniqueIds.prototype.get_string_id = function (str) { ft.type_assert(str, 'string'); - if ( this.stringIds.hasOwnProperty(str) ) { + if (this.stringIds.hasOwnProperty(str)) { return this.stringIds[str]; } else { this.stringIds[str] = ++this.lastStringId; @@ -202,12 +202,12 @@ if ( typeof Object.create != 'function' ) { } }; - UniqueIds.prototype.save = function(xml_path) { + UniqueIds.prototype.save = function (xml_path) { ft.type_assert(xml_path, 'string'); var xml_node = new XmlNode("strings"); - ft.object_foreach(this.stringIds, function(key, value) { + ft.object_foreach(this.stringIds, function (key, value) { xml_node.child("string") - .attr("id" , value) + .attr("id", value) .attr("str", ft.escape_string(key)); }); xml_node.save(xml_path); @@ -219,51 +219,54 @@ if ( typeof Object.create != 'function' ) { // // ---------------------------------------------------------------------------- - var XmlNode = function(node_name, node_parent) { + var XmlNode = function (node_name, node_parent) { ft.type_assert(node_name, 'string'); ft.type_assert_if_defined(node_parent, XmlNode); - this.name = node_name; - this.parent = node_parent; - this.attrs = []; + this.name = node_name; + this.parent = node_parent; + this.attrs = []; this.children = []; }; - XmlNode.prototype.attr = function(attr_name, attr_value) { + XmlNode.prototype.attr = function (attr_name, attr_value) { ft.type_assert(attr_name, 'string'); attr_value = ft.escape_string(attr_value.toString()); - var attr = this.attrs.find(function(attr) { + var attr = this.attrs.find(function (attr) { return attr.name == attr_name; }); - if ( attr ) { + if (attr) { attr.value = attr_value; } else { - this.attrs.push({name:attr_name, value:attr_value}); + this.attrs.push({ + name: attr_name, + value: attr_value + }); } return this; }; - XmlNode.prototype.child = function(child_name) { + XmlNode.prototype.child = function (child_name) { ft.type_assert(child_name, 'string'); var child = new XmlNode(child_name, this); this.children.push(child); return child; }; - - XmlNode.prototype.parent = function() { + + XmlNode.prototype.parent = function () { ft.assert(this.parent !== undefined, "xml node parent is undefined"); return this.parent; }; - XmlNode.prototype.content = function(indent) { + XmlNode.prototype.content = function (indent) { indent = indent || ""; ft.type_assert(indent, 'string'); var str = '{0}<{1}'.format(indent, this.name); - ft.array_foreach(this.attrs, function(attr) { + ft.array_foreach(this.attrs, function (attr) { str += ' {0}="{1}"'.format(attr.name, attr.value); }); - if ( this.children.length > 0 ) { + if (this.children.length > 0) { str += ">\n"; - ft.array_foreach(this.children, function(child) { + ft.array_foreach(this.children, function (child) { str += child.content(indent + "\t") + "\n"; }); return str + "{0}<{1}/>".format(indent, this.name); @@ -272,8 +275,8 @@ if ( typeof Object.create != 'function' ) { } }; - XmlNode.prototype.save = function(xml_path) { - if ( !FLfile.write(xml_path, this.content()) ) { + XmlNode.prototype.save = function (xml_path) { + if (!FLfile.write(xml_path, this.content())) { throw "Can't save xml to {0}!".format(xml_path); } }; @@ -283,123 +286,135 @@ if ( typeof Object.create != 'function' ) { // BitmapAsset // // ---------------------------------------------------------------------------- - - var BitmapAsset = function(item, unique_ids) { + + var BitmapAsset = function (item, unique_ids) { ft.type_assert(item, BitmapItem); ft.type_assert(unique_ids, UniqueIds); this.item = item; this.uniqueIds = unique_ids; }; - - BitmapAsset.prototype.trace = function(indent) { + + BitmapAsset.prototype.trace = function (indent) { indent = indent || ""; ft.type_assert(indent, 'string'); - ft.trace_fmt("{0}-= BitmapAsset =-" , indent); + ft.trace_fmt("{0}-= BitmapAsset =-", indent); ft.trace_fmt("{0}-Id : {1}", indent, this.get_id()); ft.trace_fmt("{0}-Name : {1}", indent, this.get_name()); ft.trace_fmt("{0}-ExportFilename : {1}", indent, this.get_export_filename()); }; - - BitmapAsset.prototype.get_id = function() { + + BitmapAsset.prototype.get_id = function () { return this.uniqueIds.get_string_id(this.get_name()); }; - - BitmapAsset.prototype.get_name = function() { + + BitmapAsset.prototype.get_name = function () { return this.item.name; }; - - BitmapAsset.prototype.get_type = function() { + + BitmapAsset.prototype.get_type = function () { return "bitmap"; }; - - BitmapAsset.prototype.get_export_filename = function() { + + BitmapAsset.prototype.get_export_filename = function () { return "bitmaps/{0}.png".format(this.get_id()); }; - - BitmapAsset.prototype.get_export_fullfilename = function(export_folder) { + + BitmapAsset.prototype.get_export_fullfilename = function (export_folder) { ft.type_assert(export_folder, 'string'); return ft.combine_path( export_folder, this.get_export_filename()); }; - - BitmapAsset.prototype.export = function(export_folder, xml_node) { + + BitmapAsset.prototype.export = function (export_folder, xml_node) { ft.type_assert(export_folder, 'string'); ft.type_assert(xml_node, XmlNode); this.export_content(export_folder); this.export_description(xml_node); }; - - BitmapAsset.prototype.export_content = function(export_folder) { + + BitmapAsset.prototype.export_content = function (export_folder) { ft.type_assert(export_folder, 'string'); var fullfilename = this.get_export_fullfilename(export_folder); - if ( !this.item.exportToFile(fullfilename) ) { + if (!this.item.exportToFile(fullfilename)) { throw "Can't export bitmap ({0})!" .format(fullfilename); } }; - - BitmapAsset.prototype.export_description = function(xml_node) { + + BitmapAsset.prototype.export_description = function (xml_node) { ft.type_assert(xml_node, XmlNode); xml_node.child("bitmap") .attr("id" , this.get_id()) .attr("type" , this.get_type()) .attr("filename", this.get_export_filename()); }; - + // ---------------------------------------------------------------------------- // // SymbolAsset // // ---------------------------------------------------------------------------- - - var SymbolAsset = function(item, unique_ids) { + + var SymbolAsset = function (item, unique_ids) { ft.type_assert(item, SymbolItem); ft.type_assert(unique_ids, UniqueIds); this.item = item; this.uniqueIds = unique_ids; }; - - SymbolAsset.prototype.trace = function(indent) { + + SymbolAsset.prototype.trace = function (indent) { indent = indent || ""; ft.type_assert(indent, 'string'); - ft.trace_fmt("{0}-= SymbolAsset =-" , indent); + ft.trace_fmt("{0}-= SymbolAsset =-", indent); ft.trace_fmt("{0}-Id : {1}", indent, this.get_id()); ft.trace_fmt("{0}-Name : {1}", indent, this.get_name()); ft.trace_fmt("{0}-ExportFilename : {1}", indent, this.get_export_filename()); }; - - SymbolAsset.prototype.get_id = function() { + + SymbolAsset.prototype.get_id = function () { return this.uniqueIds.get_string_id(this.get_name()); }; - - SymbolAsset.prototype.get_name = function() { + + SymbolAsset.prototype.get_name = function () { return this.item.name; }; - - SymbolAsset.prototype.get_type = function() { + + SymbolAsset.prototype.get_type = function () { return "symbol"; }; - - SymbolAsset.prototype.get_export_filename = function() { + + SymbolAsset.prototype.get_export_filename = function () { return "symbols/{0}.xml".format(this.get_id()); }; - - SymbolAsset.prototype.get_export_fullfilename = function(export_folder) { + + SymbolAsset.prototype.get_export_fullfilename = function (export_folder) { ft.type_assert(export_folder, 'string'); return ft.combine_path( export_folder, this.get_export_filename()); }; - SymbolAsset.prototype.export = function(export_folder, xml_node) { + SymbolAsset.prototype.convert = function (document) { + ft.type_assert(document, Document); + new TimelineInst(this.item.timeline, this.uniqueIds) + .convert(document); + }; + + SymbolAsset.prototype.prepare = function (document) { + ft.type_assert(document, Document); + new TimelineInst(this.item.timeline, this.uniqueIds) + .prepare(document); + }; + + SymbolAsset.prototype.export = function (export_folder, xml_node) { ft.type_assert(export_folder, 'string'); ft.type_assert(xml_node, XmlNode); this.export_content(export_folder); this.export_description(xml_node); }; - - SymbolAsset.prototype.export_content = function(export_folder) { + + SymbolAsset.prototype.export_content = function (export_folder) { ft.type_assert(export_folder, 'string'); var xml_node = new XmlNode("symbol") .attr("id", this.get_id()); @@ -407,100 +422,146 @@ if ( typeof Object.create != 'function' ) { .export_description(xml_node); xml_node.save(this.get_export_fullfilename(export_folder)); }; - - SymbolAsset.prototype.export_description = function(xml_node) { + + SymbolAsset.prototype.export_description = function (xml_node) { ft.type_assert(xml_node, XmlNode); xml_node.child("symbol") .attr("id" , this.get_id()) .attr("type" , this.get_type()) .attr("filename", this.get_export_filename()); }; - + // ---------------------------------------------------------------------------- // // LibraryInst // // ---------------------------------------------------------------------------- - - var LibraryInst = function(library, unique_ids) { + + var LibraryInst = function (library, unique_ids) { ft.type_assert(library, Library); ft.type_assert(unique_ids, UniqueIds); this.library = library; this.uniqueIds = unique_ids; }; - - LibraryInst.prototype.is_folder_item = function(item) { + + LibraryInst.prototype.is_folder_item = function (item) { ft.type_assert(item, LibraryItem); return item.itemType == "folder"; }; - LibraryInst.prototype.is_bitmap_item = function(item) { + LibraryInst.prototype.is_bitmap_item = function (item) { ft.type_assert(item, LibraryItem); return item.itemType == "bitmap"; }; - LibraryInst.prototype.is_symbol_item = function(item) { + LibraryInst.prototype.is_symbol_item = function (item) { ft.type_assert(item, LibraryItem); return item.itemType == "graphic" || item.itemType == "movie clip"; }; - - LibraryInst.prototype.for_each_by_items = function(func, filter) { + + LibraryInst.prototype.for_each_by_items = function (func, filter) { ft.type_assert(func, Function); ft.type_assert_if_defined(filter, Function); ft.array_foreach(this.library.items, func, filter); }; - LibraryInst.prototype.export = function(export_folder, xml_node) { + LibraryInst.prototype.convert = function (document) { + ft.type_assert(document, Document); + this.for_each_by_items(function (item) { + if ( this.library.editItem(item.name) ) { + ft.trace_fmt("Convert: {0}", item.name); + new SymbolAsset(item, this.uniqueIds) + .convert(document); + document.exitEditMode(); + } + }.bind(this), this.is_symbol_item.bind(this)); + }; + + LibraryInst.prototype.prepare = function (document) { + ft.type_assert(document, Document); + this.for_each_by_items(function (item) { + if ( this.library.editItem(item.name) ) { + ft.trace_fmt("Prepare: {0}", item.name); + new SymbolAsset(item, this.uniqueIds) + .prepare(document); + document.exitEditMode(); + } + }.bind(this), this.is_symbol_item.bind(this)); + }; + + LibraryInst.prototype.export = function (export_folder, xml_node) { ft.type_assert(export_folder, 'string'); ft.type_assert(xml_node, XmlNode); - this.for_each_by_items(function(item) { - if ( this.is_bitmap_item(item) ) { + this.for_each_by_items(function (item) { + if (this.is_bitmap_item(item)) { new BitmapAsset(item, this.uniqueIds) .export(export_folder, xml_node); - } else if ( this.is_symbol_item(item) ) { + } else if (this.is_symbol_item(item)) { new SymbolAsset(item, this.uniqueIds) .export(export_folder, xml_node); } else { throw "Unsupported library item type ({0})!" .format(item.itemType); } - }.bind(this), function(item) { + }.bind(this), function (item) { return !this.is_folder_item(item); }.bind(this)); }; - + // ---------------------------------------------------------------------------- // // TimelineInst // // ---------------------------------------------------------------------------- - var TimelineInst = function(timeline, unique_ids) { + var TimelineInst = function (timeline, unique_ids) { ft.type_assert(timeline, Timeline); ft.type_assert(unique_ids, UniqueIds); this.timeline = timeline; this.uniqueIds = unique_ids; }; - - TimelineInst.prototype.trace = function(indent) { + + TimelineInst.prototype.trace = function (indent) { indent = indent || ""; ft.type_assert(indent, 'string'); ft.trace_fmt("{0}-= TimelineInst =-", indent); - ft.trace_fmt("{0}-Id : {1}" , indent, this.get_id()); - ft.trace_fmt("{0}-Name : {1}" , indent, this.get_name()); - ft.trace_fmt("{0}-Layers : {1}" , indent, this.timeline.layerCount); - ft.trace_fmt("{0}-Frames : {1}" , indent, this.timeline.frameCount); + ft.trace_fmt("{0}-Id : {1}", indent, this.get_id()); + ft.trace_fmt("{0}-Name : {1}", indent, this.get_name()); + ft.trace_fmt("{0}-Layers : {1}", indent, this.timeline.layerCount); + ft.trace_fmt("{0}-Frames : {1}", indent, this.timeline.frameCount); }; - - TimelineInst.prototype.get_id = function() { + + TimelineInst.prototype.get_id = function () { return this.uniqueIds.get_string_id(this.get_name()); }; - - TimelineInst.prototype.get_name = function() { + + TimelineInst.prototype.get_name = function () { return this.timeline.name; }; - TimelineInst.prototype.export_description = function(xml_node) { + TimelineInst.prototype.convert = function (document) { + ft.type_assert(document, Document); + if ( this.timeline.layers.length > 1 ) { + this.timeline.selectAllFrames(); + this.timeline.convertToKeyframes(); + } + ft.array_foreach(this.timeline.layers, function(layer, index) { + this.timeline.setSelectedLayers(index); + new LayerInst(layer, this.uniqueIds) + .convert(document, this.timeline); + }.bind(this)); + }; + + TimelineInst.prototype.prepare = function (document) { + ft.type_assert(document, Document); + ft.array_foreach(this.timeline.layers, function(layer, index) { + this.timeline.setSelectedLayers(index); + new LayerInst(layer, this.uniqueIds) + .prepare(document, this.timeline); + }.bind(this)); + }; + + TimelineInst.prototype.export_description = function (xml_node) { ft.type_assert(xml_node, XmlNode); var timeline_node = xml_node.child("timeline") .attr("id", this.get_id()); @@ -509,21 +570,21 @@ if ( typeof Object.create != 'function' ) { .export_description(timeline_node); }.bind(this)); }; - + // ---------------------------------------------------------------------------- // // LayerInst // // ---------------------------------------------------------------------------- - - var LayerInst = function(layer, unique_ids) { + + var LayerInst = function (layer, unique_ids) { ft.type_assert(layer, Layer); ft.type_assert(unique_ids, UniqueIds); this.layer = layer; this.uniqueIds = unique_ids; }; - - LayerInst.prototype.trace = function(indent) { + + LayerInst.prototype.trace = function (indent) { indent = indent || ""; ft.type_assert(indent, 'string'); ft.trace_fmt("{0}-= LayerInst =-", indent); @@ -531,40 +592,82 @@ if ( typeof Object.create != 'function' ) { ft.trace_fmt("{0}-Name : {1}" , indent, this.get_name()); ft.trace_fmt("{0}-Frames : {1}" , indent, this.layer.frameCount); }; - - LayerInst.prototype.get_id = function() { + + LayerInst.prototype.get_id = function () { return this.uniqueIds.get_string_id(this.get_name()); }; - - LayerInst.prototype.get_name = function() { + + LayerInst.prototype.get_name = function () { return this.layer.name; }; + + LayerInst.prototype.do_in_unlocked = function (func) { + ft.type_assert(func, Function); + var prev_locked = this.layer.locked; + var prev_visible = this.layer.visible; + this.layer.locked = false; + this.layer.visible = true; + func(); + this.layer.locked = prev_locked; + this.layer.visible = prev_visible; + }; - LayerInst.prototype.export_description = function(xml_node) { + LayerInst.prototype.convert = function (document, timeline) { + ft.type_assert(document, Document); + ft.type_assert(timeline, Timeline); + this.do_in_unlocked(function() { + ft.array_foreach(this.layer.frames, function(frame) { + frame.convertToFrameByFrameAnimation(); + }.bind(this)); + ft.array_foreach(this.layer.frames, function(frame, index) { + timeline.setSelectedFrames(index, index + 1); + var inst = new FrameInst(frame, index, this.uniqueIds); + if (inst.get_start_frame() == index) { + inst.convert(document, timeline, this.layer); + } + }.bind(this)); + }.bind(this)); + }; + + LayerInst.prototype.prepare = function (document, timeline) { + ft.type_assert(document, Document); + ft.type_assert(timeline, Timeline); + this.do_in_unlocked(function() { + ft.array_foreach(this.layer.frames, function(frame, index) { + timeline.setSelectedFrames(index, index + 1); + var inst = new FrameInst(frame, index, this.uniqueIds); + if (inst.get_start_frame() == index) { + inst.prepare(document, timeline, this.layer); + } + }.bind(this)); + }.bind(this)); + }; + + LayerInst.prototype.export_description = function (xml_node) { ft.type_assert(xml_node, XmlNode); var layer_node = xml_node.child("layer") - .attr("id" , this.get_id()) - .attr("visible" , this.layer.visible) - .attr("layer_type" , this.layer.layerType); - if ( this.layer.parentLayer ) { + .attr("id" , this.get_id()) + .attr("visible" , this.layer.visible) + .attr("layer_type", this.layer.layerType); + if (this.layer.parentLayer) { var parent_layer = new LayerInst(this.layer.parentLayer, this.uniqueIds); layer_node.attr("parent_layer", parent_layer.get_id()); } - ft.array_foreach(this.layer.frames, function(frame, index) { + ft.array_foreach(this.layer.frames, function (frame, index) { var inst = new FrameInst(frame, index, this.uniqueIds); - if ( inst.get_start_frame() == index ) { + if (inst.get_start_frame() == index) { inst.export_description(layer_node); } }.bind(this)); }; - + // ---------------------------------------------------------------------------- // // FrameInst // // ---------------------------------------------------------------------------- - var FrameInst = function(frame, index, unique_ids) { + var FrameInst = function (frame, index, unique_ids) { ft.type_assert(frame, Frame); ft.type_assert(index, 'number'); ft.type_assert(unique_ids, UniqueIds); @@ -572,8 +675,8 @@ if ( typeof Object.create != 'function' ) { this.index = index; this.uniqueIds = unique_ids; }; - - FrameInst.prototype.trace = function(indent) { + + FrameInst.prototype.trace = function (indent) { indent = indent || ""; ft.type_assert(indent, 'string'); ft.trace_fmt("{0}-= FrameInst =-", indent); @@ -581,51 +684,68 @@ if ( typeof Object.create != 'function' ) { ft.trace_fmt("{0}-Name : {1}", indent, this.get_name()); ft.trace_fmt("{0}-Elements : {1}", indent, this.frame.elements.length); }; - - FrameInst.prototype.get_id = function() { + + FrameInst.prototype.get_id = function () { return this.uniqueIds.get_string_id(this.get_name()); }; - - FrameInst.prototype.get_name = function() { + + FrameInst.prototype.get_name = function () { return this.frame.name; }; - - FrameInst.prototype.get_index = function() { + + FrameInst.prototype.get_index = function () { return this.index; }; - - FrameInst.prototype.get_start_frame = function() { + + FrameInst.prototype.get_start_frame = function () { return this.frame.startFrame; }; - - FrameInst.prototype.is_element_shape = function(element) { + + FrameInst.prototype.is_element_shape = function (element) { return element.elementType == "shape"; }; - - FrameInst.prototype.is_element_instance = function(element) { + + FrameInst.prototype.is_element_instance = function (element) { return element.elementType == "instance"; }; - - FrameInst.prototype.is_element_bitmap = function(element) { + + FrameInst.prototype.is_element_bitmap = function (element) { return this.is_element_instance(element) && element.instanceType == "bitmap"; }; - - FrameInst.prototype.is_element_symbol = function(element) { + + FrameInst.prototype.is_element_symbol = function (element) { return this.is_element_instance(element) && element.instanceType == "symbol"; }; - FrameInst.prototype.export_element = function(xml_node, element) { + FrameInst.prototype.convert = function (document, timeline, layer) { + ft.type_assert(document, Document); + ft.type_assert(timeline, Timeline); + ft.type_assert(layer, Layer); + }; + + FrameInst.prototype.prepare = function (document, timeline, layer) { + ft.type_assert(document, Document); + ft.type_assert(timeline, Timeline); + ft.type_assert(layer, Layer); + ft.array_foreach(this.frame.elements, function (element, index) { + if (this.is_element_shape(element)) { + timeline.currentFrame = this.frame.startFrame; + document.selection = [element]; + document.convertSelectionToBitmap(); + } + }.bind(this)); + }; + + FrameInst.prototype.export_element = function (xml_node, element) { ft.type_assert(xml_node, XmlNode); ft.type_assert(element, Element); - if ( this.is_element_shape(element) ) { - /// \TODO: shape to bitmap - } else if ( this.is_element_bitmap(element) ) { + if (this.is_element_bitmap(element)) { new BitmapInst(element, this.uniqueIds) .export_description(xml_node); - } else if ( this.is_element_symbol(element) ) { + } else if (this.is_element_symbol(element)) { new SymbolInst(element, this.uniqueIds) .export_description(xml_node); - } else if ( this.is_element_instance(element) ) { + } else if (this.is_element_instance(element)) { ft.assert(false, "Unsupported instance type ({0})!", element.instanceType); @@ -635,154 +755,152 @@ if ( typeof Object.create != 'function' ) { element.elementType); } }; - - FrameInst.prototype.export_description = function(xml_node) { + + FrameInst.prototype.export_description = function (xml_node) { ft.type_assert(xml_node, XmlNode); var frame_node = xml_node.child("frame") - .attr("id" , this.get_id()) - .attr("index" , this.get_index()) - .attr("duration" , this.frame.duration) - .attr("tween_type" , this.frame.tweenType) - .attr("tween_easing", this.frame.tweenEasing); - ft.array_foreach(this.frame.elements, function(element) { + .attr("id" , this.get_id()) + .attr("index" , this.get_index()) + .attr("duration", this.frame.duration); + ft.array_foreach(this.frame.elements, function (element) { this.export_element(frame_node, element); }.bind(this)); }; - + // ---------------------------------------------------------------------------- // // ElementInst // // ---------------------------------------------------------------------------- - var ElementInst = function(inst, unique_ids) { + var ElementInst = function (inst, unique_ids) { ft.type_assert(inst, Instance); ft.type_assert(unique_ids, UniqueIds); - this.inst = inst; + this.inst = inst; this.uniqueIds = unique_ids; }; - - ElementInst.prototype.trace = function(indent) { + + ElementInst.prototype.trace = function (indent) { indent = indent || ""; ft.type_assert(indent, 'string'); ft.trace_fmt("{0}-= ElementInst =-", indent); - ft.trace_fmt("{0}-Id : {1}" , indent, this.get_id()); - ft.trace_fmt("{0}-Name : {1}" , indent, this.get_name()); + ft.trace_fmt("{0}-Id : {1}", indent, this.get_id()); + ft.trace_fmt("{0}-Name : {1}", indent, this.get_name()); }; - - ElementInst.prototype.get_id = function() { + + ElementInst.prototype.get_id = function () { return this.uniqueIds.get_string_id(this.get_name()); }; - - ElementInst.prototype.get_name = function() { + + ElementInst.prototype.get_name = function () { return this.inst.name; }; - - ElementInst.prototype.export_description = function(xml_node) { + + ElementInst.prototype.export_description = function (xml_node) { ft.type_assert(xml_node, XmlNode); return xml_node.child("element") .attr("id" , this.get_id()) .attr("depth" , this.inst.depth) .attr("matrix", "{0};{1};{2};{3};{4};{5}".format( - this.inst.matrix.a, this.inst.matrix.b, - this.inst.matrix.c, this.inst.matrix.d, + this.inst.matrix.a, this.inst.matrix.b, + this.inst.matrix.c, this.inst.matrix.d, this.inst.matrix.tx, this.inst.matrix.ty)); }; - + // ---------------------------------------------------------------------------- // // BitmapInst // // ---------------------------------------------------------------------------- - - var BitmapInst = function(inst, unique_ids) { + + var BitmapInst = function (inst, unique_ids) { ElementInst.call(this, inst, unique_ids); }; - + BitmapInst.prototype = Object.create(ElementInst.prototype); - - BitmapInst.prototype.export_description = function(xml_node) { + + BitmapInst.prototype.export_description = function (xml_node) { ft.type_assert(xml_node, XmlNode); ElementInst.prototype.export_description.call(this, xml_node) .child("instance") - .attr("type" , "bitmap") - .attr("asset" , this.uniqueIds.get_string_id(this.inst.libraryItem.name)); + .attr("type" , "bitmap") + .attr("asset", this.uniqueIds.get_string_id(this.inst.libraryItem.name)); }; - + // ---------------------------------------------------------------------------- // // SymbolInst // // ---------------------------------------------------------------------------- - - var SymbolInst = function(inst, unique_ids) { + + var SymbolInst = function (inst, unique_ids) { ElementInst.call(this, inst, unique_ids); }; - + SymbolInst.prototype = Object.create(ElementInst.prototype); - - SymbolInst.prototype.export_description = function(xml_node) { + + SymbolInst.prototype.export_description = function (xml_node) { ft.type_assert(xml_node, XmlNode); var instance_node = ElementInst.prototype.export_description.call(this, xml_node) .child("instance") - .attr("type" , "symbol") - .attr("symbol_type" , this.inst.symbolType) - .attr("asset" , this.uniqueIds.get_string_id(this.inst.libraryItem.name)) - .attr("visible" , this.inst.visible) - .attr("blend_mode" , this.inst.blendMode); - if ( this.inst.colorMode !== "none" ) { + .attr("type" , "symbol") + .attr("symbol_type", this.inst.symbolType) + .attr("asset" , this.uniqueIds.get_string_id(this.inst.libraryItem.name)) + .attr("visible" , this.inst.visible) + .attr("blend_mode" , this.inst.blendMode); + if (this.inst.colorMode !== "none") { var color_mode_node = instance_node.child("color_mode") .attr("color_mode", this.inst.colorMode); - if ( this.inst.colorMode == "brightness" ) { + if (this.inst.colorMode == "brightness") { color_mode_node .attr("brightness", this.inst.brightness); - } else if ( this.inst.colorMode == "tint" ) { + } else if (this.inst.colorMode == "tint") { color_mode_node .attr("tint" , this.inst.tintPercent) .attr("color", this.inst.tintColor); - } else if ( this.inst.colorMode == "alpha" ) { + } else if (this.inst.colorMode == "alpha") { color_mode_node .attr("alpha", this.inst.colorAlphaPercent); - } else if ( this.inst.colorMode == "advanced" ) { + } else if (this.inst.colorMode == "advanced") { color_mode_node .attr("a", "{0};{1}".format(this.inst.colorAlphaAmount, this.inst.colorAlphaPercent)) - .attr("r", "{0};{1}".format(this.inst.colorRedAmount , this.inst.colorRedPercent)) + .attr("r", "{0};{1}".format(this.inst.colorRedAmount, this.inst.colorRedPercent)) .attr("g", "{0};{1}".format(this.inst.colorGreenAmount, this.inst.colorGreenPercent)) - .attr("b", "{0};{1}".format(this.inst.colorBlueAmount , this.inst.colorBluePercent)); + .attr("b", "{0};{1}".format(this.inst.colorBlueAmount, this.inst.colorBluePercent)); } else { ft.assert(false, "Unsupported color mode ({0})!", this.inst.colorMode); } } - if ( this.inst.loop !== undefined && this.inst.firstFrame !== undefined ) { + if (this.inst.loop !== undefined && this.inst.firstFrame !== undefined) { instance_node.child("looping") .attr("loop" , this.inst.loop) .attr("first_frame", this.inst.firstFrame); } - if ( this.inst.filters && this.inst.filters.length > 0 ) { + if (this.inst.filters && this.inst.filters.length > 0) { var filters_node = instance_node.child("filters"); - ft.array_foreach(this.inst.filters, function(filter) { + ft.array_foreach(this.inst.filters, function (filter) { /// \TODO export filters filters_node.child("filter") .attr("name", filter.name); }); } }; - + // ---------------------------------------------------------------------------- // // Exporter // // ---------------------------------------------------------------------------- - - var Exporter = function(document) { + + var Exporter = function (document) { ft.type_assert(document, Document); this.document = document; this.uniqueIds = new UniqueIds(); }; - - Exporter.prototype.trace = function(indent) { + + Exporter.prototype.trace = function (indent) { indent = indent || ""; ft.type_assert(indent, 'string'); ft.trace_fmt("{0}-= Exporter =-", indent); @@ -790,93 +908,112 @@ if ( typeof Object.create != 'function' ) { ft.trace_fmt("{0}-Document path : {1}", indent, this.get_document_path()); ft.trace_fmt("{0}-Export folter : {1}", indent, this.get_export_folder()); }; - - Exporter.prototype.get_document_path = function() { + + Exporter.prototype.get_document_path = function () { return ft.escape_path(this.document.pathURI); }; - Exporter.prototype.get_export_folder = function() { + Exporter.prototype.get_export_folder = function () { return ft.combine_path( this.get_document_path(), "_export/"); }; - - Exporter.prototype.get_stage_export_path = function() { + + Exporter.prototype.get_stage_export_path = function () { return ft.combine_path( this.get_export_folder(), "stage.xml"); }; - Exporter.prototype.get_library_export_path = function() { + Exporter.prototype.get_library_export_path = function () { return ft.combine_path( this.get_export_folder(), "library.xml"); }; - Exporter.prototype.get_strings_export_path = function() { + Exporter.prototype.get_strings_export_path = function () { return ft.combine_path( this.get_export_folder(), "strings.xml"); }; - - Exporter.prototype.export = function() { + + Exporter.prototype.export = function () { this.trace(); ft.trace("- Start..."); try { - this.prepare_export_folder(); + this.prepare_folders(); + this.convert_document(); + this.prepare_document(); this.export_library(); this.export_stage(); this.export_strings(); ft.trace_fmt("- Finish : {0}", this.get_export_folder()); - } catch ( e ) { + } catch (e) { ft.trace_fmt("- Error : {0}", e); } }; - - Exporter.prototype.prepare_export_folder = function() { + + Exporter.prototype.prepare_folders = function () { var export_folder = this.get_export_folder(); - if ( FLfile.exists(export_folder) ) { - if ( !FLfile.remove(export_folder) ) { + if (FLfile.exists(export_folder)) { + if (!FLfile.remove(export_folder)) { throw "Can't remove document export folder ({0})!" .format(export_folder); } } - if ( !FLfile.createFolder(export_folder) ) { + if (!FLfile.createFolder(export_folder)) { throw "Can't create document export folder ({0})!" .format(export_folder); } - if ( !FLfile.createFolder(export_folder + "bitmaps/") ) { + if (!FLfile.createFolder(export_folder + "bitmaps/")) { throw "Can't create document bitmaps export folder ({0})!" .format(export_folder); } - if ( !FLfile.createFolder(export_folder + "symbols/") ) { + if (!FLfile.createFolder(export_folder + "symbols/")) { throw "Can't create document symbols export folder ({0})!" .format(export_folder); } }; - Exporter.prototype.export_library = function() { + Exporter.prototype.convert_document = function () { + this.exit_edit_mode(); + new TimelineInst(this.document.getTimeline(), this.uniqueIds) + .convert(this.document); + new LibraryInst(this.document.library, this.uniqueIds) + .convert(this.document); + }; + + Exporter.prototype.prepare_document = function () { + this.exit_edit_mode(); + new TimelineInst(this.document.getTimeline(), this.uniqueIds) + .prepare(this.document); + new LibraryInst(this.document.library, this.uniqueIds) + .prepare(this.document); + }; + + Exporter.prototype.export_library = function () { + this.exit_edit_mode(); var xml_node = new XmlNode("library") .attr("frame_rate", this.document.frameRate); new LibraryInst(this.document.library, this.uniqueIds) .export(this.get_export_folder(), xml_node); xml_node.save(this.get_library_export_path()); }; - - Exporter.prototype.export_stage = function() { + + Exporter.prototype.export_stage = function () { this.exit_edit_mode(); var xml_node = new XmlNode("stage"); new TimelineInst(this.document.getTimeline(), this.uniqueIds) .export_description(xml_node); xml_node.save(this.get_stage_export_path(document)); }; - - Exporter.prototype.export_strings = function() { + + Exporter.prototype.export_strings = function () { this.uniqueIds.save(this.get_strings_export_path()); }; - - Exporter.prototype.exit_edit_mode = function() { - for ( var i = 0; i < 100; ++i ) { + + Exporter.prototype.exit_edit_mode = function () { + for (var i = 0; i < 100; ++i) { this.document.exitEditMode(); } }; @@ -885,10 +1022,10 @@ if ( typeof Object.create != 'function' ) { // Main // ------------------------------------ - (function() { + (function () { ft.clear_output(); - ft.array_foreach(fl.documents, function(document) { + ft.array_foreach(fl.documents, function (document) { new Exporter(document).export(); }); })(); -})(); +})(); \ No newline at end of file