CCEventMouse.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 __cocos2d_libs__CCMouseEvent__
  22. #define __cocos2d_libs__CCMouseEvent__
  23. #include "base/CCEvent.h"
  24. #include "math/CCGeometry.h"
  25. /**
  26. * @addtogroup base
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. /** @class EventMouse
  31. * @brief The mouse event.
  32. */
  33. class CC_DLL EventMouse : public Event
  34. {
  35. public:
  36. /**
  37. * MouseEventType Different types of MouseEvent.
  38. * @js NA
  39. */
  40. enum class MouseEventType
  41. {
  42. MOUSE_NONE,
  43. MOUSE_DOWN,
  44. MOUSE_UP,
  45. MOUSE_MOVE,
  46. MOUSE_SCROLL,
  47. };
  48. enum class MouseButton
  49. {
  50. BUTTON_UNSET = -1,
  51. BUTTON_LEFT = 0,
  52. BUTTON_RIGHT = 1,
  53. BUTTON_MIDDLE = 2,
  54. BUTTON_4 = 3,
  55. BUTTON_5 = 4,
  56. BUTTON_6 = 5,
  57. BUTTON_7 = 6,
  58. BUTTON_8 = 7
  59. };
  60. /** Constructor.
  61. *
  62. * @param mouseEventCode A given mouse event type.
  63. * @js ctor
  64. */
  65. EventMouse(MouseEventType mouseEventCode);
  66. /** Set mouse scroll data.
  67. *
  68. * @param scrollX The scroll data of x axis.
  69. * @param scrollY The scroll data of y axis.
  70. */
  71. void setScrollData(float scrollX, float scrollY) { _scrollX = scrollX; _scrollY = scrollY; }
  72. /** Get mouse scroll data of x axis.
  73. *
  74. * @return The scroll data of x axis.
  75. */
  76. float getScrollX() const { return _scrollX; }
  77. /** Get mouse scroll data of y axis.
  78. *
  79. * @return The scroll data of y axis.
  80. */
  81. float getScrollY() const { return _scrollY; }
  82. /** Set the cursor position.
  83. *
  84. * @param x The x coordinate of cursor position.
  85. * @param y The y coordinate of cursor position.
  86. * @js setLocation
  87. */
  88. void setCursorPosition(float x, float y) {
  89. _x = x;
  90. _y = y;
  91. _prevPoint = _point;
  92. _point.x = x;
  93. _point.y = y;
  94. if (!_startPointCaptured)
  95. {
  96. _startPoint = _point;
  97. _startPointCaptured = true;
  98. }
  99. }
  100. /** Set mouse button.
  101. *
  102. * @param button a given mouse button.
  103. * @js setButton
  104. */
  105. void setMouseButton(MouseButton button) { _mouseButton = button; }
  106. /** Get mouse button.
  107. *
  108. * @return The mouse button.
  109. * @js getButton
  110. */
  111. MouseButton getMouseButton() const { return _mouseButton; }
  112. /** Get the cursor position of x axis.
  113. *
  114. * @return The x coordinate of cursor position.
  115. * @js getLocationX
  116. */
  117. float getCursorX() const { return _x; }
  118. /** Get the cursor position of y axis.
  119. *
  120. * @return The y coordinate of cursor position.
  121. * @js getLocationY
  122. */
  123. float getCursorY() const { return _y; }
  124. /** Returns the current touch location in OpenGL coordinates.
  125. *
  126. * @return The current touch location in OpenGL coordinates.
  127. */
  128. Vec2 getLocation() const;
  129. /** Returns the previous touch location in OpenGL coordinates.
  130. *
  131. * @return The previous touch location in OpenGL coordinates.
  132. * @js NA
  133. */
  134. Vec2 getPreviousLocation() const;
  135. /** Returns the start touch location in OpenGL coordinates.
  136. *
  137. * @return The start touch location in OpenGL coordinates.
  138. * @js NA
  139. */
  140. Vec2 getStartLocation() const;
  141. /** Returns the delta of 2 current touches locations in screen coordinates.
  142. *
  143. * @return The delta of 2 current touches locations in screen coordinates.
  144. */
  145. Vec2 getDelta() const;
  146. /** Returns the current touch location in screen coordinates.
  147. *
  148. * @return The current touch location in screen coordinates.
  149. */
  150. Vec2 getLocationInView() const;
  151. /** Returns the previous touch location in screen coordinates.
  152. *
  153. * @return The previous touch location in screen coordinates.
  154. * @js NA
  155. */
  156. Vec2 getPreviousLocationInView() const;
  157. /** Returns the start touch location in screen coordinates.
  158. *
  159. * @return The start touch location in screen coordinates.
  160. * @js NA
  161. */
  162. Vec2 getStartLocationInView() const;
  163. private:
  164. MouseEventType _mouseEventType;
  165. MouseButton _mouseButton;
  166. float _x;
  167. float _y;
  168. float _scrollX;
  169. float _scrollY;
  170. bool _startPointCaptured;
  171. Vec2 _startPoint;
  172. Vec2 _point;
  173. Vec2 _prevPoint;
  174. friend class EventListenerMouse;
  175. };
  176. NS_CC_END
  177. // end of base group
  178. /// @}
  179. #endif /* defined(__cocos2d_libs__CCMouseEvent__) */