-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
theme: Adds very basic theme support. #112
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef NEVOLUTIONX_THEME_H | ||
#define NEVOLUTIONX_THEME_H | ||
|
||
#include <string> | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wreturn-type" | ||
#include "../3rdparty/json.hpp" | ||
#pragma clang diagnostic pop | ||
|
||
|
||
class Theme { | ||
public: | ||
struct MenuTheme { | ||
std::string font; | ||
// TODO: Actually support this in Font. | ||
unsigned int fontColor; | ||
}; | ||
|
||
struct ImageSet { | ||
std::string image480p; | ||
std::string image720p; | ||
}; | ||
|
||
explicit Theme(std::string themeDirectory); | ||
|
||
void setTitle(std::string const& val) { title = val; } | ||
std::string const& getTitle() const { return title; } | ||
|
||
void setBackground(ImageSet const& val) { background = val; } | ||
ImageSet const& getBackground() const { return background; } | ||
|
||
void setMenu(MenuTheme const& val) { menu = val; } | ||
MenuTheme const& getMenu() const { return menu; } | ||
|
||
std::string getAbsolutePath(std::string const& subpath) const { | ||
return rootPath + subpath; | ||
} | ||
|
||
private: | ||
void load(); | ||
|
||
std::string rootPath; | ||
std::string title{ "??MISSING??" }; | ||
ImageSet background; | ||
MenuTheme menu; | ||
}; | ||
|
||
void to_json(nlohmann::json& j, Theme const& o); | ||
void from_json(nlohmann::json const& j, Theme& o); | ||
|
||
void to_json(nlohmann::json& j, Theme::MenuTheme const& o); | ||
void from_json(nlohmann::json const& j, Theme::MenuTheme& o); | ||
|
||
void to_json(nlohmann::json& j, Theme::ImageSet const& o); | ||
void from_json(nlohmann::json const& j, Theme::ImageSet& o); | ||
|
||
#endif // NEVOLUTIONX_THEME_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"title": "Default", | ||
"background": { | ||
"480": "480.png", | ||
"720": "720.png" | ||
}, | ||
"menu": { | ||
"font": "vegur.ttf", | ||
"font_color": "FFFFFF" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,16 +212,20 @@ void from_json(nlohmann::json const& j, homescreenConfig& o) { | |
} | ||
|
||
void to_json(nlohmann::json& j, Settings const& o) { | ||
j = nlohmann::json{ { "ftp", nlohmann::json(o.ftp) }, | ||
j = nlohmann::json{ { "active_theme_directory", o.activeThemeDirectory }, | ||
{ "ftp", nlohmann::json(o.ftp) }, | ||
{ "mount", nlohmann::json(o.mount) }, | ||
#ifdef NXDK | ||
{ "network", nlohmann::json(o.net) }, | ||
#endif | ||
{ "logging", nlohmann::json(o.logging) }, | ||
{ "homescreenConfig", nlohmann::json(o.homescreen) } }; | ||
{ "homescreen", nlohmann::json(o.homescreen) } }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: specific reason why this was changed? Listing it as config may make it easier for outside devs to understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's been a long time, but looking a few lines down at line 246 it's specifically looking for "homescreen" when reading the config file, so I think saving the config would always cause the default values to be used. It also strikes me as more consistent to use "homescreen" in the same way that "logging", etc... is used rather than "loggingConfig". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Edit: Sounds Good, voice to text is really losing today |
||
} | ||
|
||
void from_json(nlohmann::json const& j, Settings& o) { | ||
if (j.contains("active_theme_directory")) { | ||
o.activeThemeDirectory = j["active_theme_directory"]; | ||
} | ||
if (j.contains("ftp")) { | ||
o.ftp = j["ftp"].get<ftpConfig>(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "theme.hpp" | ||
#include <fstream> | ||
#include <utility> | ||
|
||
#define THEME_JSON_FILE_NAME "theme.json" | ||
|
||
Theme::Theme(std::string themeDirectory) : rootPath(std::move(themeDirectory)) { | ||
if (rootPath.back() != '\\') { | ||
rootPath += "\\"; | ||
} | ||
load(); | ||
} | ||
|
||
void Theme::load() { | ||
std::string themeFilePath = rootPath + THEME_JSON_FILE_NAME; | ||
|
||
std::ifstream themeFile(themeFilePath); | ||
nlohmann::json json; | ||
// FIXME: Once nxdk supports C++ Exceptions, this needs to be put in a try-catch block! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is supported now is it not? |
||
themeFile >> json; | ||
from_json(json, *this); | ||
|
||
themeFile.close(); | ||
} | ||
|
||
void to_json(nlohmann::json& j, Theme::MenuTheme const& o) { | ||
char fontColor[16] = { 0 }; | ||
snprintf(fontColor, 15, "%X", o.fontColor); | ||
|
||
j = nlohmann::json{ { "font", nlohmann::json(o.font) }, | ||
{ "font_color", nlohmann::json(fontColor) } }; | ||
} | ||
|
||
void from_json(nlohmann::json const& j, Theme::MenuTheme& o) { | ||
if (j.contains("font")) { | ||
o.font = j["font"]; | ||
} | ||
if (j.contains("font_color")) { | ||
std::string const& colorString = j["font_color"]; | ||
sscanf(colorString.c_str(), "%X", &o.fontColor); | ||
} | ||
} | ||
|
||
void to_json(nlohmann::json& j, Theme::ImageSet const& o) { | ||
j = nlohmann::json{ { "480", nlohmann::json(o.image480p) }, | ||
{ "720", nlohmann::json(o.image720p) } }; | ||
} | ||
|
||
void from_json(nlohmann::json const& j, Theme::ImageSet& o) { | ||
if (j.contains("480")) { | ||
o.image480p = j["480"]; | ||
} | ||
if (j.contains("720")) { | ||
o.image720p = j["720"]; | ||
} | ||
} | ||
|
||
void to_json(nlohmann::json& j, Theme const& o) { | ||
j = nlohmann::json{ { "title", o.getTitle() }, | ||
{ "menu", nlohmann::json(o.getMenu()) }, | ||
{ "background", nlohmann::json(o.getBackground()) } }; | ||
} | ||
|
||
void from_json(nlohmann::json const& j, Theme& o) { | ||
if (j.contains("title")) { | ||
o.setTitle(j["title"]); | ||
} | ||
if (j.contains("menu")) { | ||
o.setMenu(j["menu"].get<Theme::MenuTheme>()); | ||
} | ||
if (j.contains("background")) { | ||
o.setBackground(j["background"].get<Theme::ImageSet>()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"settings": | ||
{ | ||
"active_theme_directory": "default", | ||
"network": | ||
{ | ||
"enable": true, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit: font not Font
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure I was referring to the
Font
class, so I think the capitalization is correct. Making it "font" would be ambiguous with the member var just above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also sounds good