1
0
mirror of synced 2025-01-18 17:14:13 +01:00

nodes: Fixed loading saved nodes multiple times

This commit is contained in:
WerWolv 2023-02-10 11:22:11 +01:00
parent 6281adc7c3
commit a59c17aa83
5 changed files with 30 additions and 8 deletions

View File

@ -38,7 +38,7 @@ namespace hex::dp {
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]] Node *getParentNode() const { return this->m_parentNode; }
[[nodiscard]] std::optional<std::vector<u8>> &getOutputData() { return this->m_outputData; }

View File

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

View File

@ -10,6 +10,9 @@
#include <map>
#include <imnodes.h>
#include <imnodes_internal.h>
namespace hex::plugin::builtin {
class ProviderExtraData {
@ -64,6 +67,15 @@ namespace hex::plugin::builtin {
struct DataProcessor {
struct Workspace {
std::unique_ptr<ImNodesContext, void(*)(ImNodesContext*)> context = { []{
ImNodesContext *ctx = ImNodes::CreateContext();
ctx->Style = ImNodes::GetStyle();
ctx->Io = ImNodes::GetIO();
ctx->AttributeFlagStack = GImNodes->AttributeFlagStack;
return ctx;
}(), ImNodes::DestroyContext };
std::list<std::unique_ptr<dp::Node>> nodes;
std::list<dp::Node*> endNodes;
std::list<dp::Link> links;

View File

@ -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());

View File

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