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.

329 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. struct MarkerListScope : public Expression::Scope
  16. {
  17. MarkerListScope (Component& comp) : component (comp) {}
  18. Expression getSymbolValue (const String& symbol) const override
  19. {
  20. auto type = RelativeCoordinate::StandardStrings::getTypeOf (symbol);
  21. if (type == RelativeCoordinate::StandardStrings::width) return Expression ((double) component.getWidth());
  22. if (type == RelativeCoordinate::StandardStrings::height) return Expression ((double) component.getHeight());
  23. MarkerList* list;
  24. if (auto* marker = findMarker (component, symbol, list))
  25. return Expression (marker->position.getExpression().evaluate (*this));
  26. return Expression::Scope::getSymbolValue (symbol);
  27. }
  28. void visitRelativeScope (const String& scopeName, Visitor& visitor) const override
  29. {
  30. if (scopeName == RelativeCoordinate::Strings::parent)
  31. {
  32. if (auto* parent = component.getParentComponent())
  33. {
  34. visitor.visit (MarkerListScope (*parent));
  35. return;
  36. }
  37. }
  38. Expression::Scope::visitRelativeScope (scopeName, visitor);
  39. }
  40. String getScopeUID() const override
  41. {
  42. return String::toHexString ((pointer_sized_int) (void*) &component) + "m";
  43. }
  44. static const MarkerList::Marker* findMarker (Component& component, const String& name, MarkerList*& list)
  45. {
  46. const MarkerList::Marker* marker = nullptr;
  47. auto* mlh = dynamic_cast<MarkerList::MarkerListHolder*> (&component);
  48. if (mlh != nullptr)
  49. {
  50. list = mlh->getMarkers (true);
  51. if (list != nullptr)
  52. marker = list->getMarker (name);
  53. }
  54. if (marker == nullptr)
  55. {
  56. if (mlh != nullptr)
  57. {
  58. list = mlh->getMarkers (false);
  59. if (list != nullptr)
  60. marker = list->getMarker (name);
  61. }
  62. }
  63. return marker;
  64. }
  65. Component& component;
  66. };
  67. //==============================================================================
  68. RelativeCoordinatePositionerBase::ComponentScope::ComponentScope (Component& comp)
  69. : component (comp)
  70. {
  71. }
  72. Expression RelativeCoordinatePositionerBase::ComponentScope::getSymbolValue (const String& symbol) const
  73. {
  74. switch (RelativeCoordinate::StandardStrings::getTypeOf (symbol))
  75. {
  76. case RelativeCoordinate::StandardStrings::x:
  77. case RelativeCoordinate::StandardStrings::left: return Expression ((double) component.getX());
  78. case RelativeCoordinate::StandardStrings::y:
  79. case RelativeCoordinate::StandardStrings::top: return Expression ((double) component.getY());
  80. case RelativeCoordinate::StandardStrings::width: return Expression ((double) component.getWidth());
  81. case RelativeCoordinate::StandardStrings::height: return Expression ((double) component.getHeight());
  82. case RelativeCoordinate::StandardStrings::right: return Expression ((double) component.getRight());
  83. case RelativeCoordinate::StandardStrings::bottom: return Expression ((double) component.getBottom());
  84. case RelativeCoordinate::StandardStrings::parent:
  85. case RelativeCoordinate::StandardStrings::unknown:
  86. default: break;
  87. }
  88. if (Component* const parent = component.getParentComponent())
  89. {
  90. MarkerList* list;
  91. if (auto* marker = MarkerListScope::findMarker (*parent, symbol, list))
  92. {
  93. MarkerListScope scope (*parent);
  94. return Expression (marker->position.getExpression().evaluate (scope));
  95. }
  96. }
  97. return Expression::Scope::getSymbolValue (symbol);
  98. }
  99. void RelativeCoordinatePositionerBase::ComponentScope::visitRelativeScope (const String& scopeName, Visitor& visitor) const
  100. {
  101. if (auto* targetComp = (scopeName == RelativeCoordinate::Strings::parent)
  102. ? component.getParentComponent()
  103. : findSiblingComponent (scopeName))
  104. visitor.visit (ComponentScope (*targetComp));
  105. else
  106. Expression::Scope::visitRelativeScope (scopeName, visitor);
  107. }
  108. String RelativeCoordinatePositionerBase::ComponentScope::getScopeUID() const
  109. {
  110. return String::toHexString ((pointer_sized_int) (void*) &component);
  111. }
  112. Component* RelativeCoordinatePositionerBase::ComponentScope::findSiblingComponent (const String& componentID) const
  113. {
  114. if (Component* const parent = component.getParentComponent())
  115. return parent->findChildWithID (componentID);
  116. return nullptr;
  117. }
  118. //==============================================================================
  119. class RelativeCoordinatePositionerBase::DependencyFinderScope : public ComponentScope
  120. {
  121. public:
  122. DependencyFinderScope (Component& comp, RelativeCoordinatePositionerBase& p, bool& result)
  123. : ComponentScope (comp), positioner (p), ok (result)
  124. {
  125. }
  126. Expression getSymbolValue (const String& symbol) const override
  127. {
  128. switch (RelativeCoordinate::StandardStrings::getTypeOf (symbol))
  129. {
  130. case RelativeCoordinate::StandardStrings::x:
  131. case RelativeCoordinate::StandardStrings::left:
  132. case RelativeCoordinate::StandardStrings::y:
  133. case RelativeCoordinate::StandardStrings::top:
  134. case RelativeCoordinate::StandardStrings::width:
  135. case RelativeCoordinate::StandardStrings::height:
  136. case RelativeCoordinate::StandardStrings::right:
  137. case RelativeCoordinate::StandardStrings::bottom:
  138. positioner.registerComponentListener (component);
  139. break;
  140. case RelativeCoordinate::StandardStrings::parent:
  141. case RelativeCoordinate::StandardStrings::unknown:
  142. default:
  143. if (auto* parent = component.getParentComponent())
  144. {
  145. MarkerList* list;
  146. if (MarkerListScope::findMarker (*parent, symbol, list) != nullptr)
  147. {
  148. positioner.registerMarkerListListener (list);
  149. }
  150. else
  151. {
  152. // The marker we want doesn't exist, so watch all lists in case they change and the marker appears later..
  153. if (auto* mlh = dynamic_cast<MarkerList::MarkerListHolder*> (parent))
  154. {
  155. positioner.registerMarkerListListener (mlh->getMarkers (true));
  156. positioner.registerMarkerListListener (mlh->getMarkers (false));
  157. }
  158. ok = false;
  159. }
  160. }
  161. break;
  162. }
  163. return ComponentScope::getSymbolValue (symbol);
  164. }
  165. void visitRelativeScope (const String& scopeName, Visitor& visitor) const override
  166. {
  167. if (Component* const targetComp = (scopeName == RelativeCoordinate::Strings::parent)
  168. ? component.getParentComponent()
  169. : findSiblingComponent (scopeName))
  170. {
  171. visitor.visit (DependencyFinderScope (*targetComp, positioner, ok));
  172. }
  173. else
  174. {
  175. // The named component doesn't exist, so we'll watch the parent for changes in case it appears later..
  176. if (Component* const parent = component.getParentComponent())
  177. positioner.registerComponentListener (*parent);
  178. positioner.registerComponentListener (component);
  179. ok = false;
  180. }
  181. }
  182. private:
  183. RelativeCoordinatePositionerBase& positioner;
  184. bool& ok;
  185. };
  186. //==============================================================================
  187. RelativeCoordinatePositionerBase::RelativeCoordinatePositionerBase (Component& comp)
  188. : Component::Positioner (comp), registeredOk (false)
  189. {
  190. }
  191. RelativeCoordinatePositionerBase::~RelativeCoordinatePositionerBase()
  192. {
  193. unregisterListeners();
  194. }
  195. void RelativeCoordinatePositionerBase::componentMovedOrResized (Component&, bool /*wasMoved*/, bool /*wasResized*/)
  196. {
  197. apply();
  198. }
  199. void RelativeCoordinatePositionerBase::componentParentHierarchyChanged (Component&)
  200. {
  201. apply();
  202. }
  203. void RelativeCoordinatePositionerBase::componentChildrenChanged (Component& changed)
  204. {
  205. if (getComponent().getParentComponent() == &changed && ! registeredOk)
  206. apply();
  207. }
  208. void RelativeCoordinatePositionerBase::componentBeingDeleted (Component& comp)
  209. {
  210. jassert (sourceComponents.contains (&comp));
  211. sourceComponents.removeFirstMatchingValue (&comp);
  212. registeredOk = false;
  213. }
  214. void RelativeCoordinatePositionerBase::markersChanged (MarkerList*)
  215. {
  216. apply();
  217. }
  218. void RelativeCoordinatePositionerBase::markerListBeingDeleted (MarkerList* markerList)
  219. {
  220. jassert (sourceMarkerLists.contains (markerList));
  221. sourceMarkerLists.removeFirstMatchingValue (markerList);
  222. }
  223. void RelativeCoordinatePositionerBase::apply()
  224. {
  225. if (! registeredOk)
  226. {
  227. unregisterListeners();
  228. registeredOk = registerCoordinates();
  229. }
  230. applyToComponentBounds();
  231. }
  232. bool RelativeCoordinatePositionerBase::addCoordinate (const RelativeCoordinate& coord)
  233. {
  234. bool ok = true;
  235. DependencyFinderScope finderScope (getComponent(), *this, ok);
  236. coord.getExpression().evaluate (finderScope);
  237. return ok;
  238. }
  239. bool RelativeCoordinatePositionerBase::addPoint (const RelativePoint& point)
  240. {
  241. const bool ok = addCoordinate (point.x);
  242. return addCoordinate (point.y) && ok;
  243. }
  244. void RelativeCoordinatePositionerBase::registerComponentListener (Component& comp)
  245. {
  246. if (! sourceComponents.contains (&comp))
  247. {
  248. comp.addComponentListener (this);
  249. sourceComponents.add (&comp);
  250. }
  251. }
  252. void RelativeCoordinatePositionerBase::registerMarkerListListener (MarkerList* const list)
  253. {
  254. if (list != nullptr && ! sourceMarkerLists.contains (list))
  255. {
  256. list->addListener (this);
  257. sourceMarkerLists.add (list);
  258. }
  259. }
  260. void RelativeCoordinatePositionerBase::unregisterListeners()
  261. {
  262. for (int i = sourceComponents.size(); --i >= 0;)
  263. sourceComponents.getUnchecked(i)->removeComponentListener (this);
  264. for (int i = sourceMarkerLists.size(); --i >= 0;)
  265. sourceMarkerLists.getUnchecked(i)->removeListener (this);
  266. sourceComponents.clear();
  267. sourceMarkerLists.clear();
  268. }
  269. } // namespace juce