Hi there,
I have a problem with some Components and the paint function. I’ll try to explain it easily:
I have a big component (TestComposite) that contains an Array of components (Test).
Each Test component contains 3 components inside:
SideBar* leftSideBar;
MiddleBar* middleBar;
SideBar* rightSideBar;
I have a Value::Listener that when it detects a ValueChanged it redraws the MiddleBar. I want it to
repaint ONLY the middlebar because to redraw each sidebar is a hard proccess.
The problem is that if my big component (TestComposite) has more than one Test component, although I only call
middleBar->repaint(); it repaints the sidebars too.
Is weird because on the first Test it doesn’t repaint the leftSideBar,
and in the last Test it doesn’t repaint the rightSideBar
I include the code I’m trying and some screenshots to help understanding.
Is really important for me to avoid the sidebars redrawing each time cause as I said is a hard proccess.
/////////////////////////////////////////////////////////////////////////////////////////////////////
// My example classes
class SideBar: public Component
{
public:
SideBar() {
setOpaque(true);
setSize(5,20);
}
virtual void paint(Graphics& g) {
printf("\n\tSideBar::paint getWidth() %d getHeight() %d", getWidth(), getHeight());
g.fillAll(Colours::red);
}
};
class MiddleBar: public Component//, public Value::Listener
{
public:
MiddleBar() {
setOpaque(true);
setSize(10,20);
}
virtual void paint(Graphics& g) {
printf("\n\tMiddleBar::paint %d getHeight() %d", getWidth(), getHeight());
g.fillAll(Colours::blue);
}
/*
virtual void valueChanged (Value &value)
{
printf("\n\t\tMiddleBar::valueChanged");
repaint();
}
*/
};
class Test: public Component, public Value::Listener
{
private:
SideBar* leftSideBar;
MiddleBar* middleBar;
SideBar* rightSideBar;
Value value;
public:
Test(Value & val) : value(val) {
setOpaque(true);
addAndMakeVisible(leftSideBar = new SideBar());
addAndMakeVisible(middleBar = new MiddleBar());
addAndMakeVisible(rightSideBar = new SideBar());
int offset = 10;
leftSideBar->setBounds(offset, offset, leftSideBar->getWidth(), leftSideBar->getHeight());
middleBar->setBounds(leftSideBar->getWidth() + offset*2, offset, middleBar->getWidth(), middleBar->getHeight());
rightSideBar->setBounds(leftSideBar->getWidth() + offset*3 + middleBar->getWidth() + offset, offset, rightSideBar->getWidth(), rightSideBar->getHeight());
setSize(100, 100);
value.addListener(this);
}
virtual void paint(Graphics& g) {
printf("\nTest::paint");
g.fillAll(Colours::green);
}
virtual void valueChanged (Value &value)
{
printf("\n\t\tTest::valueChanged");
middleBar->repaint();
}
};
class TestComposite: public Component
{
private:
Array<Test*> components;
public:
TestComposite() {
setOpaque(true);
setSize(400, 200);
}
void add(Test *test)
{
components.add(test);
addAndMakeVisible(test);
if (components.size() > 0)
{
setSize((components[0]->getWidth()+20)*(components.size()+1), components[0]->getHeight()+20*2);
}
}
virtual void resized()
{
printf("\nTestComposite::resized %d getHeight() %d", getWidth(), getHeight());
int xOffset = 20;
for (int i=0; i<components.size(); i++)
{
components[i]->setBounds(xOffset, 20, components[i]->getWidth(), components[i]->getHeight());
xOffset += components[i]->getWidth()+20;
}
}
virtual void paint(Graphics& g) {
printf("\nTestComposite::paint");
g.fillAll(Colours::yellow);
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////
// how I create the components in the juce app
Value values[4];
....
addAndMakeVisible(mTestComposite = new TestComposite());
mTestComposite->setBounds(0,0, 200, 200);
Test* test0 = new Test(values[0]);
mTestComposite->add(test0);
Test* test1 = new Test(values[1]);
mTestComposite->add(test1);
Test* test2 = new Test(values[2]);
mTestComposite->add(test2);
Test* test3 = new Test(values[3]);
mTestComposite->add(test3);
////////////////////////////////////////////////////////////////////////////////////////////////////
// How I update the values on a timer callback
void nSMonitorSectionContentComponent::timerCallback()
{
printf("\n\n\n--------------------------------------\nSMonitorSectionContentComponent::timerCallback\n");
for(int i = 0; i < 4; i++)
{
int n = mRand.nextInt(100);
if (n%2) values[i].setValue(((float)values[i].getValue())+1);
else values[i].setValue(((float)values[i].getValue())-1);
}
}
The TestComposite is the yellow bit
Each Test component is the green bits
The SideBars are red
The MiddleBar are blue
I only want to update the blue bits :’( but you can see the output in the image…
Thanks
A