From e0c35e00022490d5b0741b0217c91c5b36834a2b Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 6 Oct 2022 09:14:46 +0200 Subject: [PATCH] fix: Data processor nodes not remembering their positions correctly --- .../include/hex/data_processor/attribute.hpp | 18 +++++------ .../include/hex/data_processor/link.hpp | 18 +++++------ .../include/hex/data_processor/node.hpp | 20 +++++++++--- .../source/data_processor/attribute.cpp | 2 +- lib/libimhex/source/data_processor/link.cpp | 4 +-- lib/libimhex/source/data_processor/node.cpp | 2 +- .../content/views/view_data_processor.hpp | 3 +- .../content/views/view_data_processor.cpp | 31 +++++++++++++------ 8 files changed, 60 insertions(+), 38 deletions(-) diff --git a/lib/libimhex/include/hex/data_processor/attribute.hpp b/lib/libimhex/include/hex/data_processor/attribute.hpp index 5526c9a09..4fca79b4d 100644 --- a/lib/libimhex/include/hex/data_processor/attribute.hpp +++ b/lib/libimhex/include/hex/data_processor/attribute.hpp @@ -27,32 +27,32 @@ namespace hex::dp { Attribute(IOType ioType, Type type, std::string unlocalizedName); ~Attribute(); - [[nodiscard]] u32 getId() const { return this->m_id; } - void setId(u32 id) { this->m_id = id; } + [[nodiscard]] int getId() const { return this->m_id; } + void setId(int id) { this->m_id = id; } [[nodiscard]] IOType getIOType() const { return this->m_ioType; } [[nodiscard]] Type getType() const { return this->m_type; } [[nodiscard]] const std::string &getUnlocalizedName() const { return this->m_unlocalizedName; } - void addConnectedAttribute(u32 linkId, Attribute *to) { this->m_connectedAttributes.insert({ linkId, to }); } - void removeConnectedAttribute(u32 linkId) { this->m_connectedAttributes.erase(linkId); } - [[nodiscard]] std::map &getConnectedAttributes() { return this->m_connectedAttributes; } + void addConnectedAttribute(int linkId, Attribute *to) { this->m_connectedAttributes.insert({ linkId, to }); } + 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]] std::optional> &getOutputData() { return this->m_outputData; } - static void setIdCounter(u32 id) { + static void setIdCounter(int id) { if (id > Attribute::s_idCounter) Attribute::s_idCounter = id; } private: - u32 m_id; + int m_id; IOType m_ioType; Type m_type; std::string m_unlocalizedName; - std::map m_connectedAttributes; + std::map m_connectedAttributes; Node *m_parentNode = nullptr; std::optional> m_outputData; @@ -60,7 +60,7 @@ namespace hex::dp { friend class Node; void setParentNode(Node *node) { this->m_parentNode = node; } - static u32 s_idCounter; + static int s_idCounter; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/data_processor/link.hpp b/lib/libimhex/include/hex/data_processor/link.hpp index bacd37904..f30d4c25d 100644 --- a/lib/libimhex/include/hex/data_processor/link.hpp +++ b/lib/libimhex/include/hex/data_processor/link.hpp @@ -6,24 +6,24 @@ namespace hex::dp { class Link { public: - Link(u32 from, u32 to); + Link(int from, int to); - [[nodiscard]] u32 getId() const { return this->m_id; } - void setID(u32 id) { this->m_id = id; } + [[nodiscard]] int getId() const { return this->m_id; } + void setID(int id) { this->m_id = id; } - [[nodiscard]] u32 getFromId() const { return this->m_from; } - [[nodiscard]] u32 getToId() const { return this->m_to; } + [[nodiscard]] int getFromId() const { return this->m_from; } + [[nodiscard]] int getToId() const { return this->m_to; } - static void setIdCounter(u32 id) { + static void setIdCounter(int id) { if (id > Link::s_idCounter) Link::s_idCounter = id; } private: - u32 m_id; - u32 m_from, m_to; + int m_id; + int m_from, m_to; - static u32 s_idCounter; + static int s_idCounter; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/data_processor/node.hpp b/lib/libimhex/include/hex/data_processor/node.hpp index 1a5d9fdeb..9563e713f 100644 --- a/lib/libimhex/include/hex/data_processor/node.hpp +++ b/lib/libimhex/include/hex/data_processor/node.hpp @@ -9,6 +9,7 @@ #include #include +#include namespace hex::prv { class Provider; @@ -23,8 +24,8 @@ namespace hex::dp { virtual ~Node() = default; - [[nodiscard]] u32 getId() const { return this->m_id; } - void setId(u32 id) { this->m_id = id; } + [[nodiscard]] int getId() const { return this->m_id; } + void setId(int id) { this->m_id = id; } [[nodiscard]] const std::string &getUnlocalizedName() const { return this->m_unlocalizedName; } void setUnlocalizedName(const std::string &unlocalizedName) { this->m_unlocalizedName = unlocalizedName; } @@ -56,19 +57,28 @@ namespace hex::dp { this->m_processedInputs.clear(); } - static void setIdCounter(u32 id) { + void setPosition(ImVec2 pos) { + this->m_position = pos; + } + + [[nodiscard]] ImVec2 getPosition() const { + return this->m_position; + } + + static void setIdCounter(int id) { if (id > Node::s_idCounter) Node::s_idCounter = id; } private: - u32 m_id; + int m_id; std::string m_unlocalizedTitle, m_unlocalizedName; std::vector m_attributes; std::set m_processedInputs; prv::Overlay *m_overlay = nullptr; + ImVec2 m_position; - static u32 s_idCounter; + static int s_idCounter; Attribute *getConnectedInputAttribute(u32 index) { if (index >= this->getAttributes().size()) diff --git a/lib/libimhex/source/data_processor/attribute.cpp b/lib/libimhex/source/data_processor/attribute.cpp index aa303d85e..6e6ee3756 100644 --- a/lib/libimhex/source/data_processor/attribute.cpp +++ b/lib/libimhex/source/data_processor/attribute.cpp @@ -3,7 +3,7 @@ namespace hex::dp { - u32 Attribute::s_idCounter = 1; + int Attribute::s_idCounter = 1; Attribute::Attribute(IOType ioType, Type type, std::string unlocalizedName) : m_id(Attribute::s_idCounter++), m_ioType(ioType), m_type(type), m_unlocalizedName(std::move(unlocalizedName)) { } diff --git a/lib/libimhex/source/data_processor/link.cpp b/lib/libimhex/source/data_processor/link.cpp index d9da3721f..8884e74ff 100644 --- a/lib/libimhex/source/data_processor/link.cpp +++ b/lib/libimhex/source/data_processor/link.cpp @@ -3,9 +3,9 @@ namespace hex::dp { - u32 Link::s_idCounter = 1; + int Link::s_idCounter = 1; - Link::Link(u32 from, u32 to) : m_id(Link::s_idCounter++), m_from(from), m_to(to) { } + Link::Link(int from, int to) : m_id(Link::s_idCounter++), m_from(from), m_to(to) { } } \ No newline at end of file diff --git a/lib/libimhex/source/data_processor/node.cpp b/lib/libimhex/source/data_processor/node.cpp index ddaf2998c..f80d622c0 100644 --- a/lib/libimhex/source/data_processor/node.cpp +++ b/lib/libimhex/source/data_processor/node.cpp @@ -7,7 +7,7 @@ namespace hex::dp { - u32 Node::s_idCounter = 1; + int Node::s_idCounter = 1; Node::Node(std::string unlocalizedTitle, std::vector attributes) : m_id(Node::s_idCounter++), m_unlocalizedTitle(std::move(unlocalizedTitle)), m_attributes(std::move(attributes)) { for (auto &attr : this->m_attributes) diff --git a/plugins/builtin/include/content/views/view_data_processor.hpp b/plugins/builtin/include/content/views/view_data_processor.hpp index 6e7a5cfc4..6c5ae0647 100644 --- a/plugins/builtin/include/content/views/view_data_processor.hpp +++ b/plugins/builtin/include/content/views/view_data_processor.hpp @@ -20,12 +20,13 @@ namespace hex::plugin::builtin { void drawContent() override; private: + bool m_justSwitchedProvider = false; int m_rightClickedId = -1; ImVec2 m_rightClickedCoords; bool m_continuousEvaluation = false; - void eraseLink(u32 id); + void eraseLink(int id); void eraseNodes(const std::vector &ids); void processNodes(); diff --git a/plugins/builtin/source/content/views/view_data_processor.cpp b/plugins/builtin/source/content/views/view_data_processor.cpp index 82a90e960..5fa946cee 100644 --- a/plugins/builtin/source/content/views/view_data_processor.cpp +++ b/plugins/builtin/source/content/views/view_data_processor.cpp @@ -49,12 +49,13 @@ namespace hex::plugin::builtin { } }); - EventManager::subscribe(this, [](const auto &, const auto &) { + EventManager::subscribe(this, [this](const auto &, const auto &) { auto &data = ProviderExtraData::getCurrent().dataProcessor; for (auto &node : data.nodes) { node->setCurrentOverlay(nullptr); } data.dataOverlays.clear(); + this->m_justSwitchedProvider = true; }); EventManager::subscribe(this, [this] { @@ -103,7 +104,7 @@ namespace hex::plugin::builtin { } - void ViewDataProcessor::eraseLink(u32 id) { + void ViewDataProcessor::eraseLink(int id) { auto &data = ProviderExtraData::getCurrent().dataProcessor; auto link = std::find_if(data.links.begin(), data.links.end(), [&id](auto link) { return link.getId() == id; }); @@ -124,14 +125,14 @@ namespace hex::plugin::builtin { void ViewDataProcessor::eraseNodes(const std::vector &ids) { auto &data = ProviderExtraData::getCurrent().dataProcessor; - for (u32 id : ids) { + for (int id : ids) { auto node = std::find_if(data.nodes.begin(), data.nodes.end(), [&id](const auto &node) { return node->getId() == id; }); for (auto &attr : (*node)->getAttributes()) { - std::vector linksToRemove; + std::vector linksToRemove; for (auto &[linkId, connectedAttr] : attr.getConnectedAttributes()) linksToRemove.push_back(linkId); @@ -140,7 +141,7 @@ namespace hex::plugin::builtin { } } - for (u32 id : ids) { + for (int id : ids) { auto node = std::find_if(data.nodes.begin(), data.nodes.end(), [&id](const auto &node) { return node->getId() == id; }); std::erase_if(data.endNodes, [&id](const auto &node) { return node->getId() == id; }); @@ -287,7 +288,7 @@ namespace hex::plugin::builtin { { int nodeId; - if (ImNodes::IsNodeHovered(&nodeId) && data.currNodeError.has_value() && data.currNodeError->node->getId() == static_cast(nodeId)) { + if (ImNodes::IsNodeHovered(&nodeId) && data.currNodeError.has_value() && data.currNodeError->node->getId() == nodeId) { ImGui::BeginTooltip(); ImGui::TextUnformatted("hex.builtin.common.error"_lang); ImGui::Separator(); @@ -305,7 +306,13 @@ namespace hex::plugin::builtin { if (hasError) ImNodes::PushColorStyle(ImNodesCol_NodeOutline, 0xFF0000FF); - ImNodes::BeginNode(node->getId()); + int nodeId = node->getId(); + if (!this->m_justSwitchedProvider) + node->setPosition(ImNodes::GetNodeGridSpacePos(nodeId)); + else + ImNodes::SetNodeGridSpacePos(nodeId, node->getPosition()); + + ImNodes::BeginNode(nodeId); ImNodes::BeginNodeTitleBar(); ImGui::TextUnformatted(LangEntry(node->getUnlocalizedTitle())); @@ -342,6 +349,8 @@ namespace hex::plugin::builtin { ImNodes::EndNode(); + ImNodes::SetNodeGridSpacePos(nodeId, node->getPosition()); + if (hasError) ImNodes::PopColorStyle(); } @@ -379,9 +388,9 @@ namespace hex::plugin::builtin { dp::Attribute *fromAttr = nullptr, *toAttr = nullptr; for (auto &node : data.nodes) { for (auto &attribute : node->getAttributes()) { - if (attribute.getId() == static_cast(from)) + if (attribute.getId() == from) fromAttr = &attribute; - else if (attribute.getId() == static_cast(to)) + else if (attribute.getId() == to) toAttr = &attribute; } } @@ -429,6 +438,8 @@ namespace hex::plugin::builtin { this->eraseNodes(selectedNodes); } } + + this->m_justSwitchedProvider = false; } ImGui::End(); } @@ -443,7 +454,7 @@ namespace hex::plugin::builtin { for (auto &node : data.nodes) { auto id = node->getId(); auto &currNodeOutput = output["nodes"][std::to_string(id)]; - auto pos = ImNodes::GetNodeGridSpacePos(id); + auto pos = node->getPosition(); currNodeOutput["type"] = node->getUnlocalizedName(); currNodeOutput["pos"] = {