fix: Data processor nodes not remembering their positions correctly

This commit is contained in:
WerWolv 2022-10-06 09:14:46 +02:00
parent 5ace199dc4
commit e0c35e0002
8 changed files with 60 additions and 38 deletions

View File

@ -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<u32, Attribute *> &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<int, Attribute *> &getConnectedAttributes() { return this->m_connectedAttributes; }
[[nodiscard]] Node *getParentNode() { return this->m_parentNode; }
[[nodiscard]] std::optional<std::vector<u8>> &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<u32, Attribute *> m_connectedAttributes;
std::map<int, Attribute *> m_connectedAttributes;
Node *m_parentNode = nullptr;
std::optional<std::vector<u8>> 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;
};
}

View File

@ -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;
};
}

View File

@ -9,6 +9,7 @@
#include <vector>
#include <nlohmann/json_fwd.hpp>
#include <imgui.h>
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<Attribute> m_attributes;
std::set<u32> 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())

View File

@ -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)) {
}

View File

@ -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) { }
}

View File

@ -7,7 +7,7 @@
namespace hex::dp {
u32 Node::s_idCounter = 1;
int Node::s_idCounter = 1;
Node::Node(std::string unlocalizedTitle, std::vector<Attribute> attributes) : m_id(Node::s_idCounter++), m_unlocalizedTitle(std::move(unlocalizedTitle)), m_attributes(std::move(attributes)) {
for (auto &attr : this->m_attributes)

View File

@ -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<int> &ids);
void processNodes();

View File

@ -49,12 +49,13 @@ namespace hex::plugin::builtin {
}
});
EventManager::subscribe<EventProviderChanged>(this, [](const auto &, const auto &) {
EventManager::subscribe<EventProviderChanged>(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<EventDataChanged>(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<int> &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<u32> linksToRemove;
std::vector<int> 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<u32>(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<u32>(from))
if (attribute.getId() == from)
fromAttr = &attribute;
else if (attribute.getId() == static_cast<u32>(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"] = {