mirror of
https://github.com/BlackMATov/unity-flash-tools.git
synced 2026-03-22 04:44:08 +07:00
refactoring
This commit is contained in:
@@ -31,185 +31,232 @@ if ( !Function.prototype.bind ) {
|
||||
};
|
||||
}
|
||||
|
||||
// ------------------------------------
|
||||
// FlashTools
|
||||
// ------------------------------------
|
||||
|
||||
var FlashTools = function() {
|
||||
this.defaultIndent = " ";
|
||||
this.ClearStringIds();
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
// Common functions
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.Trace = function(text) {
|
||||
this.TypeAssert(text, 'string');
|
||||
fl.outputPanel.trace(text);
|
||||
};
|
||||
|
||||
FlashTools.prototype.TraceError = function(text) {
|
||||
this.TypeAssert(text, 'string');
|
||||
this.Trace("!!!Error!!!: " + text);
|
||||
};
|
||||
|
||||
FlashTools.prototype.Assert = function(expr, msg) {
|
||||
if ( !expr ) {
|
||||
throw msg != undefined ? "Assert! " + msg : "Assert!";
|
||||
}
|
||||
};
|
||||
|
||||
FlashTools.prototype.TypeAssert = function(item, type) {
|
||||
this.Assert(
|
||||
(typeof type === 'string' && typeof item === type) || (item instanceof type),
|
||||
"Type error: {0} != {1}".format(typeof item, type.name));
|
||||
};
|
||||
|
||||
FlashTools.prototype.TypeAssertIfDefined = function(item, type) {
|
||||
if ( item != undefined ) {
|
||||
this.TypeAssert(item, type);
|
||||
}
|
||||
};
|
||||
|
||||
FlashTools.prototype.ClearOutput = function() {
|
||||
fl.outputPanel.clear();
|
||||
};
|
||||
|
||||
FlashTools.prototype.EscapePath = function(path) {
|
||||
this.TypeAssert(path, 'string');
|
||||
return path.replace(/ /g, '%20');
|
||||
};
|
||||
|
||||
FlashTools.prototype.EscapeString = function(str) {
|
||||
this.TypeAssert(str, 'string');
|
||||
return str
|
||||
.replace(/\&/g, '&')
|
||||
.replace(/\"/g, '"')
|
||||
.replace(/\'/g, ''')
|
||||
.replace(/\</g, '<')
|
||||
.replace(/\>/g, '>');
|
||||
};
|
||||
|
||||
FlashTools.prototype.CombinePath = function(lhs, rhs) {
|
||||
this.TypeAssert(lhs, 'string');
|
||||
this.TypeAssert(rhs, 'string');
|
||||
return this.EscapePath(lhs) + this.EscapePath(rhs);
|
||||
};
|
||||
|
||||
FlashTools.prototype.ClearStringIds = function() {
|
||||
this.stringIds = {};
|
||||
this.lastStringId = 0;
|
||||
};
|
||||
|
||||
FlashTools.prototype.GetStringId = function(str) {
|
||||
this.TypeAssert(str, 'string');
|
||||
var id = this.stringIds[str];
|
||||
if ( id == undefined ) {
|
||||
this.stringIds[str] = ++this.lastStringId;
|
||||
return this.lastStringId;
|
||||
} else {
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
FlashTools.prototype.ExportStringIdsXmlContent = function(xml_node) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
for ( var str in this.stringIds ) {
|
||||
if ( this.stringIds.hasOwnProperty(str) ) {
|
||||
xml_node.Child("string")
|
||||
.Attr("id" , this.stringIds[str])
|
||||
.Attr("str", this.EscapeString(str));
|
||||
if ( !Array.prototype.find ) {
|
||||
Array.prototype.find = function(predicate) {
|
||||
if (this == null) {
|
||||
throw new TypeError('Array.prototype.find called on null or undefined');
|
||||
}
|
||||
}
|
||||
};
|
||||
if (typeof predicate !== 'function') {
|
||||
throw new TypeError('predicate must be a function');
|
||||
}
|
||||
var list = Object(this);
|
||||
var length = list.length >>> 0;
|
||||
var thisArg = arguments[1];
|
||||
var value;
|
||||
for (var i = 0; i < length; i++) {
|
||||
value = list[i];
|
||||
if (predicate.call(thisArg, value, i, list)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
}
|
||||
|
||||
// ------------------------------------
|
||||
// Xml functions
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.AssertTypeXmlNode = function(xml_node) {
|
||||
this.Assert(
|
||||
xml_node != undefined &&
|
||||
xml_node.IsXmlNode != undefined &&
|
||||
xml_node.IsXmlNode(),
|
||||
"Type error: {0} != {1}".format(typeof xml_node, "XmlNode"));
|
||||
};
|
||||
|
||||
FlashTools.prototype.XmlNode = function(node_name) {
|
||||
var Assert = this.Assert.bind(this);
|
||||
var TypeAssert = this.TypeAssert.bind(this);
|
||||
var TypeAssertIfDefined = this.TypeAssertIfDefined.bind(this);
|
||||
var EscapeString = this.EscapeString.bind(this);
|
||||
var DefaultIndent = this.defaultIndent;
|
||||
(function() {
|
||||
|
||||
var Ctor = function(node_name, node_parent) {
|
||||
TypeAssert(node_name, 'string');
|
||||
TypeAssertIfDefined(node_parent, Ctor);
|
||||
// ------------------------------------
|
||||
// ft
|
||||
// ------------------------------------
|
||||
|
||||
var ft = {};
|
||||
|
||||
ft.default_indent = " ";
|
||||
|
||||
ft.trace = function() {
|
||||
fl.outputPanel.trace(
|
||||
Array.prototype.join.call(arguments, " "));
|
||||
};
|
||||
|
||||
ft.trace_fmt = function(format) {
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
ft.trace(format.format.apply(format, args));
|
||||
};
|
||||
|
||||
ft.clear_output = function() {
|
||||
fl.outputPanel.clear();
|
||||
};
|
||||
|
||||
ft.assert = function(expr, format) {
|
||||
if ( !expr ) {
|
||||
if ( format == undefined ) {
|
||||
throw "!!!Assert!!!";
|
||||
} else {
|
||||
var args = Array.prototype.slice.call(arguments, 2);
|
||||
throw "!!!Assert!!! " + format.format.apply(format, args);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ft.type_assert = function(item, type) {
|
||||
var type_is_string = (typeof type === 'string');
|
||||
ft.assert(
|
||||
( 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);
|
||||
};
|
||||
|
||||
ft.type_assert_if_defined = function(item, type) {
|
||||
if ( item != undefined ) {
|
||||
ft.type_assert(item, type);
|
||||
}
|
||||
};
|
||||
|
||||
ft.escape_path = function(path) {
|
||||
ft.type_assert(path, 'string');
|
||||
return path.replace(/ /g, '%20');
|
||||
};
|
||||
|
||||
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.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.type_assert(arr, Array);
|
||||
ft.type_assert(func, Function);
|
||||
ft.type_assert_if_defined(filter, Function);
|
||||
for ( var i = 0; i < arr.length; ++i ) {
|
||||
var val = arr[i];
|
||||
if ( !filter || filter(val) ) {
|
||||
func(val);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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) ) {
|
||||
var val = obj[key];
|
||||
if ( !filter || filter(key, val) ) {
|
||||
func(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
// UniqueIds
|
||||
// ------------------------------------
|
||||
|
||||
var UniqueIds = function() {
|
||||
this.clear();
|
||||
};
|
||||
|
||||
UniqueIds.prototype.clear = function() {
|
||||
this.stringIds = {};
|
||||
this.lastStringId = 0;
|
||||
};
|
||||
|
||||
UniqueIds.prototype.get_string_id = function(str) {
|
||||
ft.type_assert(str, 'string');
|
||||
var id = this.stringIds[str];
|
||||
if ( id == undefined ) {
|
||||
this.stringIds[str] = ++this.lastStringId;
|
||||
return this.lastStringId;
|
||||
} else {
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
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, val) {
|
||||
xml_node.child("string")
|
||||
.attr("id" , val)
|
||||
.attr("str", ft.escape_string(key));
|
||||
});
|
||||
xml_node.save(xml_path);
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
// XmlNode
|
||||
// ------------------------------------
|
||||
|
||||
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.children = [];
|
||||
};
|
||||
|
||||
Ctor.prototype.IsXmlNode = function() {
|
||||
return true;
|
||||
};
|
||||
|
||||
Ctor.prototype.Attr = function(attr_name, attr_value) {
|
||||
TypeAssert(attr_name, 'string');
|
||||
attr_value = EscapeString(attr_value.toString());
|
||||
for ( var i = 0; i < this.attrs.length; ++i ) {
|
||||
var attr = this.attrs[i];
|
||||
if ( attr.name == attr_name ) {
|
||||
attr.value = attr_value;
|
||||
return this;
|
||||
}
|
||||
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) {
|
||||
return attr.name == attr_name;
|
||||
});
|
||||
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;
|
||||
};
|
||||
|
||||
Ctor.prototype.Child = function(child_name) {
|
||||
TypeAssert(child_name, 'string');
|
||||
var child = new Ctor(child_name, this);
|
||||
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;
|
||||
};
|
||||
|
||||
Ctor.prototype.Parent = function() {
|
||||
Assert(this.parent != undefined, "node parent is undefined");
|
||||
return this.parent;
|
||||
};
|
||||
|
||||
Ctor.prototype.Content = function(indent) {
|
||||
indent = indent == undefined ? "" : indent;
|
||||
XmlNode.prototype.content = function(indent) {
|
||||
indent = indent || "";
|
||||
var str = '{0}<{1}'.format(indent, this.name);
|
||||
if ( this.attrs.length > 0 ) {
|
||||
for ( var i = 0; i < this.attrs.length; ++i ) {
|
||||
var attr = this.attrs[i];
|
||||
str += ' {0}="{1}"'.format(attr.name, attr.value);
|
||||
}
|
||||
}
|
||||
ft.array_foreach(this.attrs, function(attr) {
|
||||
str += ' {0}="{1}"'.format(attr.name, attr.value);
|
||||
});
|
||||
if ( this.children.length > 0 ) {
|
||||
str += ">\n";
|
||||
for ( var i = 0; i < this.children.length; ++i ) {
|
||||
var child = this.children[i];
|
||||
str += child.Content(indent + DefaultIndent) + "\n";
|
||||
}
|
||||
ft.array_foreach(this.children, function(child) {
|
||||
str += child.content(indent + ft.default_indent) + "\n";
|
||||
});
|
||||
return str + "{0}<{1}/>".format(indent, this.name);
|
||||
} else {
|
||||
return str + "/>";
|
||||
}
|
||||
};
|
||||
|
||||
Ctor.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);
|
||||
}
|
||||
};
|
||||
|
||||
return new Ctor(node_name);
|
||||
|
||||
// ------------------------------------
|
||||
// FlashTools
|
||||
// ------------------------------------
|
||||
|
||||
var FlashTools = function() {
|
||||
this.uniqueIds = new UniqueIds();
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
// Xml functions
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.XmlNode = function(node_name) {
|
||||
return new XmlNode(node_name);
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
@@ -217,17 +264,17 @@ FlashTools.prototype.XmlNode = function(node_name) {
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.IsFolderLibraryItem = function(item) {
|
||||
this.TypeAssert(item, LibraryItem);
|
||||
ft.type_assert(item, LibraryItem);
|
||||
return item.itemType == "folder";
|
||||
};
|
||||
|
||||
FlashTools.prototype.IsBitmapLibraryItem = function(item) {
|
||||
this.TypeAssert(item, LibraryItem);
|
||||
ft.type_assert(item, LibraryItem);
|
||||
return item.itemType == "bitmap";
|
||||
};
|
||||
|
||||
FlashTools.prototype.IsSymbolLibraryItem = function(item) {
|
||||
this.TypeAssert(item, LibraryItem);
|
||||
ft.type_assert(item, LibraryItem);
|
||||
var item_type = item.itemType;
|
||||
return item_type == "graphic" || item_type == "component" || item_type == "movie clip";
|
||||
};
|
||||
@@ -237,28 +284,28 @@ FlashTools.prototype.IsSymbolLibraryItem = function(item) {
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.BitmapItem_TraceInfo = function(item) {
|
||||
this.TypeAssert(item, BitmapItem);
|
||||
this.Trace("{0}Name : {1}".format(this.defaultIndent, item.name));
|
||||
this.Trace("{0}ExportFilename : {1}".format(this.defaultIndent, this.BitmapItem_GetExportFilename(item)));
|
||||
ft.type_assert(item, BitmapItem);
|
||||
ft.trace_fmt("Name : {0}", item.name);
|
||||
ft.trace_fmt("ExportFilename : {0}", this.BitmapItem_GetExportFilename(item));
|
||||
};
|
||||
|
||||
FlashTools.prototype.BitmapItem_GetExportFilename = function(item) {
|
||||
this.TypeAssert(item, BitmapItem);
|
||||
var item_id = this.GetStringId(item.name);
|
||||
ft.type_assert(item, BitmapItem);
|
||||
var item_id = this.uniqueIds.get_string_id(item.name);
|
||||
return "bitmaps/{0}.png".format(item_id);
|
||||
};
|
||||
|
||||
FlashTools.prototype.BitmapItem_GetExportFullFilename = function(document, item) {
|
||||
this.TypeAssert(document, Document);
|
||||
this.TypeAssert(item, BitmapItem);
|
||||
return this.CombinePath(
|
||||
ft.type_assert(document, Document);
|
||||
ft.type_assert(item, BitmapItem);
|
||||
return ft.combine_path(
|
||||
this.Document_GetExportFolder(document),
|
||||
this.BitmapItem_GetExportFilename(item));
|
||||
};
|
||||
|
||||
FlashTools.prototype.BitmapItem_Export = function(document, item) {
|
||||
this.TypeAssert(document, Document);
|
||||
this.TypeAssert(item, BitmapItem);
|
||||
ft.type_assert(document, Document);
|
||||
ft.type_assert(item, BitmapItem);
|
||||
var item_export_path = this.BitmapItem_GetExportFullFilename(document, item);
|
||||
if ( !item.exportToFile(item_export_path) ) {
|
||||
throw "Can't export bitmap ({0})!"
|
||||
@@ -267,12 +314,12 @@ FlashTools.prototype.BitmapItem_Export = function(document, item) {
|
||||
};
|
||||
|
||||
FlashTools.prototype.BitmapItem_ExportXmlDescription = function(xml_node, item) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(item, BitmapItem);
|
||||
xml_node.Child("asset")
|
||||
.Attr("name" , this.GetStringId(item.name))
|
||||
.Attr("type" , "bitmap")
|
||||
.Attr("filename", this.BitmapItem_GetExportFilename(item));
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(item, BitmapItem);
|
||||
xml_node.child("asset")
|
||||
.attr("name" , this.uniqueIds.get_string_id(item.name))
|
||||
.attr("type" , "bitmap")
|
||||
.attr("filename", this.BitmapItem_GetExportFilename(item));
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
@@ -280,40 +327,40 @@ FlashTools.prototype.BitmapItem_ExportXmlDescription = function(xml_node, item)
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.SymbolItem_TraceInfo = function(item) {
|
||||
this.TypeAssert(item, SymbolItem);
|
||||
this.Trace("{0}Name : {1}".format(this.defaultIndent, item.name));
|
||||
this.Trace("{0}ExportFilename : {1}".format(this.defaultIndent, this.SymbolItem_GetExportFilename(item)));
|
||||
ft.type_assert(item, SymbolItem);
|
||||
ft.trace_fmt("Name : {0}", item.name);
|
||||
ft.trace_fmt("ExportFilename : {0}", this.SymbolItem_GetExportFilename(item));
|
||||
};
|
||||
|
||||
FlashTools.prototype.SymbolItem_GetExportFilename = function(item) {
|
||||
this.TypeAssert(item, SymbolItem);
|
||||
var item_id = this.GetStringId(item.name);
|
||||
ft.type_assert(item, SymbolItem);
|
||||
var item_id = this.uniqueIds.get_string_id(item.name);
|
||||
return "symbols/{0}.xml".format(item_id);
|
||||
};
|
||||
|
||||
FlashTools.prototype.SymbolItem_GetExportFullFilename = function(document, item) {
|
||||
this.TypeAssert(item, SymbolItem);
|
||||
return this.CombinePath(
|
||||
ft.type_assert(item, SymbolItem);
|
||||
return ft.combine_path(
|
||||
this.Document_GetExportFolder(document),
|
||||
this.SymbolItem_GetExportFilename(item));
|
||||
};
|
||||
|
||||
FlashTools.prototype.SymbolItem_Export = function(document, item) {
|
||||
this.TypeAssert(document, Document);
|
||||
this.TypeAssert(item, SymbolItem);
|
||||
ft.type_assert(document, Document);
|
||||
ft.type_assert(item, SymbolItem);
|
||||
var xml_node = this.XmlNode("symbol")
|
||||
.Attr("name", this.GetStringId(item.name));
|
||||
.attr("name", this.uniqueIds.get_string_id(item.name));
|
||||
this.Timeline_ExportXmlContent(xml_node, item.timeline);
|
||||
xml_node.Save(this.SymbolItem_GetExportFullFilename(document, item));
|
||||
xml_node.save(this.SymbolItem_GetExportFullFilename(document, item));
|
||||
};
|
||||
|
||||
FlashTools.prototype.SymbolItem_ExportXmlDescription = function(xml_node, item) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(item, SymbolItem);
|
||||
xml_node.Child("symbol")
|
||||
.Attr("name" , this.GetStringId(item.name))
|
||||
.Attr("type" , "symbol")
|
||||
.Attr("filename", this.SymbolItem_GetExportFilename(item));
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(item, SymbolItem);
|
||||
xml_node.child("symbol")
|
||||
.attr("name" , this.uniqueIds.get_string_id(item.name))
|
||||
.attr("type" , "symbol")
|
||||
.attr("filename", this.SymbolItem_GetExportFilename(item));
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
@@ -321,8 +368,8 @@ FlashTools.prototype.SymbolItem_ExportXmlDescription = function(xml_node, item)
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.Element_ExportXmlContent = function(xml_node, element) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(element, Element);
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(element, Element);
|
||||
|
||||
if ( element.elementType == "shape" ) {
|
||||
/// \TODO: shape to bitmap
|
||||
@@ -333,29 +380,29 @@ FlashTools.prototype.Element_ExportXmlContent = function(xml_node, element) {
|
||||
throw "Unsupported element type ({0})!"
|
||||
.format(element.elementType);
|
||||
}
|
||||
//this.Trace("Instance type : " + element.instanceType);
|
||||
//this.Trace("Library item : " + element.libraryItem.name);
|
||||
//ft.trace("Instance type : " + element.instanceType);
|
||||
//ft.trace("Library item : " + element.libraryItem.name);
|
||||
} else {
|
||||
throw "Unsupported element type ({0})!"
|
||||
.format(element.elementType);
|
||||
}
|
||||
|
||||
var element_node = xml_node.Child("element")
|
||||
.Attr("name" , this.GetStringId(element.name))
|
||||
.Attr("depth", element.depth);
|
||||
var element_node = xml_node.child("element")
|
||||
.attr("name" , this.uniqueIds.get_string_id(element.name))
|
||||
.attr("depth", element.depth);
|
||||
this.ElementTransform_ExportXmlContent(element_node, element);
|
||||
};
|
||||
|
||||
FlashTools.prototype.ElementTransform_ExportXmlContent = function(xml_node, element) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(element, Element);
|
||||
xml_node.Child("transform")
|
||||
.Attr("a" , element.matrix.a)
|
||||
.Attr("b" , element.matrix.b)
|
||||
.Attr("c" , element.matrix.c)
|
||||
.Attr("d" , element.matrix.d)
|
||||
.Attr("tx", element.matrix.tx)
|
||||
.Attr("ty", element.matrix.ty);
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(element, Element);
|
||||
xml_node.child("transform")
|
||||
.attr("a" , element.matrix.a)
|
||||
.attr("b" , element.matrix.b)
|
||||
.attr("c" , element.matrix.c)
|
||||
.attr("d" , element.matrix.d)
|
||||
.attr("tx", element.matrix.tx)
|
||||
.attr("ty", element.matrix.ty);
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
@@ -363,23 +410,22 @@ FlashTools.prototype.ElementTransform_ExportXmlContent = function(xml_node, elem
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.Frame_ExportXmlContent = function(xml_node, frame) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(frame, Frame);
|
||||
var frame_node = xml_node.Child("frame")
|
||||
.Attr("name" , this.GetStringId(frame.name))
|
||||
.Attr("start_frame", frame.startFrame)
|
||||
.Attr("duration" , frame.duration)
|
||||
.Attr("elements" , frame.elements.length);
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(frame, Frame);
|
||||
var frame_node = xml_node.child("frame")
|
||||
.attr("name" , this.uniqueIds.get_string_id(frame.name))
|
||||
.attr("start_frame", frame.startFrame)
|
||||
.attr("duration" , frame.duration)
|
||||
.attr("elements" , frame.elements.length);
|
||||
this.FrameElements_ExportXmlContent(frame_node, frame);
|
||||
};
|
||||
|
||||
FlashTools.prototype.FrameElements_ExportXmlContent = function(xml_node, frame) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(frame, Frame);
|
||||
for ( var i = 0; i < frame.elements.length; ++i ) {
|
||||
var element = frame.elements[i];
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(frame, Frame);
|
||||
ft.array_foreach(frame.elements, function(element) {
|
||||
this.Element_ExportXmlContent(xml_node, element);
|
||||
}
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
@@ -387,28 +433,27 @@ FlashTools.prototype.FrameElements_ExportXmlContent = function(xml_node, frame)
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.Layer_ExportXmlContent = function(xml_node, layer) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(layer, Layer);
|
||||
var layer_node = xml_node.Child("layer")
|
||||
.Attr("name" , this.GetStringId(layer.name))
|
||||
.Attr("type" , layer.layerType)
|
||||
.Attr("frames" , layer.frameCount)
|
||||
.Attr("locked" , layer.locked)
|
||||
.Attr("visible" , layer.visible)
|
||||
.Attr("animation_type", layer.animationType);
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(layer, Layer);
|
||||
var layer_node = xml_node.child("layer")
|
||||
.attr("name" , this.uniqueIds.get_string_id(layer.name))
|
||||
.attr("type" , layer.layerType)
|
||||
.attr("frames" , layer.frameCount)
|
||||
.attr("locked" , layer.locked)
|
||||
.attr("visible" , layer.visible)
|
||||
.attr("animation_type", layer.animationType);
|
||||
if ( layer.parentLayer ) {
|
||||
layer_node.Attr("parent_layer", this.GetStringId(layer.parentLayer.name));
|
||||
layer_node.attr("parent_layer", this.uniqueIds.get_string_id(layer.parentLayer.name));
|
||||
}
|
||||
this.LayerFrames_ExportXmlContent(layer_node, layer);
|
||||
};
|
||||
|
||||
FlashTools.prototype.LayerFrames_ExportXmlContent = function(xml_node, layer) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(layer, Layer);
|
||||
for ( var i = 0; i < layer.frames.length; ++i ) {
|
||||
var frame = layer.frames[i];
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(layer, Layer);
|
||||
ft.array_foreach(layer.frames, function(frame) {
|
||||
this.Frame_ExportXmlContent(xml_node, frame);
|
||||
}
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
@@ -416,28 +461,27 @@ FlashTools.prototype.LayerFrames_ExportXmlContent = function(xml_node, layer) {
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.Timeline_TraceInfo = function(timeline) {
|
||||
this.TypeAssert(timeline, Timeline);
|
||||
this.Trace("{0}Name : {1}".format(this.defaultIndent, timeline.name));
|
||||
this.Trace("{0}Layer count : {1}".format(this.defaultIndent, timeline.layerCount));
|
||||
this.Trace("{0}Frame count : {1}".format(this.defaultIndent, timeline.frameCount));
|
||||
ft.type_assert(timeline, Timeline);
|
||||
ft.trace_fmt("Name : {0}", timeline.name);
|
||||
ft.trace_fmt("Layer count : {0}", timeline.layerCount);
|
||||
ft.trace_fmt("Frame count : {0}", timeline.frameCount);
|
||||
};
|
||||
|
||||
FlashTools.prototype.Timeline_ExportXmlContent = function(xml_node, timeline) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(timeline, Timeline);
|
||||
var timeline_node = xml_node.Child("timeline")
|
||||
.Attr("layers", timeline.layerCount)
|
||||
.Attr("frames", timeline.frameCount);
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(timeline, Timeline);
|
||||
var timeline_node = xml_node.child("timeline")
|
||||
.attr("layers", timeline.layerCount)
|
||||
.attr("frames", timeline.frameCount);
|
||||
this.TimelineLayers_ExportXmlContent(timeline_node, timeline);
|
||||
};
|
||||
|
||||
FlashTools.prototype.TimelineLayers_ExportXmlContent = function(xml_node, timeline) {
|
||||
this.AssertTypeXmlNode(xml_node);
|
||||
this.TypeAssert(timeline, Timeline);
|
||||
for ( var i = 0; i < timeline.layers.length; ++i ) {
|
||||
var layer = timeline.layers[i];
|
||||
ft.type_assert(xml_node, XmlNode);
|
||||
ft.type_assert(timeline, Timeline);
|
||||
ft.array_foreach(timeline.layers, function(layer) {
|
||||
this.Layer_ExportXmlContent(xml_node, layer);
|
||||
}
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
@@ -445,58 +489,61 @@ FlashTools.prototype.TimelineLayers_ExportXmlContent = function(xml_node, timeli
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.Document_TraceInfo = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
this.Trace("{0}Name : {1}".format(this.defaultIndent, document.name));
|
||||
this.Trace("{0}Path : {1}".format(this.defaultIndent, this.Document_GetPath(document)));
|
||||
this.Trace("{0}ExportFolder : {1}".format(this.defaultIndent, this.Document_GetExportFolder(document)));
|
||||
ft.type_assert(document, Document);
|
||||
ft.trace_fmt("Name : {0}", document.name);
|
||||
ft.trace_fmt("Path : {0}", this.Document_GetPath(document));
|
||||
ft.trace_fmt("ExportFolder : {0}", this.Document_GetExportFolder(document));
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_GetPath = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
return this.EscapePath(document.pathURI);
|
||||
ft.type_assert(document, Document);
|
||||
return ft.escape_path(document.pathURI);
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_GetExportFolder = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
return this.Document_GetPath(document) + "_export/";
|
||||
ft.type_assert(document, Document);
|
||||
return ft.combine_path(
|
||||
this.Document_GetPath(document),
|
||||
"_export/");
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_GetStageExportPath = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
return this.Document_GetExportFolder(document) + "stage.xml";
|
||||
ft.type_assert(document, Document);
|
||||
return ft.combine_path(
|
||||
this.Document_GetExportFolder(document),
|
||||
"stage.xml");
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_GetLibraryExportPath = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
return this.Document_GetExportFolder(document) + "library.xml";
|
||||
ft.type_assert(document, Document);
|
||||
return ft.combine_path(
|
||||
this.Document_GetExportFolder(document),
|
||||
"library.xml");
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_GetStringIdsExportPath = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
return this.Document_GetExportFolder(document) + "strings.xml";
|
||||
ft.type_assert(document, Document);
|
||||
return ft.combine_path(
|
||||
this.Document_GetExportFolder(document),
|
||||
"strings.xml");
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_ExitEditMode = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
ft.type_assert(document, Document);
|
||||
for ( var i = 0; i < 100; ++i ) {
|
||||
document.exitEditMode();
|
||||
}
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_ForEachByLibraryItems = function(document, func, filter_func) {
|
||||
this.TypeAssert(document, Document);
|
||||
this.TypeAssert(func, 'function');
|
||||
this.TypeAssertIfDefined(filter_func, 'function');
|
||||
for ( var i = 0; i < document.library.items.length; ++i ) {
|
||||
var item = document.library.items[i];
|
||||
if ( filter_func == undefined || filter_func(item) ) {
|
||||
func(item);
|
||||
}
|
||||
}
|
||||
FlashTools.prototype.Document_ForEachByLibraryItems = function(document, func, filter) {
|
||||
ft.type_assert(document, Document);
|
||||
ft.type_assert(func, Function);
|
||||
ft.type_assert_if_defined(filter, Function);
|
||||
ft.array_foreach(document.library.items, func, filter);
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_PrepareExportFolder = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
ft.type_assert(document, Document);
|
||||
var export_folder = this.Document_GetExportFolder(document);
|
||||
if ( FLfile.exists(export_folder) ) {
|
||||
if ( !FLfile.remove(export_folder) ) {
|
||||
@@ -511,9 +558,9 @@ FlashTools.prototype.Document_PrepareExportFolder = function(document) {
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_ExportLibrary = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
ft.type_assert(document, Document);
|
||||
var xml_node = this.XmlNode("library")
|
||||
.Attr("frame_rate", document.frameRate);
|
||||
.attr("frame_rate", document.frameRate);
|
||||
this.Document_ForEachByLibraryItems(document, function(item) {
|
||||
if ( this.IsFolderLibraryItem(item) ) {
|
||||
// nothing
|
||||
@@ -526,36 +573,34 @@ FlashTools.prototype.Document_ExportLibrary = function(document) {
|
||||
.format(item.itemType);
|
||||
}
|
||||
}.bind(this));
|
||||
xml_node.Save(this.Document_GetLibraryExportPath(document));
|
||||
xml_node.save(this.Document_GetLibraryExportPath(document));
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_ExportBitmaps = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
ft.type_assert(document, Document);
|
||||
this.Document_ForEachByLibraryItems(document, function(item) {
|
||||
this.BitmapItem_Export(document, item);
|
||||
}.bind(this), this.IsBitmapLibraryItem.bind(this));
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_ExportSymbols = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
ft.type_assert(document, Document);
|
||||
this.Document_ForEachByLibraryItems(document, function(item) {
|
||||
this.SymbolItem_Export(document, item);
|
||||
}.bind(this), this.IsSymbolLibraryItem.bind(this));
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_ExportStage = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
ft.type_assert(document, Document);
|
||||
this.Document_ExitEditMode(document);
|
||||
var xml_node = this.XmlNode("stage");
|
||||
this.Timeline_ExportXmlContent(xml_node, document.getTimeline());
|
||||
xml_node.Save(this.Document_GetStageExportPath(document));
|
||||
xml_node.save(this.Document_GetStageExportPath(document));
|
||||
};
|
||||
|
||||
FlashTools.prototype.Document_ExportStringIds = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
var xml_node = this.XmlNode("strings");
|
||||
this.ExportStringIdsXmlContent(xml_node);
|
||||
xml_node.Save(this.Document_GetStringIdsExportPath(document));
|
||||
ft.type_assert(document, Document);
|
||||
this.uniqueIds.save(this.Document_GetStringIdsExportPath(document));
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
@@ -563,16 +608,15 @@ FlashTools.prototype.Document_ExportStringIds = function(document) {
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.ConvertAll = function() {
|
||||
var documents = fl.documents;
|
||||
for ( var i = 0; i < documents.length; ++i ) {
|
||||
this.ConvertOne(documents[i]);
|
||||
}
|
||||
ft.array_foreach(fl.documents, function(document) {
|
||||
this.ConvertOne(document);
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
FlashTools.prototype.ConvertOne = function(document) {
|
||||
this.TypeAssert(document, Document);
|
||||
this.ClearStringIds();
|
||||
this.Trace("-= Convert document start =-");
|
||||
ft.type_assert(document, Document);
|
||||
this.uniqueIds.clear();
|
||||
ft.trace("-= Convert document start =-");
|
||||
try {
|
||||
this.Document_TraceInfo(document);
|
||||
this.Document_PrepareExportFolder(document);
|
||||
@@ -581,9 +625,9 @@ FlashTools.prototype.ConvertOne = function(document) {
|
||||
this.Document_ExportSymbols(document);
|
||||
this.Document_ExportStage(document);
|
||||
this.Document_ExportStringIds(document);
|
||||
this.Trace("-= Convert document finish =-");
|
||||
ft.trace("-= Convert document finish =-");
|
||||
} catch ( e ) {
|
||||
this.Trace("-= Convert document error =- : " + e);
|
||||
ft.trace("-= Convert document error =- : " + e);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -592,11 +636,11 @@ FlashTools.prototype.ConvertOne = function(document) {
|
||||
// ------------------------------------
|
||||
|
||||
FlashTools.prototype.Test0 = function() {
|
||||
this.Assert(true);
|
||||
ft.assert(true);
|
||||
};
|
||||
|
||||
FlashTools.prototype.Test1 = function() {
|
||||
this.Assert(true);
|
||||
ft.assert(true);
|
||||
};
|
||||
|
||||
FlashTools.prototype.RunTests = function() {
|
||||
@@ -605,7 +649,7 @@ FlashTools.prototype.RunTests = function() {
|
||||
this.Test1();
|
||||
return true;
|
||||
} catch ( e ) {
|
||||
this.TraceError("Unit test fail: " + e);
|
||||
ft.trace_fmt("!!!Error!!! Unit test fail: {0}", e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -614,11 +658,10 @@ FlashTools.prototype.RunTests = function() {
|
||||
// Run
|
||||
// ------------------------------------
|
||||
|
||||
var ft = new FlashTools();
|
||||
ft.ClearOutput();
|
||||
if ( ft.RunTests() ) {
|
||||
ft.ConvertAll();
|
||||
ft.clear_output();
|
||||
var flash_tools = new FlashTools();
|
||||
if ( flash_tools.RunTests() ) {
|
||||
flash_tools.ConvertAll();
|
||||
}
|
||||
|
||||
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user