Flash(JSFL)

Flash 扩展开发

 

  1 //var dom = fl.getDocumentDOM();
  2 //var fileName = dom.name.split(".fla").join(".swf");
  3 //var swfFile = fl.configURI + "WindowSWF/" + fileName;
  4 //dom.exportSWF(swfFile, true); 
  5 
  6 
  7 
  8 cls();
  9 fl.reloadTools();
 10 fl.getDocumentDOM().save();
 11 trace("重新记载工具栏OK");
 12 
 13 trace(fl.getDocumentDOM().library);
 14 
 15 /**********************************************************************************************/
 16 //功能性函数
 17 
 18 /*给函数原型增加一个extend函数,实现继承*/  
 19 Function.prototype.extend = function(superClass){  
 20     if(typeof superClass !== 'function'){  
 21         throw new Error('fatal error:Function.prototype.extend expects a constructor of class');  
 22     }  
 23 
 24     var F = function(){}; //创建一个中间函数对象以获取父类的原型对象  
 25     F.prototype = superClass.prototype; //设置原型对象  
 26     this.prototype = new F(); //实例化F, 继承父类的原型中的属性和方法,而无需调用父类的构造函数实例化无关的父类成员  
 27     this.prototype.constructor = this; //设置构造函数指向自己  
 28     this.superClass = superClass; //同时,添加一个指向父类构造函数的引用,方便调用父类方法或者调用父类构造函数  
 29           
 30     return this;  
 31 };
 32 
 33 /*打印输出*/
 34 function trace(str){
 35     fl.outputPanel.trace(str);    
 36 }
 37 
 38 /*清屏*/
 39 function cls(){
 40     fl.outputPanel.clear();
 41 }
 42 
 43 /** 保留小数 */
 44 function formatNumber( num, retain){
 45     retain = retain || 100;
 46     return Math.round(num * retain) / 100;
 47 }
 48 
 49 /** 把一个库目录里的资源转换为资源的绝对目录 */
 50 function getFilePathByLibraryPath( path ){
 51     if( path == null || path == "" || path == undefined ){
 52         alert( "getFilePathByLibraryPath 无效的参数" );
 53     }
 54     var lib = fl.getDocumentDOM().library;
 55     var item_index = lib.findItemIndex( path );
 56     if( item_index < 0 ){
 57         alert( "此路径不存在: " + path );
 58     }
 59     var item = lib.items[item_index];
 60     var url = item.sourceFilePath;
 61     url = url.replace( "file:///", "" );
 62     url = url.replace( "|", ":" );
 63     //for ( var i in item ){
 64         //trace( " " + i + " " + ":" + item[i] );
 65     //}
 66     //trace( "转换路径 " + path + "===>>" + url );
 67     return url;
 68 }
 69 
 70 /**
 71  @brief 获取当前文档的时间轴
 72  @return Timeline object
 73  */
 74 getCurrentTimeline = function(){
 75     try{
 76         return fl.getDocumentDOM().getTimeline();
 77     }catch(e){
 78         alert( "打开 Fla 文件失败" );
 79     }
 80     return null;
 81 }
 82 
 83 /** 
 84  @brief 判断一个帧是否是关键帧, 原理是每个关键帧总是一序列帧的第一帧
 85  @param frame:Frame object 帧对象
 86  @param frameIndex:int 该帧的索引号
 87  @return boolean
 88  */
 89 isKeyFrame = function( frame, frameIndex ){
 90     if( !frame ) return false;
 91     if( frame.startFrame == frameIndex ){
 92         return true;
 93     }
 94     return false;
 95 }
 96 
 97 /**
 98  @brief 把某个图层的关键帧枚举出来
 99  @param layer: Layer object 图层对象
100  @param startFrameIndex:int 起始帧数,不一定是从0开始
101  @param endFrameIndex:int 结束帧数
102  @return vector<int>: 范围[startFrameIndex,endFrameIndex)之间的关键帧索引号数组
103  */
104 getKeyframeIndices = function(layer, startFrameIndex, endFrameIndex){
105     if( !layer ) return [];
106     var list = [];
107     for (var frameIndex=startFrameIndex; frameIndex<endFrameIndex; frameIndex++){
108         var frame = layer.frames[frameIndex];
109         var isFirstFrame = (frameIndex == startFrameIndex);
110         var isKeyframe = (isFirstFrame || frame.startFrame == frameIndex);
111     //    if (isKeyframe){
112     //        list[frameIndex] = true;
113     //    }else{
114     //        list[frameIndex] = false;
115     //    }
116         if( isKeyframe ){
117             list.push( frameIndex );
118         }
119     }
120     return list;
121 }
122 
123 /**
124  @brief 获取某个图层的所有关键帧
125  @param layer:Layer object 图层对象
126  @return vector<Frame object>
127  */
128 getKeyframeObjects = function( layer ){
129     if( !layer ) return [];
130     var list = [];
131     var index = 0;
132     for each( frame in layer.frames ){
133         if( frame.startFrame == index ){
134             list.push( frame );
135         }
136         index++;
137     }
138     return list;
139 }
140 
141 /**
142  @brief 获取一个帧上的所有元素
143  @param frame:Frame object 帧对象
144  @return vector<Element object> 该帧上的所有元素
145  */
146 getElementObjects = function( frame ){
147     if( !frame ) return [];
148     return frame.elements;
149 }
150 
151 /**
152  @brief 获取元素的类型
153  @param element:Element object 元素对象
154  @return string 类型
155  */
156 getElementType = function(element){
157     if (!element) return '';
158 
159     var elementType = '';
160     var libraryItem = element.libraryItem;
161     // element.elementType: "shape", "text", "instance", or "shapeObj"
162     // item.itemType: "undefined", "component", "movie clip", "graphic", "button", "folder", "font", "sound", "bitmap", "compiled clip", "screen", and "video".
163     switch (element.elementType){
164     case 'shape' :
165         {
166             //NOTE: a drawing object is both a group and a drawing object, so check it first
167             elementType = element.isRectangleObject ? 'rectangle object'
168                         : element.isOvalObject ? 'oval object'
169                         : element.isDrawingObject ? 'drawing object'
170                         : element.isGroup ? 'group'
171                         : 'shape';
172         }break;
173     case 'shapeObj' :
174         {
175             elementType = 'shape object'; 
176         }break;
177     case 'text' :
178         {
179             elementType = 'text'; 
180         }break;
181     case 'instance' :
182         {
183             if (element.symbolType)
184                 elementType = element.symbolType;
185             else if (libraryItem.itemType && libraryItem.itemType != 'undefined')
186                 elementType = libraryItem.itemType;
187         }break;
188     }
189     return elementType;
190 }
191 
192 /** 精减小数 */
193 roundToTwip = function(value){
194     return Math.round(value*20) / 20;
195 }
196 
197 /** 获取元素的x坐标 */
198 getX = function(element){
199     return roundToTwip(element.transformX);
200 }
201 
202 /** 获取元素的y坐标 */
203 getY = function(element){
204     return roundToTwip(element.transformY);
205 }
206 
207 /** 设置元素的x坐标 */
208 setX = function(element, x){
209     element.transformX = x;
210 }
211 
212 /** 设置元素的y坐标 */
213 setY = function(element, y){
214     element.transformY = y;
215 }
216 
217 
218 
219 /**********************************************************************************************/
220 
221 
222 
223 
224 
225 
226 
227 
228 /**********************************************************************************************/
229 
230 //Class HSTexture
231 HSTexture = function( oElement ){
232     this.imageName = oElement.libraryItem.name;
233     trace("HSTexture @ imageName : "+ this.imageName);
234 }
235 
236 //Class HSNode
237 HSNode = function( oElement ){
238     if(!oElement){
239         return;
240     }
241     this.fX = oElement.x;
242     this.fY = oElement.y;
243     this.fZ = oElement.depth;
244     this.fW = oElement.width;
245     this.fH = oElement.height;
246     this.fScaleX = oElement.scaleX;
247     this.fScaleY = oElement.scaleY;
248     this.fSkewX = oElement.skewX;
249     this.fSkewY = oElement.skewY;
250     this.fRotation = oElement.rotation;
251 }
252 
253 printList = function( infoList , strDescribe ){
254     
255     strDescribe += "Info: ";
256     var info = strDescribe;
257     for(var i = 0; i< infoList.length;++i){
258         info += infoList[i];
259     }
260     trace(info);
261 }
262 
263 printNode = function( oHSNode ){
264     trace("obectName : " + oHSNode.obectName);
265     trace("fX : " + oHSNode.fX);
266     trace("fY : " + oHSNode.fY);
267     trace("fZ : " + oHSNode.fZ);
268     trace("fW : " + oHSNode.fW);
269     trace("fH : " + oHSNode.fH);
270     trace("fScaleX : " + oHSNode.fScaleX);
271     trace("fScaleY : " + oHSNode.fScaleY);
272     trace("fSkewX : " + oHSNode.fSkewX);
273     trace("fSkewY : " + oHSNode.fSkewY);
274     trace("fRotation : " + oHSNode.fRotation);
275 }
276 
277 //Class HSDevice
278 HSDevice = function(strDeviceName ,strShowType, iScreenW , iScreenH){
279     this.sName = strDeviceName;
280     this.sShowType = strShowType;
281     this.iWidth = iScreenW;
282     this.iHeight = iScreenH;
283 }
284 
285 //Class HSSprite
286 HSSprite = function( oElement ){
287     if(!oElement){
288         return;
289     }
290     this.obectName = oElement.name;
291     this.oHSTexture = new HSTexture(oElement);    //纹理对象
292     this.nodeData = new HSNode(oElement);
293 }
294 
295 HSSprite.prototype.printInfo = function(){
296     trace("SpriteName : " + this.obectName);
297     trace("SpriteTextureName : " + this.oHSTexture.imageName);
298 }
299 
300 //Class HSMenu
301 HSMenu = function(){
302     this.obectName = "";        //代表图层的名字
303     this.frameCount = 4;
304     this.normalList    = [];        //HSSprite
305     this.passList    = [];        //HSSprite
306     this.pressdownList    = [];    //HSSprite
307     this.upspringList    = [];    //HSSprite
308     this.nodeData = null;        //HSNodeData
309 }
310 
311 HSMenu.prototype.printInfo = function(){
312     trace("MenuName : " + this.obectName);
313     printList(this.normalList,"normalList");
314     printList(this.passList,"passList");
315     printList(this.pressdownList,"pressdownList");
316     printList(this.upspringList,"upspringList");
317     printNode(this.nodeData);
318 }
319 
320 
321 //Class HSLayer
322 HSLayer = function( oLayer ){
323     this.type = oLayer.layerType;
324     this.name = oLayer.name;
325     this.hsSpriteList = [];
326     this.hsMenuList = [];    //HSMenu
327     
328     var frameCount = oLayer.frameCount;
329     for(var frameIndex = 0;frameIndex<frameCount;++frameIndex){
330         var oFrame = oLayer.frames[frameIndex];
331         
332         var elementCount = oFrame.elements.length;
333         for(var elementIndex = 0;elementIndex<elementCount;++elementIndex){
334             var oElement = oFrame.elements[elementIndex];
335             var elementLibrartItem = oElement.libraryItem;
336             
337             //trace("元素名字 : " + oElement.libraryItem.name);
338             //trace("元素类型 : " + getElementType(oElement));
339             trace("/*********************************/");
340             switch(elementLibrartItem.itemType)
341             {
342                 case "button":
343                 {
344                     trace("case \"button\":");
345                     trace(oElement.name);
346                     this.nodeData = new HSNode(oElement);
347                     //trace(elementLibrartItem.timeline.layers[0].frames[2].elements[0].libraryItem.name);
348                     var oHSMenu = this.handleMenu(elementLibrartItem.timeline ,oElement.name );
349                     oHSMenu.nodeData = new HSNode(oElement);
350                     this.hsMenuList.push(oHSMenu);
351                     for(var i = 0; i<this.hsMenuList.length;++i){
352                         this.hsMenuList[i].printInfo();
353                     }
354                     break;
355                 }                
356                 case "movie clip":
357                 {
358                     trace("case \"movie clip\":");
359                     break;
360                 }                
361                 case "bitmap":
362                 {
363                     var oHSSprite = new HSSprite(oElement);
364                     this.hsSpriteList.push(oHSSprite);
365                     oHSSprite.printInfo();
366                     break;
367                 }
368                 case "graphic":
369                 {
370                     trace("case graphic:");
371                     //需要Shape 对象
372                     trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0]);
373                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].getHalfEdge().getVertex().x);
374                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].getHalfEdge().getVertex().y);
375                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[1].getHalfEdge().getVertex().x);
376                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[1].getHalfEdge().getVertex().y);
377                     
378                     
379                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.color);
380                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.b);
381                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.c);
382                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.d);
383                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.tx);
384                     //trace(elementLibrartItem.timeline.layers[0].frames[0].elements[0].contours[0].fill.matrix.ty);
385                     //trace(typeof oElement.symbolType);
386                     
387                     break;
388                 }
389                 default:
390                 {
391                     alert( elementLibrartItem.itemType + " 不识别");
392                     break;
393                 }                
394                 
395             }
396             trace("/*********************************/");
397             
398             
399             //var symbolItemTimeline = oElement.libraryItem.timeline;
400             //var layerList = symbolItemTimeline.layers;
401             //var layerListCount = symbolItemTimeline.layerCount;
402             //for(var symIndex = 0;symIndex < layerListCount;++symIndex){
403                 //trace("symbol :" + layerList[symIndex].name);
404             //}
405         }
406     }
407 }
408 
409 HSLayer.prototype.handleMenu = function( oTimeline , strMenuName){
410     trace("handleMenu");
411     
412     //var layerCount = oTimeline.layerCount;
413     var oLayer = oTimeline.layers[0];
414     var oMenu = new HSMenu();
415     oMenu.obectName = strMenuName;
416     var frameCount = oLayer.frameCount;
417     //
418     trace("frameCount : " + frameCount);
419     for(var frameIndex = 0; frameIndex < 4;++frameIndex){
420         var elementList = oLayer.frames[frameIndex].elements;
421         var elementCount = elementList.length;
422         var frameSprite = [];
423         for(var elementIndex = 0;elementIndex < elementCount;++elementIndex){
424             var oElement = elementList[elementIndex];                
425             var oElementType = oElement.libraryItem.itemType;
426             //trace(oElementType);
427             if(elementIndex == 0){
428                 oMenu.nodeData = new HSNode(oElement);
429             }
430             if(oElementType == "bitmap"){
431                 trace(oElement.libraryItem.name + "   " + elementIndex);
432                 frameSprite.push(oElement.libraryItem.name);
433             }else{
434                 alert(oLayer.name + " 包含了图元,如有问题请联系:Ambition");
435             }
436         }
437         switch(frameIndex){
438             case 0:
439                 oMenu.normalList = frameSprite;
440                 break;
441             case 1:
442                 oMenu.passList = frameSprite;
443                 break;
444             case 2:
445                 oMenu.pressdownList = frameSprite;
446                 break;
447             case 3:
448                 oMenu.upspringList = frameSprite;
449                 break;
450         }
451     }
452     return oMenu;
453 }
454 
455 HSLayer.prototype.handleBitmap = function(){
456     
457 }
458 
459 HSLayer.prototype.handleMC = function(){
460     
461 }
462 
463 HSLayer.prototype.handleGraphic = function(){
464     
465 }
466 
467 
468 
469 
470 
471 
472 
473 SaveXml = function(strDeviceName,strShowType,iScreenW,iScreenH){
474     trace(strShowType);
475     var device = new HSDevice(strDeviceName,strShowType,iScreenW,iScreenH);
476     //得到时间轴
477     var timeLine = getCurrentTimeline();
478     
479     var layerList = [];
480     
481     switch(device.sShowType)
482     {
483         case "All":
484         {
485             trace(device.sShowType);
486             var layerCount = timeLine.layerCount;
487             for(var layerIndex = 0;layerIndex < layerCount;++layerIndex){
488                 var oLayer = timeLine.layers[layerIndex];
489                 var hsLayer = new HSLayer(oLayer);
490                 //图层类型包括:guide folder normal mask normal
491                 if("normal" != oLayer.layerType){
492                     alert("[ " + oLayer.name + " ] 类型不为normal类型,因此跳过,有疑问请联系: Ambition");
493                     continue;
494                 }
495                 //layerList.push(hsLayer);
496             }            
497             break;
498         }
499         default:
500         {
501             alert("不支持显示类型 : " + device.sShowType);
502             break;
503         }
504     }
505     trace(layerList);
506     trace(timeLine);
507     
508     return "";
509 }
510 /**********************************************************************************************/
511 
512 SaveXml("android","All",480,800);

 

posted on 2012-12-21 10:52  游戏开发:主席  阅读(2225)  评论(0编辑  收藏  举报

导航