3
0

ModuleNode.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/IO/Path/Path.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/std/smart_ptr/make_shared.h>
  12. // Graph Model
  13. #include <GraphModel/Model/Graph.h>
  14. #include <GraphModel/Model/GraphContext.h>
  15. #include <GraphModel/Model/Module/InputOutputNodes.h>
  16. #include <GraphModel/Model/Module/ModuleGraphManager.h>
  17. #include <GraphModel/Model/Module/ModuleNode.h>
  18. #include <GraphModel/Model/Slot.h>
  19. namespace GraphModel
  20. {
  21. void ModuleNode::Reflect(AZ::ReflectContext* context)
  22. {
  23. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  24. if (serializeContext)
  25. {
  26. serializeContext->Class<ModuleNode, Node>()
  27. ->Version(0)
  28. ->Field("m_moduleGraphFileId", &ModuleNode::m_moduleGraphFileId)
  29. ->Field("m_nodeTitle", &ModuleNode::m_nodeTitle)
  30. ;
  31. }
  32. }
  33. ModuleNode::ModuleNode(GraphPtr ownerGraph, AZ::Uuid moduleGraphFileId, AZStd::string_view moduleGraphFileName)
  34. : Node(ownerGraph)
  35. , m_moduleGraphFileId(moduleGraphFileId)
  36. {
  37. // The module file name (without extension) is the node title
  38. m_nodeTitle = AZ::IO::PathView(moduleGraphFileName).Filename().String();
  39. AZ_Error(ownerGraph->GetSystemName(), !m_nodeTitle.empty(), "Could not get node name from file string [%.*s]", AZ_STRING_ARG(moduleGraphFileName));
  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(AZStd::make_shared<GraphModel::SlotDefinition>(
  75. GraphModel::SlotDirection::Input,
  76. GraphModel::SlotType::Data,
  77. inputNode->GetName(),
  78. inputNode->GetDisplayName(),
  79. inputNode->GetDescription(),
  80. GraphModel::DataTypeList{ inputNode->GetNodeDataType() },
  81. inputNode->GetDefaultValue()));
  82. }
  83. else if (AZStd::shared_ptr<const GraphOutputNode> outputNode = azrtti_cast<const GraphOutputNode*>(node))
  84. {
  85. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  86. GraphModel::SlotDirection::Output,
  87. GraphModel::SlotType::Data,
  88. outputNode->GetName(),
  89. outputNode->GetDisplayName(),
  90. outputNode->GetDescription(),
  91. GraphModel::DataTypeList{ outputNode->GetNodeDataType() }));
  92. }
  93. }
  94. }
  95. }
  96. }