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") {
|
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-05-12 08:38:32 +02:00
|
|
|
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.extras" }, 3000);
|
2023-03-20 13:11:43 +01:00
|
|
|
|
2023-05-12 08:38:32 +02:00
|
|
|
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-03-21 15:33:43 +01:00
|
|
|
auto &entries = ContentRegistry::Settings::impl::getEntries();
|
2022-02-18 22:34:54 +01:00
|
|
|
|
|
|
|
std::vector<std::decay_t<decltype(entries)>::const_iterator> sortedCategories;
|
|
|
|
|
|
|
|
for (auto it = entries.cbegin(); it != entries.cend(); it++) {
|
2022-03-04 20:52:39 +01:00
|
|
|
sortedCategories.emplace_back(it);
|
2022-02-18 22:34:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(sortedCategories.begin(), sortedCategories.end(), [](auto &item0, auto &item1) {
|
|
|
|
return item0->first.slot < item1->first.slot;
|
|
|
|
});
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
const auto &descriptions = ContentRegistry::Settings::impl::getCategoryDescriptions();
|
2022-02-18 22:34:54 +01:00
|
|
|
|
|
|
|
for (auto &it : sortedCategories) {
|
2022-02-21 21:46:25 +01:00
|
|
|
auto &[category, settings] = *it;
|
2022-10-02 17:30:26 +02:00
|
|
|
if (ImGui::BeginTabItem(LangEntry(category.name))) {
|
|
|
|
const std::string &categoryDesc = descriptions.contains(category.name) ? descriptions.at(category.name) : category.name;
|
|
|
|
|
2022-03-05 23:32:30 +01:00
|
|
|
LangEntry descriptionEntry{categoryDesc};
|
2022-10-02 17:30:26 +02:00
|
|
|
ImGui::TextFormattedWrapped("{}", descriptionEntry);
|
2022-03-05 23:32:30 +01:00
|
|
|
ImGui::InfoTooltip(descriptionEntry);
|
2021-09-20 23:40:36 +02:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2022-02-21 21:46:25 +01:00
|
|
|
for (auto &[name, requiresRestart, callback] : settings) {
|
2023-03-21 15:33:43 +01:00
|
|
|
auto &setting = ContentRegistry::Settings::impl::getSettingsData()[category.name][name];
|
2022-07-31 20:07:15 +02:00
|
|
|
if (callback(LangEntry(name), setting)) {
|
|
|
|
log::debug("Setting [{}]: {} was changed to {}", category.name, name, [&] -> std::string{
|
|
|
|
if (setting.is_number())
|
|
|
|
return std::to_string(setting.get<int>());
|
|
|
|
else if (setting.is_string())
|
|
|
|
return setting.get<std::string>();
|
|
|
|
else
|
|
|
|
return "";
|
|
|
|
}());
|
2021-09-20 23:40:36 +02:00
|
|
|
EventManager::post<EventSettingsChanged>();
|
2022-02-21 21:46:25 +01:00
|
|
|
|
|
|
|
if (requiresRestart)
|
|
|
|
this->m_restartRequested = true;
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
}
|