CGfxButton.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. function CGfxButton(iXPos,iYPos,oSprite){
  2. var _bDisable;
  3. var _iWidth;
  4. var _iHeight;
  5. var _aCbCompleted;
  6. var _aCbOwner;
  7. var _aParams = [];
  8. var _oButton;
  9. this._init =function(iXPos,iYPos,oSprite){
  10. _bDisable = false;
  11. _aCbCompleted=new Array();
  12. _aCbOwner =new Array();
  13. _iWidth = oSprite.width;
  14. _iHeight = oSprite.height;
  15. _oButton = createBitmap( oSprite);
  16. _oButton.x = iXPos;
  17. _oButton.y = iYPos;
  18. _oButton.regX = oSprite.width/2;
  19. _oButton.regY = oSprite.height/2;
  20. _oButton.cursor = "pointer";
  21. s_oStage.addChild(_oButton);
  22. this._initListener();
  23. };
  24. this.unload = function(){
  25. _oButton.off("mousedown", this.buttonDown);
  26. _oButton.off("pressup" , this.buttonRelease);
  27. s_oStage.removeChild(_oButton);
  28. };
  29. this.setVisible = function(bVisible){
  30. _oButton.visible = bVisible;
  31. };
  32. this._initListener = function(){
  33. _oButton.on("mousedown", this.buttonDown);
  34. _oButton.on("pressup" , this.buttonRelease);
  35. };
  36. this.addEventListener = function( iEvent,cbCompleted, cbOwner ){
  37. _aCbCompleted[iEvent]=cbCompleted;
  38. _aCbOwner[iEvent] = cbOwner;
  39. };
  40. this.addEventListenerWithParams = function(iEvent,cbCompleted, cbOwner,aParams){
  41. _aCbCompleted[iEvent]=cbCompleted;
  42. _aCbOwner[iEvent] = cbOwner;
  43. _aParams = aParams;
  44. };
  45. this.buttonRelease = function(){
  46. if(_bDisable){
  47. return;
  48. }
  49. playSound("click", 1, false);
  50. _oButton.scaleX = 1;
  51. _oButton.scaleY = 1;
  52. if(_aCbCompleted[ON_MOUSE_UP]){
  53. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP],_aParams);
  54. }
  55. };
  56. this.buttonDown = function(){
  57. if(_bDisable){
  58. return;
  59. }
  60. _oButton.scaleX = 0.9;
  61. _oButton.scaleY = 0.9;
  62. if(_aCbCompleted[ON_MOUSE_DOWN]){
  63. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN],_aParams);
  64. }
  65. };
  66. this.setPosition = function(iXPos,iYPos){
  67. _oButton.x = iXPos;
  68. _oButton.y = iYPos;
  69. };
  70. this.setX = function(iXPos){
  71. _oButton.x = iXPos;
  72. };
  73. this.setY = function(iYPos){
  74. _oButton.y = iYPos;
  75. };
  76. this.enable = function(){
  77. _bDisable = false;
  78. _oButton.filters = [];
  79. _oButton.cache(0,0,_iWidth,_iHeight);
  80. };
  81. this.disable = function(){
  82. _bDisable = true;
  83. var matrix = new createjs.ColorMatrix().adjustSaturation(-100).adjustBrightness(40);
  84. _oButton.filters = [
  85. new createjs.ColorMatrixFilter(matrix)
  86. ];
  87. _oButton.cache(0,0,_iWidth,_iHeight);
  88. };
  89. this.getButtonImage = function(){
  90. return _oButton;
  91. };
  92. this.getX = function(){
  93. return _oButton.x;
  94. };
  95. this.getY = function(){
  96. return _oButton.y;
  97. };
  98. this._init(iXPos,iYPos,oSprite);
  99. return this;
  100. }