Skip to content
This repository was archived by the owner on Dec 23, 2025. It is now read-only.

Commit 591882e

Browse files
committed
Clean up freeze button
1 parent d37f632 commit 591882e

File tree

4 files changed

+72
-31
lines changed

4 files changed

+72
-31
lines changed

src/ui/EditorContent.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ EditorContent::EditorContent (SimpleReverbAudioProcessor& p, juce::UndoManager&
2929
, dampDial (*apvts.getParameter (ParamIDs::damp), &um)
3030
, widthDial (*apvts.getParameter (ParamIDs::width), &um)
3131
, mixDial (*apvts.getParameter (ParamIDs::mix), &um)
32-
, freezeAttachment (*apvts.getParameter (ParamIDs::freeze), freezeButton, &um)
32+
, freezeButton (*apvts.getParameter (ParamIDs::freeze), &um)
3333
{
3434
setWantsKeyboardFocus (true);
3535
setFocusContainerType (FocusContainerType::keyboardFocusContainer);
@@ -51,7 +51,7 @@ void EditorContent::resized()
5151
widthDial.setBounds (baseDialBounds.withX (342));
5252
mixDial.setBounds (baseDialBounds.withX (440));
5353

54-
freezeButton.setBounds (249, 110, 68, 32);
54+
freezeButton.setBounds (259, 110, 48, 32);
5555
}
5656

5757
bool EditorContent::keyPressed (const juce::KeyPress& k)

src/ui/EditorContent.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,5 @@ class EditorContent : public juce::Component
4848

4949
FreezeButton freezeButton;
5050

51-
juce::ButtonParameterAttachment freezeAttachment;
52-
5351
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EditorContent)
5452
};

src/ui/FreezeButton.cpp

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,79 @@
2222
#include "FreezeButton.h"
2323
#include <BinaryData.h>
2424

25-
FreezeButton::FreezeButton()
26-
: juce::Button (juce::String {})
25+
FreezeButton::FreezeButton (juce::RangedAudioParameter& param, juce::UndoManager* um)
26+
: audioParam (param)
27+
, paramAttachment (audioParam, [&] (float v) { updateState (static_cast<bool> (v)); }, um)
2728
{
28-
setOpaque (true);
29+
paramAttachment.sendInitialUpdate();
30+
2931
setWantsKeyboardFocus (true);
30-
setClickingTogglesState (true);
31-
onClick = [&] { freezeColour = getToggleState() ? MyColours::blue : MyColours::midGrey; };
32+
setRepaintsOnMouseActivity (true);
33+
setColour (onColourId, MyColours::blue);
34+
setColour (offColourId, MyColours::midGrey);
35+
setColour (focusColourId, MyColours::midGrey.brighter (0.25f));
3236

3337
const auto svg = juce::Drawable::createFromImageData (BinaryData::FreezeIcon_svg, BinaryData::FreezeIcon_svgSize);
3438
jassert (svg != nullptr);
3539

3640
if (svg != nullptr)
37-
freezeIconPath = svg->getOutlineAsPath();
41+
iconPath = svg->getOutlineAsPath();
3842
}
3943

4044
void FreezeButton::resized()
4145
{
42-
freezeIconBounds = getLocalBounds().toFloat().reduced (6.0f);
43-
freezeIconPath.applyTransform (freezeIconPath.getTransformToScaleToFit (freezeIconBounds, true));
46+
iconBounds = getLocalBounds().toFloat();
47+
iconPath.applyTransform (iconPath.getTransformToScaleToFit (iconBounds, true));
4448
}
4549

4650
void FreezeButton::paint (juce::Graphics& g)
4751
{
48-
g.fillAll (MyColours::black);
49-
50-
g.setColour (freezeColour);
51-
g.fillPath (freezeIconPath);
52+
g.setColour (findColour (state ? onColourId : hasKeyboardFocus (true) ? focusColourId : offColourId));
53+
g.fillPath (iconPath);
5254
}
5355

5456
void FreezeButton::mouseDown (const juce::MouseEvent& e)
5557
{
56-
juce::Button::mouseDown (e);
58+
juce::ignoreUnused (e);
5759

58-
const auto centre = freezeIconBounds.getCentre();
59-
const auto trans = juce::AffineTransform::scale (0.95f, 0.95f, centre.x, centre.y);
60-
freezeIconPath.applyTransform (trans);
60+
paramAttachment.setValueAsCompleteGesture (! state);
61+
62+
const auto centre = iconBounds.getCentre();
63+
iconPath.applyTransform (juce::AffineTransform::scale (0.95f, 0.95f, centre.x, centre.y));
6164
}
6265

6366
void FreezeButton::mouseUp (const juce::MouseEvent& e)
6467
{
65-
juce::Button::mouseUp (e);
68+
juce::ignoreUnused (e);
69+
70+
iconPath.applyTransform (iconPath.getTransformToScaleToFit (iconBounds, true));
71+
}
72+
73+
void FreezeButton::focusGained (FocusChangeType cause)
74+
{
75+
juce::ignoreUnused (cause);
76+
repaint();
77+
}
78+
79+
void FreezeButton::focusLost (FocusChangeType cause)
80+
{
81+
juce::ignoreUnused (cause);
82+
repaint();
83+
}
84+
85+
bool FreezeButton::keyPressed (const juce::KeyPress& key)
86+
{
87+
if (key == juce::KeyPress::returnKey)
88+
{
89+
paramAttachment.setValueAsCompleteGesture (! state);
90+
return true;
91+
}
6692

67-
const auto trans = freezeIconPath.getTransformToScaleToFit (freezeIconBounds, true);
68-
freezeIconPath.applyTransform (trans);
93+
return false;
6994
}
7095

71-
void FreezeButton::paintButton (juce::Graphics& g, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown)
96+
void FreezeButton::updateState (bool newState)
7297
{
73-
juce::ignoreUnused (g, shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown);
98+
state = newState;
99+
repaint();
74100
}

src/ui/FreezeButton.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,42 @@
2222
#pragma once
2323

2424
#include "MyColours.h"
25+
#include <juce_audio_processors/juce_audio_processors.h>
2526
#include <juce_gui_basics/juce_gui_basics.h>
2627

27-
class FreezeButton : public juce::Button
28+
class FreezeButton : public juce::Component
2829
{
2930
public:
30-
FreezeButton();
31+
enum ColourIds
32+
{
33+
onColourId,
34+
offColourId,
35+
focusColourId
36+
};
37+
38+
explicit FreezeButton (juce::RangedAudioParameter& param, juce::UndoManager* um = nullptr);
3139

3240
void paint (juce::Graphics& g) override;
3341
void resized() override;
3442

3543
void mouseDown (const juce::MouseEvent& event) override;
3644
void mouseUp (const juce::MouseEvent& event) override;
3745

38-
void paintButton (juce::Graphics& g, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown) override;
46+
void focusGained (FocusChangeType cause) override;
47+
void focusLost (FocusChangeType cause) override;
48+
49+
bool keyPressed (const juce::KeyPress& key) override;
3950

4051
private:
41-
juce::Path freezeIconPath;
42-
juce::Rectangle<float> freezeIconBounds;
43-
juce::Colour freezeColour { MyColours::midGrey };
52+
void updateState (bool newState);
53+
54+
bool state { false };
55+
56+
juce::Path iconPath;
57+
juce::Rectangle<float> iconBounds;
58+
59+
juce::RangedAudioParameter& audioParam;
60+
juce::ParameterAttachment paramAttachment;
4461

4562
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FreezeButton)
4663
};

0 commit comments

Comments
 (0)