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.

154 lines
5.4KB

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