29namespace ComponentBuilderHelpers
31 static String getStateId (
const ValueTree& state)
40 for (
int i = components.size(); --i >= 0;)
42 Component*
const c = components.getUnchecked (i);
44 if (c->getComponentID() ==
compId)
45 return components.removeAndReturn (i);
51 static Component* findComponentWithID (Component& c,
const String&
compId)
54 if (c.getComponentID() ==
compId)
57 for (
auto* child : c.getChildren())
64 static Component* createNewComponent (ComponentBuilder::TypeHandler& type,
65 const ValueTree& state, Component* parent)
67 Component*
const c = type.addNewComponentFromState (state, parent);
68 jassert (c !=
nullptr && c->getParentComponent() == parent);
69 c->setComponentID (getStateId (state));
73 static void updateComponent (ComponentBuilder& builder,
const ValueTree& state)
75 if (Component*
topLevelComp = builder.getManagedComponent())
77 ComponentBuilder::TypeHandler*
const type = builder.getHandlerForState (state);
78 const String uid (getStateId (state));
80 if (type ==
nullptr || uid.isEmpty())
83 if (state.getParent().isValid())
84 updateComponent (builder, state.getParent());
89 type->updateComponentFromState (
changedComp, state);
122 if (component ==
nullptr)
131 return component.get();
139 return ComponentBuilderHelpers::createNewComponent (*type,
state,
nullptr);
151 jassert (type->builder ==
nullptr);
154 type->builder =
this;
161 for (
int i = 0; i < types.size(); ++i)
179 return types [index];
193 return imageProvider;
198 ComponentBuilderHelpers::updateComponent (*
this, tree);
201void ComponentBuilder::valueTreeChildAdded (ValueTree& tree, ValueTree&)
203 ComponentBuilderHelpers::updateComponent (*
this, tree);
206void ComponentBuilder::valueTreeChildRemoved (ValueTree& tree, ValueTree&,
int)
208 ComponentBuilderHelpers::updateComponent (*
this, tree);
211void ComponentBuilder::valueTreeChildOrderChanged (ValueTree& tree,
int,
int)
213 ComponentBuilderHelpers::updateComponent (*
this, tree);
216void ComponentBuilder::valueTreeParentChanged (ValueTree& tree)
218 ComponentBuilderHelpers::updateComponent (*
this, tree);
240 using namespace ComponentBuilderHelpers;
264 c = ComponentBuilderHelpers::createNewComponent (*type,
childState, &parent);
Holds a resizable array of primitive or copy-by-value objects.
void ensureStorageAllocated(int minNumElements)
Increases the array's internal storage to hold a minimum number of elements.
This class is used when references to images need to be stored in ValueTrees.
The class is a base class for objects that manage the loading of a type of component from a ValueTree...
const Identifier type
Returns the type of the ValueTrees that this handler can parse.
ComponentBuilder * getBuilder() const noexcept
Returns the builder that this type is registered with.
virtual ~TypeHandler()
Destructor.
TypeHandler(const Identifier &valueTreeType)
Creates a TypeHandler.
Loads and maintains a tree of Components from a ValueTree that represents them.
int getNumHandlers() const noexcept
Returns the number of registered type handlers.
ComponentBuilder()
Creates a builder that doesn't have a state object.
ValueTree state
This is the ValueTree data object that the builder is working with.
void registerStandardComponentTypes()
Registers handlers for various standard juce components.
ImageProvider * getImageProvider() const noexcept
Returns the current image provider that this builder is using, or nullptr if none has been set.
void updateChildComponents(Component &parent, const ValueTree &children)
Updates the children of a parent component by updating them from the children of a given ValueTree.
void setImageProvider(ImageProvider *newImageProvider) noexcept
Gives the builder an ImageProvider object that the type handlers can use when loading images from sto...
TypeHandler * getHandler(int index) const noexcept
Returns one of the registered type handlers.
static const Identifier idProperty
An identifier for the property of the ValueTrees that is used to store a unique ID for that component...
~ComponentBuilder() override
Destructor.
TypeHandler * getHandlerForState(const ValueTree &state) const
Tries to find a registered type handler that can load a component from the given ValueTree.
Component * createComponent()
Creates and returns a new instance of the component that the ValueTree represents.
Component * getManagedComponent()
Returns the builder's component (creating it if necessary).
void registerTypeHandler(TypeHandler *type)
Adds a type handler that the builder can use when trying to load components.
The base class for all JUCE user-interface objects.
int getNumChildComponents() const noexcept
Returns the number of child components that this component contains.
Component * getChildComponent(int index) const noexcept
Returns one of this component's child components, by it index.
Represents a string identifier, designed for accessing properties by name.
An array designed for holding objects.
void ensureStorageAllocated(int minNumElements) noexcept
Increases the array's internal storage to hold a minimum number of elements.
A powerful tree structure that can be used to hold free-form data, and which can handle its own undo ...
ValueTree getChild(int index) const
Returns one of this tree's sub-trees.
int getNumChildren() const noexcept
Returns the number of child trees inside this one.
void addListener(Listener *listener)
Adds a listener to receive callbacks when this tree is changed in some way.
Identifier getType() const noexcept
Returns the type of this tree.
void removeListener(Listener *listener)
Removes a listener that was previously added with addListener().
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...