juce_ComponentBuilder.cpp 8.5 KB

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() == component.get());
  94. #endif
  95. }
  96. Component* ComponentBuilder::getManagedComponent()
  97. {
  98. if (component == nullptr)
  99. {
  100. component.reset (createComponent());
  101. #if JUCE_DEBUG
  102. componentRef = component.get();
  103. #endif
  104. }
  105. return component.get();
  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. }
  146. void ComponentBuilder::setImageProvider (ImageProvider* newImageProvider) noexcept
  147. {
  148. imageProvider = newImageProvider;
  149. }
  150. ComponentBuilder::ImageProvider* ComponentBuilder::getImageProvider() const noexcept
  151. {
  152. return imageProvider;
  153. }
  154. void ComponentBuilder::valueTreePropertyChanged (ValueTree& tree, const Identifier&)
  155. {
  156. ComponentBuilderHelpers::updateComponent (*this, tree);
  157. }
  158. void ComponentBuilder::valueTreeChildAdded (ValueTree& tree, ValueTree&)
  159. {
  160. ComponentBuilderHelpers::updateComponent (*this, tree);
  161. }
  162. void ComponentBuilder::valueTreeChildRemoved (ValueTree& tree, ValueTree&, int)
  163. {
  164. ComponentBuilderHelpers::updateComponent (*this, tree);
  165. }
  166. void ComponentBuilder::valueTreeChildOrderChanged (ValueTree& tree, int, int)
  167. {
  168. ComponentBuilderHelpers::updateComponent (*this, tree);
  169. }
  170. void ComponentBuilder::valueTreeParentChanged (ValueTree& tree)
  171. {
  172. ComponentBuilderHelpers::updateComponent (*this, tree);
  173. }
  174. //==============================================================================
  175. ComponentBuilder::TypeHandler::TypeHandler (const Identifier& valueTreeType)
  176. : type (valueTreeType), builder (nullptr)
  177. {
  178. }
  179. ComponentBuilder::TypeHandler::~TypeHandler()
  180. {
  181. }
  182. ComponentBuilder* ComponentBuilder::TypeHandler::getBuilder() const noexcept
  183. {
  184. // A type handler needs to be registered with a ComponentBuilder before using it!
  185. jassert (builder != nullptr);
  186. return builder;
  187. }
  188. void ComponentBuilder::updateChildComponents (Component& parent, const ValueTree& children)
  189. {
  190. using namespace ComponentBuilderHelpers;
  191. auto numExistingChildComps = parent.getNumChildComponents();
  192. Array<Component*> componentsInOrder;
  193. componentsInOrder.ensureStorageAllocated (numExistingChildComps);
  194. {
  195. OwnedArray<Component> existingComponents;
  196. existingComponents.ensureStorageAllocated (numExistingChildComps);
  197. for (int i = 0; i < numExistingChildComps; ++i)
  198. existingComponents.add (parent.getChildComponent (i));
  199. auto newNumChildren = children.getNumChildren();
  200. for (int i = 0; i < newNumChildren; ++i)
  201. {
  202. auto childState = children.getChild (i);
  203. auto* c = removeComponentWithID (existingComponents, getStateId (childState));
  204. if (c == nullptr)
  205. {
  206. if (auto* type = getHandlerForState (childState))
  207. c = ComponentBuilderHelpers::createNewComponent (*type, childState, &parent);
  208. else
  209. jassertfalse;
  210. }
  211. if (c != nullptr)
  212. componentsInOrder.add (c);
  213. }
  214. // (remaining unused items in existingComponents get deleted here as it goes out of scope)
  215. }
  216. // Make sure the z-order is correct..
  217. if (componentsInOrder.size() > 0)
  218. {
  219. componentsInOrder.getLast()->toFront (false);
  220. for (int i = componentsInOrder.size() - 1; --i >= 0;)
  221. componentsInOrder.getUnchecked(i)->toBehind (componentsInOrder.getUnchecked (i + 1));
  222. }
  223. }
  224. } // namespace juce