CTextButton.js 3.2 KB

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