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.

129 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. MouseEvent::MouseEvent (MouseInputSource inputSource,
  18. Point<float> pos,
  19. ModifierKeys modKeys,
  20. float force,
  21. Component* const eventComp,
  22. Component* const originator,
  23. Time time,
  24. Point<float> downPos,
  25. Time downTime,
  26. const int numClicks,
  27. const bool mouseWasDragged) noexcept
  28. : position (pos),
  29. x (roundToInt (pos.x)),
  30. y (roundToInt (pos.y)),
  31. mods (modKeys),
  32. pressure (force),
  33. eventComponent (eventComp),
  34. originalComponent (originator),
  35. eventTime (time),
  36. mouseDownTime (downTime),
  37. source (inputSource),
  38. mouseDownPos (downPos),
  39. numberOfClicks ((uint8) numClicks),
  40. wasMovedSinceMouseDown ((uint8) (mouseWasDragged ? 1 : 0))
  41. {
  42. }
  43. MouseEvent::~MouseEvent() noexcept
  44. {
  45. }
  46. //==============================================================================
  47. MouseEvent MouseEvent::getEventRelativeTo (Component* const otherComponent) const noexcept
  48. {
  49. jassert (otherComponent != nullptr);
  50. return MouseEvent (source, otherComponent->getLocalPoint (eventComponent, position),
  51. mods, pressure, otherComponent, originalComponent, eventTime,
  52. otherComponent->getLocalPoint (eventComponent, mouseDownPos),
  53. mouseDownTime, numberOfClicks, wasMovedSinceMouseDown != 0);
  54. }
  55. MouseEvent MouseEvent::withNewPosition (Point<float> newPosition) const noexcept
  56. {
  57. return MouseEvent (source, newPosition, mods, pressure, eventComponent,
  58. originalComponent, eventTime, mouseDownPos, mouseDownTime,
  59. numberOfClicks, wasMovedSinceMouseDown != 0);
  60. }
  61. MouseEvent MouseEvent::withNewPosition (Point<int> newPosition) const noexcept
  62. {
  63. return MouseEvent (source, newPosition.toFloat(), mods, pressure, eventComponent,
  64. originalComponent, eventTime, mouseDownPos, mouseDownTime,
  65. numberOfClicks, wasMovedSinceMouseDown != 0);
  66. }
  67. //==============================================================================
  68. bool MouseEvent::mouseWasDraggedSinceMouseDown() const noexcept
  69. {
  70. return wasMovedSinceMouseDown != 0;
  71. }
  72. bool MouseEvent::mouseWasClicked() const noexcept
  73. {
  74. return ! mouseWasDraggedSinceMouseDown();
  75. }
  76. int MouseEvent::getLengthOfMousePress() const noexcept
  77. {
  78. if (mouseDownTime.toMilliseconds() > 0)
  79. return jmax (0, (int) (eventTime - mouseDownTime).inMilliseconds());
  80. return 0;
  81. }
  82. //==============================================================================
  83. Point<int> MouseEvent::getPosition() const noexcept { return Point<int> (x, y); }
  84. Point<int> MouseEvent::getScreenPosition() const { return eventComponent->localPointToGlobal (getPosition()); }
  85. Point<int> MouseEvent::getMouseDownPosition() const noexcept { return mouseDownPos.roundToInt(); }
  86. Point<int> MouseEvent::getMouseDownScreenPosition() const { return eventComponent->localPointToGlobal (mouseDownPos).roundToInt(); }
  87. Point<int> MouseEvent::getOffsetFromDragStart() const noexcept { return (position - mouseDownPos).roundToInt(); }
  88. int MouseEvent::getDistanceFromDragStart() const noexcept { return roundToInt (mouseDownPos.getDistanceFrom (position)); }
  89. int MouseEvent::getMouseDownX() const noexcept { return roundToInt (mouseDownPos.x); }
  90. int MouseEvent::getMouseDownY() const noexcept { return roundToInt (mouseDownPos.y); }
  91. int MouseEvent::getDistanceFromDragStartX() const noexcept { return getOffsetFromDragStart().x; }
  92. int MouseEvent::getDistanceFromDragStartY() const noexcept { return getOffsetFromDragStart().y; }
  93. int MouseEvent::getScreenX() const { return getScreenPosition().x; }
  94. int MouseEvent::getScreenY() const { return getScreenPosition().y; }
  95. int MouseEvent::getMouseDownScreenX() const { return getMouseDownScreenPosition().x; }
  96. int MouseEvent::getMouseDownScreenY() const { return getMouseDownScreenPosition().y; }
  97. bool MouseEvent::isPressureValid() const noexcept { return pressure > 0.0f && pressure < 1.0f; }
  98. //==============================================================================
  99. static int doubleClickTimeOutMs = 400;
  100. int MouseEvent::getDoubleClickTimeout() noexcept { return doubleClickTimeOutMs; }
  101. void MouseEvent::setDoubleClickTimeout (const int newTime) noexcept { doubleClickTimeOutMs = newTime; }