home *** CD-ROM | disk | FTP | other *** search
/ Joystick Magazine 2003 November / CD1_JOY_153.iso / demos / NHL2004Demo.exe / fe / COMMON / js / easoImage.js < prev    next >
Text File  |  2003-08-20  |  3KB  |  92 lines

  1. //A nice little wrapper class for dealing with multiple image states.
  2. //Simply construct the image passing in the src, width, and height
  3. //Then you can set the images for each state ('over', 'down', 'disabled') via setImage
  4. //Or you can define a name convention for all images:
  5.     //For Example if your image name is something like--btn1_nml.gif
  6.     //Then you probably have an over state for the image called btn1_hov.gif
  7.     //So call setNameConvention('_nml', '_hov');
  8. //
  9. function eaImage(src, width, height)
  10. {
  11.     this.width        = width;
  12.     this.height        = height;
  13.     this.nml        = new Image();
  14.     this.nml.src    = src;
  15.     this.nml.width    = width;
  16.     this.nml.height    = height;    
  17.     this.ovr    = new Image();
  18.     this.dwn    = new Image();
  19.     this.dis    = new Image();
  20. };
  21. eaImage.prototype.getImage            = function(sState) 
  22.     switch (sState) {
  23.         case "over":        return this.ovr;
  24.         case "down":        return this.dwn;
  25.         case "disabled":    return this.dis;
  26.         default:            return this.nml;
  27.     };
  28.     sState = null;
  29. };
  30. eaImage.prototype.getImageSrc        = function(sState)
  31. {
  32.     return this.getImage(sState).src;
  33. };
  34. eaImage.prototype.setImage            = function(sState, src, width, height)
  35. {
  36.     switch(sState) {
  37.         case "over":        this.ovr.width=width;this.ovr.height=height;this.ovr.src=src;break;
  38.         case "down":        this.dwn.width=width;this.dwn.height=height;this.dwn.src= src;break;
  39.         case "disabled":     this.dis.width=width;this.dis.height= height;this.dis.src=src;break;
  40.         default:            this.nml.width=width;this.nml.height= height;this.nml.src=src;break;
  41.     };
  42.     sState = src = width = height = null;
  43. };            
  44. eaImage.prototype.setNameConvention        = function(nml, ovr, dwn, dis)
  45. {    
  46.     
  47.     this.nmlExp        = new RegExp(nml, "g");
  48.     this.ovr.src    = this.nml.src.replace(this.nmlExp, ovr);
  49.     this.ovr.width    = this.width;
  50.     this.ovr.height    = this.height;
  51.     this.dwn.src    = this.nml.src.replace(this.nmlExp, dwn);
  52.     this.dwn.width    = this.width;
  53.     this.dwn.height    = this.height;
  54.     this.dis.src    = this.nml.src.replace(this.nmlExp, dis);
  55.     this.dis.width    = this.width;
  56.     this.dis.height    = this.height;
  57.     delete this.nmlExp;
  58. };
  59. eaImage.prototype.toHTMLString            = function(sState, sAttrs)
  60. {
  61.     var returnStr        = "";
  62.     sAttrs    = (sAttrs) ? sAttrs + "" : "";
  63.     
  64.     this.tmpImg        = null;
  65.     switch (sState) {
  66.         case "over":         this.tmpImg = this.ovr; break;
  67.         case "down":        this.tmpImg = this.dwn; break;
  68.         case "disabled":     this.tmpImg = this.dis; break;
  69.         default:            this.tmpImg = this.nml; break;
  70.     };
  71.     returnStr            = "<img src='" + this.tmpImg.src + "' width='" + this.tmpImg.width + "' height='" + this.tmpImg.height +"' " + sAttrs + ">"
  72.     delete this.tmpImg;
  73.     return returnStr;
  74. };
  75. //The following two methods are static class methods for batching the params together
  76. eaImage.createWithSrc    = function(nmlSrc, nmlWidth, nmlHeight, ovrSrc, dwnSrc, disSrc)
  77. {
  78.     var eaImage        = new eaImage(nmlSrc, nmlWidth, nmlHeight);
  79.     with (eaImage) {
  80.         setImage("over", ovrSrc, nmlWidth, nmlHeight);
  81.         setImage("down", dwnSrc, nmlWidth, nmlHeight);
  82.         setImage("dis", disSrc, nmlWidth, nmlHeight);
  83.     };
  84.     return eaImage;
  85. };
  86. eaImage.createWithConvention    = function(nmlSrc, nmlWidth, nmlHeight, nmlExp, ovrExp, dwnExp, disExp)
  87. {
  88.     var returnImg        = new eaImage(nmlSrc, nmlWidth, nmlHeight);
  89.     returnImg.setNameConvention(nmlExp, ovrExp, nmlExp, nmlExp);
  90.     return returnImg;
  91. };