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 cc.Assert(delegate != null, "TouchHandler.initWithDelegate():touch delegate should not be null"); 89 this._delegate = delegate; 90 this._priority = priority; 91 this._enabledSelectors = 0; 92 return true; 93 } 94 }); 95 96 /** 97 * Create a TouchHandler with a delegate and a priority 98 * @param {cc.TouchDelegate} delegate 99 * @param {Number} priority 100 * @return {cc.TouchHandler} 101 */ 102 cc.TouchHandler.create = function (delegate, priority) { 103 var handler = new cc.TouchHandler(); 104 if (handler) { 105 handler.initWithDelegate(delegate, priority); 106 } 107 return handler; 108 }; 109 110 /** 111 * cc.StandardTouchHandler 112 * It forwardes each event to the delegate. 113 * @class 114 * @extends cc.TouchHandler 115 */ 116 cc.StandardTouchHandler = cc.TouchHandler.extend(/** @lends cc.StandardTouchHandler# */{ 117 /** 118 * Initializes a TouchHandler with a delegate and a priority 119 * @param {cc.TouchDelegate} delegate 120 * @param {Number} priority 121 * @return {Boolean} 122 */ 123 initWithDelegate:function (delegate, priority) { 124 return cc.TouchHandler.prototype.initWithDelegate.call(this, delegate, priority); 125 } 126 }); 127 128 /** 129 * Create a TouchHandler with a delegate and a priority 130 * @param {Object} delegate 131 * @param {Number} priority 132 * @return {cc.StandardTouchHandler} 133 */ 134 cc.StandardTouchHandler.create = function (delegate, priority) { 135 var handler = new cc.StandardTouchHandler(); 136 if (handler) { 137 handler.initWithDelegate(delegate, priority); 138 } 139 return handler; 140 }; 141 142 /** 143 * @class 144 * @extends cc.TouchHandler 145 */ 146 cc.TargetedTouchHandler = cc.TouchHandler.extend(/** @lends cc.TargetedTouchHandler# */{ 147 _swallowsTouches:false, 148 _claimedTouches:null, 149 150 /** 151 * Whether or not the touches are swallowed 152 * @return {Boolean} 153 */ 154 isSwallowsTouches:function () { 155 return this._swallowsTouches; 156 }, 157 158 /** 159 * @param {Boolean} swallowsTouches 160 */ 161 setSwallowsTouches:function (swallowsTouches) { 162 this._swallowsTouches = swallowsTouches; 163 }, 164 165 /** 166 * MutableSet that contains the claimed touches 167 * @return {Array} 168 */ 169 getClaimedTouches:function () { 170 return this._claimedTouches; 171 }, 172 173 /** 174 * Initializes a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not 175 * @param {cc.TouchDelegate} delegate 176 * @param {Number} priority 177 * @param {Boolean} swallow 178 * @return {Boolean} 179 */ 180 initWithDelegate:function (delegate, priority, swallow) { 181 if (cc.TouchHandler.prototype.initWithDelegate.call(this, delegate, priority)) { 182 this._claimedTouches = []; 183 this._swallowsTouches = swallow; 184 return true; 185 } 186 return false; 187 } 188 }); 189 190 /** 191 * Create a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not 192 * @param {Object} delegate 193 * @param {Number} priority 194 * @param {Boolean} swallow 195 * @return {cc.TargetedTouchHandler} 196 */ 197 cc.TargetedTouchHandler.create = function (delegate, priority, swallow) { 198 var handler = new cc.TargetedTouchHandler(); 199 if (handler) { 200 handler.initWithDelegate(delegate, priority, swallow); 201 } 202 return handler; 203 }; 204