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.

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