ModuleNode.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <AzFramework/StringFunc/StringFunc.h>
  10. // Graph Model
  11. #include <GraphModel/Model/Graph.h>
  12. #include <GraphModel/Model/Slot.h>
  13. #include <GraphModel/Model/Module/ModuleNode.h>
  14. #include <GraphModel/Model/Module/ModuleGraphManager.h>
  15. #include <GraphModel/Model/Module/InputOutputNodes.h>
  16. #include <GraphModel/Model/GraphContext.h>
  17. namespace GraphModel
  18. {
  19. void ModuleNode::Reflect(AZ::ReflectContext* context)
  20. {
  21. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  22. if (serializeContext)
  23. {
  24. serializeContext->Class<ModuleNode, Node>()
  25. ->Version(0)
  26. ->Field("m_moduleGraphFileId", &ModuleNode::m_moduleGraphFileId)
  27. ->Field("m_nodeTitle", &ModuleNode::m_nodeTitle)
  28. ;
  29. }
  30. }
  31. ModuleNode::ModuleNode(GraphPtr ownerGraph, AZ::Uuid moduleGraphFileId, AZStd::string_view moduleGraphFileName)
  32. : Node(ownerGraph)
  33. , m_moduleGraphFileId(moduleGraphFileId)
  34. {
  35. // The module file name (without extension) is the node title
  36. if (!AzFramework::StringFunc::Path::GetFileName(moduleGraphFileName.data(), m_nodeTitle))
  37. {
  38. AZ_Error(ownerGraph->GetSystemName(), false, "Could not get node name from file string [%s]", moduleGraphFileName.data());
  39. }
  40. LoadModuleGraph(ownerGraph->GetContext()->GetModuleGraphManager());
  41. RegisterSlots();
  42. CreateSlotData();
  43. }
  44. void ModuleNode::PostLoadSetup(GraphPtr ownerGraph, NodeId id)
  45. {
  46. LoadModuleGraph(ownerGraph->GetContext()->GetModuleGraphManager());
  47. Node::PostLoadSetup(ownerGraph, id);
  48. }
  49. const char* ModuleNode::GetTitle() const
  50. {
  51. return m_nodeTitle.c_str();
  52. }
  53. void ModuleNode::LoadModuleGraph(ModuleGraphManagerPtr moduleGraphManager)
  54. {
  55. auto result = moduleGraphManager->GetModuleGraph(m_moduleGraphFileId);
  56. if (result.IsSuccess())
  57. {
  58. m_moduleGraph = result.GetValue();
  59. }
  60. else
  61. {
  62. AZ_Warning(GetGraph()->GetSystemName(), false, "%s (Module Node [%s])", result.GetError().data(), m_nodeTitle.data());
  63. }
  64. }
  65. void ModuleNode::RegisterSlots()
  66. {
  67. if (m_moduleGraph)
  68. {
  69. for (auto iter : m_moduleGraph->GetNodes())
  70. {
  71. ConstNodePtr node = iter.second;
  72. if (AZStd::shared_ptr<const GraphInputNode> inputNode = azrtti_cast<const GraphInputNode*>(node))
  73. {
  74. RegisterSlot(GraphModel::SlotDefinition::CreateInputData(
  75. inputNode->GetName(),
  76. inputNode->GetDisplayName(),
  77. inputNode->GetNodeDataType(),
  78. inputNode->GetDefaultValue(),
  79. inputNode->GetDescription()));
  80. }
  81. else if (AZStd::shared_ptr<const GraphOutputNode> outputNode = azrtti_cast<const GraphOutputNode*>(node))
  82. {
  83. RegisterSlot(GraphModel::SlotDefinition::CreateOutputData(
  84. outputNode->GetName(),
  85. outputNode->GetDisplayName(),
  86. outputNode->GetNodeDataType(),
  87. outputNode->GetDescription()));
  88. }
  89. }
  90. }
  91. }
  92. }