From 8909142fb4f17e85a4cae4ab697a00b6a2ec9a8a Mon Sep 17 00:00:00 2001 From: friedkeenan Date: Thu, 13 Sep 2018 14:16:33 -0500 Subject: [PATCH] Implement Theming (#57) * Implement theming and fix spelling errors, fixes issue #6. Now requires libconfig "separator" was spelled "seperator" * Added default case for when the system theme is not the values for light/dark. The default case acts like the system theme is the dark one. --- Makefile.nx | 2 +- Makefile.pc | 2 +- common/message-box.c | 2 +- common/theme.c | 130 ++++++++++++++++++++++++++++++++++--------- common/theme.h | 5 +- 5 files changed, 111 insertions(+), 30 deletions(-) diff --git a/Makefile.nx b/Makefile.nx index 5eef221..f86a0c0 100644 --- a/Makefile.nx +++ b/Makefile.nx @@ -53,7 +53,7 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 ASFLAGS := -g $(ARCH) LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) -LIBS := `freetype-config --libs` +LIBS := `freetype-config --libs` -lconfig #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing diff --git a/Makefile.pc b/Makefile.pc index ea24176..21694e9 100644 --- a/Makefile.pc +++ b/Makefile.pc @@ -26,7 +26,7 @@ test : pc_main/main.cpp pc_main/pc_launch.c \ build_pc/invalid_icon.bin.o build_pc/folder_icon.bin.o \ build_pc/hbmenu_logo_light.bin.o build_pc/hbmenu_logo_dark.bin.o \ #build_pc/tahoma24.o build_pc/tahoma12.o build_pc/interuimedium20.o build_pc/interuimedium30.o build_pc/interuiregular14.o build_pc/interuiregular18.o - gcc -Wall -O2 -g -DVERSION=\"v$(APP_VERSION)\" $(EXTRA_CFLAGS) `pkg-config freetype2 --cflags` $^ -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ `pkg-config freetype2 --libs` -lm -lz $(EXTRA_LDFLAGS) -I. -iquote $(DEVKITPRO)/libnx/include -Ibuild_pc -g -o $@ + gcc -Wall -O2 -g -DVERSION=\"v$(APP_VERSION)\" $(EXTRA_CFLAGS) `pkg-config freetype2 --cflags` $^ -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ `pkg-config freetype2 --libs` -lm -lz -lconfig $(EXTRA_LDFLAGS) -I. -iquote $(DEVKITPRO)/libnx/include -Ibuild_pc -g -o $@ build_pc/tahoma12.o : data/tahoma12.nxfnt mkdir -p $(dir $@) diff --git a/common/message-box.c b/common/message-box.c index d5bee7d..53f49ed 100644 --- a/common/message-box.c +++ b/common/message-box.c @@ -58,7 +58,7 @@ void drawMsgBoxBgToBuff(color_t *buff, int width, int height) { color = base_color; if (y == height - 80) { - color = themeCurrent.seperatorColor; + color = themeCurrent.separatorColor; } off = (y * width + x); diff --git a/common/theme.c b/common/theme.c index 7334290..459159b 100644 --- a/common/theme.c +++ b/common/theme.c @@ -6,42 +6,120 @@ #include "hbmenu_logo_light_bin.h" #include "hbmenu_logo_dark_bin.h" +bool colorFromSetting(config_setting_t *rgba, color_t *col) { + if(rgba == NULL) + return false; + *col = MakeColor(config_setting_get_int_elem(rgba, 0), config_setting_get_int_elem(rgba, 1), config_setting_get_int_elem(rgba, 2), config_setting_get_int_elem(rgba, 3)); + return true; +} + void themeStartup(ThemePreset preset) { + theme_t themeLight = (theme_t) { + .textColor = MakeColor(0, 0, 0, 255), + .frontWaveColor = MakeColor(100, 212, 250, 255), + .middleWaveColor = MakeColor(100, 153, 255, 255), + .backWaveColor = MakeColor(154, 171, 255, 255), + .backgroundColor = MakeColor(233, 236, 241, 255), + .highlightColor = MakeColor(91, 237, 224, 255), + .separatorColor = MakeColor(219, 218, 219, 255), + .enableWaveBlending = 0, + .buttonAText = "\uE0E0", + .buttonBText = "\uE0E1", + //.buttonAImage = button_a_light_bin, + //.buttonBImage = button_b_light_bin, + .hbmenuLogoImage = hbmenu_logo_light_bin + }; + + theme_t themeDark = (theme_t) { + .textColor = MakeColor(255, 255, 255, 255), + .frontWaveColor = MakeColor(96, 204, 204, 255), + .middleWaveColor = MakeColor(66, 154, 159, 255), + .backWaveColor = MakeColor(73, 103, 169, 255), + .backgroundColor = MakeColor(45, 45, 50, 255), + .highlightColor = MakeColor(91, 237, 224, 255), + .separatorColor = MakeColor(219, 218, 219, 255), + .enableWaveBlending = 0, + .buttonAText = "\uE0A0", + .buttonBText = "\uE0A1", + //.buttonAImage = button_a_dark_bin, + //.buttonBImage = button_b_dark_bin, + .hbmenuLogoImage = hbmenu_logo_dark_bin + }; + + char tmp_path[PATH_MAX] = {0}; + + #ifdef __SWITCH__ + tmp_path[0] = '/'; + #endif + + strncat(tmp_path, "config/nx-hbmenu/themes/theme.cfg", sizeof(tmp_path)-2); + + theme_t *themeDefault; + config_t cfg = {0}; + config_init(&cfg); + config_setting_t *theme; + color_t text, frontWave, middleWave, backWave, background, highlight, separator; + int waveBlending; + const char *AText, *BText; + bool good_cfg = config_read_file(&cfg, tmp_path); + switch (preset) { case THEME_PRESET_LIGHT: - themeCurrent = (theme_t) { - .textColor = MakeColor(0, 0, 0, 255), - .frontWaveColor = MakeColor(100, 212, 250, 255), - .middleWaveColor = MakeColor(100, 153, 255, 255), - .backWaveColor = MakeColor(154, 171, 255, 255), - .backgroundColor = MakeColor(233, 236, 241, 255), - .highlightColor = MakeColor(91, 237, 224, 255), - .seperatorColor = MakeColor(219, 218, 219, 255), - .enableWaveBlending = 0, - .buttonAText = "\uE0E0", - .buttonBText = "\uE0E1", - //.buttonAImage = button_a_light_bin, - //.buttonBImage = button_b_light_bin, - .hbmenuLogoImage = hbmenu_logo_light_bin - }; + default: + themeDefault = &themeLight; + if (good_cfg) + theme = config_lookup(&cfg, "lightTheme"); break; case THEME_PRESET_DARK: + themeDefault = &themeDark; + if (good_cfg) + theme = config_lookup(&cfg, "darkTheme"); + break; + } + + if (good_cfg) { + if (theme != NULL) { + if (!colorFromSetting(config_setting_lookup(theme, "textColor"), &text)) + text = themeDefault->textColor; + if (!colorFromSetting(config_setting_lookup(theme, "frontWaveColor"), &frontWave)) + frontWave = themeDefault->frontWaveColor; + if (!colorFromSetting(config_setting_lookup(theme, "middleWaveColor"), &middleWave)) + middleWave = themeDefault->middleWaveColor; + if (!colorFromSetting(config_setting_lookup(theme, "backWaveColor"), &backWave)) + backWave = themeDefault->backWaveColor; + if (!colorFromSetting(config_setting_lookup(theme, "backgroundColor"), &background)) + background = themeDefault->backgroundColor; + if (!colorFromSetting(config_setting_lookup(theme, "highlightColor"), &highlight)) + highlight = themeDefault->highlightColor; + if (!colorFromSetting(config_setting_lookup(theme, "separatorColor"), &separator)) + separator = themeDefault->separatorColor; + if (!config_setting_lookup_int(theme, "enableWaveBlending", &waveBlending)) + waveBlending = themeDefault->enableWaveBlending; + if (!config_setting_lookup_string(theme, "buttonAText", &AText)) + AText = themeDefault->buttonAText; + if (!config_setting_lookup_string(theme, "buttonBText", &BText)) + BText = themeDefault->buttonBText; themeCurrent = (theme_t) { - .textColor = MakeColor(255, 255, 255, 255), - .frontWaveColor = MakeColor(96, 204, 204, 255), - .middleWaveColor = MakeColor(66, 154, 159, 255), - .backWaveColor = MakeColor(73, 103, 169, 255), - .backgroundColor = MakeColor(45, 45, 50, 255), - .highlightColor = MakeColor(91, 237, 224, 255), - .seperatorColor = MakeColor(219, 218, 219, 255), - .enableWaveBlending = 0, - .buttonAText = "\uE0A0", - .buttonBText = "\uE0A1", + .textColor = text, + .frontWaveColor = frontWave, + .middleWaveColor = middleWave, + .backWaveColor = backWave, + .backgroundColor = background, + .highlightColor = highlight, + .separatorColor = separator, + .enableWaveBlending = waveBlending, + .buttonAText = AText, + .buttonBText = BText, //.buttonAImage = button_a_dark_bin, //.buttonBImage = button_b_dark_bin, .hbmenuLogoImage = hbmenu_logo_dark_bin }; - break; + } else { + themeCurrent = *themeDefault; + } + } else { + themeCurrent = *themeDefault; } + config_destroy(&cfg); } diff --git a/common/theme.h b/common/theme.h index 3c645f5..3b4204c 100644 --- a/common/theme.h +++ b/common/theme.h @@ -1,6 +1,7 @@ #pragma once #include "common.h" +#include typedef struct { @@ -10,7 +11,7 @@ typedef struct color_t backWaveColor; color_t backgroundColor; color_t highlightColor; - color_t seperatorColor; + color_t separatorColor; color_t activeColor; bool enableWaveBlending; const char *buttonAText; @@ -26,6 +27,8 @@ typedef enum THEME_PRESET_DARK, } ThemePreset; +bool colorFromSetting(config_setting_t *rgba, color_t *col); + void themeStartup(ThemePreset preset); theme_t themeCurrent;