CCTouch.js 4.8 KB

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