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.

473 lines
17KB

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