CGfxButton.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. function CGfxButton(iXPos,iYPos,oSprite,oParentContainer){
  2. var _bDisable;
  3. var _iWidth;
  4. var _iHeight;
  5. var _aCbCompleted;
  6. var _aCbOwner;
  7. var _aParams = [];
  8. var _oListenerDown;
  9. var _oListenerUp;
  10. var _oButton;
  11. var _oParentContainer;
  12. this._init =function(iXPos,iYPos,oSprite){
  13. _bDisable = false;
  14. _aCbCompleted=new Array();
  15. _aCbOwner =new Array();
  16. _iWidth = oSprite.width;
  17. _iHeight = oSprite.height;
  18. _oButton = createBitmap( oSprite);
  19. _oButton.x = iXPos;
  20. _oButton.y = iYPos;
  21. _oButton.regX = oSprite.width/2;
  22. _oButton.regY = oSprite.height/2;
  23. if (!s_bMobile)
  24. _oButton.cursor = "pointer";
  25. _oParentContainer.addChild(_oButton);
  26. this._initListener();
  27. };
  28. this.unload = function(){
  29. _oButton.off("mousedown", _oListenerDown);
  30. _oButton.off("pressup" , _oListenerUp);
  31. _oParentContainer.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. _oListenerUp = _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. _oButton.scaleX = 1;
  54. _oButton.scaleY = 1;
  55. if(_aCbCompleted[ON_MOUSE_UP]){
  56. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP],_aParams);
  57. }
  58. };
  59. this.buttonDown = function(){
  60. if(_bDisable){
  61. return;
  62. }
  63. _oButton.scaleX = 0.9;
  64. _oButton.scaleY = 0.9;
  65. if(_aCbCompleted[ON_MOUSE_DOWN]){
  66. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN],_aParams);
  67. }
  68. };
  69. this.setPosition = function(iXPos,iYPos){
  70. _oButton.x = iXPos;
  71. _oButton.y = iYPos;
  72. };
  73. this.setX = function(iXPos){
  74. _oButton.x = iXPos;
  75. };
  76. this.setY = function(iYPos){
  77. _oButton.y = iYPos;
  78. };
  79. this.enable = function(){
  80. _bDisable = false;
  81. };
  82. this.disable = function(){
  83. _bDisable = true;
  84. };
  85. this.getButtonImage = function(){
  86. return _oButton;
  87. };
  88. this.getX = function(){
  89. return _oButton.x;
  90. };
  91. this.getY = function(){
  92. return _oButton.y;
  93. };
  94. _oParentContainer = oParentContainer;
  95. this._init(iXPos,iYPos,oSprite);
  96. return this;
  97. }