1
0
mirror of synced 2024-12-04 20:17:57 +01:00
ImHex/plugins/builtin/source/content/views/view_tools.cpp

86 lines
3.3 KiB
C++
Raw Normal View History

2021-12-07 22:47:41 +01:00
#include "content/views/view_tools.hpp"
#include <imgui_internal.h>
2020-11-15 00:46:38 +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
ViewTools::ViewTools() : View("hex.builtin.view.tools.name") {
this->m_dragStartIterator = ContentRegistry::Tools::impl::getEntries().end();
}
void ViewTools::drawContent() {
auto &tools = ContentRegistry::Tools::impl::getEntries();
2021-12-07 22:47:41 +01:00
if (ImGui::Begin(View::toWindowName("hex.builtin.view.tools.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
// Draw all tools
2023-11-10 20:47:08 +01:00
for (auto iter = tools.begin(); iter != tools.end(); ++iter) {
auto &[name, function, detached] = *iter;
// If the tool has been detached from the main window, don't draw it here anymore
if (detached) continue;
// Draw the tool
if (ImGui::CollapsingHeader(LangEntry(name))) {
function();
ImGui::NewLine();
} else {
// Handle dragging the tool out of the main window
// 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;
// If the user released the mouse button, stop dragging the tool
if (!ImGui::IsMouseDown(0))
this->m_dragStartIterator = tools.end();
// Detach the tool if the user dragged it out of the main window
if (!ImGui::IsItemHovered() && this->m_dragStartIterator == iter) {
detached = true;
}
}
}
2020-11-15 00:46:38 +01:00
}
ImGui::End();
}
void ViewTools::drawAlwaysVisible() {
// Make sure the tool windows never get drawn over the welcome screen
if (!ImHexApi::Provider::isValid())
return;
auto &tools = ContentRegistry::Tools::impl::getEntries();
2023-11-10 20:47:08 +01:00
for (auto iter = tools.begin(); iter != tools.end(); ++iter) {
auto &[name, function, detached] = *iter;
// If the tool is still attached to the main window, don't draw it here
if (!detached) continue;
// Create a new window for the tool
if (ImGui::Begin(View::toWindowName(name).c_str(), &detached, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize)) {
if (!ImGui::IsWindowDocked())
ImGui::SetWindowSize(scaled(ImVec2(600, 0)));
ImGui::BringWindowToDisplayFront(ImGui::GetCurrentWindow());
// Draw the tool
function();
// Handle the first frame after the tool has been detached
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
}
}