#include "ToggleButton.h" namespace serum { ToggleButton::ToggleButton (const juce::String& lbl) : label (lbl) { setRepaintsOnMouseActivity (true); } void ToggleButton::attach (juce::AudioProcessorValueTreeState& apvts, const juce::String& paramId) { param = apvts.getParameter (paramId); if (param != nullptr) { state = param->getValue() > 0.5f; attachment = std::make_unique (*param, [this] (float newValue) { setToggleState (newValue > 0.5f); }); } } void ToggleButton::setToggleState (bool s) { if (state == s) return; state = s; repaint(); } void ToggleButton::paint (juce::Graphics& g) { const auto bounds = getLocalBounds().toFloat(); const float ledSize = juce::jmin (bounds.getHeight() - 16.0f, 16.0f); const juce::Rectangle led ((bounds.getWidth() - ledSize) * 0.5f, 4.0f, ledSize, ledSize); if (state) { g.setColour (onColour.withAlpha (0.35f)); g.fillEllipse (led.expanded (6.0f)); g.setColour (onColour); } else { g.setColour (theme::outline); } g.fillEllipse (led); g.setColour (state ? juce::Colours::black : theme::textDim); g.drawEllipse (led, 1.0f); g.setColour (state ? theme::text : theme::textDim); g.setFont (juce::Font (11.0f)); g.drawText (label, 0, (int) (led.getBottom() + 2), getWidth(), 14, juce::Justification::centred, false); } void ToggleButton::mouseDown (const juce::MouseEvent&) { const bool newState = ! state; if (onClick) { onClick (newState); } else if (param != nullptr) { param->setValueNotifyingHost (newState ? 1.0f : 0.0f); } setToggleState (newState); } } // namespace serum