CTextButton.js 4.0 KB

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