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.

134 lines
4.7KB

  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. RelativeParallelogram::RelativeParallelogram()
  16. {
  17. }
  18. RelativeParallelogram::RelativeParallelogram (const Rectangle<float>& r)
  19. : topLeft (r.getTopLeft()), topRight (r.getTopRight()), bottomLeft (r.getBottomLeft())
  20. {
  21. }
  22. RelativeParallelogram::RelativeParallelogram (const RelativePoint& topLeft_, const RelativePoint& topRight_, const RelativePoint& bottomLeft_)
  23. : topLeft (topLeft_), topRight (topRight_), bottomLeft (bottomLeft_)
  24. {
  25. }
  26. RelativeParallelogram::RelativeParallelogram (const String& topLeft_, const String& topRight_, const String& bottomLeft_)
  27. : topLeft (topLeft_), topRight (topRight_), bottomLeft (bottomLeft_)
  28. {
  29. }
  30. RelativeParallelogram::~RelativeParallelogram()
  31. {
  32. }
  33. void RelativeParallelogram::resolveThreePoints (Point<float>* points, Expression::Scope* const scope) const
  34. {
  35. points[0] = topLeft.resolve (scope);
  36. points[1] = topRight.resolve (scope);
  37. points[2] = bottomLeft.resolve (scope);
  38. }
  39. void RelativeParallelogram::resolveFourCorners (Point<float>* points, Expression::Scope* const scope) const
  40. {
  41. resolveThreePoints (points, scope);
  42. points[3] = points[1] + (points[2] - points[0]);
  43. }
  44. const Rectangle<float> RelativeParallelogram::getBounds (Expression::Scope* const scope) const
  45. {
  46. Point<float> points[4];
  47. resolveFourCorners (points, scope);
  48. return Rectangle<float>::findAreaContainingPoints (points, 4);
  49. }
  50. void RelativeParallelogram::getPath (Path& path, Expression::Scope* const scope) const
  51. {
  52. Point<float> points[4];
  53. resolveFourCorners (points, scope);
  54. path.startNewSubPath (points[0]);
  55. path.lineTo (points[1]);
  56. path.lineTo (points[3]);
  57. path.lineTo (points[2]);
  58. path.closeSubPath();
  59. }
  60. AffineTransform RelativeParallelogram::resetToPerpendicular (Expression::Scope* const scope)
  61. {
  62. Point<float> corners[3];
  63. resolveThreePoints (corners, scope);
  64. const Line<float> top (corners[0], corners[1]);
  65. const Line<float> left (corners[0], corners[2]);
  66. const Point<float> newTopRight (corners[0] + Point<float> (top.getLength(), 0.0f));
  67. const Point<float> newBottomLeft (corners[0] + Point<float> (0.0f, left.getLength()));
  68. topRight.moveToAbsolute (newTopRight, scope);
  69. bottomLeft.moveToAbsolute (newBottomLeft, scope);
  70. return AffineTransform::fromTargetPoints (corners[0], corners[0],
  71. corners[1], newTopRight,
  72. corners[2], newBottomLeft);
  73. }
  74. bool RelativeParallelogram::isDynamic() const
  75. {
  76. return topLeft.isDynamic() || topRight.isDynamic() || bottomLeft.isDynamic();
  77. }
  78. bool RelativeParallelogram::operator== (const RelativeParallelogram& other) const noexcept
  79. {
  80. return topLeft == other.topLeft && topRight == other.topRight && bottomLeft == other.bottomLeft;
  81. }
  82. bool RelativeParallelogram::operator!= (const RelativeParallelogram& other) const noexcept
  83. {
  84. return ! operator== (other);
  85. }
  86. Point<float> RelativeParallelogram::getInternalCoordForPoint (const Point<float>* const corners, Point<float> target) noexcept
  87. {
  88. const Point<float> tr (corners[1] - corners[0]);
  89. const Point<float> bl (corners[2] - corners[0]);
  90. target -= corners[0];
  91. return Point<float> (Line<float> (Point<float>(), tr).getIntersection (Line<float> (target, target - bl)).getDistanceFromOrigin(),
  92. Line<float> (Point<float>(), bl).getIntersection (Line<float> (target, target - tr)).getDistanceFromOrigin());
  93. }
  94. Point<float> RelativeParallelogram::getPointForInternalCoord (const Point<float>* const corners, const Point<float> point) noexcept
  95. {
  96. return corners[0]
  97. + Line<float> (Point<float>(), corners[1] - corners[0]).getPointAlongLine (point.x)
  98. + Line<float> (Point<float>(), corners[2] - corners[0]).getPointAlongLine (point.y);
  99. }
  100. Rectangle<float> RelativeParallelogram::getBoundingBox (const Point<float>* const p) noexcept
  101. {
  102. const Point<float> points[] = { p[0], p[1], p[2], p[1] + (p[2] - p[0]) };
  103. return Rectangle<float>::findAreaContainingPoints (points, 4);
  104. }
  105. } // namespace juce