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.

160 lines
5.3KB

  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. //==============================================================================
  21. /**
  22. Stores a 3D orientation, which can be rotated by dragging with the mouse.
  23. @tags{OpenGL}
  24. */
  25. class Draggable3DOrientation
  26. {
  27. public:
  28. using VectorType = Vector3D<float>;
  29. using QuaternionType = Quaternion<float>;
  30. /** Creates a Draggable3DOrientation, initially set up to be aligned along the X axis. */
  31. Draggable3DOrientation (float objectRadius = 0.5f) noexcept
  32. : radius (jmax (0.1f, objectRadius)),
  33. quaternion (VectorType::xAxis(), 0)
  34. {
  35. }
  36. /** Creates a Draggable3DOrientation from a user-supplied quaternion. */
  37. Draggable3DOrientation (const Quaternion<float>& quaternionToUse,
  38. float objectRadius = 0.5f) noexcept
  39. : radius (jmax (0.1f, objectRadius)),
  40. quaternion (quaternionToUse)
  41. {
  42. }
  43. /** Resets the orientation, specifying the axis to align it along. */
  44. void reset (const VectorType& axis) noexcept
  45. {
  46. quaternion = QuaternionType (axis, 0);
  47. }
  48. /** Sets the viewport area within which mouse-drag positions will occur.
  49. You'll need to set this rectangle before calling mouseDown. The centre of the
  50. rectangle is assumed to be the centre of the object that will be rotated, and
  51. the size of the rectangle will be used to scale the object radius - see setRadius().
  52. */
  53. void setViewport (Rectangle<int> newArea) noexcept
  54. {
  55. area = newArea;
  56. }
  57. /** Sets the size of the rotated object, as a proportion of the viewport's size.
  58. @see setViewport
  59. */
  60. void setRadius (float newRadius) noexcept
  61. {
  62. radius = jmax (0.1f, newRadius);
  63. }
  64. /** Begins a mouse-drag operation.
  65. You must call this before any calls to mouseDrag(). The position that is supplied
  66. will be treated as being relative to the centre of the rectangle passed to setViewport().
  67. */
  68. template <typename Type>
  69. void mouseDown (Point<Type> mousePos) noexcept
  70. {
  71. lastMouse = mousePosToProportion (mousePos.toFloat());
  72. }
  73. /** Continues a mouse-drag operation.
  74. After calling mouseDown() to begin a drag sequence, you can call this method
  75. to continue it.
  76. */
  77. template <typename Type>
  78. void mouseDrag (Point<Type> mousePos) noexcept
  79. {
  80. auto oldPos = projectOnSphere (lastMouse);
  81. lastMouse = mousePosToProportion (mousePos.toFloat());
  82. auto newPos = projectOnSphere (lastMouse);
  83. quaternion *= rotationFromMove (oldPos, newPos);
  84. }
  85. /** Returns the matrix that should be used to apply the current orientation.
  86. @see applyToOpenGLMatrix
  87. */
  88. Matrix3D<float> getRotationMatrix() const noexcept
  89. {
  90. return quaternion.getRotationMatrix();
  91. }
  92. /** Provides direct access to the quaternion. */
  93. QuaternionType& getQuaternion() noexcept
  94. {
  95. return quaternion;
  96. }
  97. private:
  98. Rectangle<int> area;
  99. float radius;
  100. QuaternionType quaternion;
  101. Point<float> lastMouse;
  102. Point<float> mousePosToProportion (Point<float> mousePos) const noexcept
  103. {
  104. auto scale = jmin (area.getWidth(), area.getHeight()) / 2;
  105. // You must call setViewport() to give this object a valid window size before
  106. // calling any of the mouse input methods!
  107. jassert (scale > 0);
  108. return { (mousePos.x - (float) area.getCentreX()) / (float) scale,
  109. ((float) area.getCentreY() - mousePos.y) / (float) scale };
  110. }
  111. VectorType projectOnSphere (Point<float> pos) const noexcept
  112. {
  113. auto radiusSquared = radius * radius;
  114. auto xySquared = pos.x * pos.x + pos.y * pos.y;
  115. return { pos.x, pos.y,
  116. xySquared < radiusSquared * 0.5f ? std::sqrt (radiusSquared - xySquared)
  117. : (radiusSquared / (2.0f * std::sqrt (xySquared))) };
  118. }
  119. QuaternionType rotationFromMove (const VectorType& from, const VectorType& to) const noexcept
  120. {
  121. auto rotationAxis = (to ^ from);
  122. if (rotationAxis.lengthIsBelowEpsilon())
  123. rotationAxis = VectorType::xAxis();
  124. auto d = jlimit (-1.0f, 1.0f, (from - to).length() / (2.0f * radius));
  125. return QuaternionType::fromAngle (2.0f * std::asin (d), rotationAxis);
  126. }
  127. };
  128. } // namespace juce