CTextButton.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. function CTextButton(iXPos, iYPos, oSprite, szText, szFont, szColor, iFontSize, oParentContainer) {
  2. var _aCbCompleted;
  3. var _aCbOwner;
  4. var _oButton;
  5. var _oMouseDown;
  6. var _oMouseUp;
  7. this._init = function (iXPos, iYPos, oSprite, szText, szFont, szColor, iFontSize, oParentContainer) {
  8. _aCbCompleted = new Array();
  9. _aCbOwner = new Array();
  10. var oButtonBg = createBitmap(oSprite);
  11. var iStepShadow = Math.ceil(iFontSize / 20);
  12. var oTextBack = new createjs.Text(szText, "bold " + iFontSize + "px " + szFont, "#000000");
  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. var oText = new createjs.Text(szText, "bold " + iFontSize + "px " + szFont, 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, oText);
  30. if (!s_bMobile)
  31. _oButton.cursor = "pointer";
  32. oParentContainer.addChild(_oButton);
  33. this._initListener();
  34. };
  35. this.unload = function () {
  36. _oButton.off("mousedown", _oMouseDown);
  37. _oButton.off("pressup", _oMouseUp);
  38. oParentContainer.removeChild(_oButton);
  39. };
  40. this.setVisible = function (bVisible) {
  41. _oButton.visible = bVisible;
  42. };
  43. this._initListener = function () {
  44. _oMouseDown = _oButton.on("mousedown", this.buttonDown);
  45. _oMouseUp = _oButton.on("pressup", this.buttonRelease);
  46. };
  47. this.addEventListener = function (iEvent, cbCompleted, cbOwner) {
  48. _aCbCompleted[iEvent] = cbCompleted;
  49. _aCbOwner[iEvent] = cbOwner;
  50. };
  51. this.buttonRelease = function () {
  52. _oButton.scaleX = 1;
  53. _oButton.scaleY = 1;
  54. playSound("click", 1, false);
  55. if (_aCbCompleted[ON_MOUSE_UP]) {
  56. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP]);
  57. }
  58. };
  59. this.buttonDown = function () {
  60. _oButton.scaleX = 0.9;
  61. _oButton.scaleY = 0.9;
  62. if (_aCbCompleted[ON_MOUSE_DOWN]) {
  63. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN]);
  64. }
  65. };
  66. this.setPosition = function (iXPos, iYPos) {
  67. _oButton.x = iXPos;
  68. _oButton.y = iYPos;
  69. };
  70. this.setX = function (iXPos) {
  71. _oButton.x = iXPos;
  72. };
  73. this.setY = function (iYPos) {
  74. _oButton.y = iYPos;
  75. };
  76. this.getButtonImage = function () {
  77. return _oButton;
  78. };
  79. this.getX = function () {
  80. return _oButton.x;
  81. };
  82. this.getY = function () {
  83. return _oButton.y;
  84. };
  85. this._init(iXPos, iYPos, oSprite, szText, szFont, szColor, iFontSize, oParentContainer);
  86. return this;
  87. }