CCTouch.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CC_TOUCH_H__
  23. #define __CC_TOUCH_H__
  24. #include "base/CCRef.h"
  25. #include "math/CCGeometry.h"
  26. NS_CC_BEGIN
  27. /**
  28. * @addtogroup base
  29. * @{
  30. */
  31. /** @class Touch
  32. * @brief Encapsulates the Touch information, such as touch point, id and so on,
  33. and provides the methods that commonly used.
  34. */
  35. class CC_DLL Touch : public Ref
  36. {
  37. public:
  38. /**
  39. * Dispatch mode, how the touches are dispatched.
  40. * @js NA
  41. */
  42. enum class DispatchMode {
  43. ALL_AT_ONCE, /** All at once. */
  44. ONE_BY_ONE, /** One by one. */
  45. };
  46. /** Constructor.
  47. * @js ctor
  48. */
  49. Touch()
  50. : _id(0),
  51. _startPointCaptured(false),
  52. _curForce(0.f),
  53. _maxForce(0.f)
  54. {}
  55. /** Returns the current touch location in OpenGL coordinates.
  56. *
  57. * @return The current touch location in OpenGL coordinates.
  58. */
  59. Vec2 getLocation() const;
  60. /** Returns the previous touch location in OpenGL coordinates.
  61. *
  62. * @return The previous touch location in OpenGL coordinates.
  63. */
  64. Vec2 getPreviousLocation() const;
  65. /** Returns the start touch location in OpenGL coordinates.
  66. *
  67. * @return The start touch location in OpenGL coordinates.
  68. */
  69. Vec2 getStartLocation() const;
  70. /** Returns the delta of 2 current touches locations in screen coordinates.
  71. *
  72. * @return The delta of 2 current touches locations in screen coordinates.
  73. */
  74. Vec2 getDelta() const;
  75. /** Returns the current touch location in screen coordinates.
  76. *
  77. * @return The current touch location in screen coordinates.
  78. */
  79. Vec2 getLocationInView() const;
  80. /** Returns the previous touch location in screen coordinates.
  81. *
  82. * @return The previous touch location in screen coordinates.
  83. */
  84. Vec2 getPreviousLocationInView() const;
  85. /** Returns the start touch location in screen coordinates.
  86. *
  87. * @return The start touch location in screen coordinates.
  88. */
  89. Vec2 getStartLocationInView() const;
  90. /** Set the touch information. It always used to monitor touch event.
  91. *
  92. * @param id A given id
  93. * @param x A given x coordinate.
  94. * @param y A given y coordinate.
  95. */
  96. void setTouchInfo(int id, float x, float y)
  97. {
  98. _id = id;
  99. _prevPoint = _point;
  100. _point.x = x;
  101. _point.y = y;
  102. _curForce = 0.0f;
  103. _maxForce = 0.0f;
  104. if (!_startPointCaptured)
  105. {
  106. _startPoint = _point;
  107. _startPointCaptured = true;
  108. _prevPoint = _point;
  109. }
  110. }
  111. /** Set the touch information. It always used to monitor touch event.
  112. *
  113. * @param id A given id
  114. * @param x A given x coordinate.
  115. * @param y A given y coordinate.
  116. * @param force Current force for 3d touch.
  117. * @param maxForce maximum possible force for 3d touch.
  118. */
  119. void setTouchInfo(int id, float x, float y, float force, float maxForce)
  120. {
  121. _id = id;
  122. _prevPoint = _point;
  123. _point.x = x;
  124. _point.y = y;
  125. _curForce = force;
  126. _maxForce = maxForce;
  127. if (!_startPointCaptured)
  128. {
  129. _startPoint = _point;
  130. _startPointCaptured = true;
  131. _prevPoint = _point;
  132. }
  133. }
  134. /** Get touch id.
  135. * @js getId
  136. * @lua getId
  137. *
  138. * @return The id of touch.
  139. */
  140. int getID() const
  141. {
  142. return _id;
  143. }
  144. /** Returns the current touch force for 3d touch.
  145. *
  146. * @return The current touch force for 3d touch.
  147. */
  148. float getCurrentForce() const;
  149. /** Returns the maximum touch force for 3d touch.
  150. *
  151. * @return The maximum touch force for 3d touch.
  152. */
  153. float getMaxForce() const;
  154. private:
  155. int _id;
  156. bool _startPointCaptured;
  157. Vec2 _startPoint;
  158. Vec2 _point;
  159. Vec2 _prevPoint;
  160. float _curForce;
  161. float _maxForce;
  162. };
  163. // end of base group
  164. /// @}
  165. NS_CC_END
  166. #endif // __PLATFORM_TOUCH_H__