Skip to content

Commit

Permalink
Moved dynamic_cast out of hot path
Browse files Browse the repository at this point in the history
  • Loading branch information
ffAudio committed May 25, 2024
1 parent a506a39 commit 8ba2754
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 37 deletions.
71 changes: 43 additions & 28 deletions modules/foleys_gui_magic/Widgets/foleys_MagicLevelMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,43 @@ MagicLevelMeter::MagicLevelMeter()
setColour (outlineColourId, juce::Colours::silver);
setColour (tickmarkColourId, juce::Colours::silver);

lookAndFeelChanged();

startTimerHz (30);
}

void MagicLevelMeter::paint (juce::Graphics& g)
{
if (auto* lnf = dynamic_cast<LookAndFeelMethods*>(&getLookAndFeel()))
{
lnf->drawLevelMeter (g, *this, source, getLocalBounds());
return;
}
actualLookAndFeel->drawLevelMeter (g, *this, source, getLocalBounds());
}

void MagicLevelMeter::setLevelSource (MagicLevelSource* newSource)
{
source = newSource;
}

void MagicLevelMeter::timerCallback()
{
repaint();
}

void MagicLevelMeter::lookAndFeelChanged()
{
auto* lnf = dynamic_cast<LookAndFeelMethods*> (&getLookAndFeel());

if (lnf)
actualLookAndFeel = lnf;
else
actualLookAndFeel = &lookAndFeelFallback;

repaint();
}

// ================================================================================

const auto backgroundColour = findColour (backgroundColourId);
void MagicLevelMeter::LookAndFeelFallback::drawLevelMeter (juce::Graphics& g, MagicLevelMeter& meter, MagicLevelSource* source, juce::Rectangle<int> bounds)
{
const auto backgroundColour = meter.findColour (backgroundColourId);
if (!backgroundColour.isTransparent())
g.fillAll (backgroundColour);

Expand All @@ -67,39 +92,29 @@ void MagicLevelMeter::paint (juce::Graphics& g)
if (numChannels == 0)
return;

auto bounds = getLocalBounds().reduced (3).toFloat();
auto inner = bounds.reduced (3).toFloat();

const auto width = bounds.getWidth() / numChannels;
const auto barBackgroundColour = findColour (barBackgroundColourId);
const auto barFillColour = findColour (barFillColourId);
const auto outlineColour = findColour (outlineColourId);
const auto width = inner.getWidth() / static_cast<float> (numChannels);
const auto barBackgroundColour = meter.findColour (barBackgroundColourId);
const auto barFillColour = meter.findColour (barFillColourId);
const auto outlineColour = meter.findColour (outlineColourId);

const auto infinity = -100.0f;
for (int i=0; i < numChannels; ++i)
for (int i = 0; i < numChannels; ++i)
{
auto bar = bounds.removeFromLeft (width).reduced (1);
auto bar = inner.removeFromLeft (width).reduced (1);
g.setColour (barBackgroundColour);
g.fillRect (bar);
g.setColour (outlineColour);
g.drawRect (bar, 1.0f);
bar.reduce (1, 1);
g.setColour (barFillColour);
g.fillRect (bar.withTop (juce::jmap (juce::Decibels::gainToDecibels (source->getRMSvalue (i), infinity),
infinity, 0.0f, bar.getBottom(), bar.getY())));
g.drawHorizontalLine (juce::roundToInt (juce::jmap (juce::Decibels::gainToDecibels (source->getMaxValue (i), infinity),
infinity, 0.0f, bar.getBottom (), bar.getY ())),
static_cast<float>(bar.getX ()), static_cast<float>(bar.getRight ()));
g.fillRect (bar.withTop (juce::jmap (juce::Decibels::gainToDecibels (source->getRMSvalue (i), infinity), infinity, 0.0f, bar.getBottom(), bar.getY())));
g.drawHorizontalLine (juce::roundToInt (
juce::jmap (juce::Decibels::gainToDecibels (source->getMaxValue (i), infinity), infinity, 0.0f, bar.getBottom(), bar.getY())),
static_cast<float> (bar.getX()), static_cast<float> (bar.getRight()));
}
}

void MagicLevelMeter::setLevelSource (MagicLevelSource* newSource)
{
source = newSource;
}

void MagicLevelMeter::timerCallback()
{
repaint();
}

} // namespace foleys
} // namespace foleys
28 changes: 19 additions & 9 deletions modules/foleys_gui_magic/Widgets/foleys_MagicLevelMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ namespace foleys

class MagicLevelSource;

class MagicLevelMeter : public juce::Component,
public juce::SettableTooltipClient,
private juce::Timer
class MagicLevelMeter
: public juce::Component
, public juce::SettableTooltipClient
, private juce::Timer
{
public:
enum ColourIds
Expand All @@ -56,11 +57,8 @@ class MagicLevelMeter : public juce::Component,

struct LookAndFeelMethods
{
virtual ~LookAndFeelMethods()=default;
virtual void drawLevelMeter (juce::Graphics& g,
MagicLevelMeter& meter,
MagicLevelSource* source,
juce::Rectangle<int> bounds) = 0;
virtual ~LookAndFeelMethods() = default;
virtual void drawLevelMeter (juce::Graphics& g, MagicLevelMeter& meter, MagicLevelSource* source, juce::Rectangle<int> bounds) = 0;
};

MagicLevelMeter();
Expand All @@ -71,11 +69,23 @@ class MagicLevelMeter : public juce::Component,

void timerCallback() override;

void lookAndFeelChanged() override;

private:
juce::WeakReference<MagicLevelSource> source;

class LookAndFeelFallback : public LookAndFeel, public LookAndFeelMethods
{
public:
LookAndFeelFallback() = default;
void drawLevelMeter (juce::Graphics& g, MagicLevelMeter& meter, MagicLevelSource* source, juce::Rectangle<int> bounds) override;
};

LookAndFeelFallback lookAndFeelFallback;
LookAndFeelMethods* actualLookAndFeel = &lookAndFeelFallback;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MagicLevelMeter)
};


} // namespace foleys
} // namespace foleys

0 comments on commit 8ba2754

Please sign in to comment.