2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_tools.hpp"
|
2022-10-13 10:47:35 +02:00
|
|
|
#include <imgui_internal.h>
|
2020-11-15 00:46:38 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2020-11-15 00:46:38 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ViewTools::ViewTools() : View::Window("hex.builtin.view.tools.name") {
|
2023-06-05 09:08:41 +02:00
|
|
|
this->m_dragStartIterator = ContentRegistry::Tools::impl::getEntries().end();
|
|
|
|
}
|
2020-11-15 21:31:04 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewTools::drawContent() {
|
2023-03-21 15:33:43 +01:00
|
|
|
auto &tools = ContentRegistry::Tools::impl::getEntries();
|
2022-10-13 10:47:35 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Draw all tools
|
|
|
|
for (auto iter = tools.begin(); iter != tools.end(); ++iter) {
|
|
|
|
auto &[name, function, detached] = *iter;
|
2022-10-13 10:47:35 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// If the tool has been detached from the main window, don't draw it here anymore
|
|
|
|
if (detached) continue;
|
2023-08-29 12:14:12 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Draw the tool
|
2023-11-21 14:38:01 +01:00
|
|
|
if (ImGui::CollapsingHeader(Lang(name))) {
|
2023-11-21 13:47:50 +01:00
|
|
|
function();
|
|
|
|
ImGui::NewLine();
|
|
|
|
} else {
|
|
|
|
// Handle dragging the tool out of the main window
|
2022-10-13 10:47:35 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// If the user clicks on the header, start dragging the tool remember the iterator
|
|
|
|
if (ImGui::IsMouseClicked(0) && ImGui::IsItemActivated() && this->m_dragStartIterator == tools.end())
|
|
|
|
this->m_dragStartIterator = iter;
|
2023-01-01 01:01:24 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// If the user released the mouse button, stop dragging the tool
|
|
|
|
if (!ImGui::IsMouseDown(0))
|
|
|
|
this->m_dragStartIterator = tools.end();
|
2022-10-13 10:47:35 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Detach the tool if the user dragged it out of the main window
|
|
|
|
if (!ImGui::IsItemHovered() && this->m_dragStartIterator == iter) {
|
|
|
|
detached = true;
|
2021-01-13 13:18:03 +01:00
|
|
|
}
|
2023-11-21 13:47:50 +01:00
|
|
|
|
2021-01-13 13:18:03 +01:00
|
|
|
}
|
2020-11-15 00:46:38 +01:00
|
|
|
}
|
2023-06-05 09:08:41 +02:00
|
|
|
}
|
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
void ViewTools::drawAlwaysVisibleContent() {
|
2023-10-12 15:15:05 +02:00
|
|
|
// Make sure the tool windows never get drawn over the welcome screen
|
|
|
|
if (!ImHexApi::Provider::isValid())
|
|
|
|
return;
|
|
|
|
|
2023-06-05 09:08:41 +02:00
|
|
|
auto &tools = ContentRegistry::Tools::impl::getEntries();
|
2022-10-13 10:47:35 +02:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
for (auto iter = tools.begin(); iter != tools.end(); ++iter) {
|
2022-10-13 10:47:35 +02:00
|
|
|
auto &[name, function, detached] = *iter;
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// If the tool is still attached to the main window, don't draw it here
|
2022-10-13 10:47:35 +02:00
|
|
|
if (!detached) continue;
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Create a new window for the tool
|
2022-10-13 10:47:35 +02:00
|
|
|
if (ImGui::Begin(View::toWindowName(name).c_str(), &detached, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize)) {
|
2023-11-12 00:10:16 +01:00
|
|
|
if (!ImGui::IsWindowDocked())
|
|
|
|
ImGui::SetWindowSize(scaled(ImVec2(600, 0)));
|
|
|
|
|
|
|
|
ImGui::BringWindowToDisplayFront(ImGui::GetCurrentWindow());
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw the tool
|
2022-10-13 10:47:35 +02:00
|
|
|
function();
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Handle the first frame after the tool has been detached
|
2022-10-13 10:47:35 +02:00
|
|
|
if (ImGui::IsWindowAppearing() && this->m_dragStartIterator == iter) {
|
|
|
|
this->m_dragStartIterator = tools.end();
|
|
|
|
|
|
|
|
// Attach the newly created window to the cursor, so it gets dragged around
|
|
|
|
GImGui->MovingWindow = ImGui::GetCurrentWindow();
|
|
|
|
GImGui->ActiveId = GImGui->MovingWindow->MoveId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
2020-11-15 00:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|