commit 48b5bbbf0722ffb873368e6f0109d249009c17be
parent 52b9f5e49341b8eedb447507435207c1a07bb2a5
Author: dsp56300 <dsp56300@users.noreply.github.com>
Date: Mon, 14 Mar 2022 21:34:37 +0100
register named components at editor main class / streamline error handling
Diffstat:
4 files changed, 68 insertions(+), 15 deletions(-)
diff --git a/source/jucePlugin/PluginEditor.cpp b/source/jucePlugin/PluginEditor.cpp
@@ -77,8 +77,16 @@ void AudioPluginAudioProcessorEditor::LoadSkin(int index) {
}
else if(index == 2)
{
- m_virusEditor.reset(new genericUI::Editor(std::string(BinaryData::VirusC_json), m_parameterBinding, processorRef.getController()));
- setSize(m_virusEditor->getWidth(), m_virusEditor->getHeight());
+ try
+ {
+ m_virusEditor.reset(new genericUI::Editor(std::string(BinaryData::VirusC_json), m_parameterBinding, processorRef.getController()));
+ setSize(m_virusEditor->getWidth(), m_virusEditor->getHeight());
+ }
+ catch(const std::runtime_error& _err)
+ {
+ LOG("ERROR: Failed to create editor: " << _err.what());
+ return;
+ }
}
else {
m_virusEditor.reset(new VirusEditor(m_parameterBinding, processorRef));
diff --git a/source/jucePlugin/genericUI/editor.cpp b/source/jucePlugin/genericUI/editor.cpp
@@ -27,7 +27,7 @@ namespace genericUI
int dataSize;
const auto* data = getResourceByFilename(dataName, dataSize);
if (!data)
- throw std::runtime_error("Failed to find data named " + dataName);
+ throw std::runtime_error("Failed to find image named " + dataName);
auto drawable = juce::Drawable::createFromImageData(data, dataSize);
m_drawables.insert(std::make_pair(texture, std::move(drawable)));
}
@@ -47,6 +47,38 @@ namespace genericUI
return existing ? existing->createCopy() : nullptr;
}
+ void Editor::registerComponent(const std::string& _name, juce::Component* _component)
+ {
+ const auto itExisting = m_componentsByName.find(_name);
+
+ if(itExisting != m_componentsByName.end())
+ {
+ itExisting->second.push_back(_component);
+ }
+ else
+ {
+ m_componentsByName.insert(std::make_pair(_name, std::vector{_component}));
+ }
+ }
+
+ const std::vector<juce::Component*>& Editor::findComponents(const std::string& _name) const
+ {
+ const auto it = m_componentsByName.find(_name);
+ if(it != m_componentsByName.end())
+ return it->second;
+
+ static std::vector<juce::Component*> empty;
+ return empty;
+ }
+
+ juce::Component* Editor::findComponent(const std::string& _name) const
+ {
+ const auto comps = findComponents(_name);
+ if(comps.size() > 1)
+ throw std::runtime_error("Failed to find unique component named " + _name + ", found more than one object with that name");
+ return comps.empty() ? nullptr : comps.front();
+ }
+
const char* Editor::getResourceByFilename(const std::string& _filename, int& _outDataSize)
{
for(size_t i=0; i<BinaryData::namedResourceListSize; ++i)
diff --git a/source/jucePlugin/genericUI/editor.h b/source/jucePlugin/genericUI/editor.h
@@ -26,6 +26,11 @@ namespace genericUI
VirusParameterBinding& getParameterBinding() const { return m_parameterBinding; }
Virus::Controller& getController() const { return m_controller; }
+ void registerComponent(const std::string& _name, juce::Component* _component);
+
+ const std::vector<juce::Component*>& findComponents(const std::string& _name) const;
+ juce::Component* findComponent(const std::string& _name) const;
+
private:
static const char* getResourceByFilename(const std::string& _filename, int& _outDataSize);
@@ -34,5 +39,7 @@ namespace genericUI
VirusParameterBinding& m_parameterBinding;
Virus::Controller& m_controller;
+
+ std::map<std::string, std::vector<juce::Component*>> m_componentsByName;
};
}
diff --git a/source/jucePlugin/genericUI/uiObject.cpp b/source/jucePlugin/genericUI/uiObject.cpp
@@ -1,7 +1,5 @@
#include "uiObject.h"
-#include <cassert>
-
#include <juce_audio_processors/juce_audio_processors.h>
#include <utility>
@@ -61,7 +59,12 @@ namespace genericUI
const auto w = getPropertyInt("width");
const auto h = getPropertyInt("height");
- assert(w > 0 && h > 0);
+ if(w < 1 || h < 1)
+ {
+ std::stringstream ss;
+ ss << "Size " << w << "x" << h << " for object named " << m_name << " is invalid, each side must be > 0";
+ throw std::runtime_error(ss.str());
+ }
_target.setTopLeftPosition(x, y);
_target.setSize(w, h);
@@ -126,11 +129,7 @@ namespace genericUI
{
m_juceObjects.clear();
- if(hasComponent("root"))
- {
- createJuceObject<juce::Component>(_editor);
- }
- else if(hasComponent("rotary"))
+ if(hasComponent("rotary"))
{
createJuceObject<juce::Slider>(_editor);
}
@@ -159,13 +158,13 @@ namespace genericUI
{
createJuceObject<juce::Label>(_editor);
}
- else if(hasComponent("component"))
+ else if(hasComponent("root") || hasComponent("component"))
{
createJuceObject<juce::Component>(_editor);
}
else
{
- assert(false && "unknown object type");
+ throw std::runtime_error("Failed to determine object type for object named " + m_name);
}
return m_juceObjects.empty() ? nullptr : m_juceObjects.front().get();
@@ -251,8 +250,13 @@ namespace genericUI
{
auto c = std::make_unique<T>();
apply(_editor, *c);
+ auto* comp = c.get();
m_juceObjects.emplace_back(std::move(c));
- return c.get();
+
+ if(!m_name.empty())
+ _editor.registerComponent(m_name, comp);
+
+ return comp;
}
template<typename T>
@@ -264,7 +268,9 @@ namespace genericUI
return;
const auto index = _editor.getController().getParameterTypeByName(param);
- assert(index >= 0 && "parameter type not found");
+
+ if(index == Virus::Param_Invalid)
+ throw std::runtime_error("Parameter named " + param + " not found");
auto& binding = _editor.getParameterBinding();