2022-12-29 19:26:00 +01:00
|
|
|
#include <hex/api/theme_manager.hpp>
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/event_manager.hpp>
|
2022-12-29 19:26:00 +01:00
|
|
|
|
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2023-03-23 20:35:16 +01:00
|
|
|
namespace hex {
|
2022-12-29 19:26:00 +01:00
|
|
|
|
2023-07-26 13:50:51 +02:00
|
|
|
namespace {
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
AutoReset<std::map<std::string, nlohmann::json>> s_themes;
|
|
|
|
AutoReset<std::map<std::string, ThemeManager::ThemeHandler>> s_themeHandlers;
|
|
|
|
AutoReset<std::map<std::string, ThemeManager::StyleHandler>> s_styleHandlers;
|
|
|
|
AutoReset<std::string> s_imageTheme;
|
|
|
|
AutoReset<std::string> s_currTheme;
|
2023-07-26 13:50:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:26:00 +01:00
|
|
|
|
2023-02-16 18:06:40 +01:00
|
|
|
void ThemeManager::addThemeHandler(const std::string &name, const ColorMap &colorMap, const std::function<ImColor(u32)> &getFunction, const std::function<void(u32, ImColor)> &setFunction) {
|
2024-01-30 11:21:34 +01:00
|
|
|
(*s_themeHandlers)[name] = { colorMap, getFunction, setFunction };
|
2022-12-29 19:26:00 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 18:06:40 +01:00
|
|
|
void ThemeManager::addStyleHandler(const std::string &name, const StyleMap &styleMap) {
|
2024-01-30 11:21:34 +01:00
|
|
|
(*s_styleHandlers)[name] = { styleMap };
|
2022-12-29 19:26:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThemeManager::addTheme(const std::string &content) {
|
2023-11-16 09:32:32 +01:00
|
|
|
try {
|
|
|
|
auto theme = nlohmann::json::parse(content);
|
|
|
|
|
|
|
|
if (theme.contains("name") && theme.contains("colors")) {
|
2024-01-30 11:21:34 +01:00
|
|
|
(*s_themes)[theme["name"].get<std::string>()] = theme;
|
2023-11-16 09:32:32 +01:00
|
|
|
} else {
|
|
|
|
hex::log::error("Invalid theme file");
|
|
|
|
}
|
|
|
|
} catch (const nlohmann::json::parse_error &e) {
|
|
|
|
hex::log::error("Invalid theme file: {}", e.what());
|
2022-12-29 19:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ImColor> ThemeManager::parseColorString(const std::string &colorString) {
|
2023-02-17 10:02:43 +01:00
|
|
|
if (colorString == "auto")
|
|
|
|
return ImVec4(0, 0, 0, -1);
|
|
|
|
|
2022-12-29 19:26:00 +01:00
|
|
|
if (colorString.length() != 9 || colorString[0] != '#')
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
u32 color = 0;
|
|
|
|
|
|
|
|
for (u32 i = 1; i < 9; i++) {
|
|
|
|
color <<= 4;
|
|
|
|
if (colorString[i] >= '0' && colorString[i] <= '9')
|
|
|
|
color |= colorString[i] - '0';
|
|
|
|
else if (colorString[i] >= 'A' && colorString[i] <= 'F')
|
|
|
|
color |= colorString[i] - 'A' + 10;
|
|
|
|
else if (colorString[i] >= 'a' && colorString[i] <= 'f')
|
|
|
|
color |= colorString[i] - 'a' + 10;
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-02-17 10:02:43 +01:00
|
|
|
if (color == 0x00000000)
|
|
|
|
return ImVec4(0, 0, 0, -1);
|
|
|
|
|
2022-12-29 19:26:00 +01:00
|
|
|
return ImColor(hex::changeEndianess(color, std::endian::big));
|
|
|
|
}
|
|
|
|
|
2023-02-16 18:06:40 +01:00
|
|
|
nlohmann::json ThemeManager::exportCurrentTheme(const std::string &name) {
|
|
|
|
nlohmann::json theme = {
|
|
|
|
{ "name", name },
|
2023-07-04 08:42:33 +02:00
|
|
|
{ "image_theme", s_imageTheme },
|
2023-02-16 18:06:40 +01:00
|
|
|
{ "colors", {} },
|
|
|
|
{ "styles", {} },
|
|
|
|
{ "base", s_currTheme }
|
|
|
|
};
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
for (const auto &[type, handler] : *s_themeHandlers) {
|
2023-02-16 18:06:40 +01:00
|
|
|
theme["colors"][type] = {};
|
|
|
|
|
|
|
|
for (const auto &[key, value] : handler.colorMap) {
|
|
|
|
auto color = handler.getFunction(value);
|
|
|
|
theme["colors"][type][key] = fmt::format("#{:08X}", hex::changeEndianess(u32(color), std::endian::big));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
for (const auto &[type, handler] : *s_styleHandlers) {
|
2023-02-16 18:06:40 +01:00
|
|
|
theme["styles"][type] = {};
|
|
|
|
|
|
|
|
for (const auto &[key, style] : handler.styleMap) {
|
2023-12-27 16:33:49 +01:00
|
|
|
if (std::holds_alternative<float*>(style.value)) {
|
2023-02-16 18:06:40 +01:00
|
|
|
theme["styles"][type][key] = *std::get<float*>(style.value);
|
2023-12-27 16:33:49 +01:00
|
|
|
} else if (std::holds_alternative<ImVec2*>(style.value)) {
|
2023-02-16 18:06:40 +01:00
|
|
|
theme["styles"][type][key] = {
|
|
|
|
std::get<ImVec2*>(style.value)->x,
|
|
|
|
std::get<ImVec2*>(style.value)->y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:26:00 +01:00
|
|
|
void ThemeManager::changeTheme(std::string name) {
|
2024-01-30 11:21:34 +01:00
|
|
|
if (!s_themes->contains(name)) {
|
|
|
|
if (s_themes->empty()) {
|
2022-12-29 19:26:00 +01:00
|
|
|
return;
|
|
|
|
} else {
|
2024-01-30 11:21:34 +01:00
|
|
|
const std::string &defaultTheme = s_themes->begin()->first;
|
2022-12-29 19:26:00 +01:00
|
|
|
hex::log::error("Theme '{}' does not exist, using default theme '{}' instead!", name, defaultTheme);
|
|
|
|
name = defaultTheme;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto &theme = (*s_themes)[name];
|
2022-12-29 19:26:00 +01:00
|
|
|
|
|
|
|
if (theme.contains("base")) {
|
|
|
|
if (theme["base"].is_string()) {
|
2023-02-16 18:55:21 +01:00
|
|
|
if (theme["base"] != name)
|
|
|
|
changeTheme(theme["base"].get<std::string>());
|
2022-12-29 19:26:00 +01:00
|
|
|
} else {
|
|
|
|
hex::log::error("Theme '{}' has invalid base theme!", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
if (theme.contains("colors") && !s_themeHandlers->empty()) {
|
2022-12-29 19:26:00 +01:00
|
|
|
for (const auto&[type, content] : theme["colors"].items()) {
|
2024-01-30 11:21:34 +01:00
|
|
|
if (!s_themeHandlers->contains(type)) {
|
2022-12-29 19:26:00 +01:00
|
|
|
log::warn("No theme handler found for '{}'", type);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto &handler = (*s_themeHandlers)[type];
|
2023-02-16 18:06:40 +01:00
|
|
|
for (const auto &[key, value] : content.items()) {
|
|
|
|
if (!handler.colorMap.contains(key)) {
|
|
|
|
log::warn("No color found for '{}.{}'", type, key);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto color = parseColorString(value.get<std::string>());
|
|
|
|
if (!color.has_value()) {
|
|
|
|
log::warn("Invalid color '{}' for '{}.{}'", value.get<std::string>(), type, key);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
(*s_themeHandlers)[type].setFunction((*s_themeHandlers)[type].colorMap.at(key), color.value());
|
2023-02-16 18:06:40 +01:00
|
|
|
}
|
2022-12-29 19:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
if (theme.contains("styles") && !s_styleHandlers->empty()) {
|
2023-02-16 18:06:40 +01:00
|
|
|
for (const auto&[type, content] : theme["styles"].items()) {
|
2024-01-30 11:21:34 +01:00
|
|
|
if (!s_styleHandlers->contains(type)) {
|
2023-02-16 18:06:40 +01:00
|
|
|
log::warn("No style handler found for '{}'", type);
|
2022-12-29 19:26:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
auto &handler = (*s_styleHandlers)[type];
|
2023-02-16 18:06:40 +01:00
|
|
|
for (const auto &[key, value] : content.items()) {
|
|
|
|
if (!handler.styleMap.contains(key))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto &style = handler.styleMap.at(key);
|
2023-03-18 10:52:50 +01:00
|
|
|
const float scale = style.needsScaling ? 1_scaled : 1.0F;
|
2023-02-16 18:06:40 +01:00
|
|
|
|
|
|
|
if (value.is_number_float()) {
|
2024-01-30 11:21:34 +01:00
|
|
|
if (const auto newValue = std::get_if<float*>(&style.value); newValue != nullptr)
|
2023-03-18 10:52:50 +01:00
|
|
|
**newValue = value.get<float>() * scale;
|
2023-02-16 18:06:40 +01:00
|
|
|
else
|
|
|
|
log::warn("Style variable '{}' was of type ImVec2 but a float was expected.", name);
|
|
|
|
} else if (value.is_array() && value.size() == 2 && value[0].is_number_float() && value[1].is_number_float()) {
|
2024-01-30 11:21:34 +01:00
|
|
|
if (const auto newValue = std::get_if<ImVec2*>(&style.value); newValue != nullptr)
|
2023-03-18 10:52:50 +01:00
|
|
|
**newValue = ImVec2(value[0].get<float>() * scale, value[1].get<float>() * scale);
|
2023-02-16 18:06:40 +01:00
|
|
|
else
|
|
|
|
log::warn("Style variable '{}' was of type float but a ImVec2 was expected.", name);
|
|
|
|
} else {
|
|
|
|
hex::log::error("Theme '{}' has invalid style value for '{}.{}'!", name, type, key);
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 19:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-04 08:42:33 +02:00
|
|
|
if (theme.contains("image_theme")) {
|
|
|
|
if (theme["image_theme"].is_string()) {
|
|
|
|
s_imageTheme = theme["image_theme"].get<std::string>();
|
2022-12-29 19:26:00 +01:00
|
|
|
} else {
|
2023-07-04 08:42:33 +02:00
|
|
|
hex::log::error("Theme '{}' has invalid image theme!", name);
|
|
|
|
s_imageTheme = "dark";
|
2022-12-29 19:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-16 18:06:40 +01:00
|
|
|
|
|
|
|
s_currTheme = name;
|
2023-05-22 12:00:35 +02:00
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventThemeChanged::post();
|
2022-12-29 19:26:00 +01:00
|
|
|
}
|
|
|
|
|
2023-07-04 08:42:33 +02:00
|
|
|
const std::string &ThemeManager::getImageTheme() {
|
|
|
|
return s_imageTheme;
|
2022-12-29 19:26:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ThemeManager::getThemeNames() {
|
|
|
|
std::vector<std::string> themeNames;
|
2024-01-30 11:21:34 +01:00
|
|
|
for (const auto &[name, theme] : *s_themes)
|
2022-12-29 19:26:00 +01:00
|
|
|
themeNames.push_back(name);
|
|
|
|
|
|
|
|
return themeNames;
|
|
|
|
}
|
|
|
|
|
2023-01-05 09:29:16 +01:00
|
|
|
void ThemeManager::reset() {
|
2024-01-30 11:21:34 +01:00
|
|
|
s_themes->clear();
|
|
|
|
s_styleHandlers->clear();
|
|
|
|
s_themeHandlers->clear();
|
|
|
|
s_imageTheme->clear();
|
|
|
|
s_currTheme->clear();
|
2023-07-26 13:50:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::map<std::string, ThemeManager::ThemeHandler> &ThemeManager::getThemeHandlers() {
|
|
|
|
return s_themeHandlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, ThemeManager::StyleHandler> &ThemeManager::getStyleHandlers() {
|
|
|
|
return s_styleHandlers;
|
2023-01-05 09:29:16 +01:00
|
|
|
}
|
|
|
|
|
2022-12-29 19:26:00 +01:00
|
|
|
}
|