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

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