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 * cc.TouchHandler 29 * Object than contains the delegate and priority of the event handler. 30 * @class 31 * @extends cc.Class 32 */ 33 cc.TouchHandler = cc.Class.extend(/** @lends cc.TouchHandler# */{ 34 _delegate:null, 35 _priority:0, 36 _enabledSelectors:0, 37 38 /** 39 * @return {cc.TouchDelegate} 40 */ 41 getDelegate:function () { 42 return this._delegate; 43 }, 44 45 /** 46 * @param {cc.TouchDelegate} delegate 47 */ 48 setDelegate:function (delegate) { 49 this._delegate = delegate; 50 }, 51 52 /** 53 * @return {Number} 54 */ 55 getPriority:function () { 56 return this._priority; 57 }, 58 59 /** 60 * @param {Number} priority 61 */ 62 setPriority:function (priority) { 63 this._priority = priority; 64 }, 65 66 /** 67 * Enabled selectors 68 * @return {Number} 69 */ 70 getEnabledSelectors:function () { 71 return this._enabledSelectors; 72 }, 73 74 /** 75 * @param {Number} value 76 */ 77 setEnalbedSelectors:function (value) { 78 this._enabledSelectors = value; 79 }, 80 81 /** 82 * initializes a TouchHandler with a delegate and a priority 83 * @param {cc.TouchDelegate} delegate 84 * @param {Number} priority 85 * @return {Boolean} 86 */ 87 initWithDelegate:function (delegate, priority) { 88 if(!delegate) 89 throw "cc.TouchHandler.initWithDelegate(): touch delegate should not be null"; 90 this._delegate = delegate; 91 this._priority = priority; 92 this._enabledSelectors = 0; 93 return true; 94 } 95 }); 96 97 /** 98 * Create a TouchHandler with a delegate and a priority 99 * @param {cc.TouchDelegate} delegate 100 * @param {Number} priority 101 * @return {cc.TouchHandler} 102 */ 103 cc.TouchHandler.create = function (delegate, priority) { 104 var handler = new cc.TouchHandler(); 105 if (handler) { 106 handler.initWithDelegate(delegate, priority); 107 } 108 return handler; 109 }; 110 111 /** 112 * cc.StandardTouchHandler 113 * It forwardes each event to the delegate. 114 * @class 115 * @extends cc.TouchHandler 116 */ 117 cc.StandardTouchHandler = cc.TouchHandler.extend(/** @lends cc.StandardTouchHandler# */{ 118 /** 119 * Initializes a TouchHandler with a delegate and a priority 120 * @param {cc.TouchDelegate} delegate 121 * @param {Number} priority 122 * @return {Boolean} 123 */ 124 initWithDelegate:function (delegate, priority) { 125 return cc.TouchHandler.prototype.initWithDelegate.call(this, delegate, priority); 126 } 127 }); 128 129 /** 130 * Create a TouchHandler with a delegate and a priority 131 * @param {Object} delegate 132 * @param {Number} priority 133 * @return {cc.StandardTouchHandler} 134 */ 135 cc.StandardTouchHandler.create = function (delegate, priority) { 136 var handler = new cc.StandardTouchHandler(); 137 if (handler) { 138 handler.initWithDelegate(delegate, priority); 139 } 140 return handler; 141 }; 142 143 /** 144 * @class 145 * @extends cc.TouchHandler 146 */ 147 cc.TargetedTouchHandler = cc.TouchHandler.extend(/** @lends cc.TargetedTouchHandler# */{ 148 _swallowsTouches:false, 149 _claimedTouches:null, 150 151 /** 152 * Whether or not the touches are swallowed 153 * @return {Boolean} 154 */ 155 isSwallowsTouches:function () { 156 return this._swallowsTouches; 157 }, 158 159 /** 160 * @param {Boolean} swallowsTouches 161 */ 162 setSwallowsTouches:function (swallowsTouches) { 163 this._swallowsTouches = swallowsTouches; 164 }, 165 166 /** 167 * MutableSet that contains the claimed touches 168 * @return {Array} 169 */ 170 getClaimedTouches:function () { 171 return this._claimedTouches; 172 }, 173 174 /** 175 * Initializes a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not 176 * @param {cc.TouchDelegate} delegate 177 * @param {Number} priority 178 * @param {Boolean} swallow 179 * @return {Boolean} 180 */ 181 initWithDelegate:function (delegate, priority, swallow) { 182 if (cc.TouchHandler.prototype.initWithDelegate.call(this, delegate, priority)) { 183 this._claimedTouches = []; 184 this._swallowsTouches = swallow; 185 return true; 186 } 187 return false; 188 } 189 }); 190 191 /** 192 * Create a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not 193 * @param {Object} delegate 194 * @param {Number} priority 195 * @param {Boolean} swallow 196 * @return {cc.TargetedTouchHandler} 197 */ 198 cc.TargetedTouchHandler.create = function (delegate, priority, swallow) { 199 var handler = new cc.TargetedTouchHandler(); 200 if (handler) { 201 handler.initWithDelegate(delegate, priority, swallow); 202 } 203 return handler; 204 }; 205