CTextButton.js 3.9 KB

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