The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

136 lines
6.1KB

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