CGfxButton.js 3.2 KB

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