/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Code/Tools/Libs/GuiFoundation/VisualGraph/Implementation/Pin.cpp
251 строка
6 KB
Jan Krassnigg
Visual graph infrastructure cleanup (#1821)
19 фев 2026, 09:02
Не верифицирован
19 фев 2026, 09:02
7f7f12f
Код
Авторство
О чём код?
#include <GuiFoundation/GuiFoundationPCH.h> #include <Foundation/Strings/TranslationLookup.h> #include <GuiFoundation/VisualGraph/Connection.h> #include <GuiFoundation/VisualGraph/Pin.h> #include <QApplication> #include <QPalette> ezQtVisualGraphPin::ezQtVisualGraphPin() { auto palette = QApplication::palette(); QPen pen(palette.light().color(), 3, Qt::SolidLine); setPen(pen); setBrush(palette.base()); setFlag(QGraphicsItem::ItemSendsGeometryChanges); setFlag(QGraphicsItem::ItemSendsScenePositionChanges); m_pLabel = new QGraphicsTextItem(this); } ezQtVisualGraphPin::~ezQtVisualGraphPin() = default; void ezQtVisualGraphPin::AddConnection(ezQtVisualGraphConnection* pConnection) { EZ_ASSERT_DEBUG(!m_Connections.Contains(pConnection), "Connection already present!"); m_Connections.PushBack(pConnection); ConnectedStateChanged(true); UpdateConnections(); } void ezQtVisualGraphPin::RemoveConnection(ezQtVisualGraphConnection* pConnection) { EZ_ASSERT_DEBUG(m_Connections.Contains(pConnection), "Connection not present!"); m_Connections.RemoveAndSwap(pConnection); if (m_Connections.IsEmpty()) ConnectedStateChanged(false); } void ezQtVisualGraphPin::ConnectedStateChanged(bool bConnected) { UpdatePinColors(); } void ezQtVisualGraphPin::SetPin(const ezVisualGraphPin& pin) { m_pPin = &pin; if (m_bTranslatePinName) { m_pLabel->setPlainText(ezMakeQString(ezTranslate(pin.GetName()))); } else { m_pLabel->setPlainText(pin.GetName()); } auto rectLabel = m_pLabel->boundingRect(); const int iRadus = rectLabel.height(); QRectF bounds; if (pin.GetType() == ezVisualGraphPin::Type::Input) { m_pLabel->setPos(iRadus, 0); bounds = QRectF(0, 0, iRadus, iRadus); } else { m_pLabel->setPos(0, 0); bounds = QRectF(rectLabel.width(), 0, iRadus, iRadus); } const int shrink = 3; bounds.adjust(shrink, shrink, -shrink, -shrink); m_PinCenter = bounds.center(); { QPainterPath p; switch (m_pPin->m_Shape) { case ezVisualGraphPin::Shape::Circle: p.addEllipse(bounds); break; case ezVisualGraphPin::Shape::Rect: p.addRect(bounds); break; case ezVisualGraphPin::Shape::RoundRect: p.addRoundedRect(bounds, 2, 2); break; case ezVisualGraphPin::Shape::Arrow: { QPolygonF arrow; arrow.append(bounds.topLeft()); arrow.append(QPointF(bounds.center().x(), bounds.top())); arrow.append(QPointF(bounds.right(), bounds.center().y())); arrow.append(QPointF(bounds.center().x(), bounds.bottom())); arrow.append(bounds.bottomLeft()); arrow.append(bounds.topLeft()); p.addPolygon(arrow); break; } EZ_DEFAULT_CASE_NOT_IMPLEMENTED; } setPath(p); } UpdatePinColors(); } QPointF ezQtVisualGraphPin::GetPinPos() const { return mapToScene(m_PinCenter); } QPointF ezQtVisualGraphPin::GetPinDir() const { if (m_pPin->GetType() == ezVisualGraphPin::Type::Input) { return QPointF(-1.0f, 0.0f); } else { return QPointF(1.0f, 0.0f); ; } } QRectF ezQtVisualGraphPin::GetPinRect() const { auto rectLabel = m_pLabel->boundingRect(); rectLabel.translate(m_pLabel->pos()); if (m_pPin->GetType() == ezVisualGraphPin::Type::Input) { rectLabel.adjust(-5, 0, 0, 0); } else { rectLabel.adjust(0, 0, 5, 0); } return rectLabel; } void ezQtVisualGraphPin::UpdateConnections() { for (ezQtVisualGraphConnection* pConnection : m_Connections) { if (m_pPin->GetType() == ezVisualGraphPin::Type::Input) { pConnection->SetDirIn(GetPinDir()); pConnection->SetPosIn(GetPinPos()); } else { pConnection->SetDirOut(GetPinDir()); pConnection->SetPosOut(GetPinPos()); } } } void ezQtVisualGraphPin::SetHighlightState(ezQtVisualGraphPinHighlight state) { if (m_HighlightState != state) { m_HighlightState = state; if (UpdatePinColors()) { update(); } } } void ezQtVisualGraphPin::SetActive(bool bActive) { m_bIsActive = bActive; if (UpdatePinColors()) { update(); } } bool ezQtVisualGraphPin::UpdatePinColors(const ezColorGammaUB* pOverwriteColor) { ezColorGammaUB pinColor = pOverwriteColor != nullptr ? *pOverwriteColor : GetPin()->GetColor(); QColor base = QApplication::palette().window().color(); if (!m_bIsActive) pinColor = ezMath::Lerp<ezColor>(ezColorGammaUB(base.red(), base.green(), base.blue()), pinColor, 0.2f); switch (m_HighlightState) { case ezQtVisualGraphPinHighlight::None: { QPen p = pen(); p.setColor(ezToQtColor(pinColor)); setPen(p); setBrush(HasAnyConnections() ? pen().color().darker(125) : base); } break; case ezQtVisualGraphPinHighlight::CannotConnect: case ezQtVisualGraphPinHighlight::CannotConnectSameDirection: { QPen p = pen(); p.setColor(base.lighter()); setPen(p); setBrush(base); } break; case ezQtVisualGraphPinHighlight::CanReplaceConnection: case ezQtVisualGraphPinHighlight::CanAddConnection: { QPen p = pen(); p.setColor(ezToQtColor(pinColor)); setPen(p); setBrush(base); } break; } QColor labelColor = QApplication::palette().buttonText().color(); if (!m_bIsActive) labelColor = labelColor.darker(150); m_pLabel->setDefaultTextColor(labelColor); return true; } QVariant ezQtVisualGraphPin::itemChange(GraphicsItemChange change, const QVariant& value) { if (change == QGraphicsItem::ItemScenePositionHasChanged) { UpdateConnections(); } return QGraphicsPathItem::itemChange(change, value); }