Make sure important data is synchronized between ImHex and plugins

This commit is contained in:
WerWolv
2021-01-04 00:19:56 +01:00
parent c7c654d310
commit eed7ef1ac3
24 changed files with 148 additions and 113 deletions

View File

@@ -1,23 +1,25 @@
#include "helpers/event.hpp"
#include <helpers/shared_data.hpp>
namespace hex {
void EventManager::post(Events eventType, const void *userData) {
for (auto &handler : this->m_eventHandlers)
for (auto &handler : *SharedData::get().eventHandlers)
if (eventType == handler.eventType)
handler.callback(userData);
}
void EventManager::subscribe(Events eventType, void *owner, std::function<void(const void*)> callback) {
for (auto &handler : this->m_eventHandlers)
for (auto &handler : *SharedData::get().eventHandlers)
if (eventType == handler.eventType && owner == handler.owner)
return;
this->m_eventHandlers.push_back(EventHandler { owner, eventType, callback });
SharedData::get().eventHandlers->push_back(EventHandler { owner, eventType, callback });
}
void EventManager::unsubscribe(Events eventType, void *sender) {
std::erase_if(this->m_eventHandlers, [&eventType, &sender](EventHandler handler) {
std::erase_if(*SharedData::get().eventHandlers, [&eventType, &sender](EventHandler handler) {
return eventType == handler.eventType && sender == handler.owner;
});
}

View File

@@ -6,6 +6,8 @@
#include <string>
#include <vector>
#include <helpers/shared_data.hpp>
namespace hex {
@@ -15,11 +17,11 @@ namespace hex {
bool View::handleShortcut(int key, int mods) { return false; }
std::vector<std::function<void()>>& View::getDeferedCalls() {
return View::s_deferedCalls;
return *SharedData::get().deferredCalls;
}
void View::postEvent(Events eventType, const void *userData) {
View::s_eventManager.post(eventType, userData);
EventManager::post(eventType, userData);
}
void View::drawCommonInterfaces() {
@@ -44,22 +46,6 @@ namespace hex {
ImGui::OpenPopup("Error");
}
void View::setWindowPosition(s32 x, s32 y) {
View::s_windowPos = ImVec2(x, y);
}
const ImVec2& View::getWindowPosition() {
return View::s_windowPos;
}
void View::setWindowSize(s32 width, s32 height) {
View::s_windowSize = ImVec2(width, height);
}
const ImVec2& View::getWindowSize() {
return View::s_windowSize;
}
bool View::hasViewMenuItemEntry() {
return true;
}
@@ -82,15 +68,15 @@ namespace hex {
}
void View::subscribeEvent(Events eventType, std::function<void(const void*)> callback) {
View::s_eventManager.subscribe(eventType, this, callback);
EventManager::subscribe(eventType, this, callback);
}
void View::unsubscribeEvent(Events eventType) {
View::s_eventManager.unsubscribe(eventType, this);
EventManager::unsubscribe(eventType, this);
}
void View::doLater(std::function<void()> &&function) {
View::s_deferedCalls.push_back(function);
SharedData::get().deferredCalls->push_back(function);
}
void View::confirmButtons(const char *textLeft, const char *textRight, std::function<void()> leftButtonFn, std::function<void()> rightButtonFn) {