|
| 1 | +#include "theme.h" |
| 2 | +#include <fstream> |
| 3 | +#include <utility> |
| 4 | + |
| 5 | +#define THEME_JSON_FILE_NAME "theme.json" |
| 6 | + |
| 7 | +Theme::Theme(std::string themeDirectory) : rootPath(std::move(themeDirectory)) { |
| 8 | + if (rootPath.back() != '\\') { |
| 9 | + rootPath += "\\"; |
| 10 | + } |
| 11 | + load(); |
| 12 | +} |
| 13 | + |
| 14 | +void Theme::load() { |
| 15 | + std::string themeFilePath = rootPath + THEME_JSON_FILE_NAME; |
| 16 | + |
| 17 | + std::ifstream themeFile(themeFilePath); |
| 18 | + nlohmann::json json; |
| 19 | + // FIXME: Once nxdk supports C++ Exceptions, this needs to be put in a try-catch block! |
| 20 | + themeFile >> json; |
| 21 | + from_json(json, *this); |
| 22 | + |
| 23 | + themeFile.close(); |
| 24 | +} |
| 25 | + |
| 26 | +void to_json(nlohmann::json& j, Theme::MenuTheme const& o) { |
| 27 | + char fontColor[16] = { 0 }; |
| 28 | + snprintf(fontColor, 15, "%X", o.fontColor); |
| 29 | + |
| 30 | + j = nlohmann::json{ { "font", nlohmann::json(o.font) }, |
| 31 | + { "font_color", nlohmann::json(fontColor) } }; |
| 32 | +} |
| 33 | + |
| 34 | +void from_json(nlohmann::json const& j, Theme::MenuTheme& o) { |
| 35 | + if (j.contains("font")) { |
| 36 | + o.font = j["font"]; |
| 37 | + } |
| 38 | + if (j.contains("font_color")) { |
| 39 | + std::string const& colorString = j["font_color"]; |
| 40 | + sscanf(colorString.c_str(), "%X", &o.fontColor); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +void to_json(nlohmann::json& j, Theme::ImageSet const& o) { |
| 45 | + j = nlohmann::json{ { "480", nlohmann::json(o.image480p) }, |
| 46 | + { "720", nlohmann::json(o.image720p) } }; |
| 47 | +} |
| 48 | + |
| 49 | +void from_json(nlohmann::json const& j, Theme::ImageSet& o) { |
| 50 | + if (j.contains("480")) { |
| 51 | + o.image480p = j["480"]; |
| 52 | + } |
| 53 | + if (j.contains("720")) { |
| 54 | + o.image720p = j["720"]; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void to_json(nlohmann::json& j, Theme const& o) { |
| 59 | + j = nlohmann::json{ { "title", o.getTitle() }, |
| 60 | + { "menu", nlohmann::json(o.getMenu()) }, |
| 61 | + { "background", nlohmann::json(o.getBackground()) } }; |
| 62 | +} |
| 63 | + |
| 64 | +void from_json(nlohmann::json const& j, Theme& o) { |
| 65 | + if (j.contains("title")) { |
| 66 | + o.setTitle(j["title"]); |
| 67 | + } |
| 68 | + if (j.contains("menu")) { |
| 69 | + o.setMenu(j["menu"].get<Theme::MenuTheme>()); |
| 70 | + } |
| 71 | + if (j.contains("background")) { |
| 72 | + o.setBackground(j["background"].get<Theme::ImageSet>()); |
| 73 | + } |
| 74 | +} |
0 commit comments