Skip to content

Commit b9803f8

Browse files
committed
theme: Adds very basic theme support.
1 parent 2936285 commit b9803f8

File tree

14 files changed

+174
-16
lines changed

14 files changed

+174
-16
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_executable(${PROJECT_NAME}
3434
Includes/settingsMenu.cpp
3535
Includes/subAppRouter.cpp
3636
Includes/subsystems.cpp
37+
Includes/theme.cpp
3738
Includes/timing.cpp
3839
Includes/timeMenu.cpp
3940
Includes/videoMenu.cpp

Includes/config.cpp

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

192192
void to_json(nlohmann::json& j, Settings const& o) {
193-
j = nlohmann::json{ { "ftp", nlohmann::json(o.ftp) },
193+
j = nlohmann::json{ { "active_theme_directory", o.activeThemeDirectory },
194+
{ "ftp", nlohmann::json(o.ftp) },
194195
{ "mount", nlohmann::json(o.mount) },
195196
#ifdef NXDK
196197
{ "network", nlohmann::json(o.net) },
197198
#endif
198199
{ "logging", nlohmann::json(o.logging) },
199-
{ "homescreenConfig", nlohmann::json(o.homescreen) } };
200+
{ "homescreen", nlohmann::json(o.homescreen) } };
200201
}
201202

202203
void from_json(nlohmann::json const& j, Settings& o) {
204+
if (j.contains("active_theme_directory")) {
205+
o.activeThemeDirectory = j["active_theme_directory"];
206+
}
203207
if (j.contains("ftp")) {
204208
o.ftp = j["ftp"].get<ftpConfig>();
205209
}

Includes/config.hpp

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

112112
class Settings {
113113
public:
114-
Settings() = default;
114+
std::string activeThemeDirectory{ "default" };
115115
#ifdef NXDK
116116
netConfig net;
117117
#endif
@@ -131,6 +131,7 @@ class Config {
131131

132132
void setChanged();
133133
void storeToDisk();
134+
134135
Settings settings;
135136
nlohmann::json menu;
136137
};

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("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/theme.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
o.fontColor = j["font_color"];
40+
}
41+
}
42+
43+
void to_json(nlohmann::json& j, Theme::ImageSet const& o) {
44+
j = nlohmann::json{ { "480", nlohmann::json(o.image480p) },
45+
{ "720", nlohmann::json(o.image720p) } };
46+
}
47+
48+
void from_json(nlohmann::json const& j, Theme::ImageSet& o) {
49+
if (j.contains("480")) {
50+
o.image480p = j["480"];
51+
}
52+
if (j.contains("720")) {
53+
o.image720p = j["720"];
54+
}
55+
}
56+
57+
void to_json(nlohmann::json& j, Theme const& o) {
58+
j = nlohmann::json{ { "title", o.getTitle() },
59+
{ "menu", nlohmann::json(o.getMenu()) },
60+
{ "background", nlohmann::json(o.getBackground()) } };
61+
}
62+
63+
void from_json(nlohmann::json const& j, Theme& o) {
64+
if (j.contains("title")) {
65+
o.setTitle(j["title"]);
66+
}
67+
if (j.contains("menu")) {
68+
o.setMenu(j["menu"].get<Theme::MenuTheme>());
69+
}
70+
if (j.contains("background")) {
71+
o.setBackground(j["background"].get<Theme::ImageSet>());
72+
}
73+
}

Includes/theme.h

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

Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ SRCS += \
2020
$(INCDIR)/settingsMenu.cpp \
2121
$(INCDIR)/subAppRouter.cpp \
2222
$(INCDIR)/subsystems.cpp \
23+
$(INCDIR)/theme.cpp \
2324
$(INCDIR)/timeMenu.cpp \
2425
$(INCDIR)/timing.cpp \
2526
$(INCDIR)/videoMenu.cpp \
@@ -44,12 +45,17 @@ CFLAGS += -O2
4445
CXXFLAGS += -O2
4546
endif
4647

47-
new_all: copy_resources all
48-
4948
include $(NXDK_DIR)/Makefile
5049

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

5460
$(OUTPUT_DIR)/config.json: $(CURDIR)/sampleconfig.json
5561
@mkdir -p $(OUTPUT_DIR)

0 commit comments

Comments
 (0)