Maybe someone can spot something I can’t. When I declare a lone instance ,call repaint and and call classinstance.setBounds(getLocalBounds()) in resize is appears fine. This is not the case now Im reading from a vector of class instances.
Heres my code, with the Spheres and pluginEditor .h / .cpp
Spheres. h
class Spheres : public juce::Component
{
public:
Spheres();
~Spheres() override;
void setPosition(float x_, float y_);
void paint (juce::Graphics&) override;
void resized() override;
private:
float x;
float y;
int sphereRadius { 10 };
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Spheres)
};
Spheres.cpp
Spheres::Spheres()
{
x = 0;
y = 0;
}
Spheres::~Spheres()
{
}
void Spheres::setPosition(float x_, float y_)
{
x = x_;
y = y_;
}
void Spheres::paint (juce::Graphics& g)
{
//the spheres should be drawing to the middle of the screen.
g.setOrigin(getWidth() / 2 - 10, getHeight() / 2 - 10);
g.setColour(juce::Colours::red);
g.drawEllipse(x, -y, sphereRadius * 2, sphereRadius * 2, 2);
}
void Spheres::resized()
{
}
pluginEditor.h
class Mandelbrot_pluginAudioProcessorEditor : public juce::AudioProcessorEditor
,public juce::Timer
,public juce::Slider::Listener
{
public:
Mandelbrot_pluginAudioProcessorEditor (Mandelbrot_pluginAudioProcessor&);
~Mandelbrot_pluginAudioProcessorEditor() override;
int borderRadius = 180;
float t { 0.0 }; //==============================================================================
void paint (juce::Graphics&) override;
void resized() override;
void timerCallback() override;
void sliderValueChanged(juce::Slider* slider) override;
private:
juce::Slider xPos_Slider;
juce::Slider yPos_Slider;
std::vector<std::unique_ptr<Spheres>> sphereArray;
// This reference is provided as a quick way for your editor to
// access the processor object that created it.
Mandelbrot_pluginAudioProcessor& audioProcessor;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Mandelbrot_pluginAudioProcessorEditor)
};
pluginEditor.cpp
Mandelbrot_pluginAudioProcessorEditor::Mandelbrot_pluginAudioProcessorEditor (Mandelbrot_pluginAudioProcessor& p)
: AudioProcessorEditor (&p), audioProcessor (p)
{
setSize (600, 400);
for (auto i = 0; i < 4; i++)
{
sphereArray.push_back(std::make_unique<Spheres>());
addAndMakeVisible(*sphereArray.back());
}
//add sliders, set range, value ect..
xPos_Slider.setSliderStyle(juce::Slider::SliderStyle::LinearVertical);
xPos_Slider.setRange(-1.0f, 1.0f, 0.01f);
xPos_Slider.setValue(0.0f);
xPos_Slider.addListener(this);
addAndMakeVisible(xPos_Slider);
//ypos slider
yPos_Slider.setSliderStyle(juce::Slider::SliderStyle::LinearVertical);
yPos_Slider.setRange(-1.0f, 1.0f, 0.001f);
yPos_Slider.setValue(0.0f);
yPos_Slider.addListener(this);
addAndMakeVisible(yPos_Slider);
// start timer to create a loop for animation
Timer::startTimer(60);
}
Mandelbrot_pluginAudioProcessorEditor::~Mandelbrot_pluginAudioProcessorEditor()
{
Timer::stopTimer();
}
//==============================================================================
void Mandelbrot_pluginAudioProcessorEditor::paint (juce::Graphics& g)
{
// drawing big circle with rotating arm
g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
g.setOrigin(getWidth() / 2, getHeight() / 2);
g.drawEllipse(0 - borderRadius, 0 - borderRadius, borderRadius * 2, borderRadius * 2, 2);
g.drawLine(0, 0, borderRadius * cos(t), borderRadius * sin(t), 2);
}
void Mandelbrot_pluginAudioProcessorEditor::timerCallback()
{
repaint();
for (auto& sphere : sphereArray )
{
sphere->repaint();
sphere->setPosition(xPos_Slider.getValue(), yPos_Slider.getValue());
}
t += 0.01;
}
void Mandelbrot_pluginAudioProcessorEditor::sliderValueChanged(juce::Slider *slider)
{
if (slider == & xPos_Slider)
{
}
else if (slider == & yPos_Slider)
{
}
}
void Mandelbrot_pluginAudioProcessorEditor::resized()
{
// set positions
for (auto& sphere : sphereArray)
{
sphere->setBounds (getLocalBounds());
}
xPos_Slider.setBounds(getWidth() / 12, getHeight() / 2 - 100, 20, 200);
yPos_Slider.setBounds(getWidth() / 12 + 50, getHeight() / 2 - 100, 20, 200);
}
Im wondering if anyone can spot a reason why the four Spheres class instances are not being rendered?
Thanks for your help so far!