Hi everyone,
I’m having trouble getting the alpha of ListBoxes to work properly.
It looks like the alpha is ignored or not handled correctly.
Here is some example code with a slider that changes the alpha of the main component, I expect that it should change the alpha of the listbox, but for some reason it doesn’t.
Hopefully someone can tell me what’s wrong in my code… or is it a bug?
// A component with children at different levels to test nested alpha
struct NestedRects : juce::Component
{
NestedRects(int numNested)
{
if (numNested > 0)
{
nested_ = std::make_unique<NestedRects>(numNested - 1);
addAndMakeVisible(*nested_);
}
}
void paint(juce::Graphics& g) override
{
g.fillAll(colour_);
}
void resized() override
{
if (nested_)
{
nested_->setBounds(getLocalBounds().reduced(20));
}
}
juce::Colour colour_ = juce::Colour::fromRGB(juce::Random().nextInt(255),
juce::Random().nextInt(255),
juce::Random().nextInt(255));
std::unique_ptr<NestedRects> nested_;
};
class MainComponent : public juce::Component
{
public:
//==============================================================================
MainComponent()
{
addAndMakeVisible(listBox_);
listBox_.updateContent();
addAndMakeVisible(nestedRects_);
addAndMakeVisible(alphaSlider_);
alphaSlider_.setRange(0.0, 1.0);
alphaSlider_.onValueChange = [&]
{
setAlpha(alphaSlider_.getValue());
};
setSize (600, 400);
}
~MainComponent() override {}
//==============================================================================
void paint (juce::Graphics& g) override
{
g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
}
void resized() override
{
listBox_.setBounds(10, 10, 100, 400);
nestedRects_.setBounds(150, 75, 300, 300);
alphaSlider_.setBounds(150, 10, 200, 40);
}
private:
//==============================================================================
struct : juce::ListBoxModel
{
int getNumRows() override
{
return 10;
}
void paintListBoxItem (int rowNumber, juce::Graphics &g, int width, int height, bool rowIsSelected) override
{
g.fillAll(juce::Colours::white);
g.setColour(juce::Colours::black);
g.drawText("Item " + juce::String(rowNumber), 0, 0, width, height, juce::Justification::centredLeft);
}
} model_;
juce::ListBox listBox_ {"", &model_};
NestedRects nestedRects_ { 4 };
juce::Slider alphaSlider_;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
Thank you all,
Jelle
