From a59c17aa83474641a71b731be75b50121e7aae64 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 10 Feb 2023 11:22:11 +0100 Subject: [PATCH] nodes: Fixed loading saved nodes multiple times --- .../include/hex/data_processor/attribute.hpp | 2 +- .../include/hex/data_processor/link.hpp | 2 +- .../content/helpers/provider_extra_data.hpp | 12 ++++++++++++ .../source/content/data_processor_nodes.cpp | 4 ++++ .../content/views/view_data_processor.cpp | 18 ++++++++++++------ 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/libimhex/include/hex/data_processor/attribute.hpp b/lib/libimhex/include/hex/data_processor/attribute.hpp index 4fca79b4d..f0575042f 100644 --- a/lib/libimhex/include/hex/data_processor/attribute.hpp +++ b/lib/libimhex/include/hex/data_processor/attribute.hpp @@ -38,7 +38,7 @@ namespace hex::dp { void removeConnectedAttribute(int linkId) { this->m_connectedAttributes.erase(linkId); } [[nodiscard]] std::map &getConnectedAttributes() { return this->m_connectedAttributes; } - [[nodiscard]] Node *getParentNode() { return this->m_parentNode; } + [[nodiscard]] Node *getParentNode() const { return this->m_parentNode; } [[nodiscard]] std::optional> &getOutputData() { return this->m_outputData; } diff --git a/lib/libimhex/include/hex/data_processor/link.hpp b/lib/libimhex/include/hex/data_processor/link.hpp index f30d4c25d..3495a6d1f 100644 --- a/lib/libimhex/include/hex/data_processor/link.hpp +++ b/lib/libimhex/include/hex/data_processor/link.hpp @@ -9,7 +9,7 @@ namespace hex::dp { Link(int from, int to); [[nodiscard]] int getId() const { return this->m_id; } - void setID(int id) { this->m_id = id; } + void setId(int id) { this->m_id = id; } [[nodiscard]] int getFromId() const { return this->m_from; } [[nodiscard]] int getToId() const { return this->m_to; } diff --git a/plugins/builtin/include/content/helpers/provider_extra_data.hpp b/plugins/builtin/include/content/helpers/provider_extra_data.hpp index e4c97a3f4..036a6d66b 100644 --- a/plugins/builtin/include/content/helpers/provider_extra_data.hpp +++ b/plugins/builtin/include/content/helpers/provider_extra_data.hpp @@ -10,6 +10,9 @@ #include +#include +#include + namespace hex::plugin::builtin { class ProviderExtraData { @@ -64,6 +67,15 @@ namespace hex::plugin::builtin { struct DataProcessor { struct Workspace { + std::unique_ptr context = { []{ + ImNodesContext *ctx = ImNodes::CreateContext(); + ctx->Style = ImNodes::GetStyle(); + ctx->Io = ImNodes::GetIO(); + ctx->AttributeFlagStack = GImNodes->AttributeFlagStack; + + return ctx; + }(), ImNodes::DestroyContext }; + std::list> nodes; std::list endNodes; std::list links; diff --git a/plugins/builtin/source/content/data_processor_nodes.cpp b/plugins/builtin/source/content/data_processor_nodes.cpp index 6444e564e..7e02a1620 100644 --- a/plugins/builtin/source/content/data_processor_nodes.cpp +++ b/plugins/builtin/source/content/data_processor_nodes.cpp @@ -1372,6 +1372,10 @@ namespace hex::plugin::builtin { return std::nullopt; }; + auto prevContext = ImNodes::GetCurrentContext(); + ImNodes::SetCurrentContext(this->m_workspace.context.get()); + ON_SCOPE_EXIT { ImNodes::SetCurrentContext(prevContext); }; + // Forward inputs to input nodes values for (auto &attribute : this->getAttributes()) { auto index = indexFromId(attribute.getId()); diff --git a/plugins/builtin/source/content/views/view_data_processor.cpp b/plugins/builtin/source/content/views/view_data_processor.cpp index 02c64f790..db0cee856 100644 --- a/plugins/builtin/source/content/views/view_data_processor.cpp +++ b/plugins/builtin/source/content/views/view_data_processor.cpp @@ -219,7 +219,9 @@ namespace hex::plugin::builtin { auto &data = ProviderExtraData::getCurrent().dataProcessor; auto &workspace = *data.workspaceStack.back(); + bool popWorkspace = false; if (ImGui::Begin(View::toWindowName("hex.builtin.view.data_processor.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { + ImNodes::SetCurrentContext(workspace.context.get()); if (ImGui::IsMouseReleased(ImGuiMouseButton_Right) && ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows)) { ImNodes::ClearNodeSelection(); @@ -436,8 +438,7 @@ namespace hex::plugin::builtin { if (data.workspaceStack.size() > 1) { ImGui::SetCursorPos(ImVec2(ImGui::GetContentRegionAvail().x - ImGui::GetTextLineHeightWithSpacing() * 1.2F, ImGui::GetTextLineHeightWithSpacing() * 0.2F)); if (ImGui::IconButton(ICON_VS_CLOSE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGray))) { - data.workspaceStack.pop_back(); - this->m_updateNodePositions = true; + popWorkspace = true; } } @@ -520,6 +521,11 @@ namespace hex::plugin::builtin { } } ImGui::End(); + + if (popWorkspace) { + data.workspaceStack.pop_back(); + this->m_updateNodePositions = true; + } } nlohmann::json ViewDataProcessor::saveNode(const dp::Node *node) { @@ -619,6 +625,9 @@ namespace hex::plugin::builtin { newNode->setPosition(ImVec2(node["pos"]["x"], node["pos"]["y"])); + if (!node["data"].is_null()) + newNode->load(node["data"]); + bool hasOutput = false; bool hasInput = false; u32 attrIndex = 0; @@ -633,9 +642,6 @@ namespace hex::plugin::builtin { attrIndex++; } - if (!node["data"].is_null()) - newNode->load(node["data"]); - if (hasInput && !hasOutput) workspace.endNodes.push_back(newNode.get()); @@ -649,7 +655,7 @@ namespace hex::plugin::builtin { int linkId = link["id"]; maxLinkId = std::max(linkId, maxLinkId); - newLink.setID(linkId); + newLink.setId(linkId); workspace.links.push_back(newLink); dp::Attribute *fromAttr = nullptr, *toAttr = nullptr;