NamedPoseEditorComponent.cpp 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include "NamedPoseEditorComponent.h"
  9. #include "NamedPoseComponent.h"
  10. #include <AzCore/Component/ComponentApplicationBus.h>
  11. #include <LmbrCentral/Scripting/EditorTagComponentBus.h>
  12. #include <SimulationInterfaces/NamedPose.h>
  13. namespace SimulationInterfaces
  14. {
  15. void NamedPoseEditorComponent::Activate()
  16. {
  17. EditorComponentBase::Activate();
  18. UpdateConfiguration();
  19. LmbrCentral::TagComponentNotificationsBus::Handler::BusConnect(GetEntityId());
  20. AZ::TransformNotificationBus::Handler::BusConnect(GetEntityId());
  21. AZ::EntityBus::Handler::BusConnect(GetEntityId());
  22. }
  23. void NamedPoseEditorComponent::Deactivate()
  24. {
  25. AZ::EntityBus::Handler::BusDisconnect();
  26. AZ::TransformNotificationBus::Handler::BusDisconnect();
  27. LmbrCentral::TagComponentNotificationsBus::Handler::BusDisconnect();
  28. EditorComponentBase::Deactivate();
  29. }
  30. void NamedPoseEditorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  31. {
  32. required.push_back(AZ_CRC_CE("TagService")); // tag component
  33. }
  34. void NamedPoseEditorComponent::BuildGameEntity(AZ::Entity* gameEntity)
  35. {
  36. // Create Game component
  37. [[maybe_unused]] auto component = gameEntity->CreateComponent<NamedPoseComponent>(m_config);
  38. AZ_Assert(component, "NamedPoseEditorComponent:: failed to create runtime component");
  39. }
  40. void NamedPoseEditorComponent::Reflect(AZ::ReflectContext* context)
  41. {
  42. if (auto* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  43. {
  44. serialize->Class<NamedPoseEditorComponent, AzToolsFramework::Components::EditorComponentBase>()->Version(0)->Field(
  45. "NamedPoseConfig", &NamedPoseEditorComponent::m_config);
  46. if (AZ::EditContext* ec = serialize->GetEditContext())
  47. {
  48. ec->Class<NamedPoseEditorComponent>("NamedPoseEditorComponent", "Component used to define names pose in simulation")
  49. ->ClassElement(AZ::Edit::ClassElements::EditorData, "NamedPoseEditorComponent")
  50. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  51. ->Attribute(AZ::Edit::Attributes::Category, "Simulation Interfaces")
  52. ->DataElement(AZ::Edit::UIHandlers::Default, &NamedPoseEditorComponent::m_config, "NamedPoseConfig", "");
  53. }
  54. }
  55. }
  56. void NamedPoseEditorComponent::OnTagAdded(const LmbrCentral::Tag&)
  57. {
  58. LmbrCentral::EditorTagComponentRequestBus::EventResult(
  59. m_config.m_tags, GetEntityId(), &LmbrCentral::EditorTagComponentRequests::GetTags);
  60. }
  61. void NamedPoseEditorComponent::OnTagRemoved(const LmbrCentral::Tag&)
  62. {
  63. LmbrCentral::EditorTagComponentRequestBus::EventResult(
  64. m_config.m_tags, GetEntityId(), &LmbrCentral::EditorTagComponentRequests::GetTags);
  65. }
  66. void NamedPoseEditorComponent::OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world)
  67. {
  68. m_config.m_pose = world;
  69. }
  70. void NamedPoseEditorComponent::OnEntityNameChanged(const AZStd::string& name)
  71. {
  72. m_config.m_name = name;
  73. }
  74. void NamedPoseEditorComponent::UpdateConfiguration()
  75. {
  76. // gather all informations to create entity
  77. // name of named pose is derived from the entity name
  78. AZ::ComponentApplicationBus::BroadcastResult(m_config.m_name, &AZ::ComponentApplicationRequests::GetEntityName, GetEntityId());
  79. // pose is derived from transform component
  80. AZ::TransformBus::EventResult(m_config.m_pose, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  81. // tags are taken from editor tag component
  82. LmbrCentral::EditorTagComponentRequestBus::EventResult(
  83. m_config.m_tags, GetEntityId(), &LmbrCentral::EditorTagComponentRequests::GetTags);
  84. }
  85. } // namespace SimulationInterfaces