BaseNode.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/Component/ComponentApplicationBus.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/std/smart_ptr/make_shared.h>
  12. #include <AzToolsFramework/Entity/EditorEntityInfoBus.h>
  13. // Qt
  14. #include <QString>
  15. // Graph Model
  16. #include <GraphModel/Model/Slot.h>
  17. // Landscape Canvas
  18. #include "BaseNode.h"
  19. #include <Editor/Core/Core.h>
  20. #include <Editor/Core/GraphContext.h>
  21. namespace LandscapeCanvas
  22. {
  23. void BaseNode::Reflect(AZ::ReflectContext* context)
  24. {
  25. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  26. if (serializeContext)
  27. {
  28. serializeContext->Class<BaseNode, GraphModel::Node>()
  29. ->Version(0)
  30. ->Field("m_vegetationEntityId", &BaseNode::m_vegetationEntityId)
  31. ->Field("m_componentId", &BaseNode::m_componentId)
  32. ;
  33. }
  34. }
  35. BaseNode::BaseNode(GraphModel::GraphPtr graph)
  36. : Node(graph)
  37. {
  38. }
  39. void BaseNode::PostLoadSetup(GraphModel::GraphPtr graph, GraphModel::NodeId id)
  40. {
  41. Node::PostLoadSetup(graph, id);
  42. // Make sure to refresh the data in the entity name slot after any load or reload in case the name changed.
  43. RefreshEntityName();
  44. }
  45. void BaseNode::PostLoadSetup()
  46. {
  47. Node::PostLoadSetup();
  48. // Make sure to refresh the data in the entity name slot after any load or reload in case the name changed.
  49. RefreshEntityName();
  50. }
  51. void BaseNode::CreateEntityNameSlot()
  52. {
  53. // Property field to show the name of the corresponding Vegetation Entity
  54. GraphModel::DataTypePtr stringDataType = GetGraphContext()->GetDataType(LandscapeCanvasDataTypeEnum::String);
  55. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  56. GraphModel::SlotDirection::Input,
  57. GraphModel::SlotType::Property,
  58. ENTITY_NAME_SLOT_ID,
  59. ENTITY_NAME_SLOT_LABEL.toUtf8().constData(),
  60. ENTITY_NAME_SLOT_DESCRIPTION.toUtf8().constData(),
  61. GraphModel::DataTypeList{ stringDataType },
  62. stringDataType->GetDefaultValue()));
  63. }
  64. void BaseNode::SetVegetationEntityId(const AZ::EntityId& entityId)
  65. {
  66. m_vegetationEntityId = entityId;
  67. RefreshEntityName();
  68. }
  69. void BaseNode::RefreshEntityName()
  70. {
  71. // Update the Entity Name (if we have the property slot)
  72. GraphModel::SlotPtr slot = GetSlot(ENTITY_NAME_SLOT_ID);
  73. if (slot)
  74. {
  75. AZStd::string name;
  76. AzToolsFramework::EditorEntityInfoRequestBus::EventResult(
  77. name, m_vegetationEntityId, &AzToolsFramework::EditorEntityInfoRequestBus::Events::GetName);
  78. slot->SetValue(AZStd::any(name));
  79. }
  80. }
  81. void BaseNode::SetComponentId(const AZ::ComponentId& componentId)
  82. {
  83. m_componentId = componentId;
  84. }
  85. AZ::ComponentDescriptor::DependencyArrayType BaseNode::GetOptionalRequiredServices() const
  86. {
  87. return {};
  88. }
  89. AZ::Component* BaseNode::GetComponent() const
  90. {
  91. AZ::Entity* gradientEntity = nullptr;
  92. AZ::ComponentApplicationBus::BroadcastResult(gradientEntity, &AZ::ComponentApplicationRequests::FindEntity, GetVegetationEntityId());
  93. if (!gradientEntity)
  94. {
  95. return nullptr;
  96. }
  97. AZ::Component* component = gradientEntity->FindComponent(GetComponentId());
  98. if (component)
  99. {
  100. return component;
  101. }
  102. return nullptr;
  103. }
  104. bool BaseNode::IsAreaExtender() const
  105. {
  106. BaseNodeType baseNodeType = GetBaseNodeType();
  107. return baseNodeType == LandscapeCanvas::BaseNode::VegetationAreaModifier
  108. || baseNodeType == LandscapeCanvas::BaseNode::VegetationAreaFilter
  109. || baseNodeType == LandscapeCanvas::BaseNode::VegetationAreaSelector
  110. || baseNodeType == LandscapeCanvas::BaseNode::TerrainExtender
  111. || baseNodeType == LandscapeCanvas::BaseNode::TerrainSurfaceExtender
  112. ;
  113. }
  114. } // namespace LandscapeCanvas