1
0
mirror of synced 2024-12-22 04:25:54 +01:00
ImHex/plugins/builtin/source/content/views/view_tools.cpp

104 lines
4.1 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>
#include <hex/api/layout_manager.hpp>
#include <fonts/codicons_font.h>
2021-12-07 22:47:41 +01:00
namespace hex::plugin::builtin {
2020-11-15 00:46:38 +01:00
2024-01-08 21:51:48 +01:00
ViewTools::ViewTools() : View::Window("hex.builtin.view.tools.name", ICON_VS_TOOLS) {
2023-12-19 13:10:25 +01:00
m_dragStartIterator = ContentRegistry::Tools::impl::getEntries().end();
LayoutManager::registerLoadCallback([this](std::string_view line) {
auto parts = wolv::util::splitString(std::string(line), "=");
if (parts.size() != 2)
return;
m_detachedTools[parts[0]] = parts[1] == "1";
});
LayoutManager::registerStoreCallback([this](ImGuiTextBuffer *buffer) {
for (auto &[unlocalizedName, function] : ContentRegistry::Tools::impl::getEntries()) {
auto detached = m_detachedTools[unlocalizedName];
buffer->appendf("%s=%d\n", unlocalizedName.get().c_str(), detached);
}
});
}
void ViewTools::drawContent() {
auto &tools = ContentRegistry::Tools::impl::getEntries();
// Draw all tools
for (auto iter = tools.begin(); iter != tools.end(); ++iter) {
auto &[unlocalizedName, function] = *iter;
// If the tool has been detached from the main window, don't draw it here anymore
if (m_detachedTools[unlocalizedName]) continue;
// Draw the tool
if (ImGui::CollapsingHeader(Lang(unlocalizedName))) {
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
2023-12-19 13:10:25 +01:00
if (ImGui::IsMouseClicked(0) && ImGui::IsItemActivated() && m_dragStartIterator == tools.end())
m_dragStartIterator = iter;
// If the user released the mouse button, stop dragging the tool
if (!ImGui::IsMouseDown(0))
2023-12-19 13:10:25 +01:00
m_dragStartIterator = tools.end();
// Detach the tool if the user dragged it out of the main window
2023-12-19 13:10:25 +01:00
if (!ImGui::IsItemHovered() && m_dragStartIterator == iter) {
m_detachedTools[unlocalizedName] = true;
}
}
2020-11-15 00:46:38 +01:00
}
}
void ViewTools::drawAlwaysVisibleContent() {
// 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 &[unlocalizedName, function] = *iter;
// If the tool is still attached to the main window, don't draw it here
if (!m_detachedTools[unlocalizedName]) continue;
// Load the window height that is dependent on the tool content
const auto windowName = View::toWindowName(unlocalizedName);
const auto height = m_windowHeights[ImGui::FindWindowByName(windowName.c_str())];
if (height > 0)
ImGui::SetNextWindowSizeConstraints(ImVec2(400_scaled, height), ImVec2(FLT_MAX, height));
// Create a new window for the tool
if (ImGui::Begin(windowName.c_str(), &m_detachedTools[unlocalizedName], ImGuiWindowFlags_NoCollapse)) {
// Draw the tool content
function();
// Handle the first frame after the tool has been detached
2023-12-19 13:10:25 +01:00
if (ImGui::IsWindowAppearing() && m_dragStartIterator == iter) {
m_dragStartIterator = tools.end();
// Attach the newly created window to the cursor, so it gets dragged around
GImGui->MovingWindow = ImGui::GetCurrentWindowRead();
GImGui->ActiveId = GImGui->MovingWindow->MoveId;
}
const auto window = ImGui::GetCurrentWindowRead();
m_windowHeights[ImGui::GetCurrentWindowRead()] = ImGui::CalcWindowNextAutoFitSize(window).y;
}
ImGui::End();
}
2020-11-15 00:46:38 +01:00
}
}