Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.         DynAPI Distribution
  3.         ImageAnimation Class
  4.  
  5.         The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6.  
  7.         requires: Thread
  8. */
  9.  
  10. function ImageAnimation(dynimage) {
  11.         this.Thread = Thread;
  12.         this.Thread(dynimage);
  13.  
  14.         this.imgAnim = new Array();
  15.         this.imgAnim.playing = null;
  16. }
  17. var p = dynapi.setPrototype('ImageAnimation','Thread');
  18. p.addAnimation = function (imgArray) {
  19.         var animNum = this.imgAnim.length;
  20.         this.imgAnim[animNum] = imgArray;
  21.         this.imgAnim[animNum].loops = false;
  22.         this.imgAnim[animNum].resets = false;
  23.         this.imgAnim[animNum].frame = 0;
  24.         this.imgAnim[animNum].playing = true;
  25.         this.imgAnim[animNum].direction = 0;
  26.         this.imgAnim[animNum].alternates = false;
  27.         return animNum;
  28. };
  29. p.getFrame = function (animNum,frameNum) {
  30.         return this.imgAnim[animNum][frameNum];
  31. };
  32.  
  33. p.setLoops = function (animNum,loop) {
  34.         this.imgAnim[animNum].loops = loop;
  35. };
  36. p.setResets = function (animNum) {
  37.         this.imgAnim[animNum].resets = true;
  38. };
  39. p.setAlternates = function (animNum,alt) {
  40.         this.imgAnim[animNum].loops = true;
  41.         this.imgAnim[animNum].alternates = alt;
  42. };
  43.  
  44. p.playAnimation = function (animNum) {
  45.         if (animNum!=null && this.imgAnim.playing!=animNum) {
  46.                 this.playing = true;
  47.                 this.imgAnim.playing = animNum;
  48.                 if (this.dlyr!=null) this.dlyr.invokeEvent("imgstart");
  49.                 this.start();
  50.         }
  51. };
  52. p.stopAnimation = function () {
  53.         this.imgAnim.playing = null;
  54.         this.playing = false;
  55.         this.stop();
  56.         if (this.dlyr!=null) this.dlyr.invokeEvent("imgrun");
  57. };
  58. p.run = function () {
  59.         if (!this.playing || this.imgAnim.playing==null || this.dlyr==null) return;
  60.        
  61.         var anim = this.imgAnim[this.imgAnim.playing];
  62.        
  63.         if (anim.frame==0 && this.img==anim[anim.frame]) {
  64.                 anim.frame++;   // skip 1st frame if same
  65.         }
  66.         if (this.dlyr!=null) this.dlyr.invokeEvent("imgrun");
  67.         this.dlyr.setImage(anim[anim.frame]);
  68.        
  69.         if (anim.frame>=anim.length-1) {
  70.                 if (anim.loops) {
  71.                         if (anim.alternates && anim.direction==0 && anim.frame==anim.length-1) {
  72.                                 anim.direction = 1;
  73.                                 anim.frame = anim.length-2;
  74.                         }
  75.                         else anim.frame = 0;
  76.                 }
  77.                 else if (anim.resets) {
  78.                         anim.frame = 0;
  79.                         this.stop()
  80.                 }
  81.                 else {
  82.                         this.stop()
  83.                 }
  84.         }
  85.         else {
  86.                 if (anim.alternates) {
  87.                         if (anim.frame==0 && anim.direction==1) {
  88.                                 anim.direction = 0;
  89.                                 anim.frame = 1;
  90.                         }
  91.                         else if (anim.direction==0) {
  92.                                 anim.frame++;
  93.                         }
  94.                         else if (anim.direction==1) {
  95.                                 anim.frame--;
  96.                         }
  97.                 }
  98.                 else {
  99.                         anim.frame++;
  100.                 }
  101.         }
  102. };
  103.