Graph.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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& [nodeId, node] : m_nodes)
  48. {
  49. node->PostLoadSetup(shared_from_this(), nodeId);
  50. // Find the highest NodeId in the graph so we can figure out what the next one should be
  51. m_nextNodeId = AZ::GetMax(m_nextNodeId, nodeId + 1);
  52. }
  53. for (auto& connection : m_connections)
  54. {
  55. connection->PostLoadSetup(shared_from_this());
  56. }
  57. AZStd::erase_if(m_connections, [](const auto& connection){
  58. return !connection || !connection->GetSourceSlot() || !connection->GetTargetSlot();
  59. });
  60. }
  61. NodeId Graph::PostLoadSetup(NodePtr node)
  62. {
  63. node->m_graph = shared_from_this();
  64. NodeId nodeId = AddNode(node);
  65. node->PostLoadSetup();
  66. return nodeId;
  67. }
  68. GraphContextPtr Graph::GetContext() const
  69. {
  70. AZ_Assert(m_graphContext, "Graph::m_graphContext is not set");
  71. return m_graphContext;
  72. }
  73. const char* Graph::GetSystemName() const
  74. {
  75. return GetContext()->GetSystemName();
  76. }
  77. ConnectionPtr Graph::FindConnection(ConstSlotPtr sourceSlot, ConstSlotPtr targetSlot)
  78. {
  79. if (sourceSlot && targetSlot)
  80. {
  81. for (ConnectionPtr connection : m_connections)
  82. {
  83. if (connection->GetSourceSlot() == sourceSlot && connection->GetTargetSlot() == targetSlot)
  84. {
  85. return connection;
  86. }
  87. }
  88. }
  89. return nullptr;
  90. }
  91. bool Graph::Contains(SlotPtr slot) const
  92. {
  93. if (slot)
  94. {
  95. for (const auto& nodePair : m_nodes)
  96. {
  97. if (nodePair.second->Contains(slot))
  98. {
  99. return true;
  100. }
  101. }
  102. }
  103. return false;
  104. }
  105. NodePtr Graph::GetNode(NodeId nodeId)
  106. {
  107. auto nodeIter = m_nodes.find(nodeId);
  108. return nodeIter != m_nodes.end() ? nodeIter->second : nullptr;
  109. }
  110. const Graph::NodeMap& Graph::GetNodes()
  111. {
  112. return m_nodes;
  113. }
  114. Graph::ConstNodeMap Graph::GetNodes() const
  115. {
  116. return Graph::ConstNodeMap(m_nodes.begin(), m_nodes.end());
  117. }
  118. size_t Graph::GetNodeCount() const
  119. {
  120. return m_nodes.size();
  121. }
  122. NodeId Graph::AddNode(NodePtr node)
  123. {
  124. AZ_Assert(Node::INVALID_NODE_ID == node->GetId(), "It appears this node already exists in a Graph");
  125. AZ_Assert(this == node->GetGraph().get(), "The Node was not created for this Graph");
  126. node->m_id = m_nextNodeId++;
  127. m_nodes.insert(AZStd::make_pair(node->m_id, node));
  128. ClearCachedData();
  129. return node->m_id;
  130. }
  131. bool Graph::RemoveNode(ConstNodePtr node)
  132. {
  133. // First delete any connections that are attached to the node.
  134. AZStd::erase_if(m_connections, [&](const auto& connection){
  135. return !connection || !connection->GetSourceSlot() || !connection->GetTargetSlot() || connection->GetSourceNode() == node || connection->GetTargetNode() == node;
  136. });
  137. // Also, remove any node wrapping stored for this node
  138. UnwrapNode(node);
  139. ClearCachedData();
  140. return m_nodes.erase(node->GetId()) > 0;
  141. }
  142. void Graph::WrapNode(NodePtr wrapperNode, NodePtr node, AZ::u32 layoutOrder)
  143. {
  144. AZ_Assert(m_nodes.find(wrapperNode->GetId()) != m_nodes.end(), "The wrapperNode must be in the graph before having a node wrapped on it");
  145. AZ_Assert(m_nodes.find(node->GetId()) != m_nodes.end(), "The node must be in the graph before being wrapped");
  146. AZ_Assert(wrapperNode->GetNodeType() == NodeType::WrapperNode, "The node containing the wrapped node must be of node type WrapperNode");
  147. AZ_Assert(node->GetNodeType() != NodeType::WrapperNode, "Nested WrapperNodes are not allowed");
  148. AZ_Assert(m_nodeWrappings.find(node->GetId()) == m_nodeWrappings.end(), "The specified node is already wrapped on another WrapperNode");
  149. m_nodeWrappings[node->GetId()] = AZStd::make_pair(wrapperNode->GetId(), layoutOrder);
  150. ClearCachedData();
  151. }
  152. void Graph::UnwrapNode(ConstNodePtr node)
  153. {
  154. ClearCachedData();
  155. m_nodeWrappings.erase(node->GetId());
  156. }
  157. bool Graph::IsNodeWrapped(NodePtr node) const
  158. {
  159. return m_nodeWrappings.contains(node->GetId());
  160. }
  161. const Graph::NodeWrappingMap& Graph::GetNodeWrappings()
  162. {
  163. return m_nodeWrappings;
  164. }
  165. const Graph::ConnectionList& Graph::GetConnections()
  166. {
  167. return m_connections;
  168. }
  169. size_t Graph::GetConnectionCount() const
  170. {
  171. return m_connections.size();
  172. }
  173. ConnectionPtr Graph::AddConnection(SlotPtr sourceSlot, SlotPtr targetSlot)
  174. {
  175. if (ConnectionPtr existingConnection = FindConnection(sourceSlot, targetSlot))
  176. {
  177. return existingConnection;
  178. }
  179. if (Contains(sourceSlot) && Contains(targetSlot))
  180. {
  181. m_connections.push_back(AZStd::make_shared<Connection>(shared_from_this(), sourceSlot, targetSlot));
  182. ClearCachedData();
  183. return m_connections.back();
  184. }
  185. AZ_Error(GetSystemName(), false, "Tried to add a connection between slots that don't exist in this Graph.");
  186. return nullptr;
  187. }
  188. bool Graph::RemoveConnection(ConstConnectionPtr connection)
  189. {
  190. if (AZStd::erase_if(m_connections, [&](const auto& existingConnection) {
  191. return existingConnection == connection ||
  192. (existingConnection && connection &&
  193. existingConnection->GetSourceSlot() == connection->GetSourceSlot() &&
  194. existingConnection->GetTargetSlot() == connection->GetTargetSlot());
  195. }) > 0)
  196. {
  197. ClearCachedData();
  198. return true;
  199. }
  200. return false;
  201. }
  202. AZStd::shared_ptr<Slot> Graph::FindSlot(const Endpoint& endpoint)
  203. {
  204. auto nodeIter = m_nodes.find(endpoint.first);
  205. return nodeIter != m_nodes.end() ? nodeIter->second->GetSlot(endpoint.second) : AZStd::shared_ptr<Slot>{};
  206. }
  207. void Graph::ClearCachedData()
  208. {
  209. for (auto& nodePair : m_nodes)
  210. {
  211. nodePair.second->ClearCachedData();
  212. }
  213. }
  214. void Graph::SetUiMetadata(const GraphModelIntegration::GraphCanvasMetadata& uiMetadata)
  215. {
  216. m_uiMetadata = uiMetadata;
  217. }
  218. const GraphModelIntegration::GraphCanvasMetadata& Graph::GetUiMetadata() const
  219. {
  220. return m_uiMetadata;
  221. }
  222. GraphModelIntegration::GraphCanvasMetadata& Graph::GetUiMetadata()
  223. {
  224. return m_uiMetadata;
  225. }
  226. } // namespace GraphModel