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.

256 lines
7.8KB

  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. RelativePointPath::RelativePointPath()
  16. : usesNonZeroWinding (true),
  17. containsDynamicPoints (false)
  18. {
  19. }
  20. RelativePointPath::RelativePointPath (const RelativePointPath& other)
  21. : usesNonZeroWinding (true),
  22. containsDynamicPoints (false)
  23. {
  24. for (int i = 0; i < other.elements.size(); ++i)
  25. elements.add (other.elements.getUnchecked(i)->clone());
  26. }
  27. RelativePointPath::RelativePointPath (const Path& path)
  28. : usesNonZeroWinding (path.isUsingNonZeroWinding()),
  29. containsDynamicPoints (false)
  30. {
  31. for (Path::Iterator i (path); i.next();)
  32. {
  33. switch (i.elementType)
  34. {
  35. case Path::Iterator::startNewSubPath: elements.add (new StartSubPath (RelativePoint (i.x1, i.y1))); break;
  36. case Path::Iterator::lineTo: elements.add (new LineTo (RelativePoint (i.x1, i.y1))); break;
  37. case Path::Iterator::quadraticTo: elements.add (new QuadraticTo (RelativePoint (i.x1, i.y1), RelativePoint (i.x2, i.y2))); break;
  38. case Path::Iterator::cubicTo: elements.add (new CubicTo (RelativePoint (i.x1, i.y1), RelativePoint (i.x2, i.y2), RelativePoint (i.x3, i.y3))); break;
  39. case Path::Iterator::closePath: elements.add (new CloseSubPath()); break;
  40. default: jassertfalse; break;
  41. }
  42. }
  43. }
  44. RelativePointPath::~RelativePointPath()
  45. {
  46. }
  47. bool RelativePointPath::operator== (const RelativePointPath& other) const noexcept
  48. {
  49. if (elements.size() != other.elements.size()
  50. || usesNonZeroWinding != other.usesNonZeroWinding
  51. || containsDynamicPoints != other.containsDynamicPoints)
  52. return false;
  53. for (int i = 0; i < elements.size(); ++i)
  54. {
  55. ElementBase* const e1 = elements.getUnchecked(i);
  56. ElementBase* const e2 = other.elements.getUnchecked(i);
  57. if (e1->type != e2->type)
  58. return false;
  59. int numPoints1, numPoints2;
  60. const RelativePoint* const points1 = e1->getControlPoints (numPoints1);
  61. const RelativePoint* const points2 = e2->getControlPoints (numPoints2);
  62. jassert (numPoints1 == numPoints2);
  63. for (int j = numPoints1; --j >= 0;)
  64. if (points1[j] != points2[j])
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool RelativePointPath::operator!= (const RelativePointPath& other) const noexcept
  70. {
  71. return ! operator== (other);
  72. }
  73. void RelativePointPath::swapWith (RelativePointPath& other) noexcept
  74. {
  75. elements.swapWith (other.elements);
  76. std::swap (usesNonZeroWinding, other.usesNonZeroWinding);
  77. std::swap (containsDynamicPoints, other.containsDynamicPoints);
  78. }
  79. void RelativePointPath::createPath (Path& path, Expression::Scope* scope) const
  80. {
  81. for (int i = 0; i < elements.size(); ++i)
  82. elements.getUnchecked(i)->addToPath (path, scope);
  83. }
  84. bool RelativePointPath::containsAnyDynamicPoints() const
  85. {
  86. return containsDynamicPoints;
  87. }
  88. void RelativePointPath::addElement (ElementBase* newElement)
  89. {
  90. if (newElement != nullptr)
  91. {
  92. elements.add (newElement);
  93. containsDynamicPoints = containsDynamicPoints || newElement->isDynamic();
  94. }
  95. }
  96. //==============================================================================
  97. RelativePointPath::ElementBase::ElementBase (const ElementType type_) : type (type_)
  98. {
  99. }
  100. bool RelativePointPath::ElementBase::isDynamic()
  101. {
  102. int numPoints;
  103. const RelativePoint* const points = getControlPoints (numPoints);
  104. for (int i = numPoints; --i >= 0;)
  105. if (points[i].isDynamic())
  106. return true;
  107. return false;
  108. }
  109. //==============================================================================
  110. RelativePointPath::StartSubPath::StartSubPath (const RelativePoint& pos)
  111. : ElementBase (startSubPathElement), startPos (pos)
  112. {
  113. }
  114. void RelativePointPath::StartSubPath::addToPath (Path& path, Expression::Scope* scope) const
  115. {
  116. path.startNewSubPath (startPos.resolve (scope));
  117. }
  118. RelativePoint* RelativePointPath::StartSubPath::getControlPoints (int& numPoints)
  119. {
  120. numPoints = 1;
  121. return &startPos;
  122. }
  123. RelativePointPath::ElementBase* RelativePointPath::StartSubPath::clone() const
  124. {
  125. return new StartSubPath (startPos);
  126. }
  127. //==============================================================================
  128. RelativePointPath::CloseSubPath::CloseSubPath()
  129. : ElementBase (closeSubPathElement)
  130. {
  131. }
  132. void RelativePointPath::CloseSubPath::addToPath (Path& path, Expression::Scope*) const
  133. {
  134. path.closeSubPath();
  135. }
  136. RelativePoint* RelativePointPath::CloseSubPath::getControlPoints (int& numPoints)
  137. {
  138. numPoints = 0;
  139. return nullptr;
  140. }
  141. RelativePointPath::ElementBase* RelativePointPath::CloseSubPath::clone() const
  142. {
  143. return new CloseSubPath();
  144. }
  145. //==============================================================================
  146. RelativePointPath::LineTo::LineTo (const RelativePoint& endPoint_)
  147. : ElementBase (lineToElement), endPoint (endPoint_)
  148. {
  149. }
  150. void RelativePointPath::LineTo::addToPath (Path& path, Expression::Scope* scope) const
  151. {
  152. path.lineTo (endPoint.resolve (scope));
  153. }
  154. RelativePoint* RelativePointPath::LineTo::getControlPoints (int& numPoints)
  155. {
  156. numPoints = 1;
  157. return &endPoint;
  158. }
  159. RelativePointPath::ElementBase* RelativePointPath::LineTo::clone() const
  160. {
  161. return new LineTo (endPoint);
  162. }
  163. //==============================================================================
  164. RelativePointPath::QuadraticTo::QuadraticTo (const RelativePoint& controlPoint, const RelativePoint& endPoint)
  165. : ElementBase (quadraticToElement)
  166. {
  167. controlPoints[0] = controlPoint;
  168. controlPoints[1] = endPoint;
  169. }
  170. void RelativePointPath::QuadraticTo::addToPath (Path& path, Expression::Scope* scope) const
  171. {
  172. path.quadraticTo (controlPoints[0].resolve (scope),
  173. controlPoints[1].resolve (scope));
  174. }
  175. RelativePoint* RelativePointPath::QuadraticTo::getControlPoints (int& numPoints)
  176. {
  177. numPoints = 2;
  178. return controlPoints;
  179. }
  180. RelativePointPath::ElementBase* RelativePointPath::QuadraticTo::clone() const
  181. {
  182. return new QuadraticTo (controlPoints[0], controlPoints[1]);
  183. }
  184. //==============================================================================
  185. RelativePointPath::CubicTo::CubicTo (const RelativePoint& controlPoint1, const RelativePoint& controlPoint2, const RelativePoint& endPoint)
  186. : ElementBase (cubicToElement)
  187. {
  188. controlPoints[0] = controlPoint1;
  189. controlPoints[1] = controlPoint2;
  190. controlPoints[2] = endPoint;
  191. }
  192. void RelativePointPath::CubicTo::addToPath (Path& path, Expression::Scope* scope) const
  193. {
  194. path.cubicTo (controlPoints[0].resolve (scope),
  195. controlPoints[1].resolve (scope),
  196. controlPoints[2].resolve (scope));
  197. }
  198. RelativePoint* RelativePointPath::CubicTo::getControlPoints (int& numPoints)
  199. {
  200. numPoints = 3;
  201. return controlPoints;
  202. }
  203. RelativePointPath::ElementBase* RelativePointPath::CubicTo::clone() const
  204. {
  205. return new CubicTo (controlPoints[0], controlPoints[1], controlPoints[2]);
  206. }
  207. } // namespace juce