Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. /*     
  2.         DynAPI Distribution
  3.         ImageClip Widget Class by Raymond Irving (xwisdom@yahoo.com)
  4.  
  5.         The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6.        
  7.         requires: DynLayer
  8. */
  9.  
  10. function ImageClip(clipImage,x,y,w,h,color,cols,rows,speed) {
  11.  
  12.         if (clipImage && clipImage.constructor==Object){
  13.                 var args=clipImage; // dictionary input
  14.                 clipImage = args.clipImage;
  15.                 x = args.x;
  16.                 y = args.y;
  17.                 w = args.w;
  18.                 h = args.h;
  19.                 color = args.color;
  20.                 cols = args.cols;
  21.                 rows = args.rows;
  22.                 speed = args.speed;
  23.         }
  24.  
  25.         this.DynLayer=DynLayer;
  26.         this.DynLayer(null,x,y,w,h,color);
  27.  
  28.         this.cols=(cols||0);
  29.         this.rows=(rows||0);
  30.         this.speed=(speed||100);
  31.         this.playing==false;
  32.         this.internalLoop=false;
  33.  
  34.         // create child layer for image
  35.         this.addChild(new DynLayer(null,0,0,this.w*this.cols,this.h*this.rows),'lyrCanvas');
  36.  
  37.         this.setClipImage(clipImage,cols,rows);
  38.         this.addEventListener(ImageClip.events);
  39.         this.setOverflow('hidden');
  40. };
  41. var p = dynapi.setPrototype('ImageClip','DynLayer');
  42. p.addImage = function(img,col,row) {
  43.         if(!img) return;
  44.         if(!this._imgFrames) this._imgFrames = [''];
  45.         index = ((col*row)>=0)? col*row:this._imgFrames.length;
  46.         this._imgFrames[index] = img;
  47.         if(!this._clipImage && index==1) this.setFrame(1);
  48. };
  49. p.getFrame = function(){
  50.         return this._frame;
  51. };
  52. p.setClipImage=function(clipImage,cols,rows){
  53.         if(!clipImage) return;
  54.         this._clipImage=clipImage;
  55.         clipImage = (clipImage.getHTML)? clipImage.getHTML():'<img height='+this.lyrCanvas.h+' width='+this.lyrCanvas.w+' border=0 src="'+((clipImage.src)? clipImage.src:clipImage)+'">';
  56.         this.setupFrames(cols,rows);
  57.         this.lyrCanvas.setHTML(clipImage);
  58. };
  59. p.setupFrames=function(cols,rows){
  60.         this.cols=cols||this.cols;
  61.         this.rows=rows||this.rows;
  62.         this.lyrCanvas.setSize(this.w*this.cols,this.h*this.rows);
  63. };
  64. p.setSpeed=function(sp){
  65.         this.speed=(sp||this.speed);
  66. };
  67. p.setFrame=function(fn){
  68.         var img,imgs = this._imgFrames
  69.         if (isNaN(fn)==true) return;
  70.         var icol=Math.floor((fn-1)/this.rows);
  71.         var irow=((fn-1)-(icol*this.rows));
  72.         if (fn<=(this.cols*this.rows) && fn>0){
  73.                 this._frame=fn;
  74.                 img=(imgs && imgs[fn])? imgs[fn]:null;
  75.                 if(img){
  76.                         img=(img.getHTML)? img.getHTML():'<img height='+this.lyrCanvas.h+' width='+this.lyrCanvas.w+' border=0 src="'+((img.src)? img.src:img)+'">';
  77.                         this.lyrCanvas.setHTML(img);
  78.                         this.lyrCanvas.setLocation(0,0);
  79.                 }
  80.                 else {
  81.                         icol=icol*-1;
  82.                         irow=irow*-1;
  83.                         this.lyrCanvas.setLocation(this.w*icol,this.h*irow);
  84.                 }
  85.                 this.invokeEvent("framechange");
  86.         }
  87. };
  88. p.setFrameMartix=function(col,row){
  89.         this.setFrame(col*row);
  90. };
  91. p.playAnimation=function(loop,sequence){
  92.         if (this.playing==true) return;
  93.         if (this.internalLoop!=true) {
  94.                 if(!sequence) sequence = '1>'+this.cols;
  95.                 this.aniseq=sequence.split(',');
  96.                 this.doloop=loop;
  97.         }
  98.         this.ls=0;
  99.         this.internalLoop=false;
  100.         this.playing=true;
  101.         this.timerSEQ=0;
  102.         this.playSEQ();
  103.         this.invokeEvent("frameplay");
  104. };
  105. p.stopAnimation=function(){
  106.         if (this.timerSEQ>=0) window.clearTimeout(this.timerSEQ);
  107.         this.playing=false;
  108.         this.invokeEvent("framestop");
  109. };
  110. p.nextSEQ=function() {
  111.         if (this.playing==false) return;
  112.         this.ls++;
  113.         if (this.ls<this.aniseq.length) {
  114.                 this.playSEQ();
  115.         }else{
  116.                 this.playing=false
  117.                 if (this.doloop) {
  118.                         this.internalLoop=true;
  119.                         this.playAnimation();
  120.                 }
  121.         }
  122. };
  123. p.playSEQ=function(inx) {
  124.         var st,ar,sq;
  125.         if (this.playing==false) return;
  126.         sq=this.aniseq[this.ls];
  127.         //forward
  128.         st=sq.indexOf('>');
  129.         if (st>0) {
  130.                 ar=sq.split(">");
  131.                 if (inx!=null) inx++;
  132.                 else inx=parseInt(ar[0]);
  133.                 this.setFrame(inx);
  134.                 if (inx>parseInt(ar[1])){
  135.                         this.nextSEQ();
  136.                         return;
  137.                 }else{
  138.                         this.timerSEQ=window.setTimeout(this+'.playSEQ('+inx+')',this.speed)
  139.                         return;
  140.                 }
  141.         }
  142.         //reverse
  143.         st=sq.indexOf("<");
  144.         if (st>0) {
  145.                 ar=sq.split("<");
  146.                 if (inx!=null) inx--;
  147.                 else inx=parseInt(ar[1]);
  148.                 this.setFrame(inx);
  149.                 if (inx<=parseInt(ar[0])){
  150.                         this.nextSEQ();
  151.                         return;
  152.                 }else{
  153.                         this.timerSEQ=window.setTimeout(this+'.playSEQ('+inx+')',this.speed)
  154.                         return;
  155.                 }
  156.         }
  157.         // sleep
  158.         st=sq.indexOf("p");
  159.         if (st==0) {
  160.                 sq=sq.replace("p","");
  161.                 this.timerSEQ=window.setTimeout(this+'.nextSEQ();',parseInt(sq));
  162.                 return;
  163.         }
  164.         // display single frame
  165.         if (isNaN(sq)==false) {
  166.                 if ((sq)>0) this.setFrame(sq);
  167.                 this.timerSEQ=window.setTimeout(this+'.nextSEQ();',this.speed);
  168.         }
  169.  
  170. };
  171.  
  172.