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 * Allocates 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.handlerWithDelegate = 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 if (this._super(delegate, priority)) { 125 return true; 126 } 127 return false; 128 } 129 }); 130 131 /** 132 * Allocates a TouchHandler with a delegate and a priority 133 * @param {cc.TouchDelegate} delegate 134 * @param {Number} priority 135 * @return {cc.StandardTouchHandler} 136 */ 137 cc.StandardTouchHandler.handlerWithDelegate = function (delegate, priority) { 138 var handler = new cc.StandardTouchHandler(); 139 if (handler) { 140 handler.initWithDelegate(delegate, priority); 141 } 142 return handler; 143 }; 144 145 /** 146 * @class 147 * @extends cc.TouchHandler 148 */ 149 cc.TargetedTouchHandler = cc.TouchHandler.extend(/** @lends cc.TargetedTouchHandler# */{ 150 _swallowsTouches:false, 151 _claimedTouches:null, 152 153 /** 154 * Whether or not the touches are swallowed 155 * @return {Boolean} 156 */ 157 isSwallowsTouches:function () { 158 return this._swallowsTouches; 159 }, 160 161 /** 162 * @param {Boolean} swallowsTouches 163 */ 164 setSwallowsTouches:function (swallowsTouches) { 165 this._swallowsTouches = swallowsTouches; 166 }, 167 168 /** 169 * MutableSet that contains the claimed touches 170 * @return {Array} 171 */ 172 getClaimedTouches:function () { 173 return this._claimedTouches; 174 }, 175 176 /** 177 * Initializes a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not 178 * @param {cc.TouchDelegate} delegate 179 * @param {Number} priority 180 * @param {Boolean} swallow 181 * @return {Boolean} 182 */ 183 initWithDelegate:function (delegate, priority, swallow) { 184 if (this._super(delegate, priority)) { 185 this._claimedTouches = []; 186 this._swallowsTouches = swallow; 187 return true; 188 } 189 return false; 190 } 191 }); 192 193 /** 194 * Allocates a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not 195 * @param {cc.TouchDelegate} delegate 196 * @param {Number} priority 197 * @param {Boolean} swallow 198 * @return {cc.TargetedTouchHandler} 199 */ 200 cc.TargetedTouchHandler.handlerWithDelegate = function (delegate, priority, swallow) { 201 var handler = new cc.TargetedTouchHandler(); 202 if (handler) { 203 handler.initWithDelegate(delegate, priority, swallow); 204 } 205 return handler; 206 }; 207