LandscapeCanvasSystemComponent.h 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/std/containers/unordered_map.h>
  11. #include <AzCore/std/smart_ptr/make_shared.h>
  12. #include <AzToolsFramework/ActionManager/ActionManagerRegistrationNotificationBus.h>
  13. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  14. #include <LandscapeCanvas/LandscapeCanvasBus.h>
  15. namespace LandscapeCanvas
  16. {
  17. class LandscapeCanvasSystemComponent
  18. : public AZ::Component
  19. , private AzToolsFramework::EditorEvents::Bus::Handler
  20. , private LandscapeCanvasNodeFactoryRequestBus::Handler
  21. , private LandscapeCanvasSerializationRequestBus::Handler
  22. , private AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler
  23. {
  24. public:
  25. AZ_COMPONENT(LandscapeCanvasSystemComponent, "{891CA15A-725A-430F-B395-BCA005CFF606}");
  26. LandscapeCanvasSystemComponent();
  27. ~LandscapeCanvasSystemComponent() override;
  28. static void Reflect(AZ::ReflectContext* context);
  29. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  30. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  31. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  32. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  33. ////////////////////////////////////////////////////////////////////////
  34. // AztoolsFramework::EditorEvents::Bus::Handler overrides
  35. void NotifyRegisterViews() override;
  36. ////////////////////////////////////////////////////////////////////////
  37. // ActionManagerRegistrationNotificationBus overrides ...
  38. void OnActionContextRegistrationHook() override;
  39. protected:
  40. ////////////////////////////////////////////////////////////////////////
  41. // AZ::Component interface implementation
  42. void Init() override;
  43. void Activate() override;
  44. void Deactivate() override;
  45. ////////////////////////////////////////////////////////////////////////
  46. ////////////////////////////////////////////////////////////////////////
  47. // LandscapeCanvas::LandscapeCanvasNodeFactoryRequestBus::Handler overrides
  48. BaseNode::BaseNodePtr CreateNodeForType(GraphModel::GraphPtr graph, const AZ::TypeId& typeId) override;
  49. GraphModel::NodePtr CreateNodeForTypeName(GraphModel::GraphPtr graph, AZStd::string_view nodeName) override;
  50. const AZ::TypeId GetComponentTypeId(const AZ::TypeId& nodeTypeId) override;
  51. int GetNodeRegisteredIndex(const AZ::TypeId& nodeTypeId) const override;
  52. ////////////////////////////////////////////////////////////////////////
  53. ////////////////////////////////////////////////////////////////////////
  54. // LandscapeCanvas::LandscapeCanvasSerializationRequestBus::Handler overrides
  55. const LandscapeCanvasSerialization& GetSerializedMappings() override;
  56. void SetDeserializedEntities(const AZStd::unordered_map<AZ::EntityId, AZ::EntityId>& entities) override;
  57. ////////////////////////////////////////////////////////////////////////
  58. using NodeFactoryFunction = AZStd::function<BaseNode::BaseNodePtr(GraphModel::GraphPtr)>;
  59. template<typename NodeType>
  60. void RegisterFactoryMethod(const AZ::TypeId& typeId)
  61. {
  62. NodeFactoryFunction factory = [](GraphModel::GraphPtr graph)
  63. {
  64. return AZStd::make_shared<NodeType>(graph);
  65. };
  66. m_nodeFactory[typeId] = factory;
  67. m_nodeComponentTypeIds[AZ::RttiTypeId<NodeType>()] = AZStd::make_pair(typeId, aznumeric_cast<AZ::u32>(m_nodeComponentTypeIds.size()));
  68. }
  69. AZ::SerializeContext* m_serializeContext = nullptr;
  70. // Map where the key is the Component TypeId and the value is the factory function for creating a new node for that component type
  71. AZStd::unordered_map<AZ::TypeId, NodeFactoryFunction> m_nodeFactory;
  72. // Map where the key is the node RTTI TypeId and the value is a pair with the
  73. // corresponding Component TypeId plus an index so we can keep track of the order they were registered
  74. AZStd::unordered_map<AZ::TypeId, AZStd::pair<AZ::TypeId, AZ::u32>> m_nodeComponentTypeIds;
  75. LandscapeCanvasSerialization m_serialization;
  76. };
  77. }