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.

139 lines
6.3KB

  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. MouseEvent::MouseEvent (MouseInputSource inputSource,
  21. Point<float> pos,
  22. ModifierKeys modKeys,
  23. float force,
  24. float o, float r,
  25. float tX, float tY,
  26. Component* const eventComp,
  27. Component* const originator,
  28. Time time,
  29. Point<float> downPos,
  30. Time downTime,
  31. const int numClicks,
  32. const bool mouseWasDragged) noexcept
  33. : position (pos),
  34. x (roundToInt (pos.x)),
  35. y (roundToInt (pos.y)),
  36. mods (modKeys),
  37. pressure (force),
  38. orientation (o), rotation (r),
  39. tiltX (tX), tiltY (tY),
  40. mouseDownPosition (downPos),
  41. eventComponent (eventComp),
  42. originalComponent (originator),
  43. eventTime (time),
  44. mouseDownTime (downTime),
  45. source (inputSource),
  46. numberOfClicks ((uint8) numClicks),
  47. wasMovedSinceMouseDown ((uint8) (mouseWasDragged ? 1 : 0))
  48. {
  49. }
  50. //==============================================================================
  51. MouseEvent MouseEvent::getEventRelativeTo (Component* const otherComponent) const noexcept
  52. {
  53. jassert (otherComponent != nullptr);
  54. return MouseEvent (source, otherComponent->getLocalPoint (eventComponent, position),
  55. mods, pressure, orientation, rotation, tiltX, tiltY,
  56. otherComponent, originalComponent, eventTime,
  57. otherComponent->getLocalPoint (eventComponent, mouseDownPosition),
  58. mouseDownTime, numberOfClicks, wasMovedSinceMouseDown != 0);
  59. }
  60. MouseEvent MouseEvent::withNewPosition (Point<float> newPosition) const noexcept
  61. {
  62. return MouseEvent (source, newPosition, mods, pressure, orientation, rotation, tiltX, tiltY,
  63. eventComponent, originalComponent, eventTime, mouseDownPosition, mouseDownTime,
  64. numberOfClicks, wasMovedSinceMouseDown != 0);
  65. }
  66. MouseEvent MouseEvent::withNewPosition (Point<int> newPosition) const noexcept
  67. {
  68. return MouseEvent (source, newPosition.toFloat(), mods, pressure, orientation, rotation,
  69. tiltX, tiltY, eventComponent, originalComponent, eventTime, mouseDownPosition,
  70. mouseDownTime, numberOfClicks, wasMovedSinceMouseDown != 0);
  71. }
  72. //==============================================================================
  73. bool MouseEvent::mouseWasDraggedSinceMouseDown() const noexcept
  74. {
  75. return wasMovedSinceMouseDown != 0;
  76. }
  77. bool MouseEvent::mouseWasClicked() const noexcept
  78. {
  79. return ! mouseWasDraggedSinceMouseDown();
  80. }
  81. int MouseEvent::getLengthOfMousePress() const noexcept
  82. {
  83. if (mouseDownTime.toMilliseconds() > 0)
  84. return jmax (0, (int) (eventTime - mouseDownTime).inMilliseconds());
  85. return 0;
  86. }
  87. //==============================================================================
  88. Point<int> MouseEvent::getPosition() const noexcept { return Point<int> (x, y); }
  89. Point<int> MouseEvent::getScreenPosition() const { return eventComponent->localPointToGlobal (getPosition()); }
  90. Point<int> MouseEvent::getMouseDownPosition() const noexcept { return mouseDownPosition.roundToInt(); }
  91. Point<int> MouseEvent::getMouseDownScreenPosition() const { return eventComponent->localPointToGlobal (mouseDownPosition).roundToInt(); }
  92. Point<int> MouseEvent::getOffsetFromDragStart() const noexcept { return (position - mouseDownPosition).roundToInt(); }
  93. int MouseEvent::getDistanceFromDragStart() const noexcept { return roundToInt (mouseDownPosition.getDistanceFrom (position)); }
  94. int MouseEvent::getMouseDownX() const noexcept { return roundToInt (mouseDownPosition.x); }
  95. int MouseEvent::getMouseDownY() const noexcept { return roundToInt (mouseDownPosition.y); }
  96. int MouseEvent::getDistanceFromDragStartX() const noexcept { return getOffsetFromDragStart().x; }
  97. int MouseEvent::getDistanceFromDragStartY() const noexcept { return getOffsetFromDragStart().y; }
  98. int MouseEvent::getScreenX() const { return getScreenPosition().x; }
  99. int MouseEvent::getScreenY() const { return getScreenPosition().y; }
  100. int MouseEvent::getMouseDownScreenX() const { return getMouseDownScreenPosition().x; }
  101. int MouseEvent::getMouseDownScreenY() const { return getMouseDownScreenPosition().y; }
  102. bool MouseEvent::isPressureValid() const noexcept { return pressure > 0.0f && pressure < 1.0f; }
  103. bool MouseEvent::isOrientationValid() const noexcept { return orientation >= 0.0f && orientation <= MathConstants<float>::twoPi; }
  104. bool MouseEvent::isRotationValid() const noexcept { return rotation >= 0 && rotation <= MathConstants<float>::twoPi; }
  105. bool MouseEvent::isTiltValid (bool isX) const noexcept { return isX ? (tiltX >= -1.0f && tiltX <= 1.0f) : (tiltY >= -1.0f && tiltY <= 1.0f); }
  106. //==============================================================================
  107. static int doubleClickTimeOutMs = 400;
  108. int MouseEvent::getDoubleClickTimeout() noexcept { return doubleClickTimeOutMs; }
  109. void MouseEvent::setDoubleClickTimeout (const int newTime) noexcept { doubleClickTimeOutMs = newTime; }
  110. } // namespace juce