Skip to content

Commit

Permalink
Replace effect checkbox with toggle switch and fix bug on macOS when …
Browse files Browse the repository at this point in the history
…changing range of slider
  • Loading branch information
jameshball committed Mar 3, 2024
1 parent 4db2fcc commit 579a16f
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Source/LookAndFeel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "LookAndFeel.h"
#include "components/SwitchButton.h"

OscirenderLookAndFeel::OscirenderLookAndFeel() {
// slider
Expand All @@ -13,6 +14,9 @@ OscirenderLookAndFeel::OscirenderLookAndFeel() {
// buttons
setColour(juce::ToggleButton::tickDisabledColourId, juce::Colours::white);
setColour(juce::TextButton::buttonColourId, Colours::veryDark);
setColour(jux::SwitchButton::switchColour, juce::Colours::white);
setColour(jux::SwitchButton::switchOnBackgroundColour, Colours::accentColor);
setColour(jux::SwitchButton::switchOffBackgroundColour, Colours::dark);

// windows & menus
setColour(juce::ResizableWindow::backgroundColourId, Colours::grey);
Expand Down
2 changes: 1 addition & 1 deletion Source/components/EffectComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ EffectComponent::EffectComponent(OscirenderAudioProcessor& p, Effect& effect, in
menu.addCustomItem(2, min, 160, 40, false);
menu.addCustomItem(3, max, 160, 40, false);

menu.showMenuAsync(juce::PopupMenu::Options(), [this](int result) {});
menu.showMenuAsync(juce::PopupMenu::Options().withParentComponent(audioProcessor.getActiveEditor()), [this](int result) {});
};

effect.addListener(index, this);
Expand Down
2 changes: 1 addition & 1 deletion Source/components/EffectsListComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void EffectsListComponent::paintOverChildren(juce::Graphics& g) {
void EffectsListComponent::resized() {
auto area = getLocalBounds();
area.removeFromLeft(20);
selected.setBounds(area.removeFromLeft(30));
selected.setBounds(area.removeFromLeft(30).withSizeKeepingCentre(30, 20));
list.setBounds(area);
}

Expand Down
3 changes: 2 additions & 1 deletion Source/components/EffectsListComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../audio/Effect.h"
#include "EffectComponent.h"
#include "ComponentList.h"
#include "SwitchButton.h"
#include <random>

// Application-specific data container
Expand Down Expand Up @@ -155,7 +156,7 @@ class EffectsListComponent : public DraggableListBoxItem
Effect& effect;
ComponentListModel listModel;
juce::ListBox list;
juce::ToggleButton selected;
jux::SwitchButton selected = { "switchButton", false };
private:
OscirenderAudioProcessor& audioProcessor;
OscirenderAudioProcessorEditor& editor;
Expand Down
144 changes: 144 additions & 0 deletions Source/components/SwitchButton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
==============================================================================
MIT License
Copyright (c) 2020 Tal Aviram
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================
*/

#pragma once

#include "JuceHeader.h"

namespace jux
{
//==============================================================================
/**
Fancy looking toggable button. commonly used in modern UIs.
@see juce::ToggableButton
*/
class SwitchButton : public juce::Button
{
public:
enum ColourIds
{
switchColour = 0x1B06000,
switchOnBackgroundColour = 0x1B06001,
switchOffBackgroundColour = 0x1B06002
};

SwitchButton (juce::String name, bool isInverted, bool isVertical = false) : Button ("SwitchButton"), isInverted (isInverted), isVertical (isVertical) {
setClickingTogglesState (true);
addAndMakeVisible (switchCircle);
switchCircle.setWantsKeyboardFocus (false);
switchCircle.setInterceptsMouseClicks (false, false);
}

void setMillisecondsToSpendMoving (int newValue)
{
millisecondsToSpendMoving = newValue;
}

void paintButton (juce::Graphics& g,
bool shouldDrawButtonAsHighlighted,
bool shouldDrawButtonAsDown) override
{
auto b = getSwitchBounds();
auto cornerSize = (isVertical ? b.getWidth() : b.getHeight()) * 0.5;
g.setColour (juce::Colours::black.withAlpha (0.1f));
g.drawRoundedRectangle (b, cornerSize, 2.0f);
g.setColour (findColour (getSwitchState() ? switchOnBackgroundColour : switchOffBackgroundColour));
g.fillRoundedRectangle (b, cornerSize);

juce::Path switchPath;
switchPath.addRoundedRectangle (b, cornerSize, cornerSize);
g.fillPath (switchPath);

if (prevToggleState != getToggleState()) {
juce::Rectangle<float> switchCircleBounds;
if (! isVertical)
switchCircleBounds = { getSwitchState() ? b.getRight() - b.getHeight() : b.getX(), b.getY(), b.getHeight(), b.getHeight() };
else
switchCircleBounds = {
b.getX(),
getSwitchState() ? b.getBottom() - b.getWidth() : b.getY(),
b.getWidth(),
b.getWidth()
};
animator.animateComponent (&switchCircle, switchCircleBounds.reduced (1).toNearestInt(), 1.0, millisecondsToSpendMoving, false, 0.5, 0);
}

prevToggleState = getToggleState();
}

void resized() override
{
Button::resized();
auto b = getSwitchBounds();
juce::Rectangle<float> switchCircleBounds;
if (! isVertical)
switchCircleBounds = { getSwitchState() ? b.getRight() - b.getHeight() : b.getX(), b.getY(), b.getHeight(), b.getHeight() };
else
switchCircleBounds = {
b.getX(),
getSwitchState() ? b.getBottom() - b.getWidth() : b.getY(),
b.getHeight(),
b.getHeight()
};
switchCircle.setBounds (switchCircleBounds.reduced (1).toNearestInt());
}

private:
int millisecondsToSpendMoving { 75 };

bool getSwitchState() const
{
return isInverted ? ! getToggleState() : getToggleState();
}
bool isInverted = false;
bool isVertical = false;

juce::Rectangle<float> getSwitchBounds()
{
auto b = getLocalBounds().toFloat().reduced(4, 4).translated(0, -1);
return b;
}

juce::String onText, offText;
class SwitchCircle : public Component
{
void paint (juce::Graphics& g) override
{
g.setColour (findColour (switchColour));
g.fillEllipse (getLocalBounds().toFloat());
}
} switchCircle;
juce::ComponentAnimator animator;

bool prevToggleState = false;
};

} // namespace jux
3 changes: 2 additions & 1 deletion osci-render.jucer
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
pluginCharacteristicsValue="pluginProducesMidiOut,pluginWantsMidiIn"
pluginManufacturer="jameshball" aaxIdentifier="sh.ball.oscirender"
cppLanguageStandard="20" projectLineFeed="&#10;" headerPath="./include"
version="2.1.2" companyName="James H Ball" companyWebsite="https://osci-render.com"
version="2.1.3" companyName="James H Ball" companyWebsite="https://osci-render.com"
companyEmail="[email protected]" defines="NOMINMAX=1">
<MAINGROUP id="j5Ge2T" name="osci-render">
<GROUP id="{5ABCED88-0059-A7AF-9596-DBF91DDB0292}" name="Resources">
Expand Down Expand Up @@ -237,6 +237,7 @@
file="Source/components/MainMenuBarModel.h"/>
<FILE id="QQzSwh" name="SliderTextBox.h" compile="0" resource="0" file="Source/components/SliderTextBox.h"/>
<FILE id="QrDKRZ" name="SvgButton.h" compile="0" resource="0" file="Source/components/SvgButton.h"/>
<FILE id="qzfstC" name="SwitchButton.h" compile="0" resource="0" file="Source/components/SwitchButton.h"/>
<FILE id="y3UiR0" name="VisualiserComponent.cpp" compile="1" resource="0"
file="Source/components/VisualiserComponent.cpp"/>
<FILE id="ZueyNl" name="VisualiserComponent.h" compile="0" resource="0"
Expand Down

0 comments on commit 579a16f

Please sign in to comment.