CGfxButton.js 4.3 KB

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