Audio plugin host https://kx.studio/carla
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.

489 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. DrawableShape::DrawableShape()
  18. : strokeType (0.0f),
  19. mainFill (Colours::black),
  20. strokeFill (Colours::black)
  21. {
  22. }
  23. DrawableShape::DrawableShape (const DrawableShape& other)
  24. : Drawable (other),
  25. strokeType (other.strokeType),
  26. dashLengths (other.dashLengths),
  27. mainFill (other.mainFill),
  28. strokeFill (other.strokeFill)
  29. {
  30. }
  31. DrawableShape::~DrawableShape()
  32. {
  33. }
  34. //==============================================================================
  35. class DrawableShape::RelativePositioner : public RelativeCoordinatePositionerBase
  36. {
  37. public:
  38. RelativePositioner (DrawableShape& comp, const DrawableShape::RelativeFillType& f, bool isMain)
  39. : RelativeCoordinatePositionerBase (comp),
  40. owner (comp),
  41. fill (f),
  42. isMainFill (isMain)
  43. {
  44. }
  45. bool registerCoordinates() override
  46. {
  47. bool ok = addPoint (fill.gradientPoint1);
  48. ok = addPoint (fill.gradientPoint2) && ok;
  49. return addPoint (fill.gradientPoint3) && ok;
  50. }
  51. void applyToComponentBounds() override
  52. {
  53. ComponentScope scope (owner);
  54. if (isMainFill ? owner.mainFill.recalculateCoords (&scope)
  55. : owner.strokeFill.recalculateCoords (&scope))
  56. owner.repaint();
  57. }
  58. void applyNewBounds (const Rectangle<int>&) override
  59. {
  60. jassertfalse; // drawables can't be resized directly!
  61. }
  62. private:
  63. DrawableShape& owner;
  64. const DrawableShape::RelativeFillType fill;
  65. const bool isMainFill;
  66. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RelativePositioner)
  67. };
  68. void DrawableShape::setFill (const FillType& newFill)
  69. {
  70. setFill (RelativeFillType (newFill));
  71. }
  72. void DrawableShape::setStrokeFill (const FillType& newFill)
  73. {
  74. setStrokeFill (RelativeFillType (newFill));
  75. }
  76. void DrawableShape::setFillInternal (RelativeFillType& fill, const RelativeFillType& newFill,
  77. ScopedPointer<RelativeCoordinatePositionerBase>& pos)
  78. {
  79. if (fill != newFill)
  80. {
  81. fill = newFill;
  82. pos = nullptr;
  83. if (fill.isDynamic())
  84. {
  85. pos = new RelativePositioner (*this, fill, true);
  86. pos->apply();
  87. }
  88. else
  89. {
  90. fill.recalculateCoords (nullptr);
  91. }
  92. repaint();
  93. }
  94. }
  95. void DrawableShape::setFill (const RelativeFillType& newFill)
  96. {
  97. setFillInternal (mainFill, newFill, mainFillPositioner);
  98. }
  99. void DrawableShape::setStrokeFill (const RelativeFillType& newFill)
  100. {
  101. setFillInternal (strokeFill, newFill, strokeFillPositioner);
  102. }
  103. void DrawableShape::setStrokeType (const PathStrokeType& newStrokeType)
  104. {
  105. if (strokeType != newStrokeType)
  106. {
  107. strokeType = newStrokeType;
  108. strokeChanged();
  109. }
  110. }
  111. void DrawableShape::setDashLengths (const Array<float>& newDashLengths)
  112. {
  113. if (dashLengths != newDashLengths)
  114. {
  115. dashLengths = newDashLengths;
  116. strokeChanged();
  117. }
  118. }
  119. void DrawableShape::setStrokeThickness (const float newThickness)
  120. {
  121. setStrokeType (PathStrokeType (newThickness, strokeType.getJointStyle(), strokeType.getEndStyle()));
  122. }
  123. bool DrawableShape::isStrokeVisible() const noexcept
  124. {
  125. return strokeType.getStrokeThickness() > 0.0f && ! strokeFill.fill.isInvisible();
  126. }
  127. void DrawableShape::refreshFillTypes (const FillAndStrokeState& newState, ComponentBuilder::ImageProvider* imageProvider)
  128. {
  129. setFill (newState.getFill (FillAndStrokeState::fill, imageProvider));
  130. setStrokeFill (newState.getFill (FillAndStrokeState::stroke, imageProvider));
  131. }
  132. void DrawableShape::writeTo (FillAndStrokeState& state, ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager) const
  133. {
  134. state.setFill (FillAndStrokeState::fill, mainFill, imageProvider, undoManager);
  135. state.setFill (FillAndStrokeState::stroke, strokeFill, imageProvider, undoManager);
  136. state.setStrokeType (strokeType, undoManager);
  137. }
  138. //==============================================================================
  139. void DrawableShape::paint (Graphics& g)
  140. {
  141. transformContextToCorrectOrigin (g);
  142. g.setFillType (mainFill.fill);
  143. g.fillPath (path);
  144. if (isStrokeVisible())
  145. {
  146. g.setFillType (strokeFill.fill);
  147. g.fillPath (strokePath);
  148. }
  149. }
  150. void DrawableShape::pathChanged()
  151. {
  152. strokeChanged();
  153. }
  154. void DrawableShape::strokeChanged()
  155. {
  156. strokePath.clear();
  157. const float extraAccuracy = 4.0f;
  158. if (dashLengths.isEmpty())
  159. strokeType.createStrokedPath (strokePath, path, AffineTransform(), extraAccuracy);
  160. else
  161. strokeType.createDashedStroke (strokePath, path, dashLengths.getRawDataPointer(),
  162. dashLengths.size(), AffineTransform(), extraAccuracy);
  163. setBoundsToEnclose (getDrawableBounds());
  164. repaint();
  165. }
  166. Rectangle<float> DrawableShape::getDrawableBounds() const
  167. {
  168. if (isStrokeVisible())
  169. return strokePath.getBounds();
  170. return path.getBounds();
  171. }
  172. bool DrawableShape::hitTest (int x, int y)
  173. {
  174. bool allowsClicksOnThisComponent, allowsClicksOnChildComponents;
  175. getInterceptsMouseClicks (allowsClicksOnThisComponent, allowsClicksOnChildComponents);
  176. if (! allowsClicksOnThisComponent)
  177. return false;
  178. const float globalX = (float) (x - originRelativeToComponent.x);
  179. const float globalY = (float) (y - originRelativeToComponent.y);
  180. return path.contains (globalX, globalY)
  181. || (isStrokeVisible() && strokePath.contains (globalX, globalY));
  182. }
  183. //==============================================================================
  184. DrawableShape::RelativeFillType::RelativeFillType()
  185. {
  186. }
  187. DrawableShape::RelativeFillType::RelativeFillType (const FillType& fill_)
  188. : fill (fill_)
  189. {
  190. if (fill.isGradient())
  191. {
  192. const ColourGradient& g = *fill.gradient;
  193. gradientPoint1 = g.point1.transformedBy (fill.transform);
  194. gradientPoint2 = g.point2.transformedBy (fill.transform);
  195. gradientPoint3 = Point<float> (g.point1.x + g.point2.y - g.point1.y,
  196. g.point1.y + g.point1.x - g.point2.x)
  197. .transformedBy (fill.transform);
  198. fill.transform = AffineTransform();
  199. }
  200. }
  201. DrawableShape::RelativeFillType::RelativeFillType (const RelativeFillType& other)
  202. : fill (other.fill),
  203. gradientPoint1 (other.gradientPoint1),
  204. gradientPoint2 (other.gradientPoint2),
  205. gradientPoint3 (other.gradientPoint3)
  206. {
  207. }
  208. DrawableShape::RelativeFillType& DrawableShape::RelativeFillType::operator= (const RelativeFillType& other)
  209. {
  210. fill = other.fill;
  211. gradientPoint1 = other.gradientPoint1;
  212. gradientPoint2 = other.gradientPoint2;
  213. gradientPoint3 = other.gradientPoint3;
  214. return *this;
  215. }
  216. bool DrawableShape::RelativeFillType::operator== (const RelativeFillType& other) const
  217. {
  218. return fill == other.fill
  219. && ((! fill.isGradient())
  220. || (gradientPoint1 == other.gradientPoint1
  221. && gradientPoint2 == other.gradientPoint2
  222. && gradientPoint3 == other.gradientPoint3));
  223. }
  224. bool DrawableShape::RelativeFillType::operator!= (const RelativeFillType& other) const
  225. {
  226. return ! operator== (other);
  227. }
  228. bool DrawableShape::RelativeFillType::recalculateCoords (Expression::Scope* scope)
  229. {
  230. if (fill.isGradient())
  231. {
  232. const Point<float> g1 (gradientPoint1.resolve (scope));
  233. const Point<float> g2 (gradientPoint2.resolve (scope));
  234. AffineTransform t;
  235. ColourGradient& g = *fill.gradient;
  236. if (g.isRadial)
  237. {
  238. const Point<float> g3 (gradientPoint3.resolve (scope));
  239. const Point<float> g3Source (g1.x + g2.y - g1.y,
  240. g1.y + g1.x - g2.x);
  241. t = AffineTransform::fromTargetPoints (g1.x, g1.y, g1.x, g1.y,
  242. g2.x, g2.y, g2.x, g2.y,
  243. g3Source.x, g3Source.y, g3.x, g3.y);
  244. }
  245. if (g.point1 != g1 || g.point2 != g2 || fill.transform != t)
  246. {
  247. g.point1 = g1;
  248. g.point2 = g2;
  249. fill.transform = t;
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. bool DrawableShape::RelativeFillType::isDynamic() const
  256. {
  257. return gradientPoint1.isDynamic() || gradientPoint2.isDynamic() || gradientPoint3.isDynamic();
  258. }
  259. void DrawableShape::RelativeFillType::writeTo (ValueTree& v, ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager) const
  260. {
  261. if (fill.isColour())
  262. {
  263. v.setProperty (FillAndStrokeState::type, "solid", undoManager);
  264. v.setProperty (FillAndStrokeState::colour, String::toHexString ((int) fill.colour.getARGB()), undoManager);
  265. }
  266. else if (fill.isGradient())
  267. {
  268. v.setProperty (FillAndStrokeState::type, "gradient", undoManager);
  269. v.setProperty (FillAndStrokeState::gradientPoint1, gradientPoint1.toString(), undoManager);
  270. v.setProperty (FillAndStrokeState::gradientPoint2, gradientPoint2.toString(), undoManager);
  271. v.setProperty (FillAndStrokeState::gradientPoint3, gradientPoint3.toString(), undoManager);
  272. const ColourGradient& cg = *fill.gradient;
  273. v.setProperty (FillAndStrokeState::radial, cg.isRadial, undoManager);
  274. String s;
  275. for (int i = 0; i < cg.getNumColours(); ++i)
  276. s << ' ' << cg.getColourPosition (i)
  277. << ' ' << String::toHexString ((int) cg.getColour(i).getARGB());
  278. v.setProperty (FillAndStrokeState::colours, s.trimStart(), undoManager);
  279. }
  280. else if (fill.isTiledImage())
  281. {
  282. v.setProperty (FillAndStrokeState::type, "image", undoManager);
  283. if (imageProvider != nullptr)
  284. v.setProperty (FillAndStrokeState::imageId, imageProvider->getIdentifierForImage (fill.image), undoManager);
  285. if (fill.getOpacity() < 1.0f)
  286. v.setProperty (FillAndStrokeState::imageOpacity, fill.getOpacity(), undoManager);
  287. else
  288. v.removeProperty (FillAndStrokeState::imageOpacity, undoManager);
  289. }
  290. else
  291. {
  292. jassertfalse;
  293. }
  294. }
  295. bool DrawableShape::RelativeFillType::readFrom (const ValueTree& v, ComponentBuilder::ImageProvider* imageProvider)
  296. {
  297. const String newType (v [FillAndStrokeState::type].toString());
  298. if (newType == "solid")
  299. {
  300. const String colourString (v [FillAndStrokeState::colour].toString());
  301. fill.setColour (colourString.isEmpty() ? Colours::black
  302. : Colour::fromString (colourString));
  303. return true;
  304. }
  305. else if (newType == "gradient")
  306. {
  307. ColourGradient g;
  308. g.isRadial = v [FillAndStrokeState::radial];
  309. StringArray colourSteps;
  310. colourSteps.addTokens (v [FillAndStrokeState::colours].toString(), false);
  311. for (int i = 0; i < colourSteps.size() / 2; ++i)
  312. g.addColour (colourSteps[i * 2].getDoubleValue(),
  313. Colour::fromString (colourSteps[i * 2 + 1]));
  314. fill.setGradient (g);
  315. gradientPoint1 = RelativePoint (v [FillAndStrokeState::gradientPoint1]);
  316. gradientPoint2 = RelativePoint (v [FillAndStrokeState::gradientPoint2]);
  317. gradientPoint3 = RelativePoint (v [FillAndStrokeState::gradientPoint3]);
  318. return true;
  319. }
  320. else if (newType == "image")
  321. {
  322. Image im;
  323. if (imageProvider != nullptr)
  324. im = imageProvider->getImageForIdentifier (v [FillAndStrokeState::imageId]);
  325. fill.setTiledImage (im, AffineTransform());
  326. fill.setOpacity ((float) v.getProperty (FillAndStrokeState::imageOpacity, 1.0f));
  327. return true;
  328. }
  329. jassertfalse;
  330. return false;
  331. }
  332. //==============================================================================
  333. const Identifier DrawableShape::FillAndStrokeState::type ("type");
  334. const Identifier DrawableShape::FillAndStrokeState::colour ("colour");
  335. const Identifier DrawableShape::FillAndStrokeState::colours ("colours");
  336. const Identifier DrawableShape::FillAndStrokeState::fill ("Fill");
  337. const Identifier DrawableShape::FillAndStrokeState::stroke ("Stroke");
  338. const Identifier DrawableShape::FillAndStrokeState::path ("Path");
  339. const Identifier DrawableShape::FillAndStrokeState::jointStyle ("jointStyle");
  340. const Identifier DrawableShape::FillAndStrokeState::capStyle ("capStyle");
  341. const Identifier DrawableShape::FillAndStrokeState::strokeWidth ("strokeWidth");
  342. const Identifier DrawableShape::FillAndStrokeState::gradientPoint1 ("point1");
  343. const Identifier DrawableShape::FillAndStrokeState::gradientPoint2 ("point2");
  344. const Identifier DrawableShape::FillAndStrokeState::gradientPoint3 ("point3");
  345. const Identifier DrawableShape::FillAndStrokeState::radial ("radial");
  346. const Identifier DrawableShape::FillAndStrokeState::imageId ("imageId");
  347. const Identifier DrawableShape::FillAndStrokeState::imageOpacity ("imageOpacity");
  348. DrawableShape::FillAndStrokeState::FillAndStrokeState (const ValueTree& state_)
  349. : Drawable::ValueTreeWrapperBase (state_)
  350. {
  351. }
  352. DrawableShape::RelativeFillType DrawableShape::FillAndStrokeState::getFill (const Identifier& fillOrStrokeType, ComponentBuilder::ImageProvider* imageProvider) const
  353. {
  354. DrawableShape::RelativeFillType f;
  355. f.readFrom (state.getChildWithName (fillOrStrokeType), imageProvider);
  356. return f;
  357. }
  358. ValueTree DrawableShape::FillAndStrokeState::getFillState (const Identifier& fillOrStrokeType)
  359. {
  360. ValueTree v (state.getChildWithName (fillOrStrokeType));
  361. if (v.isValid())
  362. return v;
  363. setFill (fillOrStrokeType, FillType (Colours::black), nullptr, nullptr);
  364. return getFillState (fillOrStrokeType);
  365. }
  366. void DrawableShape::FillAndStrokeState::setFill (const Identifier& fillOrStrokeType, const RelativeFillType& newFill,
  367. ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager)
  368. {
  369. ValueTree v (state.getOrCreateChildWithName (fillOrStrokeType, undoManager));
  370. newFill.writeTo (v, imageProvider, undoManager);
  371. }
  372. PathStrokeType DrawableShape::FillAndStrokeState::getStrokeType() const
  373. {
  374. const String jointStyleString (state [jointStyle].toString());
  375. const String capStyleString (state [capStyle].toString());
  376. return PathStrokeType (state [strokeWidth],
  377. jointStyleString == "curved" ? PathStrokeType::curved
  378. : (jointStyleString == "bevel" ? PathStrokeType::beveled
  379. : PathStrokeType::mitered),
  380. capStyleString == "square" ? PathStrokeType::square
  381. : (capStyleString == "round" ? PathStrokeType::rounded
  382. : PathStrokeType::butt));
  383. }
  384. void DrawableShape::FillAndStrokeState::setStrokeType (const PathStrokeType& newStrokeType, UndoManager* undoManager)
  385. {
  386. state.setProperty (strokeWidth, (double) newStrokeType.getStrokeThickness(), undoManager);
  387. state.setProperty (jointStyle, newStrokeType.getJointStyle() == PathStrokeType::mitered
  388. ? "miter" : (newStrokeType.getJointStyle() == PathStrokeType::curved ? "curved" : "bevel"), undoManager);
  389. state.setProperty (capStyle, newStrokeType.getEndStyle() == PathStrokeType::butt
  390. ? "butt" : (newStrokeType.getEndStyle() == PathStrokeType::square ? "square" : "round"), undoManager);
  391. }
  392. static bool replaceColourInFill (DrawableShape::RelativeFillType& fill, Colour original, Colour replacement)
  393. {
  394. if (fill.fill.colour == original && fill.fill.isColour())
  395. {
  396. fill = FillType (replacement);
  397. return true;
  398. }
  399. return false;
  400. }
  401. bool DrawableShape::replaceColour (Colour original, Colour replacement)
  402. {
  403. bool changed1 = replaceColourInFill (mainFill, original, replacement);
  404. bool changed2 = replaceColourInFill (strokeFill, original, replacement);
  405. return changed1 || changed2;
  406. }