CTextButton.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. function CTextButton(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize,oParentContainer){
  2. var _bDisable;
  3. var _iCurScale;
  4. var _iWidth;
  5. var _iHeight;
  6. var _aCbCompleted;
  7. var _aCbOwner;
  8. var _oListenerDown;
  9. var _oListenerUp;
  10. var _oParams;
  11. var _oButton;
  12. var _oText;
  13. var _oButtonBg;
  14. var _oParentContainer = oParentContainer;
  15. this._init = function(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize){
  16. _bDisable = false;
  17. _iCurScale = 1;
  18. _aCbCompleted=new Array();
  19. _aCbOwner =new Array();
  20. _oButtonBg = createBitmap( oSprite);
  21. _iWidth = oSprite.width;
  22. _iHeight = oSprite.height;
  23. _oButton = new createjs.Container();
  24. _oButton.x = iXPos;
  25. _oButton.y = iYPos;
  26. _oButton.regX = oSprite.width/2;
  27. _oButton.regY = oSprite.height/2;
  28. if (!s_bMobile){
  29. _oButton.cursor = "pointer";
  30. }
  31. _oButton.addChild(_oButtonBg,_oText);
  32. _oParentContainer.addChild(_oButton);
  33. _oText = new CTLText(_oButton,
  34. 30, 25, oSprite.width-60, oSprite.height-50,
  35. iFontSize, "center", szColor, szFont, 1.1,
  36. 0, 0,
  37. szText,
  38. true, true, false,
  39. false );
  40. this._initListener();
  41. };
  42. this.unload = function(){
  43. _oButton.off("mousedown", _oListenerDown);
  44. _oButton.off("pressup" , _oListenerUp);
  45. _oParentContainer.removeChild(_oButton);
  46. };
  47. this.setVisible = function(bVisible){
  48. _oButton.visible = bVisible;
  49. };
  50. this.setAlign = function(szAlign){
  51. _oText.textAlign = szAlign;
  52. };
  53. this.setTextX = function(iX){
  54. _oText.x = iX;
  55. };
  56. this.setScale = function(iScale){
  57. _oButton.scaleX = _oButton.scaleY = iScale;
  58. _iCurScale = iScale;
  59. };
  60. this.enable = function(){
  61. _bDisable = false;
  62. };
  63. this.disable = function(){
  64. _bDisable = true;
  65. };
  66. this._initListener = function(){
  67. _oListenerDown = _oButton.on("mousedown", this.buttonDown);
  68. _oListenerUp = _oButton.on("pressup" , this.buttonRelease);
  69. };
  70. this.addEventListener = function( iEvent,cbCompleted, cbOwner ){
  71. _aCbCompleted[iEvent]=cbCompleted;
  72. _aCbOwner[iEvent] = cbOwner;
  73. };
  74. this.addEventListenerWithParams = function(iEvent,cbCompleted, cbOwner,oParams){
  75. _aCbCompleted[iEvent]=cbCompleted;
  76. _aCbOwner[iEvent] = cbOwner;
  77. _oParams = oParams;
  78. };
  79. this.buttonRelease = function(){
  80. if(_bDisable){
  81. return;
  82. }
  83. playSound("click",1,false);
  84. _oButton.scaleX = _iCurScale;
  85. _oButton.scaleY = _iCurScale;
  86. if(_aCbCompleted[ON_MOUSE_UP]){
  87. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP],_oParams);
  88. }
  89. };
  90. this.buttonDown = function(){
  91. if(_bDisable){
  92. return;
  93. }
  94. _oButton.scaleX = _iCurScale*0.9;
  95. _oButton.scaleY = _iCurScale*0.9;
  96. if(_aCbCompleted[ON_MOUSE_DOWN]){
  97. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN]);
  98. }
  99. };
  100. this.setPosition = function(iXPos,iYPos){
  101. _oButton.x = iXPos;
  102. _oButton.y = iYPos;
  103. };
  104. this.tweenPosition = function(iXPos,iYPos,iTime,iDelay,oEase,oCallback,oCallOwner){
  105. createjs.Tween.get(_oButton).wait(iDelay).to({x:iXPos,y:iYPos}, iTime,oEase).call(function(){
  106. if(oCallback !== undefined){
  107. oCallback.call(oCallOwner);
  108. }
  109. });
  110. };
  111. this.changeText = function(szText){
  112. _oText.refreshText(szText);
  113. };
  114. this.setX = function(iXPos){
  115. _oButton.x = iXPos;
  116. };
  117. this.setY = function(iYPos){
  118. _oButton.y = iYPos;
  119. };
  120. this.getButtonImage = function(){
  121. return _oButton;
  122. };
  123. this.getX = function(){
  124. return _oButton.x;
  125. };
  126. this.getY = function(){
  127. return _oButton.y;
  128. };
  129. this.getSprite = function(){
  130. return _oButton;
  131. };
  132. this.getScale = function(){
  133. return _oButton.scaleX;
  134. };
  135. this._init(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize);
  136. }