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.6KB

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