Added settings registry and settings menu
This commit is contained in:
parent
8807d6c6f3
commit
d68b931013
@ -176,6 +176,7 @@ add_executable(imhex ${application_type}
|
|||||||
source/views/view_bookmarks.cpp
|
source/views/view_bookmarks.cpp
|
||||||
source/views/view_patches.cpp
|
source/views/view_patches.cpp
|
||||||
source/views/view_command_palette.cpp
|
source/views/view_command_palette.cpp
|
||||||
|
source/views/view_settings.cpp
|
||||||
|
|
||||||
${imhex_icon}
|
${imhex_icon}
|
||||||
)
|
)
|
||||||
|
23
include/views/view_settings.hpp
Normal file
23
include/views/view_settings.hpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "views/view.hpp"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace hex {
|
||||||
|
|
||||||
|
class ViewSettings : public View {
|
||||||
|
public:
|
||||||
|
explicit ViewSettings();
|
||||||
|
~ViewSettings() override;
|
||||||
|
|
||||||
|
void drawContent() override;
|
||||||
|
void drawMenu() override;
|
||||||
|
|
||||||
|
bool hasViewMenuItemEntry() override { return false; }
|
||||||
|
private:
|
||||||
|
bool m_settingsWindowOpen = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -12,6 +12,7 @@ endif()
|
|||||||
add_library(libimhex STATIC
|
add_library(libimhex STATIC
|
||||||
source/helpers/event.cpp
|
source/helpers/event.cpp
|
||||||
source/helpers/utils.cpp
|
source/helpers/utils.cpp
|
||||||
|
source/helpers/content_registry.cpp
|
||||||
|
|
||||||
source/providers/provider.cpp
|
source/providers/provider.cpp
|
||||||
|
|
||||||
|
38
plugins/libimhex/include/helpers/content_registry.hpp
Normal file
38
plugins/libimhex/include/helpers/content_registry.hpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <hex.hpp>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
namespace hex {
|
||||||
|
|
||||||
|
class ContentRegistry {
|
||||||
|
public:
|
||||||
|
ContentRegistry() = delete;
|
||||||
|
|
||||||
|
struct Settings {
|
||||||
|
Settings() = delete;
|
||||||
|
|
||||||
|
struct Entry {
|
||||||
|
std::string name;
|
||||||
|
std::function<bool(nlohmann::json&)> callback;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void load();
|
||||||
|
static void store();
|
||||||
|
|
||||||
|
static void add(std::string_view category, std::string_view name, s64 defaultValue, const std::function<bool(nlohmann::json&)> &callback);
|
||||||
|
static void add(std::string_view category, std::string_view name, std::string_view defaultValue, const std::function<bool(nlohmann::json&)> &callback);
|
||||||
|
|
||||||
|
static std::map<std::string, std::vector<Entry>>& getEntries();
|
||||||
|
static nlohmann::json& getSettingsData();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -19,7 +19,9 @@ namespace hex {
|
|||||||
AppendPatternLanguageCode,
|
AppendPatternLanguageCode,
|
||||||
|
|
||||||
ProjectFileStore,
|
ProjectFileStore,
|
||||||
ProjectFileLoad
|
ProjectFileLoad,
|
||||||
|
|
||||||
|
SettingsChanged
|
||||||
};
|
};
|
||||||
|
|
||||||
struct EventHandler {
|
struct EventHandler {
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <any>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <helpers/event.hpp>
|
#include <helpers/event.hpp>
|
||||||
|
#include <helpers/content_registry.hpp>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
namespace hex { class SharedData; }
|
namespace hex { class SharedData; }
|
||||||
|
|
||||||
@ -31,18 +35,34 @@ namespace hex {
|
|||||||
friend void hex::plugin::internal::initializePlugin(SharedData &sharedData);
|
friend void hex::plugin::internal::initializePlugin(SharedData &sharedData);
|
||||||
friend class Window;
|
friend class Window;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T& getVariable(std::string variableName) {
|
||||||
|
return std::any_cast<T&>((*this->sharedVariables)[variableName]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void setVariable(std::string variableName, T value) {
|
||||||
|
(*this->sharedVariables)[variableName] = value;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void initializeData() {
|
void initializeData() {
|
||||||
static std::vector<EventHandler> eventHandlersStorage;
|
static std::vector<EventHandler> eventHandlersStorage;
|
||||||
static std::vector<std::function<void()>> deferredCallsStorage;
|
static std::vector<std::function<void()>> deferredCallsStorage;
|
||||||
static prv::Provider *currentProviderStorage;
|
static prv::Provider *currentProviderStorage;
|
||||||
|
static std::map<std::string, std::vector<ContentRegistry::Settings::Entry>> settingsEntriesStorage;
|
||||||
|
static std::map<std::string, std::any> sharedVariablesStorage;
|
||||||
static ImVec2 windowPosStorage, windowSizeStorage;
|
static ImVec2 windowPosStorage, windowSizeStorage;
|
||||||
|
static nlohmann::json settingsJsonStorage;
|
||||||
|
|
||||||
this->imguiContext = ImGui::GetCurrentContext();
|
this->imguiContext = ImGui::GetCurrentContext();
|
||||||
this->eventHandlers = &eventHandlersStorage;
|
this->eventHandlers = &eventHandlersStorage;
|
||||||
this->deferredCalls = &deferredCallsStorage;
|
this->deferredCalls = &deferredCallsStorage;
|
||||||
this->currentProvider = ¤tProviderStorage;
|
this->currentProvider = ¤tProviderStorage;
|
||||||
|
this->settingsEntries = &settingsEntriesStorage;
|
||||||
|
this->sharedVariables = &sharedVariablesStorage;
|
||||||
|
this->settingsJson = &settingsJsonStorage;
|
||||||
|
|
||||||
this->windowPos = &windowPosStorage;
|
this->windowPos = &windowPosStorage;
|
||||||
this->windowSize = &windowSizeStorage;
|
this->windowSize = &windowSizeStorage;
|
||||||
@ -53,6 +73,9 @@ namespace hex {
|
|||||||
this->eventHandlers = other.eventHandlers;
|
this->eventHandlers = other.eventHandlers;
|
||||||
this->deferredCalls = other.deferredCalls;
|
this->deferredCalls = other.deferredCalls;
|
||||||
this->currentProvider = other.currentProvider;
|
this->currentProvider = other.currentProvider;
|
||||||
|
this->settingsEntries = other.settingsEntries;
|
||||||
|
this->sharedVariables = other.sharedVariables;
|
||||||
|
this->settingsJson = other.settingsJson;
|
||||||
|
|
||||||
this->windowPos = other.windowPos;
|
this->windowPos = other.windowPos;
|
||||||
this->windowSize = other.windowSize;
|
this->windowSize = other.windowSize;
|
||||||
@ -63,9 +86,14 @@ namespace hex {
|
|||||||
std::vector<EventHandler> *eventHandlers;
|
std::vector<EventHandler> *eventHandlers;
|
||||||
std::vector<std::function<void()>> *deferredCalls;
|
std::vector<std::function<void()>> *deferredCalls;
|
||||||
prv::Provider **currentProvider;
|
prv::Provider **currentProvider;
|
||||||
|
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>> *settingsEntries;
|
||||||
|
nlohmann::json *settingsJson;
|
||||||
|
|
||||||
ImVec2 *windowPos;
|
ImVec2 *windowPos;
|
||||||
ImVec2 *windowSize;
|
ImVec2 *windowSize;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::map<std::string, std::any> *sharedVariables;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
50
plugins/libimhex/source/helpers/content_registry.cpp
Normal file
50
plugins/libimhex/source/helpers/content_registry.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include <helpers/content_registry.hpp>
|
||||||
|
|
||||||
|
#include <helpers/shared_data.hpp>
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
namespace hex {
|
||||||
|
|
||||||
|
/* Settings */
|
||||||
|
|
||||||
|
void ContentRegistry::Settings::load() {
|
||||||
|
std::ifstream settingsFile(std::filesystem::path(mainArgv[0]).parent_path() / "settings.json");
|
||||||
|
|
||||||
|
if (settingsFile.good())
|
||||||
|
settingsFile >> getSettingsData();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentRegistry::Settings::store() {
|
||||||
|
std::ofstream settingsFile(std::filesystem::path(mainArgv[0]).parent_path() / "settings.json", std::ios::trunc);
|
||||||
|
settingsFile << getSettingsData();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentRegistry::Settings::add(std::string_view category, std::string_view name, s64 defaultValue, const std::function<bool(nlohmann::json&)> &callback) {
|
||||||
|
ContentRegistry::Settings::getEntries()[category.data()].emplace_back(Entry{ name.data(), callback });
|
||||||
|
|
||||||
|
auto &json = (*SharedData::get().settingsJson);
|
||||||
|
|
||||||
|
if (!json.contains(category.data()))
|
||||||
|
json[category.data()] = nlohmann::json::object();
|
||||||
|
if (!json[category.data()].contains(name.data()))
|
||||||
|
json[category.data()][name.data()] = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentRegistry::Settings::add(std::string_view category, std::string_view name, std::string_view defaultValue, const std::function<bool(nlohmann::json&)> &callback) {
|
||||||
|
ContentRegistry::Settings::getEntries()[category.data()].emplace_back(Entry{ name.data(), callback });
|
||||||
|
|
||||||
|
(*SharedData::get().settingsJson)[category.data()] = nlohmann::json::object();
|
||||||
|
(*SharedData::get().settingsJson)[category.data()][name.data()] = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>>& ContentRegistry::Settings::getEntries() {
|
||||||
|
return *SharedData::get().settingsEntries;
|
||||||
|
}
|
||||||
|
|
||||||
|
nlohmann::json& ContentRegistry::Settings::getSettingsData() {
|
||||||
|
return *SharedData::get().settingsJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -15,6 +15,7 @@
|
|||||||
#include "views/view_bookmarks.hpp"
|
#include "views/view_bookmarks.hpp"
|
||||||
#include "views/view_patches.hpp"
|
#include "views/view_patches.hpp"
|
||||||
#include "views/view_command_palette.hpp"
|
#include "views/view_command_palette.hpp"
|
||||||
|
#include "views/view_settings.hpp"
|
||||||
|
|
||||||
#include "providers/provider.hpp"
|
#include "providers/provider.hpp"
|
||||||
|
|
||||||
@ -46,6 +47,7 @@ int main(int argc, char **argv) {
|
|||||||
window.addView<hex::ViewTools>();
|
window.addView<hex::ViewTools>();
|
||||||
window.addView<hex::ViewCommandPalette>();
|
window.addView<hex::ViewCommandPalette>();
|
||||||
window.addView<hex::ViewHelp>();
|
window.addView<hex::ViewHelp>();
|
||||||
|
window.addView<hex::ViewSettings>();
|
||||||
|
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
hex::View::postEvent(hex::Events::FileDropped, argv[1]);
|
hex::View::postEvent(hex::Events::FileDropped, argv[1]);
|
||||||
|
47
source/views/view_settings.cpp
Normal file
47
source/views/view_settings.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include "views/view_settings.hpp"
|
||||||
|
|
||||||
|
#include "helpers/content_registry.hpp"
|
||||||
|
|
||||||
|
namespace hex {
|
||||||
|
|
||||||
|
ViewSettings::ViewSettings() : View("Settings") {
|
||||||
|
this->getWindowOpenState() = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ViewSettings::~ViewSettings() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ViewSettings::drawContent() {
|
||||||
|
|
||||||
|
ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX));
|
||||||
|
|
||||||
|
if (ImGui::BeginPopupModal("Preferences", &this->m_settingsWindowOpen, ImGuiWindowFlags_AlwaysAutoResize)) {
|
||||||
|
for (auto &[category, entries] : ContentRegistry::Settings::getEntries()) {
|
||||||
|
ImGui::TextUnformatted(category.c_str());
|
||||||
|
ImGui::Separator();
|
||||||
|
for (auto &[name, callback] : entries) {
|
||||||
|
ImGui::TextUnformatted(name.c_str());
|
||||||
|
ImGui::SameLine();
|
||||||
|
if (callback(ContentRegistry::Settings::getSettingsData()[category][name]))
|
||||||
|
View::postEvent(Events::SettingsChanged, nullptr);
|
||||||
|
ImGui::NewLine();
|
||||||
|
}
|
||||||
|
ImGui::NewLine();
|
||||||
|
}
|
||||||
|
ImGui::EndPopup();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ViewSettings::drawMenu() {
|
||||||
|
if (ImGui::BeginMenu("Help")) {
|
||||||
|
if (ImGui::MenuItem("Preferences")) {
|
||||||
|
View::doLater([]{ ImGui::OpenPopup("Preferences"); });
|
||||||
|
this->m_settingsWindowOpen = true;
|
||||||
|
}
|
||||||
|
ImGui::EndMenu();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -12,6 +12,7 @@
|
|||||||
#include "imgui_freetype.h"
|
#include "imgui_freetype.h"
|
||||||
|
|
||||||
#include "helpers/plugin_handler.hpp"
|
#include "helpers/plugin_handler.hpp"
|
||||||
|
#include "helpers/content_registry.hpp"
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
@ -49,6 +50,8 @@ namespace hex {
|
|||||||
|
|
||||||
Window::Window() {
|
Window::Window() {
|
||||||
SharedData::get().initializeData();
|
SharedData::get().initializeData();
|
||||||
|
ContentRegistry::Settings::load();
|
||||||
|
View::postEvent(Events::SettingsChanged, nullptr);
|
||||||
|
|
||||||
this->initGLFW();
|
this->initGLFW();
|
||||||
this->initImGui();
|
this->initImGui();
|
||||||
@ -59,6 +62,7 @@ namespace hex {
|
|||||||
this->deinitImGui();
|
this->deinitImGui();
|
||||||
this->deinitGLFW();
|
this->deinitGLFW();
|
||||||
this->deinitPlugins();
|
this->deinitPlugins();
|
||||||
|
ContentRegistry::Settings::store();
|
||||||
|
|
||||||
for (auto &view : this->m_views)
|
for (auto &view : this->m_views)
|
||||||
delete view;
|
delete view;
|
||||||
|
Loading…
Reference in New Issue
Block a user