1
0
mirror of synced 2024-11-12 02:00:52 +01:00

Added file drag and drop support

This commit is contained in:
WerWolv 2020-11-17 13:58:50 +01:00
parent d05805595e
commit 6e21f703ab
8 changed files with 38 additions and 17 deletions

View File

@ -7,25 +7,26 @@ namespace hex {
enum class Events { enum class Events {
DataChanged, DataChanged,
PatternChanged PatternChanged,
FileDropped
}; };
struct EventHandler { struct EventHandler {
void *sender; void *sender;
Events eventType; Events eventType;
std::function<void(void*)> callback; std::function<void(const void*)> callback;
}; };
class EventManager { class EventManager {
public: public:
void post(Events eventType, void *userData) { void post(Events eventType, const void *userData) {
for (auto &handler : this->m_eventHandlers) for (auto &handler : this->m_eventHandlers)
if (eventType == handler.eventType) if (eventType == handler.eventType)
handler.callback(userData); handler.callback(userData);
} }
void subscribe(Events eventType, void *sender, std::function<void(void*)> callback) { void subscribe(Events eventType, void *sender, std::function<void(const void*)> callback) {
for (auto &handler : this->m_eventHandlers) for (auto &handler : this->m_eventHandlers)
if (eventType == handler.eventType && sender == handler.sender) if (eventType == handler.eventType && sender == handler.sender)
return; return;

View File

@ -25,8 +25,12 @@ namespace hex {
return View::s_deferedCalls; return View::s_deferedCalls;
} }
static void postEvent(Events eventType, const void *userData = nullptr) {
View::s_eventManager.post(eventType, userData);
}
protected: protected:
void subscribeEvent(Events eventType, std::function<void(void*)> callback) { void subscribeEvent(Events eventType, std::function<void(const void*)> callback) {
View::s_eventManager.subscribe(eventType, this, callback); View::s_eventManager.subscribe(eventType, this, callback);
} }
@ -34,10 +38,6 @@ namespace hex {
View::s_eventManager.unsubscribe(eventType, this); View::s_eventManager.unsubscribe(eventType, this);
} }
void postEvent(Events eventType, void *userData = nullptr) {
View::s_eventManager.post(eventType, userData);
}
void doLater(std::function<void()> &&function) { void doLater(std::function<void()> &&function) {
View::s_deferedCalls.push_back(function); View::s_deferedCalls.push_back(function);
} }

View File

@ -51,6 +51,7 @@ namespace hex {
void drawSearchPopup(); void drawSearchPopup();
void drawGotoPopup(); void drawGotoPopup();
void openFile(std::string path);
void copyBytes(); void copyBytes();
void copyString(); void copyString();

View File

@ -9,7 +9,7 @@
namespace hex { namespace hex {
ViewHashes::ViewHashes(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) { ViewHashes::ViewHashes(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) {
View::subscribeEvent(Events::DataChanged, [this](void*){ View::subscribeEvent(Events::DataChanged, [this](const void*){
this->m_shouldInvalidate = true; this->m_shouldInvalidate = true;
}); });
} }

View File

@ -49,6 +49,13 @@ namespace hex {
_this->m_memoryEditor.HighlightColor = 0x50C08080; _this->m_memoryEditor.HighlightColor = 0x50C08080;
return false; return false;
}; };
View::subscribeEvent(Events::FileDropped, [this](const void *userData) {
auto filePath = static_cast<const char*>(userData);
if (filePath != nullptr)
this->openFile(filePath);
});
} }
ViewHexEditor::~ViewHexEditor() { ViewHexEditor::~ViewHexEditor() {
@ -73,16 +80,20 @@ namespace hex {
this->m_fileBrowser.Display(); this->m_fileBrowser.Display();
if (this->m_fileBrowser.HasSelected()) { if (this->m_fileBrowser.HasSelected()) {
if (this->m_dataProvider != nullptr) this->openFile(this->m_fileBrowser.GetSelected().string());
delete this->m_dataProvider;
this->m_dataProvider = new prv::FileProvider(this->m_fileBrowser.GetSelected().string());
View::postEvent(Events::DataChanged);
this->m_fileBrowser.ClearSelected(); this->m_fileBrowser.ClearSelected();
} }
} }
void ViewHexEditor::openFile(std::string path) {
if (this->m_dataProvider != nullptr)
delete this->m_dataProvider;
this->m_dataProvider = new prv::FileProvider(path);
View::postEvent(Events::DataChanged);
}
void ViewHexEditor::copyBytes() { void ViewHexEditor::copyBytes() {
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);

View File

@ -22,7 +22,7 @@
namespace hex { namespace hex {
ViewInformation::ViewInformation(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) { ViewInformation::ViewInformation(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) {
View::subscribeEvent(Events::DataChanged, [this](void*) { View::subscribeEvent(Events::DataChanged, [this](const void*) {
this->m_shouldInvalidate = true; this->m_shouldInvalidate = true;
}); });
} }

View File

@ -7,7 +7,7 @@
namespace hex { namespace hex {
ViewStrings::ViewStrings(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) { ViewStrings::ViewStrings(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) {
View::subscribeEvent(Events::DataChanged, [this](void*){ View::subscribeEvent(Events::DataChanged, [this](const void*){
this->m_shouldInvalidate = true; this->m_shouldInvalidate = true;
}); });

View File

@ -137,6 +137,7 @@ namespace hex {
void Window::frameEnd() { void Window::frameEnd() {
ImGui::Render(); ImGui::Render();
int display_w, display_h; int display_w, display_h;
glfwGetFramebufferSize(this->m_window, &display_w, &display_h); glfwGetFramebufferSize(this->m_window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h); glViewport(0, 0, display_w, display_h);
@ -176,6 +177,13 @@ namespace hex {
Window::s_currShortcut = { key, mods }; Window::s_currShortcut = { key, mods };
}); });
glfwSetDropCallback(this->m_window, [](GLFWwindow *window, int count, const char **paths) {
if (count != 1)
return;
View::postEvent(Events::FileDropped, paths[0]);
});
glfwSetWindowSizeLimits(this->m_window, 720, 480, GLFW_DONT_CARE, GLFW_DONT_CARE); glfwSetWindowSizeLimits(this->m_window, 720, 480, GLFW_DONT_CARE, GLFW_DONT_CARE);
if (gladLoadGL() == 0) if (gladLoadGL() == 0)