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.

132 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. MouseEvent::MouseEvent (MouseInputSource inputSource,
  16. Point<float> pos,
  17. ModifierKeys modKeys,
  18. float force,
  19. float o, float r,
  20. float tX, float tY,
  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. orientation (o), rotation (r),
  34. tiltX (tX), tiltY (tY),
  35. mouseDownPosition (downPos),
  36. eventComponent (eventComp),
  37. originalComponent (originator),
  38. eventTime (time),
  39. mouseDownTime (downTime),
  40. source (inputSource),
  41. numberOfClicks ((uint8) numClicks),
  42. wasMovedSinceMouseDown ((uint8) (mouseWasDragged ? 1 : 0))
  43. {
  44. }
  45. //==============================================================================
  46. MouseEvent MouseEvent::getEventRelativeTo (Component* const otherComponent) const noexcept
  47. {
  48. jassert (otherComponent != nullptr);
  49. return MouseEvent (source, otherComponent->getLocalPoint (eventComponent, position),
  50. mods, pressure, orientation, rotation, tiltX, tiltY,
  51. otherComponent, originalComponent, eventTime,
  52. otherComponent->getLocalPoint (eventComponent, mouseDownPosition),
  53. mouseDownTime, numberOfClicks, wasMovedSinceMouseDown != 0);
  54. }
  55. MouseEvent MouseEvent::withNewPosition (Point<float> newPosition) const noexcept
  56. {
  57. return MouseEvent (source, newPosition, mods, pressure, orientation, rotation, tiltX, tiltY,
  58. eventComponent, originalComponent, eventTime, mouseDownPosition, mouseDownTime,
  59. numberOfClicks, wasMovedSinceMouseDown != 0);
  60. }
  61. MouseEvent MouseEvent::withNewPosition (Point<int> newPosition) const noexcept
  62. {
  63. return MouseEvent (source, newPosition.toFloat(), mods, pressure, orientation, rotation,
  64. tiltX, tiltY, eventComponent, originalComponent, eventTime, mouseDownPosition,
  65. mouseDownTime, 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 mouseDownPosition.roundToInt(); }
  86. Point<int> MouseEvent::getMouseDownScreenPosition() const { return eventComponent->localPointToGlobal (mouseDownPosition).roundToInt(); }
  87. Point<int> MouseEvent::getOffsetFromDragStart() const noexcept { return (position - mouseDownPosition).roundToInt(); }
  88. int MouseEvent::getDistanceFromDragStart() const noexcept { return roundToInt (mouseDownPosition.getDistanceFrom (position)); }
  89. int MouseEvent::getMouseDownX() const noexcept { return roundToInt (mouseDownPosition.x); }
  90. int MouseEvent::getMouseDownY() const noexcept { return roundToInt (mouseDownPosition.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. bool MouseEvent::isOrientationValid() const noexcept { return orientation >= 0.0f && orientation <= MathConstants<float>::twoPi; }
  99. bool MouseEvent::isRotationValid() const noexcept { return rotation >= 0 && rotation <= MathConstants<float>::twoPi; }
  100. bool MouseEvent::isTiltValid (bool isX) const noexcept { return isX ? (tiltX >= -1.0f && tiltX <= 1.0f) : (tiltY >= -1.0f && tiltY <= 1.0f); }
  101. //==============================================================================
  102. static int doubleClickTimeOutMs = 400;
  103. int MouseEvent::getDoubleClickTimeout() noexcept { return doubleClickTimeOutMs; }
  104. void MouseEvent::setDoubleClickTimeout (const int newTime) noexcept { doubleClickTimeOutMs = newTime; }
  105. } // namespace juce