fix: connection switching

This commit is contained in:
parker
2025-08-19 21:25:20 +01:00
parent 0b9e2bf244
commit b81dc7a889
7 changed files with 55 additions and 31 deletions

View File

@@ -1,5 +1,6 @@
#include "Engine/Operator/GeometryConnection.h"
#include "Engine/Network/NetworkManager.h"
#include <icecream.hpp>
enzo::nt::GeometryConnection::GeometryConnection(enzo::nt::OpId inputOpId, unsigned int inputIndex, enzo::nt::OpId outputOpId, unsigned int outputIndex)
:inputOperatorId_{inputOpId}, inputIndex_{inputIndex}, outputOperatorId_{outputOpId}, outputIndex_{outputIndex}
@@ -14,7 +15,11 @@ unsigned int enzo::nt::GeometryConnection::getOutputIndex() const {return output
void enzo::nt::GeometryConnection::remove()
{
NetworkManager& nm = nt::nm();
std::cout << "removing connection " << this;
nm.getGeoOperator(inputOperatorId_).removeOutputConnection(this);
nm.getGeoOperator(outputOperatorId_).removeInputConnection(inputIndex_);
removed();
}

View File

@@ -1,6 +1,8 @@
#pragma once
#include "Engine/Types.h"
#include <boost/signals2.hpp>
namespace enzo::nt
{
class GeometryOperator;
@@ -16,7 +18,12 @@ public:
enzo::nt::OpId getOutputOpId() const;
unsigned int getInputIndex() const;
unsigned int getOutputIndex() const;
friend std::ostream& operator<<(std::ostream& os, const GeometryConnection& p)
{
return os << p.inputOperatorId_ << ":" << p.inputIndex_ << " -> " << p.outputOperatorId_ << ":" << p.outputIndex_ << "\n";
}
boost::signals2::signal<void ()> removed;
void remove();
// bool isValid();
private:

View File

@@ -1,4 +1,5 @@
#include "Engine/Operator/GeometryOperator.h"
#include <functional>
#include <memory>
#include "Engine/Network/NetworkManager.h"
#include <optional>
@@ -78,22 +79,24 @@ geo::Geometry& enzo::nt::GeometryOperator::getOutputGeo(unsigned outputIndex) co
void nt::GeometryOperator::addInputConnection(std::shared_ptr<nt::GeometryConnection> newConnection)
{
// delete previous input
for(auto it=inputConnections_.begin(); it!=inputConnections_.end();)
std::shared_ptr<nt::GeometryConnection> previousConection = nullptr;
IC();
for(auto it=inputConnections_.begin(); it!=inputConnections_.end(); ++it)
{
if((*it)->getOutputIndex()==newConnection->getOutputIndex())
{
inputConnections_.erase(it);
}
else
{
++it;
previousConection = *it;
break;
}
}
if(previousConection)
{
previousConection->remove();
}
// add new newConnection
inputConnections_.push_back(newConnection);
IC();
dirtyNode();
}
@@ -113,11 +116,12 @@ void nt::GeometryOperator::removeInputConnection(unsigned int inputIndex)
{
inputConnections_.erase(it);
dirtyNode();
std::cout << "removing input connection\n";
std::cerr << "removing input connection\n";
return;
}
}
std::cerr << "Couldn't remove input connection: " << inputIndex << "\n";
IC(inputIndex, opId_, inputConnections_.size());
}
void nt::GeometryOperator::removeOutputConnection(const nt::GeometryConnection* connectionPtr)
@@ -177,13 +181,13 @@ std::string nt::GeometryOperator::getTypeName()
}
std::optional<const nt::GeometryConnection> nt::GeometryOperator::getInputConnection(size_t index) const
std::optional<std::reference_wrapper<const nt::GeometryConnection>> nt::GeometryOperator::getInputConnection(size_t index) const
{
for(auto it=inputConnections_.begin(); it!=inputConnections_.end();)
{
if((*it)->getOutputIndex()==index)
{
return std::optional<const nt::GeometryConnection>(**it);
return std::cref(**it);
}
}
return std::nullopt;

View File

@@ -4,6 +4,7 @@
#include "Engine/Operator/GeometryOpDef.h"
#include "Engine/Parameter/Parameter.h"
#include "Engine/Types.h"
#include <functional>
#include <optional>
#include <memory>
@@ -30,7 +31,7 @@ public:
std::vector<std::weak_ptr<const GeometryConnection>> getInputConnections() const;
std::vector<std::weak_ptr<const GeometryConnection>> getOutputConnections() const;
std::optional<const GeometryConnection> getInputConnection(size_t index) const;
std::optional<std::reference_wrapper<const GeometryConnection>> getInputConnection(size_t index) const;
std::vector<std::weak_ptr<prm::Parameter>> getParameters();
std::weak_ptr<prm::Parameter> getParameter(std::string parameterName);

View File

@@ -21,6 +21,11 @@ NodeEdgeGraphic::NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2,
deleteHighlightPen_.setWidth(2);
updateDeleteHighlightPen();
// remove self when connection is removed
if(auto connectionShared = connection_.lock())
{
connectionShared->removed.connect([this](){ this->remove(false); });
}
socket1_->addEdge(this);
socket2_->addEdge(this);
@@ -149,7 +154,7 @@ void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
}
void NodeEdgeGraphic::remove()
void NodeEdgeGraphic::remove(bool full)
{
// TODO: possible memory leak
// these probably aren't necessary but i'm trying to fix a bug
@@ -161,8 +166,11 @@ void NodeEdgeGraphic::remove()
socket1_->removeEdge(this);
socket2_->removeEdge(this);
if(auto connectionShared = connection_.lock())
if(full)
{
connectionShared->remove();
if(auto connectionShared = connection_.lock())
{
connectionShared->remove();
}
}
}

View File

@@ -22,7 +22,7 @@ public:
void setPos(QPointF pos1, QPointF pos2);
void setStartPos(QPointF pos);
void setEndPos(QPointF pos);
void remove();
void remove(bool full=true);
void setDeleteHighlight(bool state);
QPen deleteHighlightPen_;
QPen defaultPen_;

View File

@@ -22,29 +22,28 @@ unsigned int SocketGraphic::getIndex() const
void SocketGraphic::addEdge(NodeEdgeGraphic* edge)
{
std::cout << "adding edge\n";
if(getIO()==enzo::nt::SocketIOType::Input && edges_.size()>0)
{
std::unordered_set<NodeEdgeGraphic*> edgesCopy = edges_;
for(NodeEdgeGraphic* edge : edgesCopy)
{
if(!edge)
{
continue;
}
edge->remove();
}
edges_.clear();
}
// std::cout << "adding edge\n";
// const bool isInput = getIO()==enzo::nt::SocketIOType::Input;
// if(isInput && edges_.size()>0)
// {
// std::unordered_set<NodeEdgeGraphic*> edgesCopy = edges_;
// for(NodeEdgeGraphic* edge : edgesCopy)
// {
// if(!edge)
// {
// continue;
// }
// edge->remove();
// }
// edges_.clear();
// }
edges_.insert(edge);
}
void SocketGraphic::removeEdge(NodeEdgeGraphic* edge)
{
std::cout << "removing edge\n";
std::cout << "before size: " << edges_.size() << "\n";
edges_.erase(edge);
std::cout << "after size: " << edges_.size() << "\n";
// auto it = find(edges_.begin(), edges_.end(), edge);
// if(it!=edges_.end())
// {