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