CameraEditorSystemComponent.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "CameraEditorSystemComponent.h"
  9. #include "EditorCameraComponent.h"
  10. #include <AzCore/Component/ComponentApplicationBus.h>
  11. #include <AzCore/Debug/Trace.h>
  12. #include <AzCore/std/functional.h>
  13. #include <AzCore/Math/Vector2.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzFramework/Viewport/CameraState.h>
  16. #include <AzFramework/Viewport/ViewportScreen.h>
  17. #include <AzToolsFramework/ActionManager/Action/ActionManagerInterface.h>
  18. #include <AzToolsFramework/ActionManager/Menu/MenuManagerInterface.h>
  19. #include <AzToolsFramework/API/EntityCompositionRequestBus.h>
  20. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  21. #include <AzToolsFramework/Editor/ActionManagerIdentifiers/EditorContextIdentifiers.h>
  22. #include <AzToolsFramework/Editor/ActionManagerIdentifiers/EditorMenuIdentifiers.h>
  23. #include <AzToolsFramework/Editor/ActionManagerIdentifiers/EditorActionUpdaterIdentifiers.h>
  24. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  25. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  26. #include <AzToolsFramework/Entity/PrefabEditorEntityOwnershipInterface.h>
  27. #include <AzToolsFramework/Prefab/PrefabFocusInterface.h>
  28. #include <AzToolsFramework/UI/Prefab/ActionManagerIdentifiers/PrefabActionUpdaterIdentifiers.h>
  29. #include <AzToolsFramework/API/EditorCameraBus.h>
  30. #include "ViewportCameraSelectorWindow.h"
  31. #include <QAction>
  32. #include <QMenu>
  33. namespace Camera
  34. {
  35. void CameraEditorSystemComponent::Reflect(AZ::ReflectContext* context)
  36. {
  37. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  38. {
  39. serializeContext->Class<CameraEditorSystemComponent, AZ::Component>()
  40. ->Version(1)
  41. ;
  42. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  43. {
  44. editContext->Class<CameraEditorSystemComponent>(
  45. "Camera Editor Commands", "Performs global camera requests")
  46. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  47. ->Attribute(AZ::Edit::Attributes::Category, "Game")
  48. ;
  49. }
  50. }
  51. }
  52. void CameraEditorSystemComponent::Activate()
  53. {
  54. AzToolsFramework::EditorEvents::Bus::Handler::BusConnect();
  55. Camera::EditorCameraSystemRequestBus::Handler::BusConnect();
  56. Camera::CameraViewRegistrationRequestsBus::Handler::BusConnect();
  57. AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler::BusConnect();
  58. }
  59. void CameraEditorSystemComponent::Deactivate()
  60. {
  61. AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler::BusDisconnect();
  62. Camera::CameraViewRegistrationRequestsBus::Handler::BusDisconnect();
  63. Camera::EditorCameraSystemRequestBus::Handler::BusDisconnect();
  64. AzToolsFramework::EditorEvents::Bus::Handler::BusDisconnect();
  65. }
  66. void CameraEditorSystemComponent::CreateCameraEntityFromViewport()
  67. {
  68. auto entityContextId = AzFramework::EntityContextId::CreateNull();
  69. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  70. entityContextId, &AzToolsFramework::EditorEntityContextRequests::GetEditorEntityContextId);
  71. if (const auto prefabFocusInterface = AZ::Interface<AzToolsFramework::Prefab::PrefabFocusInterface>::Get();
  72. prefabFocusInterface && prefabFocusInterface->IsFocusedPrefabInstanceReadOnly(entityContextId))
  73. {
  74. return;
  75. }
  76. AzFramework::CameraState cameraState{};
  77. AZ::EBusReduceResult<bool, AZStd::logical_or<bool>> aggregator;
  78. Camera::EditorCameraRequestBus::BroadcastResult(
  79. aggregator, &Camera::EditorCameraRequestBus::Events::GetActiveCameraState, cameraState);
  80. AZ_Assert(aggregator.value, "Did not find active camera state");
  81. AzToolsFramework::ScopedUndoBatch undoBatch("Create Camera Entity");
  82. // Create new entity
  83. AZ::EntityId newEntityId;
  84. AZ::EBusAggregateResults<AZ::EntityId> cameras;
  85. Camera::CameraBus::BroadcastResult(cameras, &CameraBus::Events::GetCameras);
  86. AZStd::string newCameraName = AZStd::string::format("Camera%zu", cameras.values.size() + 1);
  87. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  88. newEntityId, &AzToolsFramework::EditorEntityContextRequests::CreateNewEditorEntity, newCameraName.c_str());
  89. // Add CameraComponent
  90. AzToolsFramework::AddComponents<EditorCameraComponent>::ToEntities(newEntityId);
  91. // Set transform to that of the viewport, otherwise default to Identity matrix and 60 degree FOV
  92. const auto worldFromView = AzFramework::CameraTransform(cameraState);
  93. const auto cameraTransform = AZ::Transform::CreateFromMatrix3x3AndTranslation(
  94. AZ::Matrix3x3::CreateFromMatrix3x4(worldFromView), worldFromView.GetTranslation());
  95. AZ::TransformBus::Event(newEntityId, &AZ::TransformInterface::SetWorldTM, cameraTransform);
  96. CameraRequestBus::Event(newEntityId, &CameraComponentRequests::SetFov, AZ::RadToDeg(cameraState.m_fovOrZoom));
  97. undoBatch.MarkEntityDirty(newEntityId);
  98. }
  99. void CameraEditorSystemComponent::SetViewForEntity(const AZ::EntityId& id, AZ::RPI::ViewPtr view)
  100. {
  101. m_entityViewMap[id] = view;
  102. }
  103. AZ::RPI::ViewPtr CameraEditorSystemComponent::GetViewForEntity(const AZ::EntityId& id)
  104. {
  105. if (auto viewIndex = m_entityViewMap.find(id); viewIndex != m_entityViewMap.end())
  106. {
  107. return viewIndex->second.lock();
  108. }
  109. return {};
  110. }
  111. void CameraEditorSystemComponent::OnActionRegistrationHook()
  112. {
  113. auto actionManagerInterface = AZ::Interface<AzToolsFramework::ActionManagerInterface>::Get();
  114. if (!actionManagerInterface)
  115. {
  116. return;
  117. }
  118. // Create camera entity from view
  119. {
  120. const AZStd::string_view actionIdentifier = "o3de.action.camera.createFromView";
  121. AzToolsFramework::ActionProperties actionProperties;
  122. actionProperties.m_name = "Create camera entity from view";
  123. actionProperties.m_description = "Create an entity with a camera that shows the current viewport view.";
  124. actionProperties.m_category = "Edit";
  125. actionManagerInterface->RegisterAction(
  126. EditorIdentifiers::MainWindowActionContextIdentifier,
  127. actionIdentifier,
  128. actionProperties,
  129. [this]()
  130. {
  131. CreateCameraEntityFromViewport();
  132. }
  133. );
  134. actionManagerInterface->InstallEnabledStateCallback(
  135. actionIdentifier,
  136. []() -> bool
  137. {
  138. if (const auto prefabEditorEntityOwnershipInterface =
  139. AZ::Interface<AzToolsFramework::PrefabEditorEntityOwnershipInterface>::Get();
  140. prefabEditorEntityOwnershipInterface && !prefabEditorEntityOwnershipInterface->IsRootPrefabAssigned())
  141. {
  142. return false;
  143. }
  144. auto entityContextId = AzFramework::EntityContextId::CreateNull();
  145. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  146. entityContextId, &AzToolsFramework::EditorEntityContextRequests::GetEditorEntityContextId);
  147. if (const auto prefabFocusInterface = AZ::Interface<AzToolsFramework::Prefab::PrefabFocusInterface>::Get();
  148. prefabFocusInterface && prefabFocusInterface->IsFocusedPrefabInstanceReadOnly(entityContextId))
  149. {
  150. return false;
  151. }
  152. return true;
  153. }
  154. );
  155. // Trigger update whenever entity selection changes.
  156. actionManagerInterface->AddActionToUpdater(EditorIdentifiers::LevelLoadedUpdaterIdentifier, actionIdentifier);
  157. actionManagerInterface->AddActionToUpdater(PrefabIdentifiers::PrefabFocusChangedUpdaterIdentifier, actionIdentifier);
  158. // This action is only accessible outside of Component Modes
  159. actionManagerInterface->AssignModeToAction(AzToolsFramework::DefaultActionContextModeIdentifier, actionIdentifier);
  160. }
  161. }
  162. void CameraEditorSystemComponent::OnMenuBindingHook()
  163. {
  164. auto menuManagerInterface = AZ::Interface<AzToolsFramework::MenuManagerInterface>::Get();
  165. if (!menuManagerInterface)
  166. {
  167. return;
  168. }
  169. menuManagerInterface->AddActionToMenu(EditorIdentifiers::ViewportContextMenuIdentifier, "o3de.action.camera.createFromView", 60100);
  170. }
  171. void CameraEditorSystemComponent::NotifyRegisterViews()
  172. {
  173. RegisterViewportCameraSelectorWindow();
  174. }
  175. }