CGfxButton.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. function CGfxButton(iXPos, iYPos, oSprite, oParentContainer) {
  2. var _aCbCompleted;
  3. var _aCbOwner;
  4. var _oButton;
  5. var _aParams;
  6. var _fScaleX;
  7. var _fScaleY;
  8. var _oParent;
  9. var _oTween;
  10. var _oMouseDown;
  11. var _oMouseUp;
  12. var _bBlock = false;
  13. var _oParentContainer;
  14. this._init = function (iXPos, iYPos, oSprite) {
  15. _aCbCompleted = new Array();
  16. _aCbOwner = new Array();
  17. _aParams = new Array();
  18. _oButton = createBitmap(oSprite);
  19. _oButton.x = iXPos;
  20. _oButton.y = iYPos;
  21. _fScaleX = 1;
  22. _fScaleY = 1;
  23. _oButton.regX = oSprite.width / 2;
  24. _oButton.regY = oSprite.height / 2;
  25. if (!s_bMobile)
  26. _oButton.cursor = "pointer";
  27. _oParentContainer.addChild(_oButton);
  28. this._initListener();
  29. };
  30. this.unload = function () {
  31. _oButton.off("mousedown", _oMouseDown);
  32. _oButton.off("pressup", _oMouseUp);
  33. _oParentContainer.removeChild(_oButton);
  34. };
  35. this.setVisible = function (bVisible) {
  36. _oButton.visible = bVisible;
  37. };
  38. this.setCursorType = function (szValue) {
  39. _oButton.cursor = szValue;
  40. };
  41. this._initListener = function () {
  42. _oMouseDown = _oButton.on("mousedown", this.buttonDown);
  43. _oMouseUp = _oButton.on("pressup", this.buttonRelease);
  44. };
  45. this.addEventListener = function (iEvent, cbCompleted, cbOwner) {
  46. _aCbCompleted[iEvent] = cbCompleted;
  47. _aCbOwner[iEvent] = cbOwner;
  48. };
  49. this.addEventListenerWithParams = function (iEvent, cbCompleted, cbOwner, aParams) {
  50. _aCbCompleted[iEvent] = cbCompleted;
  51. _aCbOwner[iEvent] = cbOwner;
  52. _aParams[iEvent] = aParams;
  53. };
  54. this.buttonRelease = function () {
  55. if (_bBlock) {
  56. return;
  57. }
  58. if (_fScaleX > 0) {
  59. _oButton.scaleX = 1;
  60. } else {
  61. _oButton.scaleX = -1;
  62. }
  63. _oButton.scaleY = 1;
  64. playSound("click", 1, false);
  65. if (_aCbCompleted[ON_MOUSE_UP]) {
  66. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP], _aParams[ON_MOUSE_UP]);
  67. }
  68. };
  69. this.buttonDown = function () {
  70. if (_bBlock) {
  71. return;
  72. }
  73. if (_fScaleX > 0) {
  74. _oButton.scaleX = 0.9;
  75. } else {
  76. _oButton.scaleX = -0.9;
  77. }
  78. _oButton.scaleY = 0.9;
  79. if (_aCbCompleted[ON_MOUSE_DOWN]) {
  80. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN], _aParams[ON_MOUSE_DOWN]);
  81. }
  82. };
  83. this.rotation = function (iRotation) {
  84. _oButton.rotation = iRotation;
  85. };
  86. this.getButton = function () {
  87. return _oButton;
  88. };
  89. this.setPosition = function (iXPos, iYPos) {
  90. _oButton.x = iXPos;
  91. _oButton.y = iYPos;
  92. };
  93. this.setX = function (iXPos) {
  94. _oButton.x = iXPos;
  95. };
  96. this.setY = function (iYPos) {
  97. _oButton.y = iYPos;
  98. };
  99. this.getButtonImage = function () {
  100. return _oButton;
  101. };
  102. this.block = function (bVal) {
  103. _bBlock = bVal;
  104. _oButton.scaleX = _fScaleX;
  105. _oButton.scaleY = _fScaleY;
  106. };
  107. this.setScaleX = function (fValue) {
  108. _oButton.scaleX = fValue;
  109. _fScaleX = fValue;
  110. };
  111. this.getX = function () {
  112. return _oButton.x;
  113. };
  114. this.getY = function () {
  115. return _oButton.y;
  116. };
  117. this.pulseAnimation = function () {
  118. _oTween = createjs.Tween.get(_oButton).to({scaleX: _fScaleX * 0.9, scaleY: _fScaleY * 0.9}, 850, createjs.Ease.quadOut).to({scaleX: _fScaleX, scaleY: _fScaleY}, 650, createjs.Ease.quadIn).call(function () {
  119. _oParent.pulseAnimation();
  120. });
  121. };
  122. this.trebleAnimation = function () {
  123. _oTween = createjs.Tween.get(_oButton).to({rotation: 5}, 75, createjs.Ease.quadOut).to({rotation: -5}, 140, createjs.Ease.quadIn).to({rotation: 0}, 75, createjs.Ease.quadIn).wait(750).call(function () {
  124. _oParent.trebleAnimation();
  125. });
  126. };
  127. this.removeAllTweens = function () {
  128. createjs.Tween.removeTweens(_oButton);
  129. };
  130. if (oParentContainer !== undefined) {
  131. _oParentContainer = oParentContainer;
  132. } else {
  133. _oParentContainer = s_oStage;
  134. }
  135. this._init(iXPos, iYPos, oSprite);
  136. _oParent = this;
  137. return this;
  138. }