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

133 lines
3.8 KiB
C++

#include "MesurePanel.h"
/*
==============================================================================
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 nombre de temps par mesure.
==============================================================================
*/
void RoundCBLookAndFeel::drawToggleButton(juce::Graphics& g, juce::ToggleButton& button,
bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown)
{
// dessine un cercle au lieux d'un bouton
auto fontSize = juce::jmin(15.0f, (float)button.getHeight() * 0.75f);
auto tickWidth = fontSize * 1.1f;
drawTickBox(g, button, 4.0f, ((float)button.getHeight() - tickWidth) * 0.5f,
tickWidth, tickWidth,
button.getToggleState(),
button.isEnabled(),
shouldDrawButtonAsHighlighted,
shouldDrawButtonAsDown);
}
void RoundCBLookAndFeel::drawTickBox(juce::Graphics& g, juce::Component& component,
float x, float y, float w, float h,
bool ticked, bool isEnabled,
bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown)
{
// dessine un cercle au lieux d'un bouton
juce::ignoreUnused(isEnabled, shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown);
g.setColour(component.findColour(juce::ToggleButton::tickDisabledColourId));
g.drawEllipse(x, y, w, h, 1.0f);
if (ticked)
{
g.setColour(component.findColour(juce::ToggleButton::tickColourId));
g.fillEllipse(x, y, w, h);
}
}
MesurePanel::MesurePanel(juce::Colour c) : backgroundColour(c)
{
lef.reset(new RoundCBLookAndFeel());
update(4, 0);
}
MesurePanel::~MesurePanel()
{
// evite une fuite mémoire à la fermeture due au L&F
for (auto& tb : buttons)
{
removeChildComponent(tb);
tb->setLookAndFeel(nullptr);
}
}
void MesurePanel::paint(juce::Graphics& g)
{
g.fillAll(backgroundColour);
}
void MesurePanel::update(int nb, int cur)
{
// Construit autant de bouton qu'il y a de temps
if (buttons.size() != nb)
{
// Si la mesure à changé on reinitialise
for (auto& tb : buttons)
{
removeChildComponent(tb);
tb->setVisible(false);
}
buttons.clear();
for (int i = 0; i < nb; ++i)
{
auto* tb = new juce::ToggleButton(juce::String(i));
addAndMakeVisible(buttons.add(tb));
tb->setRadioGroupId(1234);
tb->setColour(juce::ToggleButton::tickDisabledColourId, juce::Colours::black);
tb->setColour(juce::ToggleButton::tickColourId, juce::Colours::black);
tb->setLookAndFeel(lef.get());
}
}
// On sélectionne le bouton correpondant au temps courant
for (int i = 0; i < nb; ++i)
{
buttons[i]->setToggleState(i == cur, juce::dontSendNotification);
}
resized();
}
void MesurePanel::resized()
{
// Layout via une flexbox pour répartir les boutons en ligne
juce::FlexBox fb;
fb.flexWrap = juce::FlexBox::Wrap::wrap;
fb.justifyContent = juce::FlexBox::JustifyContent::center;
fb.alignContent = juce::FlexBox::AlignContent::center;
for (auto* b : buttons)
fb.items.add(juce::FlexItem(*b).withMinWidth(40.0f).withMinHeight(40.0f));
fb.performLayout(getLocalBounds().toFloat());
}