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.

161 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. Stores a 3D orientation, which can be rotated by dragging with the mouse.
  24. @tags{OpenGL}
  25. */
  26. class Draggable3DOrientation
  27. {
  28. public:
  29. using VectorType = Vector3D<float>;
  30. using QuaternionType = Quaternion<float>;
  31. /** Creates a Draggable3DOrientation, initially set up to be aligned along the X axis. */
  32. Draggable3DOrientation (float objectRadius = 0.5f) noexcept
  33. : radius (jmax (0.1f, objectRadius)),
  34. quaternion (VectorType::xAxis(), 0)
  35. {
  36. }
  37. /** Creates a Draggable3DOrientation from a user-supplied quaternion. */
  38. Draggable3DOrientation (const Quaternion<float>& quaternionToUse,
  39. float objectRadius = 0.5f) noexcept
  40. : radius (jmax (0.1f, objectRadius)),
  41. quaternion (quaternionToUse)
  42. {
  43. }
  44. /** Resets the orientation, specifying the axis to align it along. */
  45. void reset (const VectorType& axis) noexcept
  46. {
  47. quaternion = QuaternionType (axis, 0);
  48. }
  49. /** Sets the viewport area within which mouse-drag positions will occur.
  50. You'll need to set this rectangle before calling mouseDown. The centre of the
  51. rectangle is assumed to be the centre of the object that will be rotated, and
  52. the size of the rectangle will be used to scale the object radius - see setRadius().
  53. */
  54. void setViewport (Rectangle<int> newArea) noexcept
  55. {
  56. area = newArea;
  57. }
  58. /** Sets the size of the rotated object, as a proportion of the viewport's size.
  59. @see setViewport
  60. */
  61. void setRadius (float newRadius) noexcept
  62. {
  63. radius = jmax (0.1f, newRadius);
  64. }
  65. /** Begins a mouse-drag operation.
  66. You must call this before any calls to mouseDrag(). The position that is supplied
  67. will be treated as being relative to the centre of the rectangle passed to setViewport().
  68. */
  69. template <typename Type>
  70. void mouseDown (Point<Type> mousePos) noexcept
  71. {
  72. lastMouse = mousePosToProportion (mousePos.toFloat());
  73. }
  74. /** Continues a mouse-drag operation.
  75. After calling mouseDown() to begin a drag sequence, you can call this method
  76. to continue it.
  77. */
  78. template <typename Type>
  79. void mouseDrag (Point<Type> mousePos) noexcept
  80. {
  81. auto oldPos = projectOnSphere (lastMouse);
  82. lastMouse = mousePosToProportion (mousePos.toFloat());
  83. auto newPos = projectOnSphere (lastMouse);
  84. quaternion *= rotationFromMove (oldPos, newPos);
  85. }
  86. /** Returns the matrix that should be used to apply the current orientation.
  87. @see applyToOpenGLMatrix
  88. */
  89. Matrix3D<float> getRotationMatrix() const noexcept
  90. {
  91. return quaternion.getRotationMatrix();
  92. }
  93. /** Provides direct access to the quaternion. */
  94. QuaternionType& getQuaternion() noexcept
  95. {
  96. return quaternion;
  97. }
  98. private:
  99. Rectangle<int> area;
  100. float radius;
  101. QuaternionType quaternion;
  102. Point<float> lastMouse;
  103. Point<float> mousePosToProportion (Point<float> mousePos) const noexcept
  104. {
  105. auto scale = jmin (area.getWidth(), area.getHeight()) / 2;
  106. // You must call setViewport() to give this object a valid window size before
  107. // calling any of the mouse input methods!
  108. jassert (scale > 0);
  109. return { (mousePos.x - (float) area.getCentreX()) / (float) scale,
  110. ((float) area.getCentreY() - mousePos.y) / (float) scale };
  111. }
  112. VectorType projectOnSphere (Point<float> pos) const noexcept
  113. {
  114. auto radiusSquared = radius * radius;
  115. auto xySquared = pos.x * pos.x + pos.y * pos.y;
  116. return { pos.x, pos.y,
  117. xySquared < radiusSquared * 0.5f ? std::sqrt (radiusSquared - xySquared)
  118. : (radiusSquared / (2.0f * std::sqrt (xySquared))) };
  119. }
  120. QuaternionType rotationFromMove (const VectorType& from, const VectorType& to) const noexcept
  121. {
  122. auto rotationAxis = (to ^ from);
  123. if (rotationAxis.lengthIsBelowEpsilon())
  124. rotationAxis = VectorType::xAxis();
  125. auto d = jlimit (-1.0f, 1.0f, (from - to).length() / (2.0f * radius));
  126. return QuaternionType::fromAngle (2.0f * std::asin (d), rotationAxis);
  127. }
  128. };
  129. } // namespace juce