2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_settings.hpp"
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2022-02-21 21:46:25 +01:00
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
#include <content/popups/popup_question.hpp>
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ViewSettings::ViewSettings() : View("hex.builtin.view.settings.name") {
|
2023-08-29 12:14:12 +02:00
|
|
|
// Handle window open requests
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<RequestOpenWindow>(this, [this](const std::string &name) {
|
2021-04-18 20:24:42 +02:00
|
|
|
if (name == "Settings") {
|
2022-09-06 10:50:17 +02:00
|
|
|
TaskManager::doLater([this] {
|
|
|
|
ImGui::OpenPopup(View::toWindowName("hex.builtin.view.settings.name").c_str());
|
|
|
|
this->getWindowOpenState() = true;
|
|
|
|
});
|
2021-01-27 14:26:24 +01:00
|
|
|
}
|
|
|
|
});
|
2022-01-23 02:28:38 +01:00
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Add the settings menu item to the Extras menu
|
2023-05-12 08:38:32 +02:00
|
|
|
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.extras" }, 3000);
|
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.view.settings.name"_lang }, 4000, Shortcut::None, [&, this] {
|
2023-03-20 13:11:43 +01:00
|
|
|
TaskManager::doLater([] { ImGui::OpenPopup(View::toWindowName("hex.builtin.view.settings.name").c_str()); });
|
|
|
|
this->getWindowOpenState() = true;
|
2022-01-23 02:28:38 +01:00
|
|
|
});
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewSettings::~ViewSettings() {
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::unsubscribe<RequestOpenWindow>(this);
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewSettings::drawContent() {
|
|
|
|
|
2021-12-12 13:35:23 +01:00
|
|
|
if (ImGui::BeginPopupModal(View::toWindowName("hex.builtin.view.settings.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoResize)) {
|
2021-09-20 23:40:36 +02:00
|
|
|
if (ImGui::BeginTabBar("settings")) {
|
2023-10-21 23:07:33 +02:00
|
|
|
auto &categories = ContentRegistry::Settings::impl::getSettings();
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
// Draw all categories
|
|
|
|
for (auto &category : categories) {
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
// Skip empty categories
|
|
|
|
if (category.subCategories.empty())
|
|
|
|
continue;
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
// For each category, create a new tab
|
|
|
|
if (ImGui::BeginTabItem(LangEntry(category.unlocalizedName))) {
|
|
|
|
// Draw the category description
|
|
|
|
if (!category.unlocalizedDescription.empty()) {
|
|
|
|
ImGui::TextFormattedWrapped("{}", LangEntry(category.unlocalizedDescription));
|
|
|
|
ImGui::NewLine();
|
|
|
|
}
|
2023-08-29 12:14:12 +02:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
bool firstSubCategory = true;
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
// Draw all settings of that category
|
|
|
|
for (auto &subCategory : category.subCategories) {
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
// Skip empty subcategories
|
|
|
|
if (subCategory.entries.empty())
|
|
|
|
continue;
|
2023-08-29 12:14:12 +02:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
if (!subCategory.unlocalizedName.empty())
|
|
|
|
ImGui::Header(LangEntry(subCategory.unlocalizedName), firstSubCategory);
|
2022-10-02 17:30:26 +02:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
firstSubCategory = false;
|
2021-09-20 23:40:36 +02:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, scaled({5, 5}));
|
|
|
|
if (ImGui::BeginTable("##subCategory", 1, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_SizingStretchSame)) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
for (auto &setting : subCategory.entries) {
|
|
|
|
ImGui::BeginDisabled(!setting.widget->isEnabled());
|
|
|
|
bool settingChanged = setting.widget->draw(LangEntry(setting.unlocalizedName));
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
|
|
|
|
if (auto tooltip = setting.widget->getTooltip(); tooltip.has_value() && ImGui::IsItemHovered())
|
|
|
|
ImGui::InfoTooltip(LangEntry(tooltip.value()));
|
|
|
|
|
|
|
|
auto &widget = setting.widget;
|
|
|
|
|
|
|
|
// Handle a setting being changed
|
|
|
|
if (settingChanged) {
|
|
|
|
auto newValue = widget->store();
|
|
|
|
|
|
|
|
// Write new value to settings
|
|
|
|
ContentRegistry::Settings::write(category.unlocalizedName, setting.unlocalizedName, newValue);
|
|
|
|
|
|
|
|
// Print a debug message
|
|
|
|
log::debug("Setting [{} / {}]: Value was changed to {}", category.unlocalizedName, setting.unlocalizedName, nlohmann::to_string(newValue));
|
|
|
|
|
|
|
|
// Signal that the setting was changed
|
|
|
|
EventManager::post<EventSettingsChanged>();
|
|
|
|
widget->onChanged();
|
|
|
|
|
|
|
|
// Request a restart if the setting requires it
|
|
|
|
if (widget->doesRequireRestart())
|
|
|
|
this->m_restartRequested = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
2022-02-21 21:46:25 +01:00
|
|
|
}
|
2023-10-21 23:07:33 +02:00
|
|
|
ImGui::PopStyleVar();
|
|
|
|
|
2021-09-20 23:40:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
2021-09-20 23:40:36 +02:00
|
|
|
|
|
|
|
ImGui::EndTabBar();
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
ImGui::EndPopup();
|
2021-01-27 14:26:24 +01:00
|
|
|
} else
|
|
|
|
this->getWindowOpenState() = false;
|
2022-02-21 21:46:25 +01:00
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// If a restart is required, ask the user if they want to restart
|
2022-10-02 17:30:26 +02:00
|
|
|
if (!this->getWindowOpenState() && this->m_restartRequested) {
|
2023-04-16 21:34:29 +02:00
|
|
|
PopupQuestion::open("hex.builtin.view.settings.restart_question"_lang, ImHexApi::System::restartImHex, []{});
|
2022-02-21 21:46:25 +01:00
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 22:34:54 +01:00
|
|
|
}
|