CCEventExtension.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /****************************************************************************
  2. Copyright (c) 2011-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. /**
  22. * The acceleration event
  23. * @class
  24. * @extends cc.Event
  25. */
  26. cc.EventAcceleration = cc.Event.extend(/** @lends cc.EventAcceleration# */{
  27. _acc: null,
  28. ctor: function (acc) {
  29. cc.Event.prototype.ctor.call(this, cc.Event.ACCELERATION);
  30. this._acc = acc;
  31. }
  32. });
  33. /**
  34. * The keyboard event
  35. * @class
  36. * @extends cc.Event
  37. */
  38. cc.EventKeyboard = cc.Event.extend(/** @lends cc.EventKeyboard# */{
  39. _keyCode: 0,
  40. _isPressed: false,
  41. ctor: function (keyCode, isPressed) {
  42. cc.Event.prototype.ctor.call(this, cc.Event.KEYBOARD);
  43. this._keyCode = keyCode;
  44. this._isPressed = isPressed;
  45. }
  46. });
  47. //Acceleration
  48. cc._EventListenerAcceleration = cc.EventListener.extend({
  49. _onAccelerationEvent: null,
  50. ctor: function (callback) {
  51. this._onAccelerationEvent = callback;
  52. var selfPointer = this;
  53. var listener = function (event) {
  54. selfPointer._onAccelerationEvent(event._acc, event);
  55. };
  56. cc.EventListener.prototype.ctor.call(this, cc.EventListener.ACCELERATION, cc._EventListenerAcceleration.LISTENER_ID, listener);
  57. },
  58. checkAvailable: function () {
  59. cc.assert(this._onAccelerationEvent, cc._LogInfos._EventListenerAcceleration_checkAvailable);
  60. return true;
  61. },
  62. clone: function () {
  63. return new cc._EventListenerAcceleration(this._onAccelerationEvent);
  64. }
  65. });
  66. cc._EventListenerAcceleration.LISTENER_ID = "__cc_acceleration";
  67. cc._EventListenerAcceleration.create = function (callback) {
  68. return new cc._EventListenerAcceleration(callback);
  69. };
  70. //Keyboard
  71. cc._EventListenerKeyboard = cc.EventListener.extend({
  72. onKeyPressed: null,
  73. onKeyReleased: null,
  74. ctor: function () {
  75. var selfPointer = this;
  76. var listener = function (event) {
  77. if (event._isPressed) {
  78. if (selfPointer.onKeyPressed)
  79. selfPointer.onKeyPressed(event._keyCode, event);
  80. } else {
  81. if (selfPointer.onKeyReleased)
  82. selfPointer.onKeyReleased(event._keyCode, event);
  83. }
  84. };
  85. cc.EventListener.prototype.ctor.call(this, cc.EventListener.KEYBOARD, cc._EventListenerKeyboard.LISTENER_ID, listener);
  86. },
  87. clone: function () {
  88. var eventListener = new cc._EventListenerKeyboard();
  89. eventListener.onKeyPressed = this.onKeyPressed;
  90. eventListener.onKeyReleased = this.onKeyReleased;
  91. return eventListener;
  92. },
  93. checkAvailable: function () {
  94. if (this.onKeyPressed == null && this.onKeyReleased == null) {
  95. cc.log(cc._LogInfos._EventListenerKeyboard_checkAvailable);
  96. return false;
  97. }
  98. return true;
  99. }
  100. });
  101. cc._EventListenerKeyboard.LISTENER_ID = "__cc_keyboard";
  102. cc._EventListenerKeyboard.create = function () {
  103. return new cc._EventListenerKeyboard();
  104. };