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.

144 lines
6.3KB

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