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.

456 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. DrawableShape::DrawableShape()
  19. : strokeType (0.0f),
  20. mainFill (Colours::black),
  21. strokeFill (Colours::black)
  22. {
  23. }
  24. DrawableShape::DrawableShape (const DrawableShape& other)
  25. : Drawable (other),
  26. strokeType (other.strokeType),
  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& component_, const DrawableShape::RelativeFillType& fill_, bool isMainFill_)
  39. : RelativeCoordinatePositionerBase (component_),
  40. owner (component_),
  41. fill (fill_),
  42. isMainFill (isMainFill_)
  43. {
  44. }
  45. bool registerCoordinates()
  46. {
  47. bool ok = addPoint (fill.gradientPoint1);
  48. ok = addPoint (fill.gradientPoint2) && ok;
  49. return addPoint (fill.gradientPoint3) && ok;
  50. }
  51. void applyToComponentBounds()
  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>&)
  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::setStrokeThickness (const float newThickness)
  112. {
  113. setStrokeType (PathStrokeType (newThickness, strokeType.getJointStyle(), strokeType.getEndStyle()));
  114. }
  115. bool DrawableShape::isStrokeVisible() const noexcept
  116. {
  117. return strokeType.getStrokeThickness() > 0.0f && ! strokeFill.fill.isInvisible();
  118. }
  119. void DrawableShape::refreshFillTypes (const FillAndStrokeState& newState, ComponentBuilder::ImageProvider* imageProvider)
  120. {
  121. setFill (newState.getFill (FillAndStrokeState::fill, imageProvider));
  122. setStrokeFill (newState.getFill (FillAndStrokeState::stroke, imageProvider));
  123. }
  124. void DrawableShape::writeTo (FillAndStrokeState& state, ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager) const
  125. {
  126. state.setFill (FillAndStrokeState::fill, mainFill, imageProvider, undoManager);
  127. state.setFill (FillAndStrokeState::stroke, strokeFill, imageProvider, undoManager);
  128. state.setStrokeType (strokeType, undoManager);
  129. }
  130. //==============================================================================
  131. void DrawableShape::paint (Graphics& g)
  132. {
  133. transformContextToCorrectOrigin (g);
  134. g.setFillType (mainFill.fill);
  135. g.fillPath (path);
  136. if (isStrokeVisible())
  137. {
  138. g.setFillType (strokeFill.fill);
  139. g.fillPath (strokePath);
  140. }
  141. }
  142. void DrawableShape::pathChanged()
  143. {
  144. strokeChanged();
  145. }
  146. void DrawableShape::strokeChanged()
  147. {
  148. strokePath.clear();
  149. strokeType.createStrokedPath (strokePath, path, AffineTransform::identity, 4.0f);
  150. setBoundsToEnclose (getDrawableBounds());
  151. repaint();
  152. }
  153. Rectangle<float> DrawableShape::getDrawableBounds() const
  154. {
  155. if (isStrokeVisible())
  156. return strokePath.getBounds();
  157. return path.getBounds();
  158. }
  159. bool DrawableShape::hitTest (int x, int y)
  160. {
  161. bool allowsClicksOnThisComponent, allowsClicksOnChildComponents;
  162. getInterceptsMouseClicks (allowsClicksOnThisComponent, allowsClicksOnChildComponents);
  163. if (! allowsClicksOnThisComponent)
  164. return false;
  165. const float globalX = (float) (x - originRelativeToComponent.x);
  166. const float globalY = (float) (y - originRelativeToComponent.y);
  167. return path.contains (globalX, globalY)
  168. || (isStrokeVisible() && strokePath.contains (globalX, globalY));
  169. }
  170. //==============================================================================
  171. DrawableShape::RelativeFillType::RelativeFillType()
  172. {
  173. }
  174. DrawableShape::RelativeFillType::RelativeFillType (const FillType& fill_)
  175. : fill (fill_)
  176. {
  177. if (fill.isGradient())
  178. {
  179. const ColourGradient& g = *fill.gradient;
  180. gradientPoint1 = g.point1.transformedBy (fill.transform);
  181. gradientPoint2 = g.point2.transformedBy (fill.transform);
  182. gradientPoint3 = Point<float> (g.point1.x + g.point2.y - g.point1.y,
  183. g.point1.y + g.point1.x - g.point2.x)
  184. .transformedBy (fill.transform);
  185. fill.transform = AffineTransform::identity;
  186. }
  187. }
  188. DrawableShape::RelativeFillType::RelativeFillType (const RelativeFillType& other)
  189. : fill (other.fill),
  190. gradientPoint1 (other.gradientPoint1),
  191. gradientPoint2 (other.gradientPoint2),
  192. gradientPoint3 (other.gradientPoint3)
  193. {
  194. }
  195. DrawableShape::RelativeFillType& DrawableShape::RelativeFillType::operator= (const RelativeFillType& other)
  196. {
  197. fill = other.fill;
  198. gradientPoint1 = other.gradientPoint1;
  199. gradientPoint2 = other.gradientPoint2;
  200. gradientPoint3 = other.gradientPoint3;
  201. return *this;
  202. }
  203. bool DrawableShape::RelativeFillType::operator== (const RelativeFillType& other) const
  204. {
  205. return fill == other.fill
  206. && ((! fill.isGradient())
  207. || (gradientPoint1 == other.gradientPoint1
  208. && gradientPoint2 == other.gradientPoint2
  209. && gradientPoint3 == other.gradientPoint3));
  210. }
  211. bool DrawableShape::RelativeFillType::operator!= (const RelativeFillType& other) const
  212. {
  213. return ! operator== (other);
  214. }
  215. bool DrawableShape::RelativeFillType::recalculateCoords (Expression::Scope* scope)
  216. {
  217. if (fill.isGradient())
  218. {
  219. const Point<float> g1 (gradientPoint1.resolve (scope));
  220. const Point<float> g2 (gradientPoint2.resolve (scope));
  221. AffineTransform t;
  222. ColourGradient& g = *fill.gradient;
  223. if (g.isRadial)
  224. {
  225. const Point<float> g3 (gradientPoint3.resolve (scope));
  226. const Point<float> g3Source (g1.x + g2.y - g1.y,
  227. g1.y + g1.x - g2.x);
  228. t = AffineTransform::fromTargetPoints (g1.x, g1.y, g1.x, g1.y,
  229. g2.x, g2.y, g2.x, g2.y,
  230. g3Source.x, g3Source.y, g3.x, g3.y);
  231. }
  232. if (g.point1 != g1 || g.point2 != g2 || fill.transform != t)
  233. {
  234. g.point1 = g1;
  235. g.point2 = g2;
  236. fill.transform = t;
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. bool DrawableShape::RelativeFillType::isDynamic() const
  243. {
  244. return gradientPoint1.isDynamic() || gradientPoint2.isDynamic() || gradientPoint3.isDynamic();
  245. }
  246. void DrawableShape::RelativeFillType::writeTo (ValueTree& v, ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager) const
  247. {
  248. if (fill.isColour())
  249. {
  250. v.setProperty (FillAndStrokeState::type, "solid", undoManager);
  251. v.setProperty (FillAndStrokeState::colour, String::toHexString ((int) fill.colour.getARGB()), undoManager);
  252. }
  253. else if (fill.isGradient())
  254. {
  255. v.setProperty (FillAndStrokeState::type, "gradient", undoManager);
  256. v.setProperty (FillAndStrokeState::gradientPoint1, gradientPoint1.toString(), undoManager);
  257. v.setProperty (FillAndStrokeState::gradientPoint2, gradientPoint2.toString(), undoManager);
  258. v.setProperty (FillAndStrokeState::gradientPoint3, gradientPoint3.toString(), undoManager);
  259. const ColourGradient& cg = *fill.gradient;
  260. v.setProperty (FillAndStrokeState::radial, cg.isRadial, undoManager);
  261. String s;
  262. for (int i = 0; i < cg.getNumColours(); ++i)
  263. s << ' ' << cg.getColourPosition (i)
  264. << ' ' << String::toHexString ((int) cg.getColour(i).getARGB());
  265. v.setProperty (FillAndStrokeState::colours, s.trimStart(), undoManager);
  266. }
  267. else if (fill.isTiledImage())
  268. {
  269. v.setProperty (FillAndStrokeState::type, "image", undoManager);
  270. if (imageProvider != nullptr)
  271. v.setProperty (FillAndStrokeState::imageId, imageProvider->getIdentifierForImage (fill.image), undoManager);
  272. if (fill.getOpacity() < 1.0f)
  273. v.setProperty (FillAndStrokeState::imageOpacity, fill.getOpacity(), undoManager);
  274. else
  275. v.removeProperty (FillAndStrokeState::imageOpacity, undoManager);
  276. }
  277. else
  278. {
  279. jassertfalse;
  280. }
  281. }
  282. bool DrawableShape::RelativeFillType::readFrom (const ValueTree& v, ComponentBuilder::ImageProvider* imageProvider)
  283. {
  284. const String newType (v [FillAndStrokeState::type].toString());
  285. if (newType == "solid")
  286. {
  287. const String colourString (v [FillAndStrokeState::colour].toString());
  288. fill.setColour (colourString.isEmpty() ? Colours::black
  289. : Colour::fromString (colourString));
  290. return true;
  291. }
  292. else if (newType == "gradient")
  293. {
  294. ColourGradient g;
  295. g.isRadial = v [FillAndStrokeState::radial];
  296. StringArray colourSteps;
  297. colourSteps.addTokens (v [FillAndStrokeState::colours].toString(), false);
  298. for (int i = 0; i < colourSteps.size() / 2; ++i)
  299. g.addColour (colourSteps[i * 2].getDoubleValue(),
  300. Colour::fromString (colourSteps[i * 2 + 1]));
  301. fill.setGradient (g);
  302. gradientPoint1 = RelativePoint (v [FillAndStrokeState::gradientPoint1]);
  303. gradientPoint2 = RelativePoint (v [FillAndStrokeState::gradientPoint2]);
  304. gradientPoint3 = RelativePoint (v [FillAndStrokeState::gradientPoint3]);
  305. return true;
  306. }
  307. else if (newType == "image")
  308. {
  309. Image im;
  310. if (imageProvider != nullptr)
  311. im = imageProvider->getImageForIdentifier (v [FillAndStrokeState::imageId]);
  312. fill.setTiledImage (im, AffineTransform::identity);
  313. fill.setOpacity ((float) v.getProperty (FillAndStrokeState::imageOpacity, 1.0f));
  314. return true;
  315. }
  316. jassertfalse;
  317. return false;
  318. }
  319. //==============================================================================
  320. const Identifier DrawableShape::FillAndStrokeState::type ("type");
  321. const Identifier DrawableShape::FillAndStrokeState::colour ("colour");
  322. const Identifier DrawableShape::FillAndStrokeState::colours ("colours");
  323. const Identifier DrawableShape::FillAndStrokeState::fill ("Fill");
  324. const Identifier DrawableShape::FillAndStrokeState::stroke ("Stroke");
  325. const Identifier DrawableShape::FillAndStrokeState::path ("Path");
  326. const Identifier DrawableShape::FillAndStrokeState::jointStyle ("jointStyle");
  327. const Identifier DrawableShape::FillAndStrokeState::capStyle ("capStyle");
  328. const Identifier DrawableShape::FillAndStrokeState::strokeWidth ("strokeWidth");
  329. const Identifier DrawableShape::FillAndStrokeState::gradientPoint1 ("point1");
  330. const Identifier DrawableShape::FillAndStrokeState::gradientPoint2 ("point2");
  331. const Identifier DrawableShape::FillAndStrokeState::gradientPoint3 ("point3");
  332. const Identifier DrawableShape::FillAndStrokeState::radial ("radial");
  333. const Identifier DrawableShape::FillAndStrokeState::imageId ("imageId");
  334. const Identifier DrawableShape::FillAndStrokeState::imageOpacity ("imageOpacity");
  335. DrawableShape::FillAndStrokeState::FillAndStrokeState (const ValueTree& state_)
  336. : Drawable::ValueTreeWrapperBase (state_)
  337. {
  338. }
  339. DrawableShape::RelativeFillType DrawableShape::FillAndStrokeState::getFill (const Identifier& fillOrStrokeType, ComponentBuilder::ImageProvider* imageProvider) const
  340. {
  341. DrawableShape::RelativeFillType f;
  342. f.readFrom (state.getChildWithName (fillOrStrokeType), imageProvider);
  343. return f;
  344. }
  345. ValueTree DrawableShape::FillAndStrokeState::getFillState (const Identifier& fillOrStrokeType)
  346. {
  347. ValueTree v (state.getChildWithName (fillOrStrokeType));
  348. if (v.isValid())
  349. return v;
  350. setFill (fillOrStrokeType, FillType (Colours::black), nullptr, nullptr);
  351. return getFillState (fillOrStrokeType);
  352. }
  353. void DrawableShape::FillAndStrokeState::setFill (const Identifier& fillOrStrokeType, const RelativeFillType& newFill,
  354. ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager)
  355. {
  356. ValueTree v (state.getOrCreateChildWithName (fillOrStrokeType, undoManager));
  357. newFill.writeTo (v, imageProvider, undoManager);
  358. }
  359. PathStrokeType DrawableShape::FillAndStrokeState::getStrokeType() const
  360. {
  361. const String jointStyleString (state [jointStyle].toString());
  362. const String capStyleString (state [capStyle].toString());
  363. return PathStrokeType (state [strokeWidth],
  364. jointStyleString == "curved" ? PathStrokeType::curved
  365. : (jointStyleString == "bevel" ? PathStrokeType::beveled
  366. : PathStrokeType::mitered),
  367. capStyleString == "square" ? PathStrokeType::square
  368. : (capStyleString == "round" ? PathStrokeType::rounded
  369. : PathStrokeType::butt));
  370. }
  371. void DrawableShape::FillAndStrokeState::setStrokeType (const PathStrokeType& newStrokeType, UndoManager* undoManager)
  372. {
  373. state.setProperty (strokeWidth, (double) newStrokeType.getStrokeThickness(), undoManager);
  374. state.setProperty (jointStyle, newStrokeType.getJointStyle() == PathStrokeType::mitered
  375. ? "miter" : (newStrokeType.getJointStyle() == PathStrokeType::curved ? "curved" : "bevel"), undoManager);
  376. state.setProperty (capStyle, newStrokeType.getEndStyle() == PathStrokeType::butt
  377. ? "butt" : (newStrokeType.getEndStyle() == PathStrokeType::square ? "square" : "round"), undoManager);
  378. }