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.

491 lines
17KB

  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. g.setFillType (mainFill.fill);
  145. g.fillPath (path);
  146. if (isStrokeVisible())
  147. {
  148. g.setFillType (strokeFill.fill);
  149. g.fillPath (strokePath);
  150. }
  151. }
  152. void DrawableShape::pathChanged()
  153. {
  154. strokeChanged();
  155. }
  156. void DrawableShape::strokeChanged()
  157. {
  158. strokePath.clear();
  159. const float extraAccuracy = 4.0f;
  160. if (dashLengths.isEmpty())
  161. strokeType.createStrokedPath (strokePath, path, AffineTransform(), extraAccuracy);
  162. else
  163. strokeType.createDashedStroke (strokePath, path, dashLengths.getRawDataPointer(),
  164. dashLengths.size(), AffineTransform(), extraAccuracy);
  165. setBoundsToEnclose (getDrawableBounds());
  166. repaint();
  167. }
  168. Rectangle<float> DrawableShape::getDrawableBounds() const
  169. {
  170. if (isStrokeVisible())
  171. return strokePath.getBounds();
  172. return path.getBounds();
  173. }
  174. bool DrawableShape::hitTest (int x, int y)
  175. {
  176. bool allowsClicksOnThisComponent, allowsClicksOnChildComponents;
  177. getInterceptsMouseClicks (allowsClicksOnThisComponent, allowsClicksOnChildComponents);
  178. if (! allowsClicksOnThisComponent)
  179. return false;
  180. const float globalX = (float) (x - originRelativeToComponent.x);
  181. const float globalY = (float) (y - originRelativeToComponent.y);
  182. return path.contains (globalX, globalY)
  183. || (isStrokeVisible() && strokePath.contains (globalX, globalY));
  184. }
  185. //==============================================================================
  186. DrawableShape::RelativeFillType::RelativeFillType()
  187. {
  188. }
  189. DrawableShape::RelativeFillType::RelativeFillType (const FillType& fill_)
  190. : fill (fill_)
  191. {
  192. if (fill.isGradient())
  193. {
  194. const ColourGradient& g = *fill.gradient;
  195. gradientPoint1 = g.point1.transformedBy (fill.transform);
  196. gradientPoint2 = g.point2.transformedBy (fill.transform);
  197. gradientPoint3 = Point<float> (g.point1.x + g.point2.y - g.point1.y,
  198. g.point1.y + g.point1.x - g.point2.x)
  199. .transformedBy (fill.transform);
  200. fill.transform = AffineTransform();
  201. }
  202. }
  203. DrawableShape::RelativeFillType::RelativeFillType (const RelativeFillType& other)
  204. : fill (other.fill),
  205. gradientPoint1 (other.gradientPoint1),
  206. gradientPoint2 (other.gradientPoint2),
  207. gradientPoint3 (other.gradientPoint3)
  208. {
  209. }
  210. DrawableShape::RelativeFillType& DrawableShape::RelativeFillType::operator= (const RelativeFillType& other)
  211. {
  212. fill = other.fill;
  213. gradientPoint1 = other.gradientPoint1;
  214. gradientPoint2 = other.gradientPoint2;
  215. gradientPoint3 = other.gradientPoint3;
  216. return *this;
  217. }
  218. bool DrawableShape::RelativeFillType::operator== (const RelativeFillType& other) const
  219. {
  220. return fill == other.fill
  221. && ((! fill.isGradient())
  222. || (gradientPoint1 == other.gradientPoint1
  223. && gradientPoint2 == other.gradientPoint2
  224. && gradientPoint3 == other.gradientPoint3));
  225. }
  226. bool DrawableShape::RelativeFillType::operator!= (const RelativeFillType& other) const
  227. {
  228. return ! operator== (other);
  229. }
  230. bool DrawableShape::RelativeFillType::recalculateCoords (Expression::Scope* scope)
  231. {
  232. if (fill.isGradient())
  233. {
  234. const Point<float> g1 (gradientPoint1.resolve (scope));
  235. const Point<float> g2 (gradientPoint2.resolve (scope));
  236. AffineTransform t;
  237. ColourGradient& g = *fill.gradient;
  238. if (g.isRadial)
  239. {
  240. const Point<float> g3 (gradientPoint3.resolve (scope));
  241. const Point<float> g3Source (g1.x + g2.y - g1.y,
  242. g1.y + g1.x - g2.x);
  243. t = AffineTransform::fromTargetPoints (g1.x, g1.y, g1.x, g1.y,
  244. g2.x, g2.y, g2.x, g2.y,
  245. g3Source.x, g3Source.y, g3.x, g3.y);
  246. }
  247. if (g.point1 != g1 || g.point2 != g2 || fill.transform != t)
  248. {
  249. g.point1 = g1;
  250. g.point2 = g2;
  251. fill.transform = t;
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. bool DrawableShape::RelativeFillType::isDynamic() const
  258. {
  259. return gradientPoint1.isDynamic() || gradientPoint2.isDynamic() || gradientPoint3.isDynamic();
  260. }
  261. void DrawableShape::RelativeFillType::writeTo (ValueTree& v, ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager) const
  262. {
  263. if (fill.isColour())
  264. {
  265. v.setProperty (FillAndStrokeState::type, "solid", undoManager);
  266. v.setProperty (FillAndStrokeState::colour, String::toHexString ((int) fill.colour.getARGB()), undoManager);
  267. }
  268. else if (fill.isGradient())
  269. {
  270. v.setProperty (FillAndStrokeState::type, "gradient", undoManager);
  271. v.setProperty (FillAndStrokeState::gradientPoint1, gradientPoint1.toString(), undoManager);
  272. v.setProperty (FillAndStrokeState::gradientPoint2, gradientPoint2.toString(), undoManager);
  273. v.setProperty (FillAndStrokeState::gradientPoint3, gradientPoint3.toString(), undoManager);
  274. const ColourGradient& cg = *fill.gradient;
  275. v.setProperty (FillAndStrokeState::radial, cg.isRadial, undoManager);
  276. String s;
  277. for (int i = 0; i < cg.getNumColours(); ++i)
  278. s << ' ' << cg.getColourPosition (i)
  279. << ' ' << String::toHexString ((int) cg.getColour(i).getARGB());
  280. v.setProperty (FillAndStrokeState::colours, s.trimStart(), undoManager);
  281. }
  282. else if (fill.isTiledImage())
  283. {
  284. v.setProperty (FillAndStrokeState::type, "image", undoManager);
  285. if (imageProvider != nullptr)
  286. v.setProperty (FillAndStrokeState::imageId, imageProvider->getIdentifierForImage (fill.image), undoManager);
  287. if (fill.getOpacity() < 1.0f)
  288. v.setProperty (FillAndStrokeState::imageOpacity, fill.getOpacity(), undoManager);
  289. else
  290. v.removeProperty (FillAndStrokeState::imageOpacity, undoManager);
  291. }
  292. else
  293. {
  294. jassertfalse;
  295. }
  296. }
  297. bool DrawableShape::RelativeFillType::readFrom (const ValueTree& v, ComponentBuilder::ImageProvider* imageProvider)
  298. {
  299. const String newType (v [FillAndStrokeState::type].toString());
  300. if (newType == "solid")
  301. {
  302. const String colourString (v [FillAndStrokeState::colour].toString());
  303. fill.setColour (colourString.isEmpty() ? Colours::black
  304. : Colour::fromString (colourString));
  305. return true;
  306. }
  307. else if (newType == "gradient")
  308. {
  309. ColourGradient g;
  310. g.isRadial = v [FillAndStrokeState::radial];
  311. StringArray colourSteps;
  312. colourSteps.addTokens (v [FillAndStrokeState::colours].toString(), false);
  313. for (int i = 0; i < colourSteps.size() / 2; ++i)
  314. g.addColour (colourSteps[i * 2].getDoubleValue(),
  315. Colour::fromString (colourSteps[i * 2 + 1]));
  316. fill.setGradient (g);
  317. gradientPoint1 = RelativePoint (v [FillAndStrokeState::gradientPoint1]);
  318. gradientPoint2 = RelativePoint (v [FillAndStrokeState::gradientPoint2]);
  319. gradientPoint3 = RelativePoint (v [FillAndStrokeState::gradientPoint3]);
  320. return true;
  321. }
  322. else if (newType == "image")
  323. {
  324. Image im;
  325. if (imageProvider != nullptr)
  326. im = imageProvider->getImageForIdentifier (v [FillAndStrokeState::imageId]);
  327. fill.setTiledImage (im, AffineTransform());
  328. fill.setOpacity ((float) v.getProperty (FillAndStrokeState::imageOpacity, 1.0f));
  329. return true;
  330. }
  331. jassertfalse;
  332. return false;
  333. }
  334. //==============================================================================
  335. const Identifier DrawableShape::FillAndStrokeState::type ("type");
  336. const Identifier DrawableShape::FillAndStrokeState::colour ("colour");
  337. const Identifier DrawableShape::FillAndStrokeState::colours ("colours");
  338. const Identifier DrawableShape::FillAndStrokeState::fill ("Fill");
  339. const Identifier DrawableShape::FillAndStrokeState::stroke ("Stroke");
  340. const Identifier DrawableShape::FillAndStrokeState::path ("Path");
  341. const Identifier DrawableShape::FillAndStrokeState::jointStyle ("jointStyle");
  342. const Identifier DrawableShape::FillAndStrokeState::capStyle ("capStyle");
  343. const Identifier DrawableShape::FillAndStrokeState::strokeWidth ("strokeWidth");
  344. const Identifier DrawableShape::FillAndStrokeState::gradientPoint1 ("point1");
  345. const Identifier DrawableShape::FillAndStrokeState::gradientPoint2 ("point2");
  346. const Identifier DrawableShape::FillAndStrokeState::gradientPoint3 ("point3");
  347. const Identifier DrawableShape::FillAndStrokeState::radial ("radial");
  348. const Identifier DrawableShape::FillAndStrokeState::imageId ("imageId");
  349. const Identifier DrawableShape::FillAndStrokeState::imageOpacity ("imageOpacity");
  350. DrawableShape::FillAndStrokeState::FillAndStrokeState (const ValueTree& state_)
  351. : Drawable::ValueTreeWrapperBase (state_)
  352. {
  353. }
  354. DrawableShape::RelativeFillType DrawableShape::FillAndStrokeState::getFill (const Identifier& fillOrStrokeType, ComponentBuilder::ImageProvider* imageProvider) const
  355. {
  356. DrawableShape::RelativeFillType f;
  357. f.readFrom (state.getChildWithName (fillOrStrokeType), imageProvider);
  358. return f;
  359. }
  360. ValueTree DrawableShape::FillAndStrokeState::getFillState (const Identifier& fillOrStrokeType)
  361. {
  362. ValueTree v (state.getChildWithName (fillOrStrokeType));
  363. if (v.isValid())
  364. return v;
  365. setFill (fillOrStrokeType, FillType (Colours::black), nullptr, nullptr);
  366. return getFillState (fillOrStrokeType);
  367. }
  368. void DrawableShape::FillAndStrokeState::setFill (const Identifier& fillOrStrokeType, const RelativeFillType& newFill,
  369. ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager)
  370. {
  371. ValueTree v (state.getOrCreateChildWithName (fillOrStrokeType, undoManager));
  372. newFill.writeTo (v, imageProvider, undoManager);
  373. }
  374. PathStrokeType DrawableShape::FillAndStrokeState::getStrokeType() const
  375. {
  376. const String jointStyleString (state [jointStyle].toString());
  377. const String capStyleString (state [capStyle].toString());
  378. return PathStrokeType (state [strokeWidth],
  379. jointStyleString == "curved" ? PathStrokeType::curved
  380. : (jointStyleString == "bevel" ? PathStrokeType::beveled
  381. : PathStrokeType::mitered),
  382. capStyleString == "square" ? PathStrokeType::square
  383. : (capStyleString == "round" ? PathStrokeType::rounded
  384. : PathStrokeType::butt));
  385. }
  386. void DrawableShape::FillAndStrokeState::setStrokeType (const PathStrokeType& newStrokeType, UndoManager* undoManager)
  387. {
  388. state.setProperty (strokeWidth, (double) newStrokeType.getStrokeThickness(), undoManager);
  389. state.setProperty (jointStyle, newStrokeType.getJointStyle() == PathStrokeType::mitered
  390. ? "miter" : (newStrokeType.getJointStyle() == PathStrokeType::curved ? "curved" : "bevel"), undoManager);
  391. state.setProperty (capStyle, newStrokeType.getEndStyle() == PathStrokeType::butt
  392. ? "butt" : (newStrokeType.getEndStyle() == PathStrokeType::square ? "square" : "round"), undoManager);
  393. }
  394. static bool replaceColourInFill (DrawableShape::RelativeFillType& fill, Colour original, Colour replacement)
  395. {
  396. if (fill.fill.colour == original && fill.fill.isColour())
  397. {
  398. fill = FillType (replacement);
  399. return true;
  400. }
  401. return false;
  402. }
  403. bool DrawableShape::replaceColour (Colour original, Colour replacement)
  404. {
  405. bool changed1 = replaceColourInFill (mainFill, original, replacement);
  406. bool changed2 = replaceColourInFill (strokeFill, original, replacement);
  407. return changed1 || changed2;
  408. }