1
0
mirror of synced 2025-01-19 01:24:15 +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); } void removeConnectedAttribute(int linkId) { this->m_connectedAttributes.erase(linkId); }
[[nodiscard]] std::map<int, Attribute *> &getConnectedAttributes() { return this->m_connectedAttributes; } [[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; } [[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); Link(int from, int to);
[[nodiscard]] int getId() const { return this->m_id; } [[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 getFromId() const { return this->m_from; }
[[nodiscard]] int getToId() const { return this->m_to; } [[nodiscard]] int getToId() const { return this->m_to; }

View File

@ -10,6 +10,9 @@
#include <map> #include <map>
#include <imnodes.h>
#include <imnodes_internal.h>
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
class ProviderExtraData { class ProviderExtraData {
@ -64,6 +67,15 @@ namespace hex::plugin::builtin {
struct DataProcessor { struct DataProcessor {
struct Workspace { 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<std::unique_ptr<dp::Node>> nodes;
std::list<dp::Node*> endNodes; std::list<dp::Node*> endNodes;
std::list<dp::Link> links; std::list<dp::Link> links;

View File

@ -1372,6 +1372,10 @@ namespace hex::plugin::builtin {
return std::nullopt; 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 // Forward inputs to input nodes values
for (auto &attribute : this->getAttributes()) { for (auto &attribute : this->getAttributes()) {
auto index = indexFromId(attribute.getId()); auto index = indexFromId(attribute.getId());

View File

@ -219,7 +219,9 @@ namespace hex::plugin::builtin {
auto &data = ProviderExtraData::getCurrent().dataProcessor; auto &data = ProviderExtraData::getCurrent().dataProcessor;
auto &workspace = *data.workspaceStack.back(); 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)) { 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)) { if (ImGui::IsMouseReleased(ImGuiMouseButton_Right) && ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows)) {
ImNodes::ClearNodeSelection(); ImNodes::ClearNodeSelection();
@ -436,8 +438,7 @@ namespace hex::plugin::builtin {
if (data.workspaceStack.size() > 1) { if (data.workspaceStack.size() > 1) {
ImGui::SetCursorPos(ImVec2(ImGui::GetContentRegionAvail().x - ImGui::GetTextLineHeightWithSpacing() * 1.2F, ImGui::GetTextLineHeightWithSpacing() * 0.2F)); ImGui::SetCursorPos(ImVec2(ImGui::GetContentRegionAvail().x - ImGui::GetTextLineHeightWithSpacing() * 1.2F, ImGui::GetTextLineHeightWithSpacing() * 0.2F));
if (ImGui::IconButton(ICON_VS_CLOSE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGray))) { if (ImGui::IconButton(ICON_VS_CLOSE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGray))) {
data.workspaceStack.pop_back(); popWorkspace = true;
this->m_updateNodePositions = true;
} }
} }
@ -520,6 +521,11 @@ namespace hex::plugin::builtin {
} }
} }
ImGui::End(); ImGui::End();
if (popWorkspace) {
data.workspaceStack.pop_back();
this->m_updateNodePositions = true;
}
} }
nlohmann::json ViewDataProcessor::saveNode(const dp::Node *node) { 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"])); newNode->setPosition(ImVec2(node["pos"]["x"], node["pos"]["y"]));
if (!node["data"].is_null())
newNode->load(node["data"]);
bool hasOutput = false; bool hasOutput = false;
bool hasInput = false; bool hasInput = false;
u32 attrIndex = 0; u32 attrIndex = 0;
@ -633,9 +642,6 @@ namespace hex::plugin::builtin {
attrIndex++; attrIndex++;
} }
if (!node["data"].is_null())
newNode->load(node["data"]);
if (hasInput && !hasOutput) if (hasInput && !hasOutput)
workspace.endNodes.push_back(newNode.get()); workspace.endNodes.push_back(newNode.get());
@ -649,7 +655,7 @@ namespace hex::plugin::builtin {
int linkId = link["id"]; int linkId = link["id"];
maxLinkId = std::max(linkId, maxLinkId); maxLinkId = std::max(linkId, maxLinkId);
newLink.setID(linkId); newLink.setId(linkId);
workspace.links.push_back(newLink); workspace.links.push_back(newLink);
dp::Attribute *fromAttr = nullptr, *toAttr = nullptr; dp::Attribute *fromAttr = nullptr, *toAttr = nullptr;