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.

280 lines
8.2KB

  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. namespace ComponentBuilderHelpers
  16. {
  17. static String getStateId (const ValueTree& state)
  18. {
  19. return state [ComponentBuilder::idProperty].toString();
  20. }
  21. static Component* removeComponentWithID (OwnedArray<Component>& components, const String& compId)
  22. {
  23. jassert (compId.isNotEmpty());
  24. for (int i = components.size(); --i >= 0;)
  25. {
  26. Component* const c = components.getUnchecked (i);
  27. if (c->getComponentID() == compId)
  28. return components.removeAndReturn (i);
  29. }
  30. return nullptr;
  31. }
  32. static Component* findComponentWithID (Component& c, const String& compId)
  33. {
  34. jassert (compId.isNotEmpty());
  35. if (c.getComponentID() == compId)
  36. return &c;
  37. for (auto* child : c.getChildren())
  38. if (auto* found = findComponentWithID (*child, compId))
  39. return found;
  40. return nullptr;
  41. }
  42. static Component* createNewComponent (ComponentBuilder::TypeHandler& type,
  43. const ValueTree& state, Component* parent)
  44. {
  45. Component* const c = type.addNewComponentFromState (state, parent);
  46. jassert (c != nullptr && c->getParentComponent() == parent);
  47. c->setComponentID (getStateId (state));
  48. return c;
  49. }
  50. static void updateComponent (ComponentBuilder& builder, const ValueTree& state)
  51. {
  52. if (Component* topLevelComp = builder.getManagedComponent())
  53. {
  54. ComponentBuilder::TypeHandler* const type = builder.getHandlerForState (state);
  55. const String uid (getStateId (state));
  56. if (type == nullptr || uid.isEmpty())
  57. {
  58. // ..handle the case where a child of the actual state node has changed.
  59. if (state.getParent().isValid())
  60. updateComponent (builder, state.getParent());
  61. }
  62. else
  63. {
  64. if (Component* const changedComp = findComponentWithID (*topLevelComp, uid))
  65. type->updateComponentFromState (changedComp, state);
  66. }
  67. }
  68. }
  69. }
  70. //==============================================================================
  71. const Identifier ComponentBuilder::idProperty ("id");
  72. ComponentBuilder::ComponentBuilder()
  73. : imageProvider (nullptr)
  74. {
  75. }
  76. ComponentBuilder::ComponentBuilder (const ValueTree& state_)
  77. : state (state_), imageProvider (nullptr)
  78. {
  79. state.addListener (this);
  80. }
  81. ComponentBuilder::~ComponentBuilder()
  82. {
  83. state.removeListener (this);
  84. #if JUCE_DEBUG
  85. // Don't delete the managed component!! The builder owns that component, and will delete
  86. // it automatically when it gets deleted.
  87. jassert (componentRef.get() == component.get());
  88. #endif
  89. }
  90. Component* ComponentBuilder::getManagedComponent()
  91. {
  92. if (component == nullptr)
  93. {
  94. component.reset (createComponent());
  95. #if JUCE_DEBUG
  96. componentRef = component.get();
  97. #endif
  98. }
  99. return component.get();
  100. }
  101. Component* ComponentBuilder::createComponent()
  102. {
  103. jassert (types.size() > 0); // You need to register all the necessary types before you can load a component!
  104. if (TypeHandler* const type = getHandlerForState (state))
  105. return ComponentBuilderHelpers::createNewComponent (*type, state, nullptr);
  106. jassertfalse; // trying to create a component from an unknown type of ValueTree
  107. return nullptr;
  108. }
  109. void ComponentBuilder::registerTypeHandler (ComponentBuilder::TypeHandler* const type)
  110. {
  111. jassert (type != nullptr);
  112. // Don't try to move your types around! Once a type has been added to a builder, the
  113. // builder owns it, and you should leave it alone!
  114. jassert (type->builder == nullptr);
  115. types.add (type);
  116. type->builder = this;
  117. }
  118. ComponentBuilder::TypeHandler* ComponentBuilder::getHandlerForState (const ValueTree& s) const
  119. {
  120. const Identifier targetType (s.getType());
  121. for (int i = 0; i < types.size(); ++i)
  122. {
  123. TypeHandler* const t = types.getUnchecked(i);
  124. if (t->type == targetType)
  125. return t;
  126. }
  127. return nullptr;
  128. }
  129. int ComponentBuilder::getNumHandlers() const noexcept
  130. {
  131. return types.size();
  132. }
  133. ComponentBuilder::TypeHandler* ComponentBuilder::getHandler (const int index) const noexcept
  134. {
  135. return types [index];
  136. }
  137. void ComponentBuilder::registerStandardComponentTypes()
  138. {
  139. }
  140. void ComponentBuilder::setImageProvider (ImageProvider* newImageProvider) noexcept
  141. {
  142. imageProvider = newImageProvider;
  143. }
  144. ComponentBuilder::ImageProvider* ComponentBuilder::getImageProvider() const noexcept
  145. {
  146. return imageProvider;
  147. }
  148. void ComponentBuilder::valueTreePropertyChanged (ValueTree& tree, const Identifier&)
  149. {
  150. ComponentBuilderHelpers::updateComponent (*this, tree);
  151. }
  152. void ComponentBuilder::valueTreeChildAdded (ValueTree& tree, ValueTree&)
  153. {
  154. ComponentBuilderHelpers::updateComponent (*this, tree);
  155. }
  156. void ComponentBuilder::valueTreeChildRemoved (ValueTree& tree, ValueTree&, int)
  157. {
  158. ComponentBuilderHelpers::updateComponent (*this, tree);
  159. }
  160. void ComponentBuilder::valueTreeChildOrderChanged (ValueTree& tree, int, int)
  161. {
  162. ComponentBuilderHelpers::updateComponent (*this, tree);
  163. }
  164. void ComponentBuilder::valueTreeParentChanged (ValueTree& tree)
  165. {
  166. ComponentBuilderHelpers::updateComponent (*this, tree);
  167. }
  168. //==============================================================================
  169. ComponentBuilder::TypeHandler::TypeHandler (const Identifier& valueTreeType)
  170. : type (valueTreeType), builder (nullptr)
  171. {
  172. }
  173. ComponentBuilder::TypeHandler::~TypeHandler()
  174. {
  175. }
  176. ComponentBuilder* ComponentBuilder::TypeHandler::getBuilder() const noexcept
  177. {
  178. // A type handler needs to be registered with a ComponentBuilder before using it!
  179. jassert (builder != nullptr);
  180. return builder;
  181. }
  182. void ComponentBuilder::updateChildComponents (Component& parent, const ValueTree& children)
  183. {
  184. using namespace ComponentBuilderHelpers;
  185. auto numExistingChildComps = parent.getNumChildComponents();
  186. Array<Component*> componentsInOrder;
  187. componentsInOrder.ensureStorageAllocated (numExistingChildComps);
  188. {
  189. OwnedArray<Component> existingComponents;
  190. existingComponents.ensureStorageAllocated (numExistingChildComps);
  191. for (int i = 0; i < numExistingChildComps; ++i)
  192. existingComponents.add (parent.getChildComponent (i));
  193. auto newNumChildren = children.getNumChildren();
  194. for (int i = 0; i < newNumChildren; ++i)
  195. {
  196. auto childState = children.getChild (i);
  197. auto* c = removeComponentWithID (existingComponents, getStateId (childState));
  198. if (c == nullptr)
  199. {
  200. if (auto* type = getHandlerForState (childState))
  201. c = ComponentBuilderHelpers::createNewComponent (*type, childState, &parent);
  202. else
  203. jassertfalse;
  204. }
  205. if (c != nullptr)
  206. componentsInOrder.add (c);
  207. }
  208. // (remaining unused items in existingComponents get deleted here as it goes out of scope)
  209. }
  210. // Make sure the z-order is correct..
  211. if (componentsInOrder.size() > 0)
  212. {
  213. componentsInOrder.getLast()->toFront (false);
  214. for (int i = componentsInOrder.size() - 1; --i >= 0;)
  215. componentsInOrder.getUnchecked(i)->toBehind (componentsInOrder.getUnchecked (i + 1));
  216. }
  217. }
  218. } // namespace juce