Tchick/Source/OptionsPanel.h
2026-02-04 11:51:22 +01:00

145 lines
4.4 KiB
C++

#pragma once
/*
==============================================================================
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 des options figure rythmique et fichier son du tick.
==============================================================================
*/
#include <JuceHeader.h>
#include "Metronome.h"
#include "Config.h"
class Config;
class Metronome;
// Surcharge les boutons pour afficher l'image de la figure rythmique
struct FigureButton : public juce::PopupMenu::CustomComponent
{
FigureButton(juce::String image, int widthIn, int heightIn, juce::Colour backgroundIn)
: juce::PopupMenu::CustomComponent(false),
idealWidth(widthIn),
idealHeight(heightIn),
background(backgroundIn)
{
// initialisation de l'interface
addAndMakeVisible(button);
// récupère l'image de la figure
auto doc = juce::XmlDocument::parse(image);
auto note = juce::Drawable::createFromSVG(*doc);
button.setImages(note.get());
button.setToggleState(false, juce::NotificationType::dontSendNotification);
button.setColour(juce::TextButton::buttonColourId, juce::Colours::whitesmoke);
button.setColour(juce::TextButton::textColourOffId, juce::Colours::black);
button.onClick = [&, this]
{
// notifie la "Combo"
this->triggerMenuItem();
};
}
void getIdealSize(int& width, int& height) override
{
width = idealWidth;
height = idealHeight;
}
void paint(juce::Graphics& g) override { g.fillAll(background); }
void resized() override { button.setBounds(getBounds()); }
// composants graphiques
juce::Colour background;
juce::DrawableButton button{ "Figures", juce::DrawableButton::ImageOnButtonBackground };
// taille du bouton
int idealWidth = 0;
int idealHeight = 0;
};
// L&F pour la combo des sons, permet de setter le background du Popup
struct ComboLookAndFeel : public juce::LookAndFeel_V4
{
void drawPopupMenuBackground(juce::Graphics& g, int width, int height) override;
void drawPopupMenuItem(juce::Graphics& g, const juce::Rectangle<int>& area,
const bool isSeparator, const bool isActive,
const bool isHighlighted, const bool isTicked,
const bool hasSubMenu, const juce::String& text,
const juce::String& shortcutKeyText,
const juce::Drawable* icon, const juce::Colour* const textColourToUse) override;
};
// structure dédiée à la gestion des sons
struct ClickSounds
{
ClickSounds() {};
// Charge la liste de sons
void init();
// Accesseurs
int getSoundCount() const { return (int)sounds.size(); };
std::string getSoundName(int i) const;
std::string getSoundPathHigh(int i) const;
std::string getSoundPathLow(int i) const;
int getIndex(std::string soundName) const;
// Liste des sons
std::vector<std::string> sounds;
};
// Panneau d'affichage des options
struct OptionsPanel : public juce::Component
{
explicit OptionsPanel(juce::Colour c, Config& configuration);
~OptionsPanel();
// Méthode de surcharge JUCE
void paint(juce::Graphics& g) override;
void resized() override;
// méthodes internes
void init(Metronome& metro);
void selectSound(Metronome& metro);
void setFigure(int figure);
juce::String getSoundName();
// composants graphiques
juce::DrawableButton figures{ "Figures", juce::DrawableButton::ImageOnButtonBackground };
juce::ComboBox sounds;
juce::Colour backgroundColour;
std::unique_ptr <ComboLookAndFeel> clef;
// variables internes
// liste des sons
ClickSounds clicksounds;
// pointeur sur la méthode qui permet de setter le metronome
std::function< void(int)> setMetroFigure {nullptr};
// permet de stocker l'id du son selectionné dans la configuration entre le constructeur et le init()
int curSoundID;
};