Fix nodes
This commit is contained in:
parent
2e0cb27987
commit
3c1c86f952
6 changed files with 297 additions and 57 deletions
120
gui/BezierConnectionPainter.cpp
Normal file
120
gui/BezierConnectionPainter.cpp
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
#include "BezierConnectionPainter.h"
|
||||
#include "WarpGraphModel.h"
|
||||
|
||||
#include <QtNodes/internal/BasicGraphicsScene.hpp>
|
||||
#include <QtNodes/internal/ConnectionGraphicsObject.hpp>
|
||||
#include <QtNodes/internal/ConnectionState.hpp>
|
||||
#include <QtNodes/internal/Definitions.hpp>
|
||||
#include <QtNodes/StyleCollection>
|
||||
|
||||
#include <QPainter>
|
||||
#include <QPainterPath>
|
||||
#include <QPainterPathStroker>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
QPainterPath BezierConnectionPainter::cubicPath(
|
||||
QtNodes::ConnectionGraphicsObject const &cgo) const {
|
||||
QPointF const &in = cgo.endPoint(QtNodes::PortType::In);
|
||||
QPointF const &out = cgo.endPoint(QtNodes::PortType::Out);
|
||||
|
||||
auto const c1c2 = cgo.pointsC1C2();
|
||||
|
||||
QPainterPath cubic(out);
|
||||
cubic.cubicTo(c1c2.first, c1c2.second, in);
|
||||
return cubic;
|
||||
}
|
||||
|
||||
void BezierConnectionPainter::paint(
|
||||
QPainter *painter,
|
||||
QtNodes::ConnectionGraphicsObject const &cgo) const {
|
||||
auto const &style = QtNodes::StyleCollection::connectionStyle();
|
||||
|
||||
bool const hovered = cgo.connectionState().hovered();
|
||||
bool const selected = cgo.isSelected();
|
||||
bool const sketch = cgo.connectionState().requiresPort();
|
||||
|
||||
auto path = cubicPath(cgo);
|
||||
|
||||
float peakLevel = 0.0f;
|
||||
auto *scene = cgo.nodeScene();
|
||||
if (scene) {
|
||||
auto *model = dynamic_cast<WarpGraphModel *>(&scene->graphModel());
|
||||
if (model) {
|
||||
auto cId = cgo.connectionId();
|
||||
peakLevel = std::max(model->nodePeakLevel(cId.outNodeId),
|
||||
model->nodePeakLevel(cId.inNodeId));
|
||||
}
|
||||
}
|
||||
|
||||
auto activeColor = [&](QColor base) -> QColor {
|
||||
if (peakLevel < 0.005f)
|
||||
return base;
|
||||
float t = std::min(peakLevel * 2.0f, 1.0f);
|
||||
int r = static_cast<int>(base.red() + t * (60 - base.red()));
|
||||
int g = static_cast<int>(base.green() + t * (210 - base.green()));
|
||||
int b = static_cast<int>(base.blue() + t * (80 - base.blue()));
|
||||
return QColor(std::clamp(r, 0, 255),
|
||||
std::clamp(g, 0, 255),
|
||||
std::clamp(b, 0, 255),
|
||||
base.alpha());
|
||||
};
|
||||
|
||||
if (hovered || selected) {
|
||||
QPen pen;
|
||||
pen.setWidth(static_cast<int>(2 * style.lineWidth()));
|
||||
pen.setColor(selected ? style.selectedHaloColor() : style.hoveredColor());
|
||||
painter->setPen(pen);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
painter->drawPath(path);
|
||||
}
|
||||
|
||||
if (sketch) {
|
||||
QPen pen;
|
||||
pen.setWidth(static_cast<int>(style.constructionLineWidth()));
|
||||
pen.setColor(style.constructionColor());
|
||||
pen.setStyle(Qt::DashLine);
|
||||
painter->setPen(pen);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
painter->drawPath(path);
|
||||
} else {
|
||||
QColor base = selected ? style.selectedColor() : style.normalColor();
|
||||
QColor color = selected ? base : activeColor(base);
|
||||
float width = style.lineWidth();
|
||||
if (!selected && peakLevel > 0.005f)
|
||||
width += peakLevel * 1.5f;
|
||||
|
||||
QPen pen;
|
||||
pen.setWidthF(width);
|
||||
pen.setColor(color);
|
||||
painter->setPen(pen);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
painter->drawPath(path);
|
||||
}
|
||||
|
||||
double const pointRadius = style.pointDiameter() / 2.0;
|
||||
painter->setPen(style.constructionColor());
|
||||
painter->setBrush(style.constructionColor());
|
||||
painter->drawEllipse(cgo.out(), pointRadius, pointRadius);
|
||||
painter->drawEllipse(cgo.in(), pointRadius, pointRadius);
|
||||
}
|
||||
|
||||
QPainterPath BezierConnectionPainter::getPainterStroke(
|
||||
QtNodes::ConnectionGraphicsObject const &cgo) const {
|
||||
auto cubic = cubicPath(cgo);
|
||||
|
||||
QPointF const &out = cgo.endPoint(QtNodes::PortType::Out);
|
||||
QPainterPath result(out);
|
||||
|
||||
unsigned int constexpr segments = 20;
|
||||
for (unsigned int i = 0; i < segments; ++i) {
|
||||
double ratio = double(i + 1) / segments;
|
||||
result.lineTo(cubic.pointAtPercent(ratio));
|
||||
}
|
||||
|
||||
QPainterPathStroker stroker;
|
||||
stroker.setWidth(10.0);
|
||||
|
||||
return stroker.createStroke(result);
|
||||
}
|
||||
15
gui/BezierConnectionPainter.h
Normal file
15
gui/BezierConnectionPainter.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#include <QtNodes/internal/AbstractConnectionPainter.hpp>
|
||||
|
||||
class BezierConnectionPainter : public QtNodes::AbstractConnectionPainter {
|
||||
public:
|
||||
void paint(QPainter *painter,
|
||||
QtNodes::ConnectionGraphicsObject const &cgo) const override;
|
||||
QPainterPath
|
||||
getPainterStroke(QtNodes::ConnectionGraphicsObject const &cgo) const override;
|
||||
|
||||
private:
|
||||
QPainterPath
|
||||
cubicPath(QtNodes::ConnectionGraphicsObject const &cgo) const;
|
||||
};
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#include "AudioLevelMeter.h"
|
||||
#include "BezierConnectionPainter.h"
|
||||
#include "GraphEditorWidget.h"
|
||||
#include "PresetManager.h"
|
||||
#include "SquareConnectionPainter.h"
|
||||
|
|
@ -9,7 +10,7 @@
|
|||
#include <QtNodes/BasicGraphicsScene>
|
||||
#include <QtNodes/ConnectionStyle>
|
||||
#include <QtNodes/GraphicsView>
|
||||
#include <QtNodes/internal/DefaultConnectionPainter.hpp>
|
||||
|
||||
#include <QtNodes/internal/NodeGraphicsObject.hpp>
|
||||
#include <QtNodes/internal/ConnectionGraphicsObject.hpp>
|
||||
#include <QtNodes/internal/UndoCommands.hpp>
|
||||
|
|
@ -203,6 +204,8 @@ GraphEditorWidget::GraphEditorWidget(warppipe::Client *client,
|
|||
"UseDataDefinedColors": false
|
||||
}})");
|
||||
|
||||
m_scene->setConnectionPainter(std::make_unique<BezierConnectionPainter>());
|
||||
|
||||
m_view = new ZoomGraphicsView(m_scene);
|
||||
m_view->setFocusPolicy(Qt::StrongFocus);
|
||||
m_view->viewport()->setFocusPolicy(Qt::StrongFocus);
|
||||
|
|
@ -1927,7 +1930,7 @@ void GraphEditorWidget::setConnectionStyle(ConnectionStyleType style) {
|
|||
m_scene->setConnectionPainter(std::make_unique<SquareConnectionPainter>());
|
||||
} else {
|
||||
m_scene->setConnectionPainter(
|
||||
std::make_unique<QtNodes::DefaultConnectionPainter>());
|
||||
std::make_unique<BezierConnectionPainter>());
|
||||
}
|
||||
|
||||
for (auto *item : m_scene->items()) {
|
||||
|
|
|
|||
|
|
@ -481,8 +481,14 @@ void WarpGraphModel::refreshFromClient() {
|
|||
if (existing != m_pwToQt.end()) {
|
||||
QtNodes::NodeId qtId = existing->second;
|
||||
auto &data = m_nodes[qtId];
|
||||
bool typeChanged = (data.info.is_virtual != nodeInfo.is_virtual);
|
||||
data.info = nodeInfo;
|
||||
|
||||
if (typeChanged) {
|
||||
m_styleCache.erase(qtId);
|
||||
Q_EMIT nodeUpdated(qtId);
|
||||
}
|
||||
|
||||
bool portsMissing =
|
||||
data.inputPorts.empty() && data.outputPorts.empty();
|
||||
if (portsMissing) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue