add: simple config system with font paths
This commit is contained in:
@@ -2,13 +2,16 @@
|
||||
set(target frame)
|
||||
|
||||
configure_file (
|
||||
"${PROJECT_SOURCE_DIR}/frame/src/Config.h.in"
|
||||
"${PROJECT_SOURCE_DIR}/frame/src/Config.h"
|
||||
"${PROJECT_SOURCE_DIR}/frame/src/BuildConfig.h.in"
|
||||
"${PROJECT_SOURCE_DIR}/frame/src/BuildConfig.h"
|
||||
)
|
||||
|
||||
set(src
|
||||
src/main.cpp
|
||||
|
||||
src/Config.hpp
|
||||
src/Config.cpp
|
||||
|
||||
src/Size.hpp
|
||||
src/Vector.hpp
|
||||
src/Rect.hpp
|
||||
@@ -69,6 +72,7 @@ endif (BUILD_VIRTUAL_DISPLAY)
|
||||
add_executable(${target} ${src})
|
||||
target_link_libraries(${target} PRIVATE
|
||||
fmt::fmt
|
||||
nlohmann_json::nlohmann_json
|
||||
)
|
||||
|
||||
if (BUILD_EPD)
|
||||
|
||||
2
frame/src/BuildConfig.h
Normal file
2
frame/src/BuildConfig.h
Normal file
@@ -0,0 +1,2 @@
|
||||
#define BUILD_VIRTUAL_DISPLAY
|
||||
/* #undef BUILD_EPD */
|
||||
51
frame/src/Config.cpp
Normal file
51
frame/src/Config.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "Config.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fmt/format.h>
|
||||
#include <fstream>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
#define PROPERTY(str, member) str.member = j.value(#member, str.member)
|
||||
|
||||
Config Config::Load()
|
||||
{
|
||||
fs::path const config_path{"config.json"};
|
||||
|
||||
if(fs::is_regular_file(config_path))
|
||||
{
|
||||
std::fstream file(config_path.string(), std::ios::in);
|
||||
|
||||
return json::parse(file);
|
||||
}
|
||||
|
||||
fmt::print("[INFO] Creating config file\n");
|
||||
Config config;
|
||||
std::fstream file(config_path.string(), std::ios::out);
|
||||
file << json(config).dump();
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
// == to json ==
|
||||
void to_json(json& j, Config const& c)
|
||||
{
|
||||
j = {{"font", c.font}};
|
||||
}
|
||||
|
||||
void to_json(json& j, FontConfig const& fc)
|
||||
{
|
||||
j = {{"base_path", fc.base_path}, {"fonts", fc.fonts}};
|
||||
}
|
||||
|
||||
// == from json ==
|
||||
void from_json(json const& j, Config& c)
|
||||
{
|
||||
PROPERTY(c, font);
|
||||
}
|
||||
|
||||
void from_json(json const& j, FontConfig& fc)
|
||||
{
|
||||
PROPERTY(fc, base_path);
|
||||
PROPERTY(fc, fonts);
|
||||
}
|
||||
26
frame/src/Config.hpp
Normal file
26
frame/src/Config.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
struct FontConfig
|
||||
{
|
||||
std::string base_path = "fonts/";
|
||||
std::unordered_map<std::string, std::string> fonts{
|
||||
{"Fira Code", "FiraCode.json"}};
|
||||
};
|
||||
|
||||
struct Config
|
||||
{
|
||||
FontConfig font{};
|
||||
|
||||
static Config Load();
|
||||
};
|
||||
|
||||
void to_json(json& j, Config const& c);
|
||||
void to_json(json& j, FontConfig const& fc);
|
||||
|
||||
void from_json(json const& j, Config& c);
|
||||
void from_json(json const& j, FontConfig& fc);
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "Display.hpp"
|
||||
|
||||
#include "../Config.h"
|
||||
#include "../BuildConfig.h"
|
||||
|
||||
#ifdef BUILD_EPD
|
||||
# include "EPD_7in5_V2.hpp"
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#include "FontRegistry.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fmt/format.h>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace frame::font
|
||||
{
|
||||
std::shared_ptr<Font> GetFont(std::string_view name)
|
||||
@@ -14,11 +17,26 @@ namespace frame::font
|
||||
return Service::get<FontRegistry>()->Load(name);
|
||||
}
|
||||
|
||||
FontRegistry& LocateRegistry()
|
||||
{
|
||||
return *Service::get<FontRegistry>();
|
||||
}
|
||||
|
||||
FontRegistry::FontRegistry()
|
||||
: IService("FontRegistry")
|
||||
{
|
||||
}
|
||||
|
||||
void FontRegistry::LoadFromConfig(FontConfig const& config)
|
||||
{
|
||||
fs::path base{config.base_path};
|
||||
|
||||
for(auto const& [name, file] : config.fonts)
|
||||
{
|
||||
Load(name, (base / file).string());
|
||||
}
|
||||
}
|
||||
|
||||
bool FontRegistry::Load(std::string_view name)
|
||||
{
|
||||
if(fonts.find(std::string{name}) != fonts.end())
|
||||
@@ -43,6 +61,29 @@ namespace frame::font
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FontRegistry::Load(std::string_view name, std::string_view path)
|
||||
{
|
||||
if(fonts.find(std::string{name}) != fonts.end())
|
||||
{
|
||||
fmt::print("Font {} allready loaded!\n", name);
|
||||
return true;
|
||||
}
|
||||
|
||||
fmt::print("Loading font \"{}\": ", name);
|
||||
auto ptr = Font::LoadFromFile(path);
|
||||
|
||||
if(ptr == nullptr)
|
||||
{
|
||||
fmt::print("Error not found!\n");
|
||||
} else
|
||||
{
|
||||
fonts[std::string{name}] = ptr;
|
||||
fmt::print("OK\n");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::shared_ptr<Font> FontRegistry::Get(std::string_view name)
|
||||
{
|
||||
auto it = fonts.find({std::string{name}});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Config.hpp"
|
||||
#include "../ServiceLocator.hpp"
|
||||
#include "Font.hpp"
|
||||
|
||||
@@ -8,9 +9,11 @@
|
||||
|
||||
namespace frame::font
|
||||
{
|
||||
class FontRegistry;
|
||||
|
||||
std::shared_ptr<Font> GetFont(std::string_view name);
|
||||
bool LoadFont(std::string_view name);
|
||||
FontRegistry& LocateRegistry();
|
||||
|
||||
class FontRegistry : public IService
|
||||
{
|
||||
@@ -19,7 +22,9 @@ namespace frame::font
|
||||
public:
|
||||
FontRegistry();
|
||||
|
||||
void LoadFromConfig(FontConfig const& config);
|
||||
bool Load(std::string_view name);
|
||||
bool Load(std::string_view name, std::string_view path);
|
||||
std::shared_ptr<Font> Get(std::string_view name);
|
||||
};
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
#include "Config.hpp"
|
||||
#include "ScreenManager.hpp"
|
||||
#include "display/Display.hpp"
|
||||
#include "font/FontRegistry.hpp"
|
||||
@@ -16,16 +17,22 @@ using namespace std::chrono_literals;
|
||||
|
||||
int main()
|
||||
{
|
||||
// == == Config == ==
|
||||
auto config = Config::Load();
|
||||
|
||||
// == == Display == ==
|
||||
auto display = frame::display::Create();
|
||||
|
||||
if(!display)
|
||||
{
|
||||
fmt::print("Error: Not display driver!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
display->Init();
|
||||
|
||||
frame::font::LoadFont("Fira Code");
|
||||
// frame::font::LoadFont("Fira Code");
|
||||
frame::font::LocateRegistry().LoadFromConfig(config.font);
|
||||
|
||||
frame::ScreenManager screen(std::move(display));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user