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