1 /****************************************************************************
  2  Copyright (c) 2011-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 touch event class
 28  * @class
 29  * @extends cc.Class
 30  *
 31  * @param {Number} x
 32  * @param {Number} y
 33  * @param {Number} id
 34  */
 35 cc.Touch = cc.Class.extend(/** @lends cc.Touch# */{
 36     _point:null,
 37     _prevPoint:null,
 38     _id:0,
 39     _startPointCaptured: false,
 40     _startPoint:null,
 41 
 42     ctor:function (x, y, id) {
 43         this.setTouchInfo(id, x, y);
 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 {x: this._point.x, y: this._point.y};
 54     },
 55 
 56 	/**
 57 	 * Returns X axis location value
 58 	 * @returns {number}
 59 	 */
 60 	getLocationX: function () {
 61 		return this._point.x;
 62 	},
 63 
 64 	/**
 65      * Returns Y axis location value
 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 {x: this._prevPoint.x, y: this._prevPoint.y};
 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 {x: this._startPoint.x, y: this._startPoint.y};
 90     },
 91 
 92     /**
 93      * Returns the delta distance from the previous touche to the current one 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 {x: this._point.x, y: this._point.y};
106     },
107 
108     /**
109      * Returns the previous touch location in screen coordinates
110      * @return {cc.Point}
111      */
112     getPreviousLocationInView: function(){
113         return {x: this._prevPoint.x, y: this._prevPoint.y};
114     },
115 
116     /**
117      * Returns the start touch location in screen coordinates
118      * @return {cc.Point}
119      */
120     getStartLocationInView: function(){
121         return {x: this._startPoint.x, y: this._startPoint.y};
122     },
123 
124     /**
125      * Returns the id of cc.Touch
126      * @return {Number}
127      */
128     getID:function () {
129         return this._id;
130     },
131 
132     /**
133      * Returns the id of cc.Touch
134      * @return {Number}
135      * @deprecated since v3.0, please use getID() instead
136      */
137     getId:function () {
138         cc.log("getId is deprecated. Please use getID instead.")
139         return this._id;
140     },
141 
142     /**
143      * Sets information to touch
144      * @param {Number} id
145      * @param  {Number} x
146      * @param  {Number} y
147      */
148     setTouchInfo:function (id, x, y) {
149         this._prevPoint = this._point;
150         this._point = cc.p(x || 0, y || 0);
151         this._id = id;
152         if(!this._startPointCaptured){
153             this._startPoint = cc.p(this._point);
154             this._startPointCaptured = true;
155         }
156     },
157 
158     _setPoint: function(x, y){
159         if(y === undefined){
160             this._point.x = x.x;
161             this._point.y = x.y;
162         }else{
163             this._point.x = x;
164             this._point.y = y;
165         }
166     },
167 
168     _setPrevPoint:function (x, y) {
169         if(y === undefined)
170             this._prevPoint = cc.p(x.x, x.y);
171         else
172             this._prevPoint = cc.p(x || 0, y || 0);
173     }
174 });