ppsspp/UI/Theme.cpp

262 lines
8.5 KiB
C++
Raw Normal View History

2022-02-11 11:32:23 +00:00
// Copyright (c) 2013- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "ppsspp_config.h"
#include <algorithm>
#include "UI/Theme.h"
#include "Common/File/FileUtil.h"
#include "Common/File/VFS/VFS.h"
#include "Common/Data/Format/IniFile.h"
2022-02-12 17:30:55 +00:00
#include "Common/File/DirListing.h"
#include "Common/Log/LogManager.h"
2022-02-11 11:32:23 +00:00
#include "Core/Config.h"
#include "Common/UI/View.h"
#include "Common/UI/Context.h"
#include "Core/System.h"
2022-02-12 17:30:55 +00:00
struct ThemeInfo {
2022-02-11 11:32:23 +00:00
std::string name;
2022-07-07 11:13:17 +00:00
uint32_t uItemStyleFg = 0xFFFFFFFF;
2022-07-08 04:43:26 +00:00
uint32_t uItemStyleBg = 0x55000000;
2022-02-11 11:32:23 +00:00
uint32_t uItemFocusedStyleFg = 0xFFFFFFFF;
2022-07-08 04:43:26 +00:00
uint32_t uItemFocusedStyleBg = 0xFFEDC24C;
2022-02-11 11:32:23 +00:00
uint32_t uItemDownStyleFg = 0xFFFFFFFF;
2022-07-08 04:43:26 +00:00
uint32_t uItemDownStyleBg = 0xFFBD9939;
uint32_t uItemDisabledStyleFg = 0x80EEEEEE;
uint32_t uItemDisabledStyleBg = 0x55000000;
2022-02-11 11:32:23 +00:00
uint32_t uHeaderStyleFg = 0xFFFFFFFF;
uint32_t uInfoStyleFg = 0xFFFFFFFF;
uint32_t uInfoStyleBg = 0x00000000;
2022-07-08 04:43:26 +00:00
uint32_t uPopupStyleBg = 0xFF303030;
uint32_t uBackgroundColor = 0xFF754D24;
2022-02-11 11:32:23 +00:00
2022-07-07 11:13:17 +00:00
std::string sUIAtlas = "ui_atlas";
2022-03-31 15:03:34 +00:00
2022-02-11 11:32:23 +00:00
bool operator == (const std::string &other) {
return name == other;
}
2022-02-12 17:30:55 +00:00
bool operator == (const ThemeInfo &other) {
2022-02-11 11:32:23 +00:00
return name == other.name;
}
};
static UI::Theme ui_theme;
2022-02-12 17:30:55 +00:00
static std::vector<ThemeInfo> themeInfos;
2022-02-11 11:32:23 +00:00
2022-03-31 15:03:34 +00:00
static Atlas ui_atlas;
static Atlas font_atlas;
2022-02-11 11:32:23 +00:00
static void LoadThemeInfo(const std::vector<Path> &directories) {
themeInfos.clear();
2022-02-12 17:30:55 +00:00
ThemeInfo def{};
2022-02-11 11:32:23 +00:00
def.name = "Default";
themeInfos.push_back(def);
2022-02-14 06:37:56 +00:00
// This will update the theme if already present, as such default in assets/theme will get priority if exist
2022-02-12 17:30:55 +00:00
auto appendTheme = [&](const ThemeInfo &info) {
2022-02-11 11:32:23 +00:00
auto beginErase = std::remove(themeInfos.begin(), themeInfos.end(), info.name);
if (beginErase != themeInfos.end()) {
themeInfos.erase(beginErase, themeInfos.end());
}
themeInfos.push_back(info);
};
for (size_t d = 0; d < directories.size(); d++) {
std::vector<File::FileInfo> fileInfo;
g_VFS.GetFileListing(directories[d].c_str(), &fileInfo, "ini:");
2022-02-11 11:32:23 +00:00
if (fileInfo.empty()) {
File::GetFilesInDir(directories[d], &fileInfo, "ini:");
}
for (size_t f = 0; f < fileInfo.size(); f++) {
IniFile ini;
bool success = false;
2022-05-26 05:07:59 +00:00
if (fileInfo[f].isDirectory)
continue;
2022-02-11 11:32:23 +00:00
Path name = fileInfo[f].fullName;
Path path = directories[d];
// Hack around Android VFS path bug. really need to redesign this.
if (name.ToString().substr(0, 7) == "assets/")
name = Path(name.ToString().substr(7));
if (path.ToString().substr(0, 7) == "assets/")
path = Path(path.ToString().substr(7));
if (ini.LoadFromVFS(g_VFS, name.ToString()) || ini.Load(fileInfo[f].fullName)) {
2022-02-11 11:32:23 +00:00
success = true;
}
if (!success)
continue;
2023-01-10 14:36:32 +00:00
// Alright, let's loop through the sections and see if any is a theme.
2022-02-11 11:32:23 +00:00
for (size_t i = 0; i < ini.Sections().size(); i++) {
Section &section = *(ini.Sections()[i].get());
2024-01-31 11:20:46 +00:00
if (section.name().empty()) {
continue;
}
2022-02-12 17:30:55 +00:00
ThemeInfo info;
2022-02-11 11:32:23 +00:00
section.Get("Name", &info.name, section.name().c_str());
2022-02-14 06:37:56 +00:00
section.Get("ItemStyleFg", &info.uItemStyleFg, info.uItemStyleFg);
section.Get("ItemStyleBg", &info.uItemStyleBg, info.uItemStyleBg);
section.Get("ItemFocusedStyleFg", &info.uItemFocusedStyleFg, info.uItemFocusedStyleFg);
section.Get("ItemFocusedStyleBg", &info.uItemFocusedStyleBg, info.uItemFocusedStyleBg);
section.Get("ItemDownStyleFg", &info.uItemDownStyleFg, info.uItemDownStyleFg);
section.Get("ItemDownStyleBg", &info.uItemDownStyleBg, info.uItemDownStyleBg);
section.Get("ItemDisabledStyleFg", &info.uItemDisabledStyleFg, info.uItemDisabledStyleFg);
section.Get("ItemDisabledStyleBg", &info.uItemDisabledStyleBg, info.uItemDisabledStyleBg);
section.Get("HeaderStyleFg", &info.uHeaderStyleFg, info.uHeaderStyleFg);
section.Get("InfoStyleFg", &info.uInfoStyleFg, info.uInfoStyleFg);
section.Get("InfoStyleBg", &info.uInfoStyleBg, info.uInfoStyleBg);
section.Get("PopupStyleBg", &info.uPopupStyleBg, info.uPopupStyleBg);
section.Get("BackgroundColor", &info.uBackgroundColor, info.uBackgroundColor);
2022-02-11 11:32:23 +00:00
2022-03-31 15:03:34 +00:00
std::string tmpPath;
section.Get("UIAtlas", &tmpPath, "");
if (!tmpPath.empty()) {
2022-03-31 15:03:34 +00:00
tmpPath = (path / tmpPath).ToString();
File::FileInfo tmpInfo;
if (g_VFS.GetFileInfo((tmpPath + ".meta").c_str(), &tmpInfo) && g_VFS.GetFileInfo((tmpPath + ".zim").c_str(), &tmpInfo)) {
2022-07-07 11:13:17 +00:00
info.sUIAtlas = tmpPath;
2022-03-31 15:03:34 +00:00
}
}
2022-02-11 11:32:23 +00:00
appendTheme(info);
}
}
}
}
2022-02-12 17:30:55 +00:00
static UI::Style MakeStyle(uint32_t fg, uint32_t bg) {
2022-02-11 11:32:23 +00:00
UI::Style s;
s.background = UI::Drawable(bg);
s.fgColor = fg;
return s;
}
2022-03-31 15:03:34 +00:00
static void LoadAtlasMetadata(Atlas &metadata, const char *filename, bool required) {
size_t atlas_data_size = 0;
const uint8_t *atlas_data = g_VFS.ReadFile(filename, &atlas_data_size);
2022-04-24 12:13:47 +00:00
bool load_success = atlas_data != nullptr && metadata.Load(atlas_data, atlas_data_size);
if (!load_success) {
if (required)
ERROR_LOG(Log::G3D, "Failed to load %s - graphics will be broken", filename);
2022-04-24 12:13:47 +00:00
else
WARN_LOG(Log::G3D, "Failed to load %s", filename);
2022-04-24 12:13:47 +00:00
// Stumble along with broken visuals instead of dying...
2022-03-31 15:03:34 +00:00
}
2022-04-24 12:13:47 +00:00
delete[] atlas_data;
2022-03-31 15:03:34 +00:00
}
void UpdateTheme(UIContext *ctx) {
2022-02-11 11:32:23 +00:00
// First run, get the default in at least
if (themeInfos.empty()) {
ReloadAllThemeInfo();
}
size_t i;
for (i = 0; i < themeInfos.size(); ++i) {
if (themeInfos[i].name == g_Config.sThemeName) {
break;
}
}
// Reset to Default if not found
if (i >= themeInfos.size()) {
g_Config.sThemeName = "Default";
i = 0;
}
#if defined(USING_WIN_UI) || PPSSPP_PLATFORM(UWP) || defined(USING_QT_UI)
ui_theme.uiFont = UI::FontStyle(FontID("UBUNTU24"), g_Config.sFont.c_str(), 22);
ui_theme.uiFontSmall = UI::FontStyle(FontID("UBUNTU24"), g_Config.sFont.c_str(), 17);
ui_theme.uiFontSmaller = UI::FontStyle(FontID("UBUNTU24"), g_Config.sFont.c_str(), 13);
2022-02-11 11:32:23 +00:00
#else
ui_theme.uiFont = UI::FontStyle(FontID("UBUNTU24"), "", 20);
ui_theme.uiFontSmall = UI::FontStyle(FontID("UBUNTU24"), "", 15);
ui_theme.uiFontSmaller = UI::FontStyle(FontID("UBUNTU24"), "", 12);
2022-02-11 11:32:23 +00:00
#endif
ui_theme.checkOn = ImageID("I_CHECKEDBOX");
ui_theme.checkOff = ImageID("I_SQUARE");
ui_theme.whiteImage = ImageID("I_SOLIDWHITE");
ui_theme.sliderKnob = ImageID("I_CIRCLE");
ui_theme.dropShadow4Grid = ImageID("I_DROP_SHADOW");
// Actual configurable themes setting start here
ui_theme.itemStyle = MakeStyle(themeInfos[i].uItemStyleFg, themeInfos[i].uItemStyleBg);
ui_theme.itemFocusedStyle = MakeStyle(themeInfos[i].uItemFocusedStyleFg, themeInfos[i].uItemFocusedStyleBg);
ui_theme.itemDownStyle = MakeStyle(themeInfos[i].uItemDownStyleFg, themeInfos[i].uItemDownStyleBg);
ui_theme.itemDisabledStyle = MakeStyle(themeInfos[i].uItemDisabledStyleFg, themeInfos[i].uItemDisabledStyleBg);
ui_theme.headerStyle.fgColor = themeInfos[i].uHeaderStyleFg;
ui_theme.infoStyle = MakeStyle(themeInfos[i].uInfoStyleFg, themeInfos[i].uInfoStyleBg);
ui_theme.popupStyle = MakeStyle(themeInfos[i].uItemStyleFg, themeInfos[i].uPopupStyleBg);
ui_theme.backgroundColor = themeInfos[i].uBackgroundColor;
2022-03-31 15:03:34 +00:00
// Load any missing atlas metadata (the images are loaded from UIContext).
2022-07-07 11:13:17 +00:00
LoadAtlasMetadata(ui_atlas, (themeInfos[i].sUIAtlas + ".meta").c_str(), true);
2022-03-31 15:03:34 +00:00
#if !(PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(ANDROID))
LoadAtlasMetadata(font_atlas, "font_atlas.meta", ui_atlas.num_fonts == 0);
#else
LoadAtlasMetadata(font_atlas, "asciifont_atlas.meta", ui_atlas.num_fonts == 0);
#endif
2022-07-07 11:13:17 +00:00
ctx->setUIAtlas(themeInfos[i].sUIAtlas + ".zim");
2022-02-11 11:32:23 +00:00
}
UI::Theme *GetTheme() {
return &ui_theme;
}
2022-03-31 15:03:34 +00:00
Atlas *GetFontAtlas() {
return &font_atlas;
}
Atlas *GetUIAtlas() {
return &ui_atlas;
}
2022-02-11 11:32:23 +00:00
void ReloadAllThemeInfo() {
std::vector<Path> directories;
directories.push_back(Path("themes")); // For VFS
directories.push_back(GetSysDirectory(DIRECTORY_CUSTOM_THEMES));
LoadThemeInfo(directories);
}
std::vector<std::string> GetThemeInfoNames() {
std::vector<std::string> names;
for (auto& i : themeInfos)
names.push_back(i.name);
return names;
}