nodeview can connect and disconnect nodes

This commit is contained in:
itsmattkc
2019-07-16 04:43:57 -04:00
parent 9d2fc71efc
commit 1a39216be2
10 changed files with 251 additions and 49 deletions
+54 -21
View File
@@ -21,21 +21,31 @@
#include "nodeviewedge.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include "common/clamp.h"
#include "common/lerp.h"
#include "nodeview.h"
const int kNodeEdgeWidth = 2;
NodeViewEdge::NodeViewEdge(QGraphicsItem *parent) :
QGraphicsLineItem(parent),
edge_(nullptr)
edge_(nullptr),
moving_(false),
connected_(false)
{
// Ensures this UI object is drawn behind other objects
setZValue(-1);
setAcceptHoverEvents(true);
}
void NodeViewEdge::SetEdge(NodeEdgePtr edge)
{
SetMoving(false);
SetConnected(true);
// Set the new edge pointer
edge_ = edge;
@@ -43,9 +53,32 @@ void NodeViewEdge::SetEdge(NodeEdgePtr edge)
Adjust();
}
NodeEdgePtr NodeViewEdge::edge()
{
return edge_;
}
qreal CalculateEdgeYPoint(NodeViewItem *item, int param_index, NodeViewItem *opposing)
{
if (item->IsExpanded()) {
return item->pos().y() + item->GetParameterConnectorRect(param_index).center().y();
} else {
qreal max_height = qMax(opposing->rect().height(), item->rect().height());
// Calculate the Y distance between the two nodes and create a 0.0-1.0 range for lerping
qreal input_value = clamp(0.5 + (opposing->pos().y() - item->pos().y()) / max_height / 4, 0.0, 1.0);
// Use a lerp function to draw the line between the two corners
qreal input_y = item->pos().y() + lerp(0.0, item->rect().height(), input_value);
// Set Y values according to calculations
return input_y;
}
}
void NodeViewEdge::Adjust()
{
if (edge_ == nullptr || scene() == nullptr) {
if (edge_ == nullptr || scene() == nullptr || moving_) {
return;
}
@@ -68,28 +101,28 @@ void NodeViewEdge::Adjust()
));
}
void NodeViewEdge::SetMoving(bool m)
{
moving_ = m;
}
void NodeViewEdge::SetConnected(bool c)
{
connected_ = c;
}
void NodeViewEdge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
setPen(QPen(widget->palette().color(QPalette::Text), 2));
QPalette::ColorGroup color_mode;
if (connected_) {
color_mode = QPalette::Active;
} else {
color_mode = QPalette::Disabled;
}
setPen(QPen(widget->palette().color(color_mode, QPalette::Text), kNodeEdgeWidth));
QGraphicsLineItem::paint(painter, option, widget);
}
qreal NodeViewEdge::CalculateEdgeYPoint(NodeViewItem *item, int param_index, NodeViewItem *opposing)
{
if (item->IsExpanded()) {
return item->pos().y() + item->GetParameterConnectorRect(param_index).center().y();
} else {
qreal max_height = qMax(opposing->rect().height(), item->rect().height());
// Calculate the Y distance between the two nodes and create a 0.0-1.0 range for lerping
qreal input_value = clamp(0.5 + (opposing->pos().y() - item->pos().y()) / max_height / 4, 0.0, 1.0);
// Use a lerp function to draw the line between the two corners
qreal input_y = item->pos().y() + lerp(0.0, item->rect().height(), input_value);
// Set Y values according to calculations
return input_y;
}
}