feat: add network connections

This commit is contained in:
parker
2025-07-04 19:02:34 +01:00
parent af6c2eb7c5
commit 2a3b041943
13 changed files with 226 additions and 79 deletions

View File

@@ -32,8 +32,8 @@ void FloatingEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsIte
painter->setPen(pen);
// painter->drawLine(socket1_->scenePos(),floatPos_);
QPointF pos1 = socket1_->getIO()==SocketGraphic::SocketType::Input ? socket1_->scenePos() : floatPos_;
QPointF pos2 = socket1_->getIO()==SocketGraphic::SocketType::Input ? floatPos_ : socket1_->scenePos();
QPointF pos1 = socket1_->getIO()==enzo::nt::SocketIOType::Input ? socket1_->scenePos() : floatPos_;
QPointF pos2 = socket1_->getIO()==enzo::nt::SocketIOType::Input ? floatPos_ : socket1_->scenePos();
float dist = std::sqrt(std::pow(pos1.x()-pos2.x(),2)+std::pow(pos1.y()-pos2.y(),2));
std::cout << "dist: " << dist << "\n";
float cubicStrength = dist*0.5;

View File

@@ -1,4 +1,6 @@
#include "gui/network/Network.h"
#include "Engine/Operator/GeometryConnection.h"
#include "Engine/Operator/GeometryOperator.h"
#include "Engine/Types.h"
#include "gui/network/DisplayFlagButton.h"
#include "gui/network/NodeEdgeGraphic.h"
@@ -8,6 +10,7 @@
#include "gui/network/FloatingEdgeGraphic.h"
#include "gui/network/SocketGraphic.h"
#include "Engine/Network/NetworkManager.h"
#include <memory>
#include <qboxlayout.h>
#include <QPushButton>
#include <QGraphicsItem>
@@ -126,14 +129,27 @@ void Network::socketClicked(SocketGraphic* socket, QMouseEvent *event)
}
// second click
// connect to opposite type
else if (socket->getIO()!=startSocket_->getIO())
else if (
socket->getIO()!=startSocket_->getIO() &&
startSocket_->getOpId()!=socket->getOpId()
)
{
auto inputSocket = startSocket_->getIO()==SocketGraphic::SocketType::Input ? startSocket_ : socket;
auto outputSocket = socket->getIO()==SocketGraphic::SocketType::Output ? socket : startSocket_;
NodeEdgeGraphic* newEdge = new NodeEdgeGraphic(inputSocket, outputSocket);
// order sockets in relation to data flow
// the input node is the node the data flows from
auto inputNodeSocket = socket->getIO()==enzo::nt::SocketIOType::Output ? socket : startSocket_;
// the output node is the node the data flows to
auto outputNodeSocket = startSocket_->getIO()==enzo::nt::SocketIOType::Input ? startSocket_ : socket;
newEdge->setPos(inputSocket->scenePos(), outputSocket->scenePos());
std::cout << "CONNECTING opid: " << inputNodeSocket->getOpId() << " -> " << outputNodeSocket->getOpId() << "\n";
nt::connectOperators(inputNodeSocket->getOpId(), inputNodeSocket->getIndex(), outputNodeSocket->getOpId(), outputNodeSocket->getIndex());
NodeEdgeGraphic* newEdge = new NodeEdgeGraphic(outputNodeSocket, inputNodeSocket);
newEdge->setPos(outputNodeSocket->scenePos(), inputNodeSocket->scenePos());
scene_->addItem(newEdge);
destroyFloatingEdge();
}

View File

@@ -5,6 +5,8 @@
#include <qnamespace.h>
#include <stdexcept>
#include <string>
#include "Engine/Network/NetworkManager.h"
#include "Engine/Operator/GeometryOperator.h"
#include "gui/network/DisplayFlagButton.h"
#include "gui/network/SocketGraphic.h"
#include <QGraphicsScene>
@@ -76,13 +78,22 @@ void NodeGraphic::initFlagButtons()
void NodeGraphic::initSockets()
{
auto* socketInput = new SocketGraphic(SocketGraphic::SocketType::Input, this);
socketInput->setPos(getSocketPosition(0, SocketGraphic::SocketType::Input));
inputs_.push_back(socketInput);
auto nm = enzo::nt::NetworkManager::getInstance();
enzo::nt::GeometryOperator& op = nm->getGeoOperator(opId_);
for(int i=0, max=op.getMaxInputs(); i<max; ++i)
{
auto* socketInput = new SocketGraphic(enzo::nt::SocketIOType::Input, opId_, i, this);
socketInput->setPos(getSocketPosition(i, enzo::nt::SocketIOType::Input));
inputs_.push_back(socketInput);
}
for(int i=0, max=op.getMaxOutputs(); i<max; ++i)
{
auto* socketOutput = new SocketGraphic(enzo::nt::SocketIOType::Output, opId_, i, this);
socketOutput->setPos(getSocketPosition(i, enzo::nt::SocketIOType::Output));
outputs_.push_back(socketOutput);
}
auto* socketOutput = new SocketGraphic(SocketGraphic::SocketType::Output, this);
socketOutput->setPos(getSocketPosition(0, SocketGraphic::SocketType::Output));
outputs_.push_back(socketOutput);
}
// void setInputEdge(NodeEdgeGraphic* edge, int indx)
@@ -183,23 +194,30 @@ void NodeGraphic::updatePositions(QPointF pos)
// but for now I'm just going based on order
for(int socketIndex=0; socketIndex<inputs_.size(); ++socketIndex)
{
inputs_[socketIndex]->posChanged(getSocketScenePosition(socketIndex, SocketGraphic::SocketType::Input));
inputs_[socketIndex]->posChanged(getSocketScenePosition(socketIndex, enzo::nt::SocketIOType::Input));
}
for(int socketIndex=0; socketIndex<outputs_.size(); ++socketIndex)
{
outputs_[socketIndex]->posChanged(getSocketScenePosition(socketIndex, SocketGraphic::SocketType::Output));
outputs_[socketIndex]->posChanged(getSocketScenePosition(socketIndex, enzo::nt::SocketIOType::Output));
}
}
QPointF NodeGraphic::getSocketPosition(int socketIndex, SocketGraphic::SocketType socketType)
QPointF NodeGraphic::getSocketPosition(int socketIndex, enzo::nt::SocketIOType socketType)
{
auto nm = enzo::nt::NetworkManager::getInstance();
enzo::nt::GeometryOperator& op = nm->getGeoOperator(opId_);
int maxSocketNumber = socketType==enzo::nt::SocketIOType::Input ? op.getMaxInputs() : op.getMaxOutputs();
float socketSpread = socketSize_*1.5*maxSocketNumber;
float xPos, yPos;
xPos = bodyRect_.center().x();
yPos = socketType == SocketGraphic::SocketType::Input ? bodyRect_.top() : bodyRect_.bottom();
yPos = socketType == enzo::nt::SocketIOType::Input ? bodyRect_.top() : bodyRect_.bottom();
xPos += ((socketIndex/static_cast<float>(maxSocketNumber-1))-0.5)*2*socketSpread;
return QPointF(xPos, yPos);
}
QPointF NodeGraphic::getSocketScenePosition(int socketIndex, SocketGraphic::SocketType socketType)
QPointF NodeGraphic::getSocketScenePosition(int socketIndex, enzo::nt::SocketIOType socketType)
{
return this->pos()+getSocketPosition(socketIndex, socketType);
}

View File

@@ -17,8 +17,8 @@ public:
SocketGraphic* getInput(int indx) const;
SocketGraphic* getOutput(int indx) const;
QPointF getSocketPosition(int socketIndex, SocketGraphic::SocketType socketType);
QPointF getSocketScenePosition(int socketIndex, SocketGraphic::SocketType socketType);
QPointF getSocketPosition(int socketIndex, enzo::nt::SocketIOType socketType);
QPointF getSocketScenePosition(int socketIndex, enzo::nt::SocketIOType socketType);
QRectF getBodyRect();
// void addEdge(NodeEdgeGraphic* edge);

View File

@@ -4,8 +4,8 @@
#include <qgraphicsitem.h>
#include "gui/network/NodeEdgeGraphic.h"
SocketGraphic::SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *parent)
: QGraphicsItem(parent), type_{type}
SocketGraphic::SocketGraphic(enzo::nt::SocketIOType type, enzo::nt::OpId opId, unsigned int socketIndex, QGraphicsItem *parent)
: QGraphicsItem(parent), type_{type}, opId_{opId}, socketIndex_{socketIndex}
{
brushActive_ = QBrush("white");
brushInactive_ = QBrush("#9f9f9f");
@@ -14,6 +14,12 @@ SocketGraphic::SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *pare
initBoundingBox();
}
unsigned int SocketGraphic::getIndex() const
{
return socketIndex_;
}
void SocketGraphic::addEdge(NodeEdgeGraphic* edge)
{
std::cout << "adding edge\n";
@@ -51,11 +57,11 @@ void SocketGraphic::posChanged(QPointF pos)
for(auto* edge : edges_)
{
// edge->setPos(startSocket_->scenePos(), socket->scenePos());
if(type_==SocketType::Input)
if(type_==enzo::nt::SocketIOType::Input)
{
edge->setStartPos(pos);
}
else if(type_==SocketType::Output)
else if(type_==enzo::nt::SocketIOType::Output)
{
edge->setEndPos(pos);
}
@@ -63,6 +69,10 @@ void SocketGraphic::posChanged(QPointF pos)
}
enzo::nt::OpId SocketGraphic::getOpId() const
{
return opId_;
}
QRectF SocketGraphic::boundingRect() const
{
@@ -71,16 +81,16 @@ QRectF SocketGraphic::boundingRect() const
QPainterPath SocketGraphic::shape() const{
QPainterPath path;
QPointF startPt(boundRect_.center().x(), type_==SocketType::Input ? boundRect_.top() : boundRect_.bottom());
QPointF startPt(boundRect_.center().x(), type_==enzo::nt::SocketIOType::Input ? boundRect_.top() : boundRect_.bottom());
path.moveTo(startPt);
path.arcTo(boundRect_, 0, type_==SocketType::Input ? 180 : -180);
path.arcTo(boundRect_, 0, type_==enzo::nt::SocketIOType::Input ? 180 : -180);
path.lineTo(boundRect_.right(), boundRect_.center().y());
path.closeSubpath();
return path;
}
SocketGraphic::SocketType SocketGraphic::getIO() { return type_; }
enzo::nt::SocketIOType SocketGraphic::getIO() { return type_; }
void SocketGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)

View File

@@ -1,4 +1,5 @@
#pragma once
#include "Engine/Types.h"
#include <QGraphicsItem>
#include <QPainter>
#include <unordered_set>
@@ -14,27 +15,28 @@ public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
enum class SocketType {
Input,
Output
};
SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *parent = nullptr);
SocketType getIO();
SocketGraphic(enzo::nt::SocketIOType type, enzo::nt::OpId opId, unsigned int socketIndex, QGraphicsItem *parent = nullptr);
enzo::nt::SocketIOType getIO();
void addEdge(NodeEdgeGraphic* edge);
void removeEdge(NodeEdgeGraphic* edge);
void posChanged(QPointF pos);
QPainterPath shape() const override;
enzo::nt::OpId getOpId() const;
unsigned int getIndex() const;
private:
int socketSize_ = 1;
unsigned int socketIndex_;
QBrush brushInactive_;
QBrush brushActive_;
bool hovered_=false;
SocketType type_;
enzo::nt::SocketIOType type_;
std::unordered_set<NodeEdgeGraphic*> edges_;
qreal paddingScale_=20;
QRectF boundRect_;
enzo::nt::OpId opId_;
void initBoundingBox();
protected: