CToggle.js 2.8 KB

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