imgui-node-editor
imgui-node-editor copied to clipboard
Find link for node - internal?
Hello. I see there is a function to grab the links related to a node, but it's in the internal header. So we're not supposed to rely on this?
You can handcraft something like. Which is close to what the demo Blueprint is doing.
Pin* FindPin( ax::NodeEditor::PinId const id )
{
if ( !id )
return nullptr;
for ( Node* pNode : m_aNodes )
{
for ( Pin* pPin : pNode->GetInputs() )
if ( pPin->GetPinID() == id )
return pPin;
for ( Pin* pPin : pNode->GetOutputs() )
if ( pPin->GetPinID() == id )
return pPin;
}
return nullptr;
}
Node* GetConnectedNode( ax::NodeEditor::PinId id )
{
if ( !id )
return nullptr;
for ( Link* pLink : m_aLinks )
{
if ( pLink->GetStartPinID() == id )
{
return FindPin( pLink->GetEndPinID() )->GetNode();
}
else if ( pLink->GetEndPinID() == id )
{
return FindPin( pLink->GetStartPinID() )->GetNode();
}
}
return nullptr;
}
void GetConnectedLinks( std::vector< ax::NodeEditor::LinkId >& aResults, ax::NodeEditor::NodeId const id ) const
{
aResults.clear();
for ( Link const* pLink : m_aLinks )
{
ax::NodeEditor::PinId const a = pLink->GetStartPinID();
ax::NodeEditor::PinId const b = pLink->GetEndPinID();
if ( GetConnectedNode( a )->GetNodeID() == id )
aResults.push_back( pLink->GetLinkID() );
else if ( GetConnectedNode( b )->GetNodeID() == id )
aResults.push_back( pLink->GetLinkID() );
}
}
Public API do not cover every base yet. So far I didn't have a need for such function because blueprints I had to deal with already had such functions.
Enhancements will be made in near future, to allow querying for node connections. In the mean time you can extend an public API using and expose what you need using internal API.
Also I would love a feedback what works for you.
So far I think you can use this snippet:
ImVector<ax::NodeEditor::LinkId> ax::NodeEditor::FindLinksForNode(NodeId nodeId)
{
ImVector<LinkId> result;
std::vector<ax::NodeEditor::Detail::Link*> links;
s_Editor->FindLinksForNode(nodeId, links, false);
result.reserve(static_cast<int>(links.size()));
for (auto link : links)
result.push_back(link->m_ID);
return result;
}