CTextButton.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function CTextButton(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize){
  2. var _aCbCompleted;
  3. var _aCbOwner;
  4. var _oButton;
  5. this._init =function(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize){
  6. _aCbCompleted=new Array();
  7. _aCbOwner =new Array();
  8. var oButtonBg = createBitmap( oSprite);
  9. var iStepShadow = Math.ceil(iFontSize/20);
  10. var oTextBack = new createjs.Text(szText,iFontSize+"px "+szFont, "#000000");
  11. oTextBack.textAlign = "center";
  12. var oBounds = oTextBack.getBounds();
  13. oTextBack.x = oSprite.width/2 + iStepShadow;
  14. oTextBack.y = ((oSprite.height) - oBounds.height)/2 + iStepShadow;
  15. var oText = new createjs.Text(szText,iFontSize+"px "+szFont, szColor);
  16. oText.textAlign = "center";
  17. var oBounds = oText.getBounds();
  18. oText.x = oSprite.width/2;
  19. oText.y = ((oSprite.height) - oBounds.height)/2;
  20. _oButton = new createjs.Container();
  21. _oButton.x = iXPos;
  22. _oButton.y = iYPos;
  23. _oButton.regX = oSprite.width/2;
  24. _oButton.regY = oSprite.height/2;
  25. _oButton.cursor = "pointer";
  26. _oButton.addChild(oButtonBg,oTextBack,oText);
  27. s_oStage.addChild(_oButton);
  28. this._initListener();
  29. };
  30. this.unload = function(){
  31. _oButton.off("mousedown");
  32. _oButton.off("pressup");
  33. };
  34. this.setVisible = function(bVisible){
  35. _oButton.visible = bVisible;
  36. };
  37. this._initListener = function(){
  38. oParent = this;
  39. _oButton.on("mousedown", this.buttonDown);
  40. _oButton.on("pressup" , this.buttonRelease);
  41. };
  42. this.addEventListener = function( iEvent,cbCompleted, cbOwner ){
  43. _aCbCompleted[iEvent]=cbCompleted;
  44. _aCbOwner[iEvent] = cbOwner;
  45. };
  46. this.buttonRelease = function(){
  47. _oButton.scaleX = 1;
  48. _oButton.scaleY = 1;
  49. playSound("click",1,false);
  50. if(_aCbCompleted[ON_MOUSE_UP]){
  51. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP]);
  52. }
  53. };
  54. this.buttonDown = function(){
  55. _oButton.scaleX = 0.9;
  56. _oButton.scaleY = 0.9;
  57. if(_aCbCompleted[ON_MOUSE_DOWN]){
  58. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN]);
  59. }
  60. };
  61. this.setPosition = function(iXPos,iYPos){
  62. _oButton.x = iXPos;
  63. _oButton.y = iYPos;
  64. };
  65. this.setX = function(iXPos){
  66. _oButton.x = iXPos;
  67. };
  68. this.setY = function(iYPos){
  69. _oButton.y = iYPos;
  70. };
  71. this.getButtonImage = function(){
  72. return _oButton;
  73. };
  74. this.getX = function(){
  75. return _oButton.x;
  76. };
  77. this.getY = function(){
  78. return _oButton.y;
  79. };
  80. this._init(iXPos,iYPos,oSprite,szText,szFont,szColor,iFontSize);
  81. return this;
  82. }