CToggle.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. function CToggle(iXPos, iYPos, oSprite, bActive,oParentContainer) {
  2. var _bActive;
  3. var _aCbCompleted;
  4. var _aCbOwner;
  5. var _aParams = [];
  6. var _oButton;
  7. var _oParentContainer;
  8. this._init = function (iXPos, iYPos, oSprite, bActive) {
  9. _aCbCompleted = new Array();
  10. _aCbOwner = new Array();
  11. var oData = {
  12. images: [oSprite],
  13. // width, height & registration point of each sprite
  14. frames: {width: oSprite.width / 2, height: oSprite.height, regX: (oSprite.width / 2) / 2, regY: oSprite.height / 2},
  15. animations: {state_true: [0], state_false: [1]}
  16. };
  17. var oSpriteSheet = new createjs.SpriteSheet(oData);
  18. _bActive = bActive;
  19. _oButton = createSprite(oSpriteSheet, "state_" + _bActive, (oSprite.width / 2) / 2, oSprite.height / 2, oSprite.width / 2, oSprite.height);
  20. _oButton.mouseEnabled = true;
  21. _oButton.x = iXPos;
  22. _oButton.y = iYPos;
  23. _oButton.stop();
  24. if (!s_bMobile)
  25. _oButton.cursor = "pointer";
  26. _oParentContainer.addChild(_oButton);
  27. this._initListener();
  28. };
  29. this.unload = function () {
  30. _oButton.off("mousedown", this.buttonDown);
  31. _oButton.off("pressup", this.buttonRelease);
  32. _oButton.mouseEnabled = false;
  33. _oParentContainer.removeChild(_oButton);
  34. };
  35. this._initListener = function () {
  36. _oButton.on("mousedown", this.buttonDown);
  37. _oButton.on("pressup", this.buttonRelease);
  38. };
  39. this.addEventListener = function (iEvent, cbCompleted, cbOwner) {
  40. _aCbCompleted[iEvent] = cbCompleted;
  41. _aCbOwner[iEvent] = cbOwner;
  42. };
  43. this.addEventListenerWithParams = function (iEvent, cbCompleted, cbOwner, aParams) {
  44. _aCbCompleted[iEvent] = cbCompleted;
  45. _aCbOwner[iEvent] = cbOwner;
  46. _aParams = aParams;
  47. };
  48. this.setActive = function (bActive) {
  49. _bActive = bActive;
  50. _oButton.gotoAndStop("state_" + _bActive);
  51. };
  52. this.buttonRelease = function () {
  53. _oButton.scaleX = 1;
  54. _oButton.scaleY = 1;
  55. playSound("click", 1, false);
  56. _bActive = !_bActive;
  57. _oButton.gotoAndStop("state_" + _bActive);
  58. if (_aCbCompleted[ON_MOUSE_UP]) {
  59. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP], _aParams);
  60. }
  61. };
  62. this.buttonDown = function () {
  63. _oButton.scaleX = 0.9;
  64. _oButton.scaleY = 0.9;
  65. if (_aCbCompleted[ON_MOUSE_DOWN]) {
  66. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN], _aParams);
  67. }
  68. };
  69. this.setPosition = function (iXPos, iYPos) {
  70. _oButton.x = iXPos;
  71. _oButton.y = iYPos;
  72. };
  73. this.setVisible = function (bVisible) {
  74. _oButton.visible = bVisible;
  75. };
  76. _oParentContainer = oParentContainer;
  77. this._init(iXPos, iYPos, oSprite, bActive);
  78. }