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.

juce_ComponentBuilder.cpp 8.5KB

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