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 * @class 29 * @extends cc.Class 30 */ 31 cc.Touch = cc.Class.extend(/** @lends cc.Touch# */{ 32 _point:null, 33 _prevPoint:cc.PointZero(), 34 _id:0, 35 36 /** 37 * Constructor 38 */ 39 ctor:function (x, y, id) { 40 this._point = cc.p(x || 0, y || 0); 41 this._id = id || 0; 42 }, 43 44 /** 45 * get point of touch 46 * @return {cc.Point} 47 */ 48 getLocation:function () { 49 return this._point; 50 }, 51 52 /** 53 * @return {cc.Point} 54 */ 55 getPreviousLocation:function () { 56 return this._prevPoint; 57 }, 58 59 /** 60 * @return {cc.Point} 61 */ 62 getDelta:function () { 63 return cc.pSub(this._point, this._prevPoint); 64 }, 65 66 /** 67 * @return {Number} 68 */ 69 getID:function () { 70 return this._id; 71 }, 72 73 /** 74 * @return {Number} 75 */ 76 getId:function () { 77 return this._id; 78 }, 79 80 /** 81 * set information to touch 82 * @param {Number} id 83 * @param {Number} x 84 * @param {Number} y 85 */ 86 setTouchInfo:function (id, x, y) { 87 this._prevPoint = this._point; 88 this._point = cc.p(x || 0, y || 0); 89 this._id = id; 90 }, 91 92 _setPrevPoint:function (x, y) { 93 this._prevPoint = cc.p(x || 0, y || 0); 94 } 95 }); 96 97 /** 98 * @class 99 * @extends cc.Class 100 */ 101 cc.TouchDelegate = cc.Class.extend(/** @lends cc.TouchDelegate# */{ 102 _eventTypeFuncMap:null, 103 104 /** 105 * Virtual function 106 * @param {cc.Touch} touch 107 * @param {event} event 108 * @return {Boolean} 109 */ 110 onTouchBegan:function (touch, event) { 111 return false; 112 }, 113 114 /** 115 * Virtual function 116 * @param {cc.Touch} touch 117 * @param {event} event 118 */ 119 onTouchMoved:function (touch, event) { 120 }, 121 122 /** 123 * Virtual function 124 * @param {cc.Touch} touch 125 * @param {event} event 126 */ 127 onTouchEnded:function (touch, event) { 128 }, 129 130 /** 131 * Virtual function 132 * @param {cc.Touch} touch 133 * @param {event} event 134 */ 135 onTouchCancelled:function (touch, event) { 136 }, 137 138 /** 139 * Virtual function 140 * @param {Array} touches 141 * @param {event} event 142 */ 143 onTouchesBegan:function (touches, event) { 144 }, 145 146 /** 147 * Virtual function 148 * @param {Array} touches 149 * @param {event} event 150 */ 151 onTouchesMoved:function (touches, event) { 152 }, 153 154 /** 155 * Virtual function 156 * @param {Array} touches 157 * @param {event} event 158 */ 159 onTouchesEnded:function (touches, event) { 160 }, 161 162 /** 163 * Virtual function 164 * @param {Array} touches 165 * @param {event} event 166 */ 167 onTouchesCancelled:function (touches, event) { 168 }, 169 170 /* 171 * In TouchesTest, class Padle inherits from cc.Sprite and cc.TargetedTouchDelegate. 172 * When it invoke cc.registerTargetedDelegate(0, true, this), 173 * it will crash in cc.TouchHandler.initWithDelegate() because of dynamic_cast() on android. 174 * I don't know why, so add these functions for the subclass to invoke it's own retain() and release 175 *Virtual function 176 */ 177 touchDelegateRetain:function () { 178 }, 179 180 /** 181 * Virtual function 182 */ 183 touchDelegateRelease:function () { 184 } 185 }); 186 187 /** 188 * Using this type of delegate results in two benefits: 189 * - 1. You don't need to deal with cc.Sets, the dispatcher does the job of splitting 190 * them. You get exactly one UITouch per call. 191 * - 2. You can *claim* a UITouch by returning YES in onTouchBegan. Updates of claimed 192 * touches are sent only to the delegate(s) that claimed them. So if you get a move/ 193 * ended/cancelled update you're sure it's your touch. This frees you from doing a 194 * lot of checks when doing multi-touch. 195 * 196 * (The name TargetedTouchDelegate relates to updates "targeting" their specific 197 * handler, without bothering the other handlers.) 198 * @class 199 * @extends cc.Class 200 */ 201 cc.TargetedTouchDelegate = cc.TouchDelegate.extend(/** @lends cc.TargetedTouchDelegate# */{ 202 203 /** 204 * Return YES to claim the touch. 205 * @param {cc.Touch} touch 206 * @param {event} event 207 * @return {Boolean} 208 */ 209 onTouchBegan:function (touch, event) { 210 return false; 211 }, 212 213 /** 214 * Virtual function 215 * @param {cc.Touch} touch 216 * @param {event} event 217 */ 218 onTouchMoved:function (touch, event) { 219 }, 220 221 /** 222 * Virtual function 223 * @param {cc.Touch} touch 224 * @param {event} event 225 */ 226 onTouchEnded:function (touch, event) { 227 }, 228 229 /** 230 * Virtual function 231 * @param {cc.Touch} touch 232 * @param {event} event 233 */ 234 onTouchCancelled:function (touch, event) { 235 } 236 }); 237 238 /** 239 * This type of delegate is the same one used by CocoaTouch. You will receive all the events (Began,Moved,Ended,Cancelled). 240 * @class 241 * @extends cc.Class 242 */ 243 cc.StandardTouchDelegate = cc.TouchDelegate.extend(/** @lends cc.StandardTouchDelegate# */{ 244 245 /** 246 * Virtual function 247 * @param {Array} touches 248 * @param {event} event 249 */ 250 onTouchesBegan:function (touches, event) { 251 }, 252 253 /** 254 * Virtual function 255 * @param {Array} touches 256 * @param {event} event 257 */ 258 onTouchesMoved:function (touches, event) { 259 }, 260 261 /** 262 * Virtual function 263 * @param {Array} touches 264 * @param {event} event 265 */ 266 onTouchesEnded:function (touches, event) { 267 }, 268 269 /** 270 * Virtual function 271 * @param {Array} touches 272 * @param {event} event 273 */ 274 onTouchesCancelled:function (touches, event) { 275 } 276 }); 277 278