1
0
mirror of synced 2024-11-28 17:40:51 +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 {
DataChanged,
PatternChanged
PatternChanged,
FileDropped
};
struct EventHandler {
void *sender;
Events eventType;
std::function<void(void*)> callback;
std::function<void(const void*)> callback;
};
class EventManager {
public:
void post(Events eventType, void *userData) {
void post(Events eventType, const void *userData) {
for (auto &handler : this->m_eventHandlers)
if (eventType == handler.eventType)
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)
if (eventType == handler.eventType && sender == handler.sender)
return;

View File

@ -25,8 +25,12 @@ namespace hex {
return View::s_deferedCalls;
}
static void postEvent(Events eventType, const void *userData = nullptr) {
View::s_eventManager.post(eventType, userData);
}
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);
}
@ -34,10 +38,6 @@ namespace hex {
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) {
View::s_deferedCalls.push_back(function);
}

View File

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

View File

@ -9,7 +9,7 @@
namespace hex {
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;
});
}

View File

@ -49,6 +49,13 @@ namespace hex {
_this->m_memoryEditor.HighlightColor = 0x50C08080;
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() {
@ -73,16 +80,20 @@ namespace hex {
this->m_fileBrowser.Display();
if (this->m_fileBrowser.HasSelected()) {
if (this->m_dataProvider != nullptr)
delete this->m_dataProvider;
this->m_dataProvider = new prv::FileProvider(this->m_fileBrowser.GetSelected().string());
View::postEvent(Events::DataChanged);
this->openFile(this->m_fileBrowser.GetSelected().string());
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() {
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);

View File

@ -22,7 +22,7 @@
namespace hex {
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;
});
}

View File

@ -7,7 +7,7 @@
namespace hex {
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;
});

View File

@ -137,6 +137,7 @@ namespace hex {
void Window::frameEnd() {
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(this->m_window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
@ -176,6 +177,13 @@ namespace hex {
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);
if (gladLoadGL() == 0)