CTextButton.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. function CTextButton(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize,bAttach){
  2. var _bDisable;
  3. var _iWidth;
  4. var _iHeight;
  5. var _aCbCompleted;
  6. var _aCbOwner;
  7. var _oParams;
  8. var _oListenerDown;
  9. var _oListenerRelease;
  10. var _oButton;
  11. var _oTextBack;
  12. var _oText;
  13. var _oButtonBg;
  14. this._init =function(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize,bAttach){
  15. _bDisable = false;
  16. _aCbCompleted=new Array();
  17. _aCbOwner =new Array();
  18. _oButtonBg = createBitmap( oSprite);
  19. _iWidth = oSprite.width;
  20. _iHeight = oSprite.height;
  21. var iStepShadow = Math.ceil(iFontSize/20);
  22. _oTextBack = new createjs.Text(szText,iFontSize+"px "+szFont, "#000000");
  23. var oBounds = _oTextBack.getBounds();
  24. _oTextBack.textAlign = "center";
  25. _oTextBack.lineWidth = _iWidth *0.9;
  26. _oTextBack.textBaseline = "alphabetic";
  27. _oTextBack.x = oSprite.width/2 + iStepShadow;
  28. _oTextBack.y = Math.floor((oSprite.height)/2) +(oBounds.height/3) + iStepShadow;
  29. _oText = new createjs.Text(szText,iFontSize+"px "+szFont, szColor);
  30. _oText.textAlign = "center";
  31. _oText.textBaseline = "alphabetic";
  32. _oText.lineWidth = _iWidth *0.9;
  33. _oText.x = oSprite.width/2;
  34. _oText.y = Math.floor((oSprite.height)/2) +(oBounds.height/3);
  35. _oButton = new createjs.Container();
  36. _oButton.x = iXPos;
  37. _oButton.y = iYPos;
  38. _oButton.regX = oSprite.width/2;
  39. _oButton.regY = oSprite.height/2;
  40. if (!s_bMobile){
  41. _oButton.cursor = "pointer";
  42. }
  43. _oButton.addChild(_oButtonBg,_oTextBack,_oText);
  44. if(bAttach !== false){
  45. s_oStage.addChild(_oButton);
  46. }
  47. this._initListener();
  48. };
  49. this.unload = function(){
  50. _oButton.off("mousedown", _oListenerDown);
  51. _oButton.off("pressup" , _oListenerRelease);
  52. s_oStage.removeChild(_oButton);
  53. };
  54. this.setVisible = function(bVisible){
  55. _oButton.visible = bVisible;
  56. };
  57. this.setAlign = function(szAlign){
  58. _oText.textAlign = szAlign;
  59. _oTextBack.textAlign = szAlign;
  60. };
  61. this.enable = function(){
  62. _bDisable = false;
  63. _oButtonBg.filters = [];
  64. _oButtonBg.cache(0,0,_iWidth,_iHeight);
  65. };
  66. this.disable = function(){
  67. _bDisable = true;
  68. var matrix = new createjs.ColorMatrix().adjustSaturation(-100).adjustBrightness(40);
  69. _oButtonBg.filters = [
  70. new createjs.ColorMatrixFilter(matrix)
  71. ];
  72. _oButtonBg.cache(0,0,_iWidth,_iHeight);
  73. };
  74. this._initListener = function(){
  75. _oListenerDown = _oButton.on("mousedown", this.buttonDown);
  76. _oListenerRelease = _oButton.on("pressup" , this.buttonRelease);
  77. };
  78. this.addEventListener = function( iEvent,cbCompleted, cbOwner ){
  79. _aCbCompleted[iEvent]=cbCompleted;
  80. _aCbOwner[iEvent] = cbOwner;
  81. };
  82. this.addEventListenerWithParams = function(iEvent,cbCompleted, cbOwner,oParams){
  83. _aCbCompleted[iEvent]=cbCompleted;
  84. _aCbOwner[iEvent] = cbOwner;
  85. _oParams = oParams;
  86. };
  87. this.buttonRelease = function(){
  88. if(_bDisable){
  89. return;
  90. }
  91. playSound("click",1,false);
  92. _oButton.scaleX = 1;
  93. _oButton.scaleY = 1;
  94. if(_aCbCompleted[ON_MOUSE_UP]){
  95. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP],_oParams);
  96. }
  97. };
  98. this.buttonDown = function(){
  99. if(_bDisable){
  100. return;
  101. }
  102. _oButton.scaleX = 0.9;
  103. _oButton.scaleY = 0.9;
  104. if(_aCbCompleted[ON_MOUSE_DOWN]){
  105. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN]);
  106. }
  107. };
  108. this.setPosition = function(iXPos,iYPos){
  109. _oButton.x = iXPos;
  110. _oButton.y = iYPos;
  111. };
  112. this.changeText = function(szText){
  113. _oText.text = szText;
  114. _oTextBack.text = 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._init(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize,bAttach);
  135. return this;
  136. }