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.

juce_DrawableShape.cpp 17KB

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