EditorComponentBase.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "EditorComponentBase.h"
  9. #include "TransformComponent.h"
  10. #include <AzCore/Math/Vector2.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  13. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  14. DECLARE_EBUS_INSTANTIATION_WITH_TRAITS(AzToolsFramework::Components::EditorComponentDescriptor, AZ::ComponentDescriptorBusTraits);
  15. namespace AzToolsFramework
  16. {
  17. namespace Components
  18. {
  19. void EditorComponentBase::Reflect(AZ::ReflectContext* context)
  20. {
  21. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  22. if (serializeContext)
  23. {
  24. serializeContext->Class<EditorComponentBase, AZ::Component>()
  25. ->Version(1)
  26. ;
  27. }
  28. }
  29. EditorComponentBase::EditorComponentBase()
  30. {
  31. m_transform = nullptr;
  32. }
  33. void EditorComponentBase::Init()
  34. {
  35. }
  36. void EditorComponentBase::Activate()
  37. {
  38. m_transform = GetEntity()->FindComponent<TransformComponent>();
  39. }
  40. void EditorComponentBase::Deactivate()
  41. {
  42. m_transform = nullptr;
  43. }
  44. void EditorComponentBase::SetDirty()
  45. {
  46. if (GetEntity())
  47. {
  48. AzToolsFramework::ToolsApplicationRequests::Bus::Broadcast(
  49. &AzToolsFramework::ToolsApplicationRequests::Bus::Events::AddDirtyEntity, GetEntity()->GetId());
  50. }
  51. else
  52. {
  53. AZ_Warning("Editor", false, "EditorComponentBase::SetDirty() failed. Couldn't add dirty entity because the pointer to the entity is NULL. Make sure the entity is Init()'d properly.");
  54. }
  55. }
  56. AZ::TransformInterface* EditorComponentBase::GetTransform() const
  57. {
  58. AZ_Assert(m_transform, "Attempt to GetTransformComponent when the entity is inactive or does not have one.");
  59. return m_transform;
  60. }
  61. AZ::Transform EditorComponentBase::GetWorldTM() const
  62. {
  63. if (m_transform)
  64. {
  65. return m_transform->GetWorldTM();
  66. }
  67. else
  68. {
  69. return AZ::Transform::Identity();
  70. }
  71. }
  72. AZ::Transform EditorComponentBase::GetLocalTM() const
  73. {
  74. if (m_transform)
  75. {
  76. return m_transform->GetLocalTM();
  77. }
  78. else
  79. {
  80. return AZ::Transform::Identity();
  81. }
  82. }
  83. bool EditorComponentBase::IsSelected() const
  84. {
  85. return AzToolsFramework::IsSelected(GetEntityId());
  86. }
  87. void EditorComponentBase::SetSerializedIdentifier(AZStd::string alias)
  88. {
  89. m_alias = alias;
  90. }
  91. AZStd::string EditorComponentBase::GetSerializedIdentifier() const
  92. {
  93. return m_alias;
  94. }
  95. }
  96. } // namespace AzToolsFramework