Tchick/Source/BPMPanel.cpp
2026-02-04 11:51:22 +01:00

156 lines
5.2 KiB
C++

/*
==============================================================================
Copyright 2022 Nicolas Chambert
This program is free software : you can redistribute itand /or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see < http://www.gnu.org/licenses/>.
==============================================================================
*/
/*
==============================================================================
Gestion du tempo.
==============================================================================
*/
#include "BPMPanel.h"
BPMPanel::BPMPanel(juce::Colour c) : backgroundColour(c)
{
// initialisation de l'interface
addComponent(decrement);
addComponent(bpm);
addComponent(increment);
addComponent(twice);
addComponent(half);
addComponent(set60);
addComponent(set90);
addComponent(set120);
addComponent(set150);
decrement.setRepeatSpeed(300, 100); // On veux pouvoir laisser le bouton appuyé
increment.setRepeatSpeed(300, 100);
bpm.setText("72", juce::dontSendNotification);
bpm.getFont().setBold(true);
bpm.setColour(juce::Label::textColourId, juce::Colours::black);
bpm.setJustificationType(juce::Justification::centred);
bpm.setFont(50);
}
void BPMPanel::addComponent(juce::Component& c)
{
addAndMakeVisible(c);
c.setColour(juce::TextButton::buttonColourId, backgroundColour);
c.setColour(juce::TextButton::textColourOffId, juce::Colours::black);
}
void BPMPanel::mouseWheelMove(const juce::MouseEvent&,
const juce::MouseWheelDetails& wheel)
{
// capture des mouseWheel pour invoquer increment/decrement
float dir = wheel.deltaY * (wheel.isReversed ? -1.0f : 1.0f);
if (dir > 0)
{
increment.onClick();
}
else
{
decrement.onClick();
}
}
void BPMPanel::paint(juce::Graphics& g)
{
g.fillAll(backgroundColour);
}
void BPMPanel::resized()
{
// Layout à la mano avec une première ligne pour les raccourcis
auto area = getLocalBounds();
int margin = 3;
area.removeFromTop(3*margin);
{
// première ligne accès rapide aux speeds
int setLinHeigth = 20;
auto setSpeed = area.removeFromTop(setLinHeigth);
int butWidth = 64;
int marginH = (setSpeed.getWidth() - butWidth * 4) / 2;
setSpeed.removeFromLeft(marginH);
set60.setBounds(setSpeed.removeFromLeft(butWidth)); setSpeed.removeFromLeft(margin);
set90.setBounds(setSpeed.removeFromLeft(butWidth)); setSpeed.removeFromLeft(margin);
set120.setBounds(setSpeed.removeFromLeft(butWidth)); setSpeed.removeFromLeft(margin);
set150.setBounds(setSpeed.removeFromLeft(butWidth)); setSpeed.removeFromLeft(margin);
}
area.removeFromTop(margin);
// Et une deuxième ligne avec increment/decrement et le tempo courant
{
// deuxième ligne BPM inc + dec
int butHeigth = 20;
int butWidth = 64;
int labeSize = 5 * butHeigth;
int totalWidth = 2 * butWidth + labeSize;
int marginH = (area.getWidth() - totalWidth) / 2;
int marginV = (area.getHeight() - labeSize) / 2;
decrement.setBounds(area.getX() + marginH, area.getY() + marginV + butHeigth, butWidth, 2 * butHeigth);
half.setBounds(area.getX() + marginH, area.getY() + marginV + 3 * butHeigth + margin, butWidth, butHeigth);
bpm.setBounds(area.getX() + marginH + butWidth, area.getY() + marginV, labeSize, labeSize);
increment.setBounds(area.getX() + marginH + butWidth + labeSize, area.getY() + marginV + butHeigth, butWidth, 2 * butHeigth);
twice.setBounds(area.getX() + marginH + butWidth + labeSize, area.getY() + marginV + 3 * butHeigth + margin, butWidth, butHeigth);
}
}
void BPMPanel::init(Metronome & metro)
{
// On établi les liaisons entre le Metronome et les composants graphique
bpm.setText(juce::String(metro.getBPM()), juce::dontSendNotification); // On récupère la valeur initiale du tempo
increment.onClick = [&, this]() { inc(metro); };
decrement.onClick = [&, this]() { dec(metro); };
set60.onClick = [&, this]() { setBPM(metro, 60); };
set90.onClick = [&, this]() { setBPM(metro, 90); };
set120.onClick = [&, this]() { setBPM(metro, 120); };
set150.onClick = [&, this]() { setBPM(metro, 150); };
half.onClick = [&, this]() { setBPM(metro, metro.getBPM() / 2); };
twice.onClick = [&, this]() { setBPM(metro, metro.getBPM() * 2); };
}
void BPMPanel::inc(Metronome& metro)
{
// Ajoute 1 au tempo
metro.setBPM(metro.getBPM() + 1);
bpm.setText(juce::String(metro.getBPM()), juce::dontSendNotification);
}
void BPMPanel::dec(Metronome& metro)
{
// reduit le tempo de 1
metro.setBPM(metro.getBPM() - 1);
bpm.setText(juce::String(metro.getBPM()), juce::dontSendNotification);
}
void BPMPanel::setBPM(Metronome& metro, int value)
{
// défini la nouvelle valeur du tempo
metro.setBPM(value);
bpm.setText(juce::String(metro.getBPM()), juce::dontSendNotification);
}