The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

440 lines
18KB

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