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. : 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& component_, const DrawableShape::RelativeFillType& fill_, bool isMainFill_)
  38. : RelativeCoordinatePositionerBase (component_),
  39. owner (component_),
  40. fill (fill_),
  41. isMainFill (isMainFill_)
  42. {
  43. }
  44. bool registerCoordinates()
  45. {
  46. bool ok = addPoint (fill.gradientPoint1);
  47. ok = addPoint (fill.gradientPoint2) && ok;
  48. return addPoint (fill.gradientPoint3) && ok;
  49. }
  50. void applyToComponentBounds()
  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>&)
  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. else
  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. }