1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 
 28 /**
 29  * you must extend the keyboardDelegate and
 30  * implement your own game logic in
 31  * keydown and keyup functions
 32  * @class
 33  * @extends cc.Class
 34  */
 35 cc.KeyboardDelegate = cc.Class.extend(/** @lends cc.KeyboardDelegate# */{
 36     /**
 37      * Call back when a key is pressed down
 38      * @param {Integer} keyCode
 39      * @example
 40      * // example
 41      * if(keyCode == cc.KEY.w){}
 42      */
 43     onKeyDown:function (keyCode) {
 44     },
 45 
 46     /**
 47      * Call back when a key is released
 48      * @param {Integer} keyCode
 49      * @example
 50      * // example
 51      * if(keyCode == cc.KEY.w){}
 52      */
 53     onKeyUp:function (keyCode) {
 54     }
 55 });
 56 
 57 /**
 58  * KeyboardHandler is an object that contains KeyboardDelegate
 59  * @class
 60  * @extends cc.Class
 61  */
 62 cc.KeyboardHandler = cc.Class.extend(/** @lends cc.KeyboardHandler# */{
 63     /**
 64      * returns the keyboard delegate
 65      * @return {cc.KeyboardDelegate}
 66      */
 67     getDelegate:function () {
 68         return this._delegate;
 69     },
 70 
 71     /**
 72      * set the keyboard delegate
 73      * @param {cc.KeyboardDelegate} delegate
 74      */
 75     setDelegate:function (delegate) {
 76         this._delegate = delegate;
 77     },
 78     /**
 79      * initializes a cc.KeyboardHandler with a delegate
 80      * @param {cc.KeyboardDelegate} delegate
 81      * @return {Boolean}
 82      */
 83     initWithDelegate:function (delegate) {
 84         cc.Assert(delegate != null, "It's a wrong delegate!");
 85 
 86         this._delegate = delegate;
 87 
 88         return true;
 89     },
 90     _delegate:null
 91 });
 92 /**
 93  * Create a KeyboardHandler with KeyboardDelegate
 94  * @param delegate
 95  * @return {cc.KeyboardHandler}
 96  */
 97 cc.KeyboardHandler.create = function (delegate) {
 98     var handler = new cc.KeyboardHandler();
 99     handler.initWithDelegate(delegate);
100     return handler;
101 };
102