Graph.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // AZ
  9. #include <AzCore/std/smart_ptr/make_shared.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. // Graph Model
  14. #include <GraphModel/Model/Graph.h>
  15. #include <GraphModel/Model/GraphContext.h>
  16. #include <GraphModel/Model/Node.h>
  17. #include <GraphModel/Model/Slot.h>
  18. #include <GraphModel/Model/Connection.h>
  19. namespace GraphModel
  20. {
  21. void Graph::Reflect(AZ::ReflectContext* context)
  22. {
  23. Node::Reflect(context);
  24. SlotIdData::Reflect(context);
  25. Slot::Reflect(context);
  26. Connection::Reflect(context);
  27. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  28. if (serializeContext)
  29. {
  30. serializeContext->Class<Graph>()
  31. ->Version(2)
  32. ->Field("m_nodes", &Graph::m_nodes)
  33. ->Field("m_connections", &Graph::m_connections)
  34. ->Field("m_uiMetadata", &Graph::m_uiMetadata)
  35. ->Field("m_nodeWrappings", &Graph::m_nodeWrappings)
  36. ;
  37. }
  38. }
  39. Graph::Graph(GraphContextPtr graphContext)
  40. : m_graphContext(graphContext)
  41. {
  42. }
  43. void Graph::PostLoadSetup(GraphContextPtr graphContext)
  44. {
  45. AZ_Assert(m_nextNodeId == 1, "This graph has been set up before");
  46. m_graphContext = graphContext;
  47. for (auto& pair : m_nodes)
  48. {
  49. const NodeId nodeId = pair.first;
  50. pair.second->PostLoadSetup(shared_from_this(), nodeId);
  51. // Find the highest NodeId in the graph so we can figure out
  52. // what the next one should be
  53. m_nextNodeId = AZ::GetMax(m_nextNodeId, nodeId + 1);
  54. }
  55. for (auto it = m_connections.begin(); it != m_connections.end();)
  56. {
  57. ConnectionPtr connection = *it;
  58. connection->PostLoadSetup(shared_from_this());
  59. if (!connection->GetSourceSlot() || !connection->GetTargetSlot())
  60. {
  61. // Discard any cached connections if the source or target slot no longer exists
  62. m_connections.erase(it);
  63. }
  64. else
  65. {
  66. // Valid slots, so update each slot's local cache of its connections
  67. connection->GetSourceSlot()->m_connections.push_back(connection);
  68. connection->GetTargetSlot()->m_connections.push_back(connection);
  69. ++it;
  70. }
  71. }
  72. }
  73. NodeId Graph::PostLoadSetup(NodePtr node)
  74. {
  75. node->m_graph = shared_from_this();
  76. NodeId nodeId = AddNode(node);
  77. node->PostLoadSetup();
  78. return nodeId;
  79. }
  80. GraphContextPtr Graph::GetContext() const
  81. {
  82. AZ_Assert(m_graphContext, "Graph::m_graphContext is not set");
  83. return m_graphContext;
  84. }
  85. const char* Graph::GetSystemName() const
  86. {
  87. return GetContext()->GetSystemName();
  88. }
  89. ConnectionPtr Graph::FindConnection(ConstSlotPtr sourceSlot, ConstSlotPtr targetSlot)
  90. {
  91. if (!sourceSlot || !targetSlot)
  92. {
  93. return nullptr;
  94. }
  95. for (ConnectionPtr searchConnection : m_connections)
  96. {
  97. if (searchConnection->GetSourceSlot() == sourceSlot && searchConnection->GetTargetSlot() == targetSlot)
  98. {
  99. return searchConnection;
  100. }
  101. }
  102. return nullptr;
  103. }
  104. bool Graph::Contains(SlotPtr slot) const
  105. {
  106. if (!slot)
  107. {
  108. return false;
  109. }
  110. for (auto pair : m_nodes)
  111. {
  112. if (pair.second->Contains(slot))
  113. {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. NodePtr Graph::GetNode(NodeId nodeId)
  120. {
  121. auto nodeIter = m_nodes.find(nodeId);
  122. if (nodeIter != m_nodes.end())
  123. {
  124. return nodeIter->second;
  125. }
  126. return nullptr;
  127. }
  128. const Graph::NodeMap& Graph::GetNodes()
  129. {
  130. return m_nodes;
  131. }
  132. Graph::ConstNodeMap Graph::GetNodes() const
  133. {
  134. Graph::ConstNodeMap constNodes;
  135. AZStd::for_each(m_nodes.begin(), m_nodes.end(), [&](auto pair) { constNodes.insert(pair); });
  136. return constNodes;
  137. }
  138. NodeId Graph::AddNode(NodePtr node)
  139. {
  140. AZ_Assert(Node::INVALID_NODE_ID == node->GetId(), "It appears this node already exists in a Graph");
  141. AZ_Assert(this == node->GetGraph().get(), "The Node was not created for this Graph");
  142. node->m_id = m_nextNodeId++;
  143. m_nodes.insert(AZStd::make_pair(node->m_id, node));
  144. return node->m_id;
  145. }
  146. bool Graph::RemoveNode(ConstNodePtr node)
  147. {
  148. // First delete any connections that are attached to the node.
  149. // It looks like this code is never run because the connections are always
  150. // deleted individually first. But still have this hear for completeness.
  151. for (int i = static_cast<int>(m_connections.size()) - 1; i >= 0; --i)
  152. {
  153. ConnectionPtr connection = m_connections[i];
  154. if (connection->GetSourceNode() == node || connection->GetTargetNode() == node)
  155. {
  156. RemoveConnection(&m_connections[i]);
  157. }
  158. }
  159. // Also, remove any node wrapping stored for this node
  160. UnwrapNode(node);
  161. return m_nodes.erase(node->GetId()) != 0;
  162. }
  163. void Graph::WrapNode(NodePtr wrapperNode, NodePtr node, AZ::u32 layoutOrder)
  164. {
  165. AZ_Assert(m_nodes.find(wrapperNode->GetId()) != m_nodes.end(), "The wrapperNode must be in the graph before having a node wrapped on it");
  166. AZ_Assert(m_nodes.find(node->GetId()) != m_nodes.end(), "The node must be in the graph before being wrapped");
  167. AZ_Assert(wrapperNode->GetNodeType() == NodeType::WrapperNode, "The node containing the wrapped node must be of node type WrapperNode");
  168. AZ_Assert(node->GetNodeType() != NodeType::WrapperNode, "Nested WrapperNodes are not allowed");
  169. AZ_Assert(m_nodeWrappings.find(node->GetId()) == m_nodeWrappings.end(), "The specified node is already wrapped on another WrapperNode");
  170. m_nodeWrappings[node->GetId()] = AZStd::make_pair(wrapperNode->GetId(), layoutOrder);
  171. }
  172. void Graph::UnwrapNode(ConstNodePtr node)
  173. {
  174. auto it = m_nodeWrappings.find(node->GetId());
  175. if (it != m_nodeWrappings.end())
  176. {
  177. m_nodeWrappings.erase(it);
  178. }
  179. }
  180. const Graph::NodeWrappingMap& Graph::GetNodeWrappings()
  181. {
  182. return m_nodeWrappings;
  183. }
  184. const Graph::ConnectionList& Graph::GetConnections()
  185. {
  186. return m_connections;
  187. }
  188. ConnectionPtr Graph::AddConnection(SlotPtr sourceSlot, SlotPtr targetSlot)
  189. {
  190. if (ConnectionPtr existingConnection = FindConnection(sourceSlot, targetSlot))
  191. {
  192. return existingConnection;
  193. }
  194. else if (Contains(sourceSlot) && Contains(targetSlot))
  195. {
  196. ConnectionPtr newConnection = AZStd::make_shared<Connection>(shared_from_this(), sourceSlot, targetSlot);
  197. m_connections.push_back(newConnection);
  198. sourceSlot->m_connections.push_back(newConnection);
  199. targetSlot->m_connections.push_back(newConnection);
  200. return newConnection;
  201. }
  202. else
  203. {
  204. AZ_Error(GetSystemName(), false, "Tried to add a connection between slots that don't exist in this Graph.");
  205. return nullptr;
  206. }
  207. }
  208. bool Graph::RemoveConnection(ConnectionList::iterator iter)
  209. {
  210. if (iter != m_connections.end())
  211. {
  212. ConnectionPtr connection = *iter;
  213. // Remove the cached connection pointers from the slots
  214. auto shouldRemove = [&connection](auto entry) {
  215. ConstConnectionPtr entryPtr = entry.lock();
  216. return !entryPtr || entryPtr == connection;
  217. };
  218. (*iter)->GetSourceSlot()->m_connections.remove_if(shouldRemove);
  219. (*iter)->GetTargetSlot()->m_connections.remove_if(shouldRemove);
  220. // Remove the actual connection
  221. m_connections.erase(iter);
  222. #if defined(AZ_ENABLE_TRACING)
  223. auto iterConnection = AZStd::find(m_connections.begin(), m_connections.end(), connection);
  224. AZ_Assert(iterConnection == m_connections.end(), "Graph is broken. The same connection object was found multiple times.");
  225. #endif
  226. return true;
  227. }
  228. else
  229. {
  230. return false;
  231. }
  232. }
  233. bool Graph::RemoveConnection(ConstConnectionPtr connection)
  234. {
  235. auto iter = AZStd::find(m_connections.begin(), m_connections.end(), connection);
  236. return RemoveConnection(iter);
  237. }
  238. AZStd::shared_ptr<Slot> Graph::FindSlot(const Endpoint& endpoint)
  239. {
  240. AZStd::shared_ptr<Slot> slot;
  241. auto nodeIter = m_nodes.find(endpoint.first);
  242. if (nodeIter != m_nodes.end())
  243. {
  244. slot = nodeIter->second->GetSlot(endpoint.second);
  245. }
  246. return slot;
  247. }
  248. void Graph::SetUiMetadata(const GraphModelIntegration::GraphCanvasMetadata& uiMetadata)
  249. {
  250. m_uiMetadata = uiMetadata;
  251. }
  252. const GraphModelIntegration::GraphCanvasMetadata& Graph::GetUiMetadata() const
  253. {
  254. return m_uiMetadata;
  255. }
  256. GraphModelIntegration::GraphCanvasMetadata& Graph::GetUiMetadata()
  257. {
  258. return m_uiMetadata;
  259. }
  260. } // namespace GraphModel