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.

341 lines
13KB

  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. DrawableComposite::DrawableComposite()
  19. : bounds (Point<float>(), Point<float> (100.0f, 0.0f), Point<float> (0.0f, 100.0f)),
  20. updateBoundsReentrant (false)
  21. {
  22. setContentArea (RelativeRectangle (RelativeCoordinate (0.0),
  23. RelativeCoordinate (100.0),
  24. RelativeCoordinate (0.0),
  25. RelativeCoordinate (100.0)));
  26. }
  27. DrawableComposite::DrawableComposite (const DrawableComposite& other)
  28. : Drawable (other),
  29. bounds (other.bounds),
  30. markersX (other.markersX),
  31. markersY (other.markersY),
  32. updateBoundsReentrant (false)
  33. {
  34. for (int i = 0; i < other.getNumChildComponents(); ++i)
  35. {
  36. const Drawable* const d = dynamic_cast <const Drawable*> (other.getChildComponent(i));
  37. if (d != nullptr)
  38. addAndMakeVisible (d->createCopy());
  39. }
  40. }
  41. DrawableComposite::~DrawableComposite()
  42. {
  43. deleteAllChildren();
  44. }
  45. Drawable* DrawableComposite::createCopy() const
  46. {
  47. return new DrawableComposite (*this);
  48. }
  49. //==============================================================================
  50. Rectangle<float> DrawableComposite::getDrawableBounds() const
  51. {
  52. Rectangle<float> r;
  53. for (int i = getNumChildComponents(); --i >= 0;)
  54. {
  55. const Drawable* const d = dynamic_cast <const Drawable*> (getChildComponent(i));
  56. if (d != nullptr)
  57. r = r.getUnion (d->isTransformed() ? d->getDrawableBounds().transformed (d->getTransform())
  58. : d->getDrawableBounds());
  59. }
  60. return r;
  61. }
  62. MarkerList* DrawableComposite::getMarkers (bool xAxis)
  63. {
  64. return xAxis ? &markersX : &markersY;
  65. }
  66. RelativeRectangle DrawableComposite::getContentArea() const
  67. {
  68. jassert (markersX.getNumMarkers() >= 2 && markersX.getMarker (0)->name == contentLeftMarkerName && markersX.getMarker (1)->name == contentRightMarkerName);
  69. jassert (markersY.getNumMarkers() >= 2 && markersY.getMarker (0)->name == contentTopMarkerName && markersY.getMarker (1)->name == contentBottomMarkerName);
  70. return RelativeRectangle (markersX.getMarker(0)->position, markersX.getMarker(1)->position,
  71. markersY.getMarker(0)->position, markersY.getMarker(1)->position);
  72. }
  73. void DrawableComposite::setContentArea (const RelativeRectangle& newArea)
  74. {
  75. markersX.setMarker (contentLeftMarkerName, newArea.left);
  76. markersX.setMarker (contentRightMarkerName, newArea.right);
  77. markersY.setMarker (contentTopMarkerName, newArea.top);
  78. markersY.setMarker (contentBottomMarkerName, newArea.bottom);
  79. }
  80. void DrawableComposite::setBoundingBox (const RelativeParallelogram& newBounds)
  81. {
  82. if (bounds != newBounds)
  83. {
  84. bounds = newBounds;
  85. if (bounds.isDynamic())
  86. {
  87. Drawable::Positioner<DrawableComposite>* const p = new Drawable::Positioner<DrawableComposite> (*this);
  88. setPositioner (p);
  89. p->apply();
  90. }
  91. else
  92. {
  93. setPositioner (nullptr);
  94. recalculateCoordinates (nullptr);
  95. }
  96. }
  97. }
  98. void DrawableComposite::resetBoundingBoxToContentArea()
  99. {
  100. const RelativeRectangle content (getContentArea());
  101. setBoundingBox (RelativeParallelogram (RelativePoint (content.left, content.top),
  102. RelativePoint (content.right, content.top),
  103. RelativePoint (content.left, content.bottom)));
  104. }
  105. void DrawableComposite::resetContentAreaAndBoundingBoxToFitChildren()
  106. {
  107. const Rectangle<float> activeArea (getDrawableBounds());
  108. setContentArea (RelativeRectangle (RelativeCoordinate (activeArea.getX()),
  109. RelativeCoordinate (activeArea.getRight()),
  110. RelativeCoordinate (activeArea.getY()),
  111. RelativeCoordinate (activeArea.getBottom())));
  112. resetBoundingBoxToContentArea();
  113. }
  114. bool DrawableComposite::registerCoordinates (RelativeCoordinatePositionerBase& pos)
  115. {
  116. bool ok = pos.addPoint (bounds.topLeft);
  117. ok = pos.addPoint (bounds.topRight) && ok;
  118. return pos.addPoint (bounds.bottomLeft) && ok;
  119. }
  120. void DrawableComposite::recalculateCoordinates (Expression::Scope* scope)
  121. {
  122. Point<float> resolved[3];
  123. bounds.resolveThreePoints (resolved, scope);
  124. const Rectangle<float> content (getContentArea().resolve (scope));
  125. AffineTransform t (AffineTransform::fromTargetPoints (content.getX(), content.getY(), resolved[0].x, resolved[0].y,
  126. content.getRight(), content.getY(), resolved[1].x, resolved[1].y,
  127. content.getX(), content.getBottom(), resolved[2].x, resolved[2].y));
  128. if (t.isSingularity())
  129. t = AffineTransform::identity;
  130. setTransform (t);
  131. }
  132. void DrawableComposite::parentHierarchyChanged()
  133. {
  134. DrawableComposite* parent = getParent();
  135. if (parent != nullptr)
  136. originRelativeToComponent = parent->originRelativeToComponent - getPosition();
  137. }
  138. void DrawableComposite::childBoundsChanged (Component*)
  139. {
  140. updateBoundsToFitChildren();
  141. }
  142. void DrawableComposite::childrenChanged()
  143. {
  144. updateBoundsToFitChildren();
  145. }
  146. void DrawableComposite::updateBoundsToFitChildren()
  147. {
  148. if (! updateBoundsReentrant)
  149. {
  150. const ScopedValueSetter<bool> setter (updateBoundsReentrant, true, false);
  151. Rectangle<int> childArea;
  152. for (int i = getNumChildComponents(); --i >= 0;)
  153. childArea = childArea.getUnion (getChildComponent(i)->getBoundsInParent());
  154. const Point<int> delta (childArea.getPosition());
  155. childArea += getPosition();
  156. if (childArea != getBounds())
  157. {
  158. if (! delta.isOrigin())
  159. {
  160. originRelativeToComponent -= delta;
  161. for (int i = getNumChildComponents(); --i >= 0;)
  162. {
  163. Component* const c = getChildComponent(i);
  164. if (c != nullptr)
  165. c->setBounds (c->getBounds() - delta);
  166. }
  167. }
  168. setBounds (childArea);
  169. }
  170. }
  171. }
  172. //==============================================================================
  173. const char* const DrawableComposite::contentLeftMarkerName = "left";
  174. const char* const DrawableComposite::contentRightMarkerName = "right";
  175. const char* const DrawableComposite::contentTopMarkerName = "top";
  176. const char* const DrawableComposite::contentBottomMarkerName = "bottom";
  177. //==============================================================================
  178. const Identifier DrawableComposite::valueTreeType ("Group");
  179. const Identifier DrawableComposite::ValueTreeWrapper::topLeft ("topLeft");
  180. const Identifier DrawableComposite::ValueTreeWrapper::topRight ("topRight");
  181. const Identifier DrawableComposite::ValueTreeWrapper::bottomLeft ("bottomLeft");
  182. const Identifier DrawableComposite::ValueTreeWrapper::childGroupTag ("Drawables");
  183. const Identifier DrawableComposite::ValueTreeWrapper::markerGroupTagX ("MarkersX");
  184. const Identifier DrawableComposite::ValueTreeWrapper::markerGroupTagY ("MarkersY");
  185. //==============================================================================
  186. DrawableComposite::ValueTreeWrapper::ValueTreeWrapper (const ValueTree& state_)
  187. : ValueTreeWrapperBase (state_)
  188. {
  189. jassert (state.hasType (valueTreeType));
  190. }
  191. ValueTree DrawableComposite::ValueTreeWrapper::getChildList() const
  192. {
  193. return state.getChildWithName (childGroupTag);
  194. }
  195. ValueTree DrawableComposite::ValueTreeWrapper::getChildListCreating (UndoManager* undoManager)
  196. {
  197. return state.getOrCreateChildWithName (childGroupTag, undoManager);
  198. }
  199. RelativeParallelogram DrawableComposite::ValueTreeWrapper::getBoundingBox() const
  200. {
  201. return RelativeParallelogram (state.getProperty (topLeft, "0, 0"),
  202. state.getProperty (topRight, "100, 0"),
  203. state.getProperty (bottomLeft, "0, 100"));
  204. }
  205. void DrawableComposite::ValueTreeWrapper::setBoundingBox (const RelativeParallelogram& newBounds, UndoManager* undoManager)
  206. {
  207. state.setProperty (topLeft, newBounds.topLeft.toString(), undoManager);
  208. state.setProperty (topRight, newBounds.topRight.toString(), undoManager);
  209. state.setProperty (bottomLeft, newBounds.bottomLeft.toString(), undoManager);
  210. }
  211. void DrawableComposite::ValueTreeWrapper::resetBoundingBoxToContentArea (UndoManager* undoManager)
  212. {
  213. const RelativeRectangle content (getContentArea());
  214. setBoundingBox (RelativeParallelogram (RelativePoint (content.left, content.top),
  215. RelativePoint (content.right, content.top),
  216. RelativePoint (content.left, content.bottom)), undoManager);
  217. }
  218. RelativeRectangle DrawableComposite::ValueTreeWrapper::getContentArea() const
  219. {
  220. MarkerList::ValueTreeWrapper markersX (getMarkerList (true));
  221. MarkerList::ValueTreeWrapper markersY (getMarkerList (false));
  222. return RelativeRectangle (markersX.getMarker (markersX.getMarkerState (0)).position,
  223. markersX.getMarker (markersX.getMarkerState (1)).position,
  224. markersY.getMarker (markersY.getMarkerState (0)).position,
  225. markersY.getMarker (markersY.getMarkerState (1)).position);
  226. }
  227. void DrawableComposite::ValueTreeWrapper::setContentArea (const RelativeRectangle& newArea, UndoManager* undoManager)
  228. {
  229. MarkerList::ValueTreeWrapper markersX (getMarkerListCreating (true, nullptr));
  230. MarkerList::ValueTreeWrapper markersY (getMarkerListCreating (false, nullptr));
  231. markersX.setMarker (MarkerList::Marker (contentLeftMarkerName, newArea.left), undoManager);
  232. markersX.setMarker (MarkerList::Marker (contentRightMarkerName, newArea.right), undoManager);
  233. markersY.setMarker (MarkerList::Marker (contentTopMarkerName, newArea.top), undoManager);
  234. markersY.setMarker (MarkerList::Marker (contentBottomMarkerName, newArea.bottom), undoManager);
  235. }
  236. MarkerList::ValueTreeWrapper DrawableComposite::ValueTreeWrapper::getMarkerList (bool xAxis) const
  237. {
  238. return state.getChildWithName (xAxis ? markerGroupTagX : markerGroupTagY);
  239. }
  240. MarkerList::ValueTreeWrapper DrawableComposite::ValueTreeWrapper::getMarkerListCreating (bool xAxis, UndoManager* undoManager)
  241. {
  242. return state.getOrCreateChildWithName (xAxis ? markerGroupTagX : markerGroupTagY, undoManager);
  243. }
  244. //==============================================================================
  245. void DrawableComposite::refreshFromValueTree (const ValueTree& tree, ComponentBuilder& builder)
  246. {
  247. const ValueTreeWrapper wrapper (tree);
  248. setComponentID (wrapper.getID());
  249. wrapper.getMarkerList (true).applyTo (markersX);
  250. wrapper.getMarkerList (false).applyTo (markersY);
  251. setBoundingBox (wrapper.getBoundingBox());
  252. builder.updateChildComponents (*this, wrapper.getChildList());
  253. }
  254. ValueTree DrawableComposite::createValueTree (ComponentBuilder::ImageProvider* imageProvider) const
  255. {
  256. ValueTree tree (valueTreeType);
  257. ValueTreeWrapper v (tree);
  258. v.setID (getComponentID());
  259. v.setBoundingBox (bounds, nullptr);
  260. ValueTree childList (v.getChildListCreating (nullptr));
  261. for (int i = 0; i < getNumChildComponents(); ++i)
  262. {
  263. const Drawable* const d = dynamic_cast <const Drawable*> (getChildComponent(i));
  264. jassert (d != nullptr); // You can't save a mix of Drawables and normal components!
  265. childList.addChild (d->createValueTree (imageProvider), -1, nullptr);
  266. }
  267. v.getMarkerListCreating (true, nullptr).readFrom (markersX, nullptr);
  268. v.getMarkerListCreating (false, nullptr).readFrom (markersY, nullptr);
  269. return tree;
  270. }