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._point = cc.p(x || 0, y || 0);
 44         this._id = id || 0;
 45     },
 46 
 47     /**
 48      * Returns the current touch location in OpenGL coordinates
 49      * @return {cc.Point}
 50      */
 51     getLocation:function () {
 52         //TODO
 53         //return cc.director.convertToGL(this._point);
 54         return {x: this._point.x, y: this._point.y};
 55     },
 56 
 57 	/**
 58 	 * Returns X axis location value
 59 	 * @returns {number}
 60 	 */
 61 	getLocationX: function () {
 62 		return this._point.x;
 63 	},
 64 
 65 	/**
 66      * Returns Y axis location value
 67 	 * @returns {number}
 68 	 */
 69 	getLocationY: function () {
 70 		return this._point.y;
 71 	},
 72 
 73     /**
 74      * Returns the previous touch location in OpenGL coordinates
 75      * @return {cc.Point}
 76      */
 77     getPreviousLocation:function () {
 78         //TODO
 79         //return cc.director.convertToGL(this._prevPoint);
 80         return {x: this._prevPoint.x, y: this._prevPoint.y};
 81     },
 82 
 83     /**
 84      * Returns the start touch location in OpenGL coordinates
 85      * @returns {cc.Point}
 86      */
 87     getStartLocation: function() {
 88         //TODO
 89         //return cc.director.convertToGL(this._startPoint);
 90         return {x: this._startPoint.x, y: this._startPoint.y};
 91     },
 92 
 93     /**
 94      * Returns the delta distance from the previous touche to the current one in screen coordinates
 95      * @return {cc.Point}
 96      */
 97     getDelta:function () {
 98         return cc.pSub(this._point, this._prevPoint);
 99     },
100 
101     /**
102      * Returns the current touch location in screen coordinates
103      * @return {cc.Point}
104      */
105     getLocationInView: function() {
106         return {x: this._point.x, y: this._point.y};
107     },
108 
109     /**
110      * Returns the previous touch location in screen coordinates
111      * @return {cc.Point}
112      */
113     getPreviousLocationInView: function(){
114         return {x: this._prevPoint.x, y: this._prevPoint.y};
115     },
116 
117     /**
118      * Returns the start touch location in screen coordinates
119      * @return {cc.Point}
120      */
121     getStartLocationInView: function(){
122         return {x: this._startPoint.x, y: this._startPoint.y};
123     },
124 
125     /**
126      * Returns the id of cc.Touch
127      * @return {Number}
128      */
129     getID:function () {
130         return this._id;
131     },
132 
133     /**
134      * Returns the id of cc.Touch
135      * @return {Number}
136      * @deprecated since v3.0, please use getID() instead
137      */
138     getId:function () {
139         cc.log("getId is deprecated. Please use getID instead.")
140         return this._id;
141     },
142 
143     /**
144      * Sets information to touch
145      * @param {Number} id
146      * @param  {Number} x
147      * @param  {Number} y
148      */
149     setTouchInfo:function (id, x, y) {
150         this._prevPoint = this._point;
151         this._point = cc.p(x || 0, y || 0);
152         this._id = id;
153         if(!this._startPointCaptured){
154             this._startPoint = cc.p(this._point);
155             this._startPointCaptured = true;
156         }
157     },
158 
159     _setPoint: function(x, y){
160         if(y === undefined){
161             this._point.x = x.x;
162             this._point.y = x.y;
163         }else{
164             this._point.x = x;
165             this._point.y = y;
166         }
167     },
168 
169     _setPrevPoint:function (x, y) {
170         if(y === undefined)
171             this._prevPoint = cc.p(x.x, x.y);
172         else
173             this._prevPoint = cc.p(x || 0, y || 0);
174     }
175 });