commit 71e23f34985eb25e16395bfc90b3700889ba8955
parent 67b001ae1655c6a10b37289ecd8d91447d1479bf
Author: dsp56300 <dsp56300@users.noreply.github.com>
Date: Wed, 28 Aug 2024 00:26:11 +0200
add ability to find components by initial parameter names
Diffstat:
3 files changed, 53 insertions(+), 6 deletions(-)
diff --git a/source/juceUiLib/editor.cpp b/source/juceUiLib/editor.cpp
@@ -160,6 +160,21 @@ namespace genericUI
{
m_componentsByName.insert(std::make_pair(_name, std::vector{_component}));
}
+
+ const auto param = _component->getProperties()["parametername"].toString().toStdString();
+ if(param.empty())
+ return;
+
+ const auto itParamExisting = m_componentsByParameter.find(param);
+
+ if(itParamExisting != m_componentsByParameter.end())
+ {
+ itParamExisting->second.push_back(_component);
+ }
+ else
+ {
+ m_componentsByParameter.insert(std::make_pair(param, std::vector{_component}));
+ }
}
void Editor::registerTabGroup(TabGroup* _group)
@@ -172,15 +187,15 @@ namespace genericUI
m_tabGroupsByName.insert(std::make_pair(n, _group));
}
- const std::vector<juce::Component*>& Editor::findComponents(const std::string& _name, uint32_t _expectedCount/* = 0*/) const
+ const std::vector<juce::Component*>& Editor::findComponents(const std::map<std::string, std::vector<juce::Component*>>& _components, const std::string& _name, const uint32_t _expectedCount, const std::string& _typename)
{
- const auto it = m_componentsByName.find(_name);
- if(it != m_componentsByName.end())
+ const auto it = _components.find(_name);
+ if(it != _components.end())
{
if(_expectedCount && it->second.size() != _expectedCount)
{
std::stringstream ss;
- ss << "Expected to find " << _expectedCount << " components named " << _name << " but found " << it->second.size();
+ ss << "Expected to find " << _expectedCount << " components with " << _typename << ' ' << _name << " but found " << it->second.size();
throw std::runtime_error(ss.str());
}
return it->second;
@@ -189,7 +204,7 @@ namespace genericUI
if(_expectedCount)
{
std::stringstream ss;
- ss << "Unable to find component named " << _name << ", expected to find " << _expectedCount << " components";
+ ss << "Unable to find component with " << _typename << ' ' << _name << ", expected to find " << _expectedCount << " components";
throw std::runtime_error(ss.str());
}
@@ -197,7 +212,17 @@ namespace genericUI
return empty;
}
- juce::Component* Editor::findComponent(const std::string& _name, bool _mustExist/* = true*/) const
+ const std::vector<juce::Component*>& Editor::findComponents(const std::string& _name, const uint32_t _expectedCount/* = 0*/) const
+ {
+ return findComponents(m_componentsByName, _name, _expectedCount, "name");
+ }
+
+ const std::vector<juce::Component*>& Editor::findComponentsByParam(const std::string& _name, const uint32_t _expectedCount) const
+ {
+ return findComponents(m_componentsByParameter, _name, _expectedCount, "parameter");
+ }
+
+ juce::Component* Editor::findComponent(const std::string& _name, const bool _mustExist/* = true*/) const
{
const auto comps = findComponents(_name);
if(comps.size() > 1)
@@ -207,6 +232,16 @@ namespace genericUI
return comps.empty() ? nullptr : comps.front();
}
+ juce::Component* Editor::findComponentByParam(const std::string& _param, const bool _mustExist) const
+ {
+ const auto comps = findComponentsByParam(_param);
+ if(comps.size() > 1)
+ throw std::runtime_error("Failed to find unique component with parameter " + _param + ", found more than one object with that parameter");
+ if(_mustExist && comps.empty())
+ throw std::runtime_error("Failed to find component with parameter " + _param);
+ return comps.empty() ? nullptr : comps.front();
+ }
+
size_t Editor::getConditionCountRecursive() const
{
return m_rootObject ? m_rootObject->getConditionCountRecursive() : 0;
diff --git a/source/juceUiLib/editor.h b/source/juceUiLib/editor.h
@@ -33,7 +33,9 @@ namespace genericUI
EditorInterface& getInterface() const { return m_interface; }
+ static const std::vector<juce::Component*>& findComponents(const std::map<std::string, std::vector<juce::Component*>>& _components, const std::string& _name, uint32_t _expectedCount, const std::string& _typename);
const std::vector<juce::Component*>& findComponents(const std::string& _name, uint32_t _expectedCount = 0) const;
+ const std::vector<juce::Component*>& findComponentsByParam(const std::string& _name, uint32_t _expectedCount = 0) const;
template<typename T>
void findComponents(std::vector<T*>& _dst, const std::string& _name, uint32_t _expectedCount = 0) const
@@ -48,6 +50,7 @@ namespace genericUI
}
juce::Component* findComponent(const std::string& _name, bool _mustExist = true) const;
+ juce::Component* findComponentByParam(const std::string& _param, bool _mustExist = true) const;
template<typename T>
T* findComponentT(const std::string& _name, bool _mustExist = true) const
@@ -56,6 +59,13 @@ namespace genericUI
return dynamic_cast<T*>(c);
}
+ template<typename T>
+ T* findComponentByParamT(const std::string& _name, const bool _mustExist = true) const
+ {
+ juce::Component* c = findComponentByParam(_name, _mustExist);
+ return dynamic_cast<T*>(c);
+ }
+
float getScale() const { return m_scale; }
TabGroup* findTabGroup(const std::string& _name, bool _mustExist = true)
@@ -114,6 +124,7 @@ namespace genericUI
std::unique_ptr<UiObject> m_rootObject;
std::map<std::string, std::vector<juce::Component*>> m_componentsByName;
+ std::map<std::string, std::vector<juce::Component*>> m_componentsByParameter;
std::map<std::string, TabGroup*> m_tabGroupsByName;
std::map<std::string, std::shared_ptr<UiObject>> m_templates;
diff --git a/source/juceUiLib/uiObject.cpp b/source/juceUiLib/uiObject.cpp
@@ -591,6 +591,7 @@ namespace genericUI
throw std::runtime_error("Parameter named " + param + " not found");
_target.getProperties().set("parameter", index);
+ _target.getProperties().set("parametername", juce::String(param));
if constexpr(std::is_base_of_v<juce::Button, T>)
{