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.

153 lines
5.1KB

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