|
22 | 22 | #include "FreezeButton.h" |
23 | 23 | #include <BinaryData.h> |
24 | 24 |
|
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) |
27 | 28 | { |
28 | | - setOpaque (true); |
| 29 | + paramAttachment.sendInitialUpdate(); |
| 30 | + |
29 | 31 | 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)); |
32 | 36 |
|
33 | 37 | const auto svg = juce::Drawable::createFromImageData (BinaryData::FreezeIcon_svg, BinaryData::FreezeIcon_svgSize); |
34 | 38 | jassert (svg != nullptr); |
35 | 39 |
|
36 | 40 | if (svg != nullptr) |
37 | | - freezeIconPath = svg->getOutlineAsPath(); |
| 41 | + iconPath = svg->getOutlineAsPath(); |
38 | 42 | } |
39 | 43 |
|
40 | 44 | void FreezeButton::resized() |
41 | 45 | { |
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)); |
44 | 48 | } |
45 | 49 |
|
46 | 50 | void FreezeButton::paint (juce::Graphics& g) |
47 | 51 | { |
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); |
52 | 54 | } |
53 | 55 |
|
54 | 56 | void FreezeButton::mouseDown (const juce::MouseEvent& e) |
55 | 57 | { |
56 | | - juce::Button::mouseDown (e); |
| 58 | + juce::ignoreUnused (e); |
57 | 59 |
|
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)); |
61 | 64 | } |
62 | 65 |
|
63 | 66 | void FreezeButton::mouseUp (const juce::MouseEvent& e) |
64 | 67 | { |
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 | + } |
66 | 92 |
|
67 | | - const auto trans = freezeIconPath.getTransformToScaleToFit (freezeIconBounds, true); |
68 | | - freezeIconPath.applyTransform (trans); |
| 93 | + return false; |
69 | 94 | } |
70 | 95 |
|
71 | | -void FreezeButton::paintButton (juce::Graphics& g, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown) |
| 96 | +void FreezeButton::updateState (bool newState) |
72 | 97 | { |
73 | | - juce::ignoreUnused (g, shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown); |
| 98 | + state = newState; |
| 99 | + repaint(); |
74 | 100 | } |
0 commit comments