diff --git a/include/flow/core/Graph.hpp b/include/flow/core/Graph.hpp index 46648c2..4656998 100644 --- a/include/flow/core/Graph.hpp +++ b/include/flow/core/Graph.hpp @@ -184,6 +184,26 @@ class Graph */ EventDispatcher OnError; + /** + * @brief Event run on Graph when a new node is added. + */ + EventDispatcher OnNodeAdded; + + /** + * @brief Event run on Graph when a new node is removed. + */ + EventDispatcher OnNodeRemoved; + + /** + * @brief Event run when 2 nodes are connected. + */ + EventDispatcher OnNodesConnected; + + /** + * @brief Event run on Graph when a connection is removed. + */ + EventDispatcher OnNodesDisconnected; + protected: mutable std::mutex _nodes_mutex; diff --git a/src/Graph.cpp b/src/Graph.cpp index cf60fcc..f2cf3bf 100644 --- a/src/Graph.cpp +++ b/src/Graph.cpp @@ -84,15 +84,16 @@ void Graph::AddNode(SharedNode node) { if (!node) return; - std::lock_guard _(_nodes_mutex); + { + std::lock_guard _(_nodes_mutex); + _nodes.emplace(node->ID(), node); + } node->OnEmitOutput.Bind("PropagateConnectionsData", [this](const UUID& id, const IndexableName& key, SharedNodeData data) { this->PropagateConnectionsData(id, key, std::move(data)); }); - - UUID id = node->ID(); - _nodes.emplace(id, std::move(node)); + OnNodeAdded.Broadcast(node); } void Graph::RemoveNode(const SharedNode& node) @@ -112,6 +113,9 @@ void Graph::RemoveNodeByID(const UUID& uuid) if (found != _nodes.end()) { found->second->Stop(); + + OnNodeRemoved.Broadcast(found->second); + _nodes.erase(found); } } @@ -227,12 +231,27 @@ SharedConnection Graph::ConnectNodes(const UUID& start_id, const IndexableName& PropagateConnectionsData(start_id, start_port_key, std::move(data)); } + OnNodesConnected.Broadcast(conn); + return conn; } void Graph::DisconnectNodes(const UUID& start_id, const IndexableName& start_port_key, const UUID& end_id, const IndexableName& end_port_key) { + + auto conns = _connections.FindConnections(start_id, start_port_key); + auto found_conn = std::find_if(conns.begin(), conns.end(), [&](const auto& conn) { + return conn->EndNodeID() == end_id && conn->EndPortKey() == end_port_key; + }); + + if (found_conn == conns.end()) + { + return; + } + + OnNodesDisconnected.Broadcast(*found_conn); + _connections.Remove(start_id, end_id); auto in_node = GetNode(start_id);