CTextButton.js 4.5 KB

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