imgui-node-editor icon indicating copy to clipboard operation
imgui-node-editor copied to clipboard

Find link for node - internal?

Open Martinii89 opened this issue 5 years ago • 2 comments
trafficstars

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?

Martinii89 avatar Jun 14 '20 13:06 Martinii89

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() );
	}
}

soufianekhiat avatar Jun 18 '20 18:06 soufianekhiat

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;
}

thedmd avatar Sep 04 '20 09:09 thedmd