Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_MouseEvent.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. Contains position and status information about a mouse event.
  24. @see MouseListener, Component::mouseMove, Component::mouseEnter, Component::mouseExit,
  25. Component::mouseDown, Component::mouseUp, Component::mouseDrag
  26. */
  27. class JUCE_API MouseEvent
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates a MouseEvent.
  32. Normally an application will never need to use this.
  33. @param source the source that's invoking the event
  34. @param position the position of the mouse, relative to the component that is passed-in
  35. @param modifiers the key modifiers at the time of the event
  36. @param pressure the pressure of the touch or stylus, in the range 0 to 1. Devices that
  37. do not support force information may return 0.0, 1.0, or a negative value,
  38. depending on the platform
  39. @param orientation the orientation of the touch input for this event in radians. The default is 0
  40. @param rotation the rotation of the pen device for this event in radians. The default is 0
  41. @param tiltX the tilt of the pen device along the x-axis between -1.0 and 1.0. The default is 0
  42. @param tiltY the tilt of the pen device along the y-axis between -1.0 and 1.0. The default is 0
  43. @param eventComponent the component that the mouse event applies to
  44. @param originator the component that originally received the event
  45. @param eventTime the time the event happened
  46. @param mouseDownPos the position of the corresponding mouse-down event (relative to the component that is passed-in).
  47. If there isn't a corresponding mouse-down (e.g. for a mouse-move), this will just be
  48. the same as the current mouse-x position.
  49. @param mouseDownTime the time at which the corresponding mouse-down event happened
  50. If there isn't a corresponding mouse-down (e.g. for a mouse-move), this will just be
  51. the same as the current mouse-event time.
  52. @param numberOfClicks how many clicks, e.g. a double-click event will be 2, a triple-click will be 3, etc
  53. @param mouseWasDragged whether the mouse has been dragged significantly since the previous mouse-down
  54. */
  55. MouseEvent (MouseInputSource source,
  56. Point<float> position,
  57. ModifierKeys modifiers,
  58. float pressure,
  59. float orientation, float rotation,
  60. float tiltX, float tiltY,
  61. Component* eventComponent,
  62. Component* originator,
  63. Time eventTime,
  64. Point<float> mouseDownPos,
  65. Time mouseDownTime,
  66. int numberOfClicks,
  67. bool mouseWasDragged) noexcept;
  68. /** Destructor. */
  69. ~MouseEvent() noexcept;
  70. //==============================================================================
  71. /** The position of the mouse when the event occurred.
  72. This value is relative to the top-left of the component to which the
  73. event applies (as indicated by the MouseEvent::eventComponent field).
  74. This is a more accurate floating-point version of the position returned by
  75. getPosition() and the integer x and y member variables.
  76. */
  77. const Point<float> position;
  78. /** The x-position of the mouse when the event occurred.
  79. This value is relative to the top-left of the component to which the
  80. event applies (as indicated by the MouseEvent::eventComponent field).
  81. For a floating-point coordinate, see MouseEvent::position
  82. */
  83. const int x;
  84. /** The y-position of the mouse when the event occurred.
  85. This value is relative to the top-left of the component to which the
  86. event applies (as indicated by the MouseEvent::eventComponent field).
  87. For a floating-point coordinate, see MouseEvent::position
  88. */
  89. const int y;
  90. /** The key modifiers associated with the event.
  91. This will let you find out which mouse buttons were down, as well as which
  92. modifier keys were held down.
  93. When used for mouse-up events, this will indicate the state of the mouse buttons
  94. just before they were released, so that you can tell which button they let go of.
  95. */
  96. const ModifierKeys mods;
  97. /** The pressure of the touch or stylus for this event.
  98. The range is 0 (soft) to 1 (hard).
  99. If the input device doesn't provide any pressure data, it may return a negative
  100. value here, or 0.0 or 1.0, depending on the platform.
  101. */
  102. const float pressure;
  103. /** The orientation of the touch input for this event in radians where 0 indicates a touch aligned with the x-axis
  104. and pointing from left to right; increasing values indicate rotation in the clockwise direction. The default is 0.
  105. */
  106. const float orientation;
  107. /** The rotation of the pen device for this event in radians. Indicates the clockwise
  108. rotation, or twist, of the pen. The default is 0.
  109. */
  110. const float rotation;
  111. /** The tilt of the pen device along the x-axis between -1.0 and 1.0. A positive value indicates
  112. a tilt to the right. The default is 0.
  113. */
  114. const float tiltX;
  115. /** The tilt of the pen device along the y-axis between -1.0 and 1.0. A positive value indicates
  116. a tilt toward the user. The default is 0.
  117. */
  118. const float tiltY;
  119. /** The component that this event applies to.
  120. This is usually the component that the mouse was over at the time, but for mouse-drag
  121. events the mouse could actually be over a different component and the events are
  122. still sent to the component that the button was originally pressed on.
  123. The x and y member variables are relative to this component's position.
  124. If you use getEventRelativeTo() to retarget this object to be relative to a different
  125. component, this pointer will be updated, but originalComponent remains unchanged.
  126. @see originalComponent
  127. */
  128. Component* const eventComponent;
  129. /** The component that the event first occurred on.
  130. If you use getEventRelativeTo() to retarget this object to be relative to a different
  131. component, this value remains unchanged to indicate the first component that received it.
  132. @see eventComponent
  133. */
  134. Component* const originalComponent;
  135. /** The time that this mouse-event occurred. */
  136. const Time eventTime;
  137. /** The time that the corresponding mouse-down event occurred. */
  138. const Time mouseDownTime;
  139. /** The source device that generated this event. */
  140. MouseInputSource source;
  141. //==============================================================================
  142. /** Returns the x coordinate of the last place that a mouse was pressed.
  143. The coordinate is relative to the component specified in MouseEvent::component.
  144. @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasDraggedSinceMouseDown
  145. */
  146. int getMouseDownX() const noexcept;
  147. /** Returns the y coordinate of the last place that a mouse was pressed.
  148. The coordinate is relative to the component specified in MouseEvent::component.
  149. @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasDraggedSinceMouseDown
  150. */
  151. int getMouseDownY() const noexcept;
  152. /** Returns the coordinates of the last place that a mouse was pressed.
  153. The coordinates are relative to the component specified in MouseEvent::component.
  154. @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasDraggedSinceMouseDown
  155. */
  156. Point<int> getMouseDownPosition() const noexcept;
  157. /** Returns the straight-line distance between where the mouse is now and where it
  158. was the last time the button was pressed.
  159. This is quite handy for things like deciding whether the user has moved far enough
  160. for it to be considered a drag operation.
  161. @see getDistanceFromDragStartX
  162. */
  163. int getDistanceFromDragStart() const noexcept;
  164. /** Returns the difference between the mouse's current x position and where it was
  165. when the button was last pressed.
  166. @see getDistanceFromDragStart
  167. */
  168. int getDistanceFromDragStartX() const noexcept;
  169. /** Returns the difference between the mouse's current y position and where it was
  170. when the button was last pressed.
  171. @see getDistanceFromDragStart
  172. */
  173. int getDistanceFromDragStartY() const noexcept;
  174. /** Returns the difference between the mouse's current position and where it was
  175. when the button was last pressed.
  176. @see getDistanceFromDragStart
  177. */
  178. Point<int> getOffsetFromDragStart() const noexcept;
  179. /** Returns true if the user seems to be performing a drag gesture.
  180. This is only meaningful if called in either a mouseUp() or mouseDrag() method.
  181. It will return true if the user has dragged the mouse more than a few pixels
  182. from the place where the mouse-down occurred.
  183. Once they have dragged it far enough for this method to return true, it will continue
  184. to return true until the mouse-up, even if they move the mouse back to the same
  185. location at which the mouse-down happened. This means that it's very handy for
  186. objects that can either be clicked on or dragged, as you can use it in the mouseDrag()
  187. callback to ignore small movements they might make while trying to click.
  188. */
  189. bool mouseWasDraggedSinceMouseDown() const noexcept;
  190. /** Returns true if the mouse event is part of a click gesture rather than a drag.
  191. This is effectively the opposite of mouseWasDraggedSinceMouseDown()
  192. */
  193. bool mouseWasClicked() const noexcept;
  194. /** For a click event, the number of times the mouse was clicked in succession.
  195. So for example a double-click event will return 2, a triple-click 3, etc.
  196. */
  197. int getNumberOfClicks() const noexcept { return numberOfClicks; }
  198. /** Returns the time that the mouse button has been held down for.
  199. If called from a mouseDrag or mouseUp callback, this will return the
  200. number of milliseconds since the corresponding mouseDown event occurred.
  201. If called in other contexts, e.g. a mouseMove, then the returned value
  202. may be 0 or an undefined value.
  203. */
  204. int getLengthOfMousePress() const noexcept;
  205. /** Returns true if the pressure value for this event is meaningful. */
  206. bool isPressureValid() const noexcept;
  207. /** Returns true if the orientation value for this event is meaningful. */
  208. bool isOrientationValid() const noexcept;
  209. /** Returns true if the rotation value for this event is meaningful. */
  210. bool isRotationValid() const noexcept;
  211. /** Returns true if the current tilt value (either x- or y-axis) is meaningful. */
  212. bool isTiltValid (bool tiltX) const noexcept;
  213. //==============================================================================
  214. /** The position of the mouse when the event occurred.
  215. This position is relative to the top-left of the component to which the
  216. event applies (as indicated by the MouseEvent::eventComponent field).
  217. For a floating-point position, see MouseEvent::position
  218. */
  219. Point<int> getPosition() const noexcept;
  220. /** Returns the mouse x position of this event, in global screen coordinates.
  221. The coordinates are relative to the top-left of the main monitor.
  222. @see getScreenPosition
  223. */
  224. int getScreenX() const;
  225. /** Returns the mouse y position of this event, in global screen coordinates.
  226. The coordinates are relative to the top-left of the main monitor.
  227. @see getScreenPosition
  228. */
  229. int getScreenY() const;
  230. /** Returns the mouse position of this event, in global screen coordinates.
  231. The coordinates are relative to the top-left of the main monitor.
  232. @see getMouseDownScreenPosition
  233. */
  234. Point<int> getScreenPosition() const;
  235. /** Returns the x coordinate at which the mouse button was last pressed.
  236. The coordinates are relative to the top-left of the main monitor.
  237. @see getMouseDownScreenPosition
  238. */
  239. int getMouseDownScreenX() const;
  240. /** Returns the y coordinate at which the mouse button was last pressed.
  241. The coordinates are relative to the top-left of the main monitor.
  242. @see getMouseDownScreenPosition
  243. */
  244. int getMouseDownScreenY() const;
  245. /** Returns the coordinates at which the mouse button was last pressed.
  246. The coordinates are relative to the top-left of the main monitor.
  247. @see getScreenPosition
  248. */
  249. Point<int> getMouseDownScreenPosition() const;
  250. //==============================================================================
  251. /** Creates a version of this event that is relative to a different component.
  252. The x and y positions of the event that is returned will have been
  253. adjusted to be relative to the new component.
  254. The component pointer that is passed-in must not be null.
  255. */
  256. MouseEvent getEventRelativeTo (Component* newComponent) const noexcept;
  257. /** Creates a copy of this event with a different position.
  258. All other members of the event object are the same, but the x and y are
  259. replaced with these new values.
  260. */
  261. MouseEvent withNewPosition (Point<float> newPosition) const noexcept;
  262. /** Creates a copy of this event with a different position.
  263. All other members of the event object are the same, but the x and y are
  264. replaced with these new values.
  265. */
  266. MouseEvent withNewPosition (Point<int> newPosition) const noexcept;
  267. //==============================================================================
  268. /** Changes the application-wide setting for the double-click time limit.
  269. This is the maximum length of time between mouse-clicks for it to be
  270. considered a double-click. It's used by the Component class.
  271. @see getDoubleClickTimeout, MouseListener::mouseDoubleClick
  272. */
  273. static void setDoubleClickTimeout (int timeOutMilliseconds) noexcept;
  274. /** Returns the application-wide setting for the double-click time limit.
  275. This is the maximum length of time between mouse-clicks for it to be
  276. considered a double-click. It's used by the Component class.
  277. @see setDoubleClickTimeout, MouseListener::mouseDoubleClick
  278. */
  279. static int getDoubleClickTimeout() noexcept;
  280. private:
  281. //==============================================================================
  282. const Point<float> mouseDownPos;
  283. const uint8 numberOfClicks, wasMovedSinceMouseDown;
  284. MouseEvent& operator= (const MouseEvent&);
  285. };
  286. //==============================================================================
  287. /**
  288. Contains status information about a mouse wheel event.
  289. @see MouseListener, MouseEvent
  290. */
  291. struct MouseWheelDetails
  292. {
  293. //==============================================================================
  294. /** The amount that the wheel has been moved in the X axis.
  295. If isReversed is true, then a negative deltaX means that the wheel has been
  296. pushed physically to the left.
  297. If isReversed is false, then a negative deltaX means that the wheel has been
  298. pushed physically to the right.
  299. */
  300. float deltaX;
  301. /** The amount that the wheel has been moved in the Y axis.
  302. If isReversed is true, then a negative deltaY means that the wheel has been
  303. pushed physically upwards.
  304. If isReversed is false, then a negative deltaY means that the wheel has been
  305. pushed physically downwards.
  306. */
  307. float deltaY;
  308. /** Indicates whether the user has reversed the direction of the wheel.
  309. See deltaX and deltaY for an explanation of the effects of this value.
  310. */
  311. bool isReversed;
  312. /** If true, then the wheel has continuous, un-stepped motion. */
  313. bool isSmooth;
  314. /** If true, then this event is part of the intertial momentum phase that follows
  315. the wheel being released. */
  316. bool isInertial;
  317. };
  318. //==============================================================================
  319. /**
  320. Contains status information about a pen event.
  321. @see MouseListener, MouseEvent
  322. */
  323. struct PenDetails
  324. {
  325. /**
  326. The rotation of the pen device in radians. Indicates the clockwise rotation, or twist,
  327. of the pen. The default is 0.
  328. */
  329. float rotation;
  330. /**
  331. Indicates the angle of tilt of the pointer in a range of -1.0 to 1.0 along the x-axis where
  332. a positive value indicates a tilt to the right. The default is 0.
  333. */
  334. float tiltX;
  335. /**
  336. Indicates the angle of tilt of the pointer in a range of -1.0 to 1.0 along the y-axis where
  337. a positive value indicates a tilt toward the user. The default is 0.
  338. */
  339. float tiltY;
  340. };
  341. } // namespace juce