/*
Copyright (c) 2007 Chris Sperry
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
//all path handling code only works with PC. I don't talk Mac and wouldn't know where to start. Good luck!
fl.outputPanel.clear();
var SHIM_NAME="Shim";
var SHIM_COMPILED_CLIP_NAME=SHIM_NAME+" Compiled Clip";
var SHIM_LAYER_NAME="autogenerated_shim_layer";
var SHIM_CLASSNAME="uk.co.flexiblefactory.components."+SHIM_NAME;
var SHIM_FOLDER_NAME="_private";
createAndInsertComponentShim(SHIM_NAME,SHIM_COMPILED_CLIP_NAME,SHIM_LAYER_NAME,SHIM_CLASSNAME,SHIM_FOLDER_NAME);
createLivePreviews();
function createLivePreviews()
{
var sourceDocument=fl.getDocumentDOM();
var sourceLibrary=sourceDocument.library;
for(var i in sourceLibrary.items)
{
var sourceItem=sourceLibrary.items[i];
if(sourceItem.itemType=="component")
{
createLivePreview(sourceItem);
}
}
}
function createLivePreview(sourceItem)
{
var sourceDocument=fl.getDocumentDOM();
var livePreviewDocument=createTargetDocument(sourceDocument);
livePreviewDocument.addItem({x:0,y:0},sourceItem);
var el=livePreviewDocument.timelines[0].layers[0].frames[0].elements[0];
el.x=0;
el.y=0;
livePreviewDocument.width=Math.floor(el.width);
livePreviewDocument.height=Math.floor(el.height);
livePreviewDocument.docClass="fl.livepreview.LivePreviewParent";
var lpdPath=livePreviewDocument.path;
var lpdPathParts=lpdPath.split("\\");
lpdPathParts.pop();
lpdPathParts.push(sourceItem.name+"_live_preview.swf");
var livePreviewSwfURI="file:///"+lpdPathParts.join("/");
livePreviewDocument.exportSWF(livePreviewSwfURI,true);
livePreviewDocument.close(false);
fileToDeleteURI=getAutogenShimFlaURI(sourceDocument.path);
FLfile.remove(fileToDeleteURI);
}
function createAndInsertComponentShim(shimName,shimCompiledClipName,shimLayerName,shimClassName,shimFolderName)
{
var sourceDocument=fl.getDocumentDOM();
var targetDocument=createTargetDocument(sourceDocument);
var sourceLibrary=sourceDocument.library;
var targetLibrary=targetDocument.library;
recreateLibrary(sourceLibrary,targetLibrary);
var shimCompiledClip=createShimCompiledClip(targetDocument,shimName,shimCompiledClipName,shimClassName);
insertShimCompiledClipInstances(sourceDocument,shimCompiledClip,shimName,shimCompiledClipName,shimLayerName,shimFolderName);
var fileToDeleteURI=getAutogenShimFlaURI(sourceDocument.path);
targetDocument.close(false);
FLfile.remove(fileToDeleteURI);
}
function createShimCompiledClip(targetDocument,shimName,shimCompiledClipName,shimClassName)
{
var targetLibrary=targetDocument.library;
var shimSourceName=shimName+" source";
var shimItem=createNewLibaryItem(targetLibrary,"movie clip",shimSourceName,true,true,shimClassName);
fl.setActiveWindow(targetDocument);
shimItem.convertToCompiledClip();
var shimCompiledClip=findLibraryItem(targetLibrary,shimSourceName+" SWF");
//fl.trace(shimCompiledClipName);
shimCompiledClip.name=shimCompiledClipName;
return shimCompiledClip;
}
function recreateLibrary(sourceLibrary,targetLibrary)
{
for(var i in sourceLibrary.items)
{
var sourceItem=sourceLibrary.items[i];
if(sourceItem.itemType=="component")
{
createNewLibaryItem(targetLibrary,"movie clip",sourceItem.name,true,true,sourceItem.linkageClassName);
}
}
}
function insertShimCompiledClipInstances(sourceDocument,shimCompiledClip,shimName,shimCompiledClipName,shimLayerName,shimFolderName)
{
fl.setActiveWindow(sourceDocument);
var sourceLibrary=sourceDocument.library;
var itemToDelete=(shimFolderName.length>0?(shimFolderName+"/"):"")+shimCompiledClipName;
sourceLibrary.deleteItem(itemToDelete);
if(shimFolderName.length>0&&(!sourceLibrary.itemExists(shimFolderName)))
{
sourceLibrary.newFolder(shimFolderName);
}
for(var i in sourceLibrary.items)
{
var sourceItem=sourceLibrary.items[i];
if(sourceItem.itemType=="component")
{
sourceLibrary.editItem(sourceItem.name)
var shimLayerIndex=sourceItem.timeline.findLayerIndex(shimLayerName);
if(shimLayerIndex!=null)
{
if(shimLayerIndex!=0)
{
sourceItem.timeline.reorderLayer(parseInt(shimLayerIndex),0); //some weird type issue. parseInt fixes.
}
sourceItem.timeline.currentLayer=0;
sourceItem.timeline.layers[0].locked=false;
sourceItem.timeline.deleteLayer(0);
}
sourceItem.timeline.currentLayer=0;
shimLayerIndex=sourceItem.timeline.addNewLayer(shimLayerName);
sourceItem.timeline.convertToBlankKeyframes(1);
sourceItem.timeline.currentLayer=0;
sourceItem.timeline.currentFrame=1;
sourceDocument.addItem({x:-200,y:-50},shimCompiledClip);
sourceItem.timeline.layers[0].locked=true;
}
}
sourceLibrary.moveToFolder(shimFolderName,shimCompiledClipName,true)
}
function createNewLibaryItem(libObj,type,name,exportForAS,exportInFirstFrame,linkageClassName)
{
libObj.addNewItem(type,name);
var item=findLibraryItem(libObj,name);
item.linkageExportForAS=exportForAS;
item.linkageExportInFirstFrame=exportInFirstFrame;
item.linkageClassName=linkageClassName;
return item;
}
function findLibraryItem(libObj,name)
{
return libObj.items[libObj.findItemIndex(name)];
}
function createTargetDocument(sourceDocument)
{
sourceDocument.save();
var sourceDocumentPath=sourceDocument.path;
var sourceDocumentPathParts=sourceDocumentPath.split("\\");
var sourceDocumentURI="file:///"+sourceDocumentPathParts.join("/");
var targetDocumentURI=getAutogenShimFlaURI(sourceDocumentPath);
if(FLfile.exists(targetDocumentURI))
{
FLfile.remove(targetDocumentURI);
}
FLfile.copy(sourceDocumentURI,targetDocumentURI);
var targetDocument=fl.openDocument(targetDocumentURI);
var items=targetDocument.library.items;
while(targetDocument.library.items.length>0)
{
targetDocument.library.deleteItem(targetDocument.library.items[0].name);
}
return targetDocument;
}
function getAutogenShimFlaURI(sourceDocumentPath)
{
var sourceDocumentPathParts=sourceDocumentPath.split("\\");
var sourceDocumentFilename=sourceDocumentPathParts.pop();
var sourceDocumentFilenameParts=sourceDocumentFilename.split(".");
var sourceDocumentFilenameExtension=sourceDocumentFilenameParts.pop();
var targetDocumentURI="file:///"+sourceDocumentPathParts.join("/")+"/"+sourceDocumentFilenameParts.join(".")+"_autogen_comp."+sourceDocumentFilenameExtension;
return targetDocumentURI;
}