spacebox/src/Configuration.hpp

77 lines
1.6 KiB
C++

#ifndef Configuration_h_
#define Configuration_h_
#include "json/json.hpp"
#include "filesystem.hpp"
#include "Node.hpp"
#include "Animation.hpp"
class Configuration : public Node
{
private:
nlohmann::json sys_config, user_config;
fs::path config_path;
int tab_width = 4;
Animation auto_refresher = Animation(&Configuration::refresh, this);
fs::file_time_type config_file_modification_time;
void set_defaults();
void merge();
public:
nlohmann::json config;
Configuration(Node*, fs::path = "config.json");
void load(fs::path path);
void load();
void auto_refresh(bool);
void refresh();
void write(fs::path path);
void write();
void update();
virtual std::string class_name() const { return "Configuration"; }
};
/* Extend GLM so nlohmann::json can read and write glm::vec2 */
namespace glm
{
template <typename T>
void to_json(nlohmann::json& j, const vec<2, T, defaultp>& v)
{
j = nlohmann::json{v.x, v.y};
}
template <typename T>
void from_json(const nlohmann::json& j, vec<2, T, defaultp>& v)
{
j.at(0).get_to(v.x);
j.at(1).get_to(v.y);
}
}
/* Extend std::filesystem so nlohmann::json can read and write std::filesystem::path */
#if defined(__MINGW32__)
namespace std::experimental::filesystem
#else
namespace std::filesystem
#endif
{
template <typename T>
void to_json(nlohmann::json& j, const path& p)
{
j = nlohmann::json{p};
}
template <typename T>
void from_json(const nlohmann::json& j, path& p)
{
j.at(0).get_to(p);
}
}
#endif