CTextButton.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. function CTextButton(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize,oContainer){
  2. var _bDisable;
  3. var _iWidth;
  4. var _iHeight;
  5. var _aCbCompleted;
  6. var _aCbOwner;
  7. var _oListenerDown;
  8. var _oListenerRelease;
  9. var _oButton;
  10. var _oButtonBg;
  11. var _oText;
  12. var _oContainer;
  13. this._init =function(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize,oContainer){
  14. _bDisable = false;
  15. _aCbCompleted=new Array();
  16. _aCbOwner =new Array();
  17. _oContainer = oContainer;
  18. _oButton = new createjs.Container();
  19. _oButton.x = iXPos;
  20. _oButton.y = iYPos;
  21. _oButton.cursor = "pointer";
  22. _oContainer.addChild(_oButton);
  23. _oButtonBg = createBitmap( oSprite);
  24. _oButtonBg.regX = oSprite.width/2;
  25. _oButtonBg.regY = oSprite.height/2;
  26. _iWidth = oSprite.width;
  27. _iHeight = oSprite.height;
  28. _oButton.addChild(_oButtonBg);
  29. var iStepShadow = Math.ceil(iFontSize/20);
  30. var iWidth = oSprite.width-40;
  31. var iHeight = oSprite.height-20;
  32. var iX = -6;
  33. var iY = -4;
  34. _oText = new CTLText(_oButton,
  35. iX-iWidth/2, iY-iHeight/2, iWidth, iHeight,
  36. 44, "center", FONT_COLOR, FONT_GAME, 1,
  37. 2, 2,
  38. szText,
  39. true, true, false,
  40. false );
  41. _oText.setShadow("#000", 2,2,2);
  42. this._initListener();
  43. };
  44. this.unload = function(){
  45. _oButton.off("mousedown",_oListenerDown);
  46. _oButton.off("pressup" , _oListenerRelease);
  47. _oContainer.removeChild(_oButton);
  48. };
  49. this.setVisible = function(bVisible){
  50. _oButton.visible = bVisible;
  51. };
  52. this.enable = function(){
  53. _bDisable = false;
  54. _oText.setColor("#fff");
  55. };
  56. this.disable = function(){
  57. _bDisable = true;
  58. _oText.setColor("#a39b9d");
  59. };
  60. this._initListener = function(){
  61. _oListenerDown = _oButton.on("mousedown", this.buttonDown);
  62. _oListenerRelease = _oButton.on("pressup" , this.buttonRelease);
  63. };
  64. this.addEventListener = function( iEvent,cbCompleted, cbOwner ){
  65. _aCbCompleted[iEvent]=cbCompleted;
  66. _aCbOwner[iEvent] = cbOwner;
  67. };
  68. this.buttonRelease = function(){
  69. if(_bDisable){
  70. return;
  71. }
  72. playSound("click", 1,false);
  73. _oButton.scaleX = 1;
  74. _oButton.scaleY = 1;
  75. if(_aCbCompleted[ON_MOUSE_UP]){
  76. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP]);
  77. }
  78. };
  79. this.buttonDown = function(){
  80. if(_bDisable){
  81. return;
  82. }
  83. _oButton.scaleX = 0.9;
  84. _oButton.scaleY = 0.9;
  85. if(_aCbCompleted[ON_MOUSE_DOWN]){
  86. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN]);
  87. }
  88. };
  89. this.setPosition = function(iXPos,iYPos){
  90. _oButton.x = iXPos;
  91. _oButton.y = iYPos;
  92. };
  93. this.changeText = function(szText){
  94. _oText.refreshText(szText);
  95. };
  96. this.setX = function(iXPos){
  97. _oButton.x = iXPos;
  98. };
  99. this.setY = function(iYPos){
  100. _oButton.y = iYPos;
  101. };
  102. this.getButtonImage = function(){
  103. return _oButton;
  104. };
  105. this.getX = function(){
  106. return _oButton.x;
  107. };
  108. this.getY = function(){
  109. return _oButton.y;
  110. };
  111. this._init(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize,oContainer);
  112. return this;
  113. }