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.

158 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_DRAGGABLE3DORIENTATION_H_INCLUDED
  18. #define JUCE_DRAGGABLE3DORIENTATION_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Stores a 3D orientation, which can be rotated by dragging with the mouse.
  22. */
  23. class Draggable3DOrientation
  24. {
  25. public:
  26. typedef Vector3D<float> VectorType;
  27. typedef Quaternion<float> QuaternionType;
  28. /** Creates a Draggable3DOrientation, initially set up to be aligned along the X axis. */
  29. Draggable3DOrientation (float objectRadius = 0.5f) noexcept
  30. : radius (jmax (0.1f, objectRadius)),
  31. quaternion (VectorType::xAxis(), 0)
  32. {
  33. }
  34. /** Creates a Draggable3DOrientation from a user-supplied quaternion. */
  35. Draggable3DOrientation (const Quaternion<float>& quaternionToUse,
  36. float objectRadius = 0.5f) noexcept
  37. : radius (jmax (0.1f, objectRadius)),
  38. quaternion (quaternionToUse)
  39. {
  40. }
  41. /** Resets the orientation, specifying the axis to align it along. */
  42. void reset (const VectorType& axis) noexcept
  43. {
  44. quaternion = QuaternionType (axis, 0);
  45. }
  46. /** Sets the viewport area within which mouse-drag positions will occur.
  47. You'll need to set this rectangle before calling mouseDown. The centre of the
  48. rectangle is assumed to be the centre of the object that will be rotated, and
  49. the size of the rectangle will be used to scale the object radius - see setRadius().
  50. */
  51. void setViewport (const Rectangle<int>& newArea) noexcept
  52. {
  53. area = newArea;
  54. }
  55. /** Sets the size of the rotated object, as a proportion of the viewport's size.
  56. @see setViewport
  57. */
  58. void setRadius (float newRadius) noexcept
  59. {
  60. radius = jmax (0.1f, newRadius);
  61. }
  62. /** Begins a mouse-drag operation.
  63. You must call this before any calls to mouseDrag(). The position that is supplied
  64. will be treated as being relative to the centre of the rectangle passed to setViewport().
  65. */
  66. template <typename Type>
  67. void mouseDown (Point<Type> mousePos) noexcept
  68. {
  69. lastMouse = mousePosToProportion (mousePos.toFloat());
  70. }
  71. /** Continues a mouse-drag operation.
  72. After calling mouseDown() to begin a drag sequence, you can call this method
  73. to continue it.
  74. */
  75. template <typename Type>
  76. void mouseDrag (Point<Type> mousePos) noexcept
  77. {
  78. const VectorType oldPos (projectOnSphere (lastMouse));
  79. lastMouse = mousePosToProportion (mousePos.toFloat());
  80. const VectorType newPos (projectOnSphere (lastMouse));
  81. quaternion *= rotationFromMove (oldPos, newPos);
  82. }
  83. /** Returns the matrix that should be used to apply the current orientation.
  84. @see applyToOpenGLMatrix
  85. */
  86. Matrix3D<float> getRotationMatrix() const noexcept
  87. {
  88. return quaternion.getRotationMatrix();
  89. }
  90. /** Provides direct access to the quaternion. */
  91. QuaternionType& getQuaternion() noexcept
  92. {
  93. return quaternion;
  94. }
  95. private:
  96. Rectangle<int> area;
  97. float radius;
  98. QuaternionType quaternion;
  99. Point<float> lastMouse;
  100. Point<float> mousePosToProportion (const Point<float> mousePos) const noexcept
  101. {
  102. const int scale = (jmin (area.getWidth(), area.getHeight()) / 2);
  103. // You must call setViewport() to give this object a valid window size before
  104. // calling any of the mouse input methods!
  105. jassert (scale > 0);
  106. return Point<float> ((mousePos.x - area.getCentreX()) / scale,
  107. (area.getCentreY() - mousePos.y) / scale);
  108. }
  109. VectorType projectOnSphere (const Point<float> pos) const noexcept
  110. {
  111. const float radiusSquared = radius * radius;
  112. const float xySquared = pos.x * pos.x + pos.y * pos.y;
  113. return VectorType (pos.x, pos.y,
  114. xySquared < radiusSquared * 0.5f ? std::sqrt (radiusSquared - xySquared)
  115. : (radiusSquared / (2.0f * std::sqrt (xySquared))));
  116. }
  117. QuaternionType rotationFromMove (const VectorType& from, const VectorType& to) const noexcept
  118. {
  119. VectorType rotationAxis (to ^ from);
  120. if (rotationAxis.lengthIsBelowEpsilon())
  121. rotationAxis = VectorType::xAxis();
  122. const float d = jlimit (-1.0f, 1.0f, (from - to).length() / (2.0f * radius));
  123. return QuaternionType::fromAngle (2.0f * std::asin (d), rotationAxis);
  124. }
  125. };
  126. #endif // JUCE_DRAGGABLE3DORIENTATION_H_INCLUDED