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 19KB

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