1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 /**
 27  * The data of touch event
 28  * @class
 29  * @extends cc.Class
 30  */
 31 cc.Touch = cc.Class.extend(/** @lends cc.Touch# */{
 32     _point:null,
 33     _prevPoint:null,
 34     _id:0,
 35     _startPointCaptured: false,
 36     _startPoint:null,
 37 
 38     /**
 39      * Constructor
 40      */
 41     ctor:function (x, y, id) {
 42         this._point = cc.p(x || 0, y || 0);
 43         this._id = id || 0;
 44     },
 45 
 46     /**
 47      * returns the current touch location in OpenGL coordinates
 48      * @return {cc.Point}
 49      */
 50     getLocation:function () {
 51         //TODO
 52         //return cc.director.convertToGL(this._point);
 53         return this._point;
 54     },
 55 
 56 	/**
 57 	 * gets location X axis data
 58 	 * @returns {number}
 59 	 */
 60 	getLocationX: function () {
 61 		return this._point.x;
 62 	},
 63 
 64 	/**
 65 	 * gets location Y axis data
 66 	 * @returns {number}
 67 	 */
 68 	getLocationY: function () {
 69 		return this._point.y;
 70 	},
 71 
 72     /**
 73      * returns the previous touch location in OpenGL coordinates
 74      * @return {cc.Point}
 75      */
 76     getPreviousLocation:function () {
 77         //TODO
 78         //return cc.director.convertToGL(this._prevPoint);
 79         return this._prevPoint;
 80     },
 81 
 82     /**
 83      * returns the start touch location in OpenGL coordinates
 84      * @returns {cc.Point}
 85      */
 86     getStartLocation: function() {
 87         //TODO
 88         //return cc.director.convertToGL(this._startPoint);
 89         return this._startPoint;
 90     },
 91 
 92     /**
 93      * returns the delta of 2 current touches locations in screen coordinates
 94      * @return {cc.Point}
 95      */
 96     getDelta:function () {
 97         return cc.pSub(this._point, this._prevPoint);
 98     },
 99 
100     /**
101      * returns the current touch location in screen coordinates
102      * @return {cc.Point}
103      */
104     getLocationInView: function() {
105         return this._point;
106     },
107 
108     /**
109      * returns the previous touch location in screen coordinates
110      * @return {cc.Point}
111      */
112     getPreviousLocationInView: function(){
113         return this._prevPoint;
114     },
115 
116     /**
117      * returns the start touch location in screen coordinates
118      * @return {cc.Point}
119      */
120     getStartLocationInView: function(){
121         return this._startPoint;
122     },
123 
124     /**
125      * @return {Number}
126      */
127     getID:function () {
128         return this._id;
129     },
130 
131     /**
132      * @return {Number}
133      */
134     getId:function () {
135         return this._id;
136     },
137 
138     /**
139      * set information to touch
140      * @param {Number} id
141      * @param  {Number} x
142      * @param  {Number} y
143      */
144     setTouchInfo:function (id, x, y) {
145         this._prevPoint = this._point;
146         this._point = cc.p(x || 0, y || 0);
147         this._id = id;
148         if(!this._startPointCaptured){
149             this._startPoint = cc.p(this._point);
150             this._startPointCaptured = true;
151         }
152     },
153 
154     _setPoint: function(x, y){
155         if(y === undefined){
156             this._point.x = x.x;
157             this._point.y = x.y;
158         }else{
159             this._point.x = x;
160             this._point.y = y;
161         }
162     },
163 
164     _setPrevPoint:function (x, y) {
165         if(y === undefined)
166             this._prevPoint = cc.p(x.x, x.y);
167         else
168             this._prevPoint = cc.p(x || 0, y || 0);
169     }
170 });
171 
172 //how the touches are dispatched
173 cc.Touch.ALL_AT_ONCE = 0;
174 cc.Touch.ONE_BY_ONE = 1;