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

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