spacebox/src/Configuration.hpp

77 lines
1.6 KiB
C++
Raw Normal View History

2019-05-02 06:45:41 -04:00
#ifndef Configuration_h_
#define Configuration_h_
#include "json/json.hpp"
2019-05-04 03:25:35 -04:00
#include "filesystem.hpp"
2019-05-02 06:45:41 -04:00
#include "Node.hpp"
#include "Animation.hpp"
2019-05-02 06:45:41 -04:00
class Configuration : public Node
2019-05-02 06:45:41 -04:00
{
private:
nlohmann::json sys_config, user_config;
2019-05-02 06:45:41 -04:00
fs::path config_path;
int tab_width = 4;
Animation auto_refresher = Animation(&Configuration::refresh, this);
fs::file_time_type config_file_modification_time;
2019-05-02 06:45:41 -04:00
void set_defaults();
2019-05-04 03:25:35 -04:00
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();
2019-05-02 06:45:41 -04:00
void write(fs::path path);
void write();
void update();
virtual std::string class_name() const { return "Configuration"; }
2019-05-02 20:11:45 -04:00
2019-05-02 06:45:41 -04:00
};
/* 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);
}
}
2019-05-02 06:45:41 -04:00
#endif