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

104 lines
3.3 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 de la sauvegarde de la configuration courante.
==============================================================================
*/
#include "Config.h"
// petit singleton
Config* Config::instance = nullptr;
Config& Config::Load()
{
if (instance == nullptr)
instance = new Config();
// On cherche le fichier json dans le repertoire courant
juce::File curDir{ juce::File::getSpecialLocation(juce::File::SpecialLocationType::currentExecutableFile).getParentDirectory() };
juce::File confFile{ curDir.getChildFile("tchick.json") };
if (confFile.exists())
{
// s'il existe, on le charge
auto content = juce::JSON::parse(confFile);
instance->fromVar(content);
}
return *instance;
}
void Config::Save(Metronome& metro, OptionsPanel& options)
{
// On initialise les paramètres
instance->bpm = metro.getBPM();
instance->measures = metro.getMeasures();
instance->gain = metro.getGain();
instance->figure = (int)metro.getFigure();
instance->sound = options.getSoundName();
// on transforme en var
juce::var obj = instance->asVar();
// on met à jours le fichier
juce::File curDir{ juce::File::getSpecialLocation(juce::File::SpecialLocationType::currentExecutableFile).getParentDirectory() };
juce::File confFile{ curDir.getChildFile("tchick.json") };
juce::FileOutputStream stream(confFile);
if (stream.openedOk())
{
// méthode préconisé pour ecraser le fichier existant
stream.setPosition(0);
stream.truncate();
juce::JSON::writeToStream(stream, obj);
}
}
juce::var Config::asVar()
{
// création du var à partir de this
juce::DynamicObject* obj = new juce::DynamicObject();
juce::NamedValueSet& properties = obj->getProperties();
properties.set("bpm", bpm);
properties.set("measures", measures);
properties.set("gain", gain);
properties.set("sound", sound);
properties.set("figure", figure);
return juce::var(obj);
}
void Config::fromVar(juce::var var)
{
// chargement des paramètres depuis un var
juce::DynamicObject* obj = var.getDynamicObject();
juce::NamedValueSet& properties = obj->getProperties();
if(properties.contains("bpm")) bpm = properties["bpm"];
if(properties.contains("measures")) measures = properties["measures"];
if(properties.contains("gain")) gain = properties["gain"];
if(properties.contains("sound")) sound = properties["sound"];
if(properties.contains("figure")) figure = properties["figure"];
}