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.

159 lines
5.4KB

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