Skip to content

Commit a504757

Browse files
committed
theme: Adds very basic theme support.
1 parent c936b6d commit a504757

File tree

15 files changed

+184
-18
lines changed

15 files changed

+184
-18
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_executable(${PROJECT_NAME}
3535
Includes/sntpClient.cpp
3636
Includes/subAppRouter.cpp
3737
Includes/subsystems.cpp
38+
Includes/theme.cpp
3839
Includes/timing.cpp
3940
Includes/timeMenu.cpp
4041
Includes/videoMenu.cpp

Includes/config.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,20 @@ void from_json(nlohmann::json const& j, homescreenConfig& o) {
212212
}
213213

214214
void to_json(nlohmann::json& j, Settings const& o) {
215-
j = nlohmann::json{ { "ftp", nlohmann::json(o.ftp) },
215+
j = nlohmann::json{ { "active_theme_directory", o.activeThemeDirectory },
216+
{ "ftp", nlohmann::json(o.ftp) },
216217
{ "mount", nlohmann::json(o.mount) },
217218
#ifdef NXDK
218219
{ "network", nlohmann::json(o.net) },
219220
#endif
220221
{ "logging", nlohmann::json(o.logging) },
221-
{ "homescreenConfig", nlohmann::json(o.homescreen) } };
222+
{ "homescreen", nlohmann::json(o.homescreen) } };
222223
}
223224

224225
void from_json(nlohmann::json const& j, Settings& o) {
226+
if (j.contains("active_theme_directory")) {
227+
o.activeThemeDirectory = j["active_theme_directory"];
228+
}
225229
if (j.contains("ftp")) {
226230
o.ftp = j["ftp"].get<ftpConfig>();
227231
}

Includes/config.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void from_json(nlohmann::json const& j, homescreenConfig& o);
133133

134134
class Settings {
135135
public:
136-
Settings() = default;
136+
std::string activeThemeDirectory{ "default" };
137137
#ifdef NXDK
138138
netConfig net;
139139
sntpConfig sntp;
@@ -154,6 +154,7 @@ class Config {
154154

155155
void setChanged();
156156
void storeToDisk();
157+
157158
Settings settings;
158159
nlohmann::json menu;
159160
};

Includes/renderer.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,12 @@ int Renderer::init() {
5959
return 0;
6060
}
6161

62-
int Renderer::init(const char* bgpath) {
62+
int Renderer::init(std::string const& backgroundImagePath) {
6363
int ret = init();
6464
if (ret != 0) {
6565
return ret;
6666
}
67-
char* bgname = (char*)malloc(strlen(bgpath) + 10);
68-
sprintf(bgname, "%s%d.png", bgpath, height);
69-
SDL_Surface* bgsurf = IMG_Load(bgname);
70-
free(bgname);
67+
SDL_Surface* bgsurf = IMG_Load(backgroundImagePath.c_str());
7168
if (bgsurf == nullptr) {
7269
InfoLog::outputLine(InfoLog::ERROR, "Creating background surface failed.\n");
7370
return 3;

Includes/renderer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define RENDERER_H
33

44
#include <SDL.h>
5+
#include <string>
56
#include <vector>
67

78
int min(int lhs, int rhs);
@@ -13,7 +14,7 @@ class Renderer {
1314
~Renderer();
1415

1516
int init();
16-
int init(const char* bg);
17+
int init(std::string const& backgroundImagePath);
1718
int clear();
1819
void flip();
1920

Includes/sntpClient.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ void sntpClient::updateTime() const {
7474
message.originTimestamp.fractionalSeconds =
7575
htonl(message.originTimestamp.fractionalSeconds);
7676
if (message.originTimestamp.seconds || message.originTimestamp.fractionalSeconds) {
77-
InfoLog::outputLine(InfoLog::INFO, "SNTP: Origin epoch is not 0: %lld\n", message.originTimestamp);
77+
InfoLog::outputLine(InfoLog::INFO, "SNTP: Origin epoch is not 0: %lld\n",
78+
message.originTimestamp);
7879
}
7980

8081
message.transmitTimestamp.seconds = htonl(message.transmitTimestamp.seconds);
@@ -95,7 +96,8 @@ void sntpClient::updateTime() const {
9596
}
9697

9798
if (delta > allowedDriftSeconds) {
98-
InfoLog::outputLine(InfoLog::DEBUG, "SNTP: Updating system clock (%llu seconds of drift)\n", delta);
99+
InfoLog::outputLine(InfoLog::DEBUG,
100+
"SNTP: Updating system clock (%llu seconds of drift)\n", delta);
99101
NTSTATUS status = NtSetSystemTime(&serverTime, nullptr);
100102
if (!NT_SUCCESS(status)) {
101103
InfoLog::outputLine(InfoLog::INFO, "SNTP: NtSetSystemTime failed: %X\n", status);

Includes/theme.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

Includes/theme.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef NEVOLUTIONX_THEME_H
2+
#define NEVOLUTIONX_THEME_H
3+
4+
#include <string>
5+
6+
#pragma clang diagnostic push
7+
#pragma clang diagnostic ignored "-Wreturn-type"
8+
#include "../3rdparty/json.hpp"
9+
#pragma clang diagnostic pop
10+
11+
12+
class Theme {
13+
public:
14+
struct MenuTheme {
15+
std::string font;
16+
// TODO: Actually support this in Font.
17+
unsigned int fontColor;
18+
};
19+
20+
struct ImageSet {
21+
std::string image480p;
22+
std::string image720p;
23+
};
24+
25+
explicit Theme(std::string themeDirectory);
26+
27+
void setTitle(std::string const& val) { title = val; }
28+
std::string const& getTitle() const { return title; }
29+
30+
void setBackground(ImageSet const& val) { background = val; }
31+
ImageSet const& getBackground() const { return background; }
32+
33+
void setMenu(MenuTheme const& val) { menu = val; }
34+
MenuTheme const& getMenu() const { return menu; }
35+
36+
std::string getAbsolutePath(std::string const& subpath) const {
37+
return rootPath + subpath;
38+
}
39+
40+
private:
41+
void load();
42+
43+
std::string rootPath;
44+
std::string title{ "??MISSING??" };
45+
ImageSet background;
46+
MenuTheme menu;
47+
};
48+
49+
void to_json(nlohmann::json& j, Theme const& o);
50+
void from_json(nlohmann::json const& j, Theme& o);
51+
52+
void to_json(nlohmann::json& j, Theme::MenuTheme const& o);
53+
void from_json(nlohmann::json const& j, Theme::MenuTheme& o);
54+
55+
void to_json(nlohmann::json& j, Theme::ImageSet const& o);
56+
void from_json(nlohmann::json const& j, Theme::ImageSet& o);
57+
58+
#endif // NEVOLUTIONX_THEME_H

Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SRCS += \
2121
$(INCDIR)/sntpClient.cpp \
2222
$(INCDIR)/subAppRouter.cpp \
2323
$(INCDIR)/subsystems.cpp \
24+
$(INCDIR)/theme.cpp \
2425
$(INCDIR)/timeMenu.cpp \
2526
$(INCDIR)/timing.cpp \
2627
$(INCDIR)/videoMenu.cpp \
@@ -45,12 +46,17 @@ CFLAGS += -O2
4546
CXXFLAGS += -O2
4647
endif
4748

48-
new_all: copy_resources all
49-
5049
include $(NXDK_DIR)/Makefile
5150

52-
copy_resources: $(OUTPUT_DIR)/config.json
53-
@cp $(RESOURCEDIR)/480.png $(RESOURCEDIR)/720.png $(RESOURCEDIR)/vegur.ttf $(OUTPUT_DIR)
51+
RESOURCES = \
52+
$(OUTPUT_DIR)/config.json \
53+
$(patsubst $(CURDIR)/Resources/%,$(OUTPUT_DIR)/%,$(wildcard $(CURDIR)/Resources/NeXThemes/*))
54+
TARGET += $(RESOURCES)
55+
$(GEN_XISO): $(RESOURCES)
56+
57+
$(OUTPUT_DIR)/NeXThemes/%: $(CURDIR)/Resources/NeXThemes/%
58+
$(VE)mkdir -p '$(dir $@)'
59+
$(VE)cp -r '$<' '$@'
5460

5561
$(OUTPUT_DIR)/config.json: $(CURDIR)/sampleconfig.json
5662
@mkdir -p $(OUTPUT_DIR)
File renamed without changes.

0 commit comments

Comments
 (0)