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.

155 lines
5.4KB

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