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.

juce_MouseEvent.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. MouseEvent::~MouseEvent() noexcept
  51. {
  52. }
  53. //==============================================================================
  54. MouseEvent MouseEvent::getEventRelativeTo (Component* const otherComponent) const noexcept
  55. {
  56. jassert (otherComponent != nullptr);
  57. return MouseEvent (source, otherComponent->getLocalPoint (eventComponent, position),
  58. mods, pressure, orientation, rotation, tiltX, tiltY,
  59. otherComponent, originalComponent, eventTime,
  60. otherComponent->getLocalPoint (eventComponent, mouseDownPosition),
  61. mouseDownTime, numberOfClicks, wasMovedSinceMouseDown != 0);
  62. }
  63. MouseEvent MouseEvent::withNewPosition (Point<float> newPosition) const noexcept
  64. {
  65. return MouseEvent (source, newPosition, mods, pressure, orientation, rotation, tiltX, tiltY,
  66. eventComponent, originalComponent, eventTime, mouseDownPosition, mouseDownTime,
  67. numberOfClicks, wasMovedSinceMouseDown != 0);
  68. }
  69. MouseEvent MouseEvent::withNewPosition (Point<int> newPosition) const noexcept
  70. {
  71. return MouseEvent (source, newPosition.toFloat(), mods, pressure, orientation, rotation,
  72. tiltX, tiltY, eventComponent, originalComponent, eventTime, mouseDownPosition,
  73. mouseDownTime, numberOfClicks, wasMovedSinceMouseDown != 0);
  74. }
  75. //==============================================================================
  76. bool MouseEvent::mouseWasDraggedSinceMouseDown() const noexcept
  77. {
  78. return wasMovedSinceMouseDown != 0;
  79. }
  80. bool MouseEvent::mouseWasClicked() const noexcept
  81. {
  82. return ! mouseWasDraggedSinceMouseDown();
  83. }
  84. int MouseEvent::getLengthOfMousePress() const noexcept
  85. {
  86. if (mouseDownTime.toMilliseconds() > 0)
  87. return jmax (0, (int) (eventTime - mouseDownTime).inMilliseconds());
  88. return 0;
  89. }
  90. //==============================================================================
  91. Point<int> MouseEvent::getPosition() const noexcept { return Point<int> (x, y); }
  92. Point<int> MouseEvent::getScreenPosition() const { return eventComponent->localPointToGlobal (getPosition()); }
  93. Point<int> MouseEvent::getMouseDownPosition() const noexcept { return mouseDownPosition.roundToInt(); }
  94. Point<int> MouseEvent::getMouseDownScreenPosition() const { return eventComponent->localPointToGlobal (mouseDownPosition).roundToInt(); }
  95. Point<int> MouseEvent::getOffsetFromDragStart() const noexcept { return (position - mouseDownPosition).roundToInt(); }
  96. int MouseEvent::getDistanceFromDragStart() const noexcept { return roundToInt (mouseDownPosition.getDistanceFrom (position)); }
  97. int MouseEvent::getMouseDownX() const noexcept { return roundToInt (mouseDownPosition.x); }
  98. int MouseEvent::getMouseDownY() const noexcept { return roundToInt (mouseDownPosition.y); }
  99. int MouseEvent::getDistanceFromDragStartX() const noexcept { return getOffsetFromDragStart().x; }
  100. int MouseEvent::getDistanceFromDragStartY() const noexcept { return getOffsetFromDragStart().y; }
  101. int MouseEvent::getScreenX() const { return getScreenPosition().x; }
  102. int MouseEvent::getScreenY() const { return getScreenPosition().y; }
  103. int MouseEvent::getMouseDownScreenX() const { return getMouseDownScreenPosition().x; }
  104. int MouseEvent::getMouseDownScreenY() const { return getMouseDownScreenPosition().y; }
  105. bool MouseEvent::isPressureValid() const noexcept { return pressure > 0.0f && pressure < 1.0f; }
  106. bool MouseEvent::isOrientationValid() const noexcept { return orientation >= 0.0f && orientation <= MathConstants<float>::twoPi; }
  107. bool MouseEvent::isRotationValid() const noexcept { return rotation >= 0 && rotation <= MathConstants<float>::twoPi; }
  108. bool MouseEvent::isTiltValid (bool isX) const noexcept { return isX ? (tiltX >= -1.0f && tiltX <= 1.0f) : (tiltY >= -1.0f && tiltY <= 1.0f); }
  109. //==============================================================================
  110. static int doubleClickTimeOutMs = 400;
  111. int MouseEvent::getDoubleClickTimeout() noexcept { return doubleClickTimeOutMs; }
  112. void MouseEvent::setDoubleClickTimeout (const int newTime) noexcept { doubleClickTimeOutMs = newTime; }
  113. } // namespace juce