1
0

CCTouch.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 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. #ifndef __CC_TOUCH_H__
  22. #define __CC_TOUCH_H__
  23. #include "base/CCRef.h"
  24. #include "math/CCGeometry.h"
  25. NS_CC_BEGIN
  26. /**
  27. * @addtogroup base
  28. * @{
  29. */
  30. /** @class Touch
  31. * @brief Encapsulates the Touch information, such as touch point, id and so on,
  32. and provides the methods that commonly used.
  33. */
  34. class CC_DLL Touch : public Ref
  35. {
  36. public:
  37. /**
  38. * Dispatch mode, how the touches are dispatched.
  39. * @js NA
  40. */
  41. enum class DispatchMode {
  42. ALL_AT_ONCE, /** All at once. */
  43. ONE_BY_ONE, /** One by one. */
  44. };
  45. /** Constructor.
  46. * @js ctor
  47. */
  48. Touch()
  49. : _id(0),
  50. _startPointCaptured(false),
  51. _curForce(0.f),
  52. _maxForce(0.f)
  53. {}
  54. /** Returns the current touch location in OpenGL coordinates.
  55. *
  56. * @return The current touch location in OpenGL coordinates.
  57. */
  58. Vec2 getLocation() const;
  59. /** Returns the previous touch location in OpenGL coordinates.
  60. *
  61. * @return The previous touch location in OpenGL coordinates.
  62. */
  63. Vec2 getPreviousLocation() const;
  64. /** Returns the start touch location in OpenGL coordinates.
  65. *
  66. * @return The start touch location in OpenGL coordinates.
  67. */
  68. Vec2 getStartLocation() const;
  69. /** Returns the delta of 2 current touches locations in screen coordinates.
  70. *
  71. * @return The delta of 2 current touches locations in screen coordinates.
  72. */
  73. Vec2 getDelta() const;
  74. /** Returns the current touch location in screen coordinates.
  75. *
  76. * @return The current touch location in screen coordinates.
  77. */
  78. Vec2 getLocationInView() const;
  79. /** Returns the previous touch location in screen coordinates.
  80. *
  81. * @return The previous touch location in screen coordinates.
  82. */
  83. Vec2 getPreviousLocationInView() const;
  84. /** Returns the start touch location in screen coordinates.
  85. *
  86. * @return The start touch location in screen coordinates.
  87. */
  88. Vec2 getStartLocationInView() const;
  89. /** Set the touch information. It always used to monitor touch event.
  90. *
  91. * @param id A given id
  92. * @param x A given x coordinate.
  93. * @param y A given y coordinate.
  94. */
  95. void setTouchInfo(int id, float x, float y)
  96. {
  97. _id = id;
  98. _prevPoint = _point;
  99. _point.x = x;
  100. _point.y = y;
  101. _curForce = 0.0f;
  102. _maxForce = 0.0f;
  103. if (!_startPointCaptured)
  104. {
  105. _startPoint = _point;
  106. _startPointCaptured = true;
  107. _prevPoint = _point;
  108. }
  109. }
  110. /** Set the touch information. It always used to monitor touch event.
  111. *
  112. * @param id A given id
  113. * @param x A given x coordinate.
  114. * @param y A given y coordinate.
  115. * @param force Current force for 3d touch.
  116. * @param maxForce maximum possible force for 3d touch.
  117. */
  118. void setTouchInfo(int id, float x, float y, float force, float maxForce)
  119. {
  120. _id = id;
  121. _prevPoint = _point;
  122. _point.x = x;
  123. _point.y = y;
  124. _curForce = force;
  125. _maxForce = maxForce;
  126. if (!_startPointCaptured)
  127. {
  128. _startPoint = _point;
  129. _startPointCaptured = true;
  130. _prevPoint = _point;
  131. }
  132. }
  133. /** Get touch id.
  134. * @js getId
  135. * @lua getId
  136. *
  137. * @return The id of touch.
  138. */
  139. int getID() const
  140. {
  141. return _id;
  142. }
  143. /** Returns the current touch force for 3d touch.
  144. *
  145. * @return The current touch force for 3d touch.
  146. */
  147. float getCurrentForce() const;
  148. /** Returns the maximum touch force for 3d touch.
  149. *
  150. * @return The maximum touch force for 3d touch.
  151. */
  152. float getMaxForce() const;
  153. private:
  154. int _id;
  155. bool _startPointCaptured;
  156. Vec2 _startPoint;
  157. Vec2 _point;
  158. Vec2 _prevPoint;
  159. float _curForce;
  160. float _maxForce;
  161. };
  162. // end of base group
  163. /// @}
  164. NS_CC_END
  165. #endif // __PLATFORM_TOUCH_H__