CameraEditorSystemComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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::EditorContextMenuBus::Handler::BusConnect();
  55. AzToolsFramework::EditorEvents::Bus::Handler::BusConnect();
  56. Camera::EditorCameraSystemRequestBus::Handler::BusConnect();
  57. Camera::CameraViewRegistrationRequestsBus::Handler::BusConnect();
  58. AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler::BusConnect();
  59. }
  60. void CameraEditorSystemComponent::Deactivate()
  61. {
  62. AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler::BusDisconnect();
  63. Camera::CameraViewRegistrationRequestsBus::Handler::BusDisconnect();
  64. Camera::EditorCameraSystemRequestBus::Handler::BusDisconnect();
  65. AzToolsFramework::EditorEvents::Bus::Handler::BusDisconnect();
  66. AzToolsFramework::EditorContextMenuBus::Handler::BusDisconnect();
  67. }
  68. void CameraEditorSystemComponent::PopulateEditorGlobalContextMenu(
  69. QMenu* menu, [[maybe_unused]] const AZStd::optional<AzFramework::ScreenPoint>& point, int flags)
  70. {
  71. if (!(flags & AzToolsFramework::EditorEvents::eECMF_HIDE_ENTITY_CREATION))
  72. {
  73. QAction* action = menu->addAction(QObject::tr("Create camera entity from view"));
  74. bool showAction = true;
  75. if (const auto prefabEditorEntityOwnershipInterface = AZ::Interface<AzToolsFramework::PrefabEditorEntityOwnershipInterface>::Get();
  76. prefabEditorEntityOwnershipInterface && !prefabEditorEntityOwnershipInterface->IsRootPrefabAssigned())
  77. {
  78. showAction = false;
  79. }
  80. auto entityContextId = AzFramework::EntityContextId::CreateNull();
  81. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  82. entityContextId, &AzToolsFramework::EditorEntityContextRequests::GetEditorEntityContextId);
  83. if (const auto prefabFocusInterface = AZ::Interface<AzToolsFramework::Prefab::PrefabFocusInterface>::Get();
  84. prefabFocusInterface && prefabFocusInterface->IsFocusedPrefabInstanceReadOnly(entityContextId))
  85. {
  86. showAction = false;
  87. }
  88. if (showAction)
  89. {
  90. QObject::connect(
  91. action, &QAction::triggered,
  92. [this]()
  93. {
  94. CreateCameraEntityFromViewport();
  95. }
  96. );
  97. }
  98. else
  99. {
  100. action->setEnabled(false);
  101. }
  102. }
  103. }
  104. void CameraEditorSystemComponent::CreateCameraEntityFromViewport()
  105. {
  106. auto entityContextId = AzFramework::EntityContextId::CreateNull();
  107. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  108. entityContextId, &AzToolsFramework::EditorEntityContextRequests::GetEditorEntityContextId);
  109. if (const auto prefabFocusInterface = AZ::Interface<AzToolsFramework::Prefab::PrefabFocusInterface>::Get();
  110. prefabFocusInterface && prefabFocusInterface->IsFocusedPrefabInstanceReadOnly(entityContextId))
  111. {
  112. return;
  113. }
  114. AzFramework::CameraState cameraState{};
  115. AZ::EBusReduceResult<bool, AZStd::logical_or<bool>> aggregator;
  116. Camera::EditorCameraRequestBus::BroadcastResult(
  117. aggregator, &Camera::EditorCameraRequestBus::Events::GetActiveCameraState, cameraState);
  118. AZ_Assert(aggregator.value, "Did not find active camera state");
  119. AzToolsFramework::ScopedUndoBatch undoBatch("Create Camera Entity");
  120. // Create new entity
  121. AZ::EntityId newEntityId;
  122. AZ::EBusAggregateResults<AZ::EntityId> cameras;
  123. Camera::CameraBus::BroadcastResult(cameras, &CameraBus::Events::GetCameras);
  124. AZStd::string newCameraName = AZStd::string::format("Camera%zu", cameras.values.size() + 1);
  125. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  126. newEntityId, &AzToolsFramework::EditorEntityContextRequests::CreateNewEditorEntity, newCameraName.c_str());
  127. // Add CameraComponent
  128. AzToolsFramework::AddComponents<EditorCameraComponent>::ToEntities(newEntityId);
  129. // Set transform to that of the viewport, otherwise default to Identity matrix and 60 degree FOV
  130. const auto worldFromView = AzFramework::CameraTransform(cameraState);
  131. const auto cameraTransform = AZ::Transform::CreateFromMatrix3x3AndTranslation(
  132. AZ::Matrix3x3::CreateFromMatrix3x4(worldFromView), worldFromView.GetTranslation());
  133. AZ::TransformBus::Event(newEntityId, &AZ::TransformInterface::SetWorldTM, cameraTransform);
  134. CameraRequestBus::Event(newEntityId, &CameraComponentRequests::SetFov, AZ::RadToDeg(cameraState.m_fovOrZoom));
  135. undoBatch.MarkEntityDirty(newEntityId);
  136. }
  137. void CameraEditorSystemComponent::SetViewForEntity(const AZ::EntityId& id, AZ::RPI::ViewPtr view)
  138. {
  139. m_entityViewMap[id] = view;
  140. }
  141. AZ::RPI::ViewPtr CameraEditorSystemComponent::GetViewForEntity(const AZ::EntityId& id)
  142. {
  143. if (auto viewIndex = m_entityViewMap.find(id); viewIndex != m_entityViewMap.end())
  144. {
  145. return viewIndex->second.lock();
  146. }
  147. return {};
  148. }
  149. void CameraEditorSystemComponent::OnActionRegistrationHook()
  150. {
  151. auto actionManagerInterface = AZ::Interface<AzToolsFramework::ActionManagerInterface>::Get();
  152. if (!actionManagerInterface)
  153. {
  154. return;
  155. }
  156. // Create camera entity from view
  157. {
  158. const AZStd::string_view actionIdentifier = "o3de.action.camera.createFromView";
  159. AzToolsFramework::ActionProperties actionProperties;
  160. actionProperties.m_name = "Create camera entity from view";
  161. actionProperties.m_description = "Create an entity with a camera that shows the current viewport view.";
  162. actionProperties.m_category = "Edit";
  163. actionManagerInterface->RegisterAction(
  164. EditorIdentifiers::MainWindowActionContextIdentifier,
  165. actionIdentifier,
  166. actionProperties,
  167. [this]()
  168. {
  169. CreateCameraEntityFromViewport();
  170. }
  171. );
  172. actionManagerInterface->InstallEnabledStateCallback(
  173. actionIdentifier,
  174. []() -> bool
  175. {
  176. if (const auto prefabEditorEntityOwnershipInterface =
  177. AZ::Interface<AzToolsFramework::PrefabEditorEntityOwnershipInterface>::Get();
  178. prefabEditorEntityOwnershipInterface && !prefabEditorEntityOwnershipInterface->IsRootPrefabAssigned())
  179. {
  180. return false;
  181. }
  182. auto entityContextId = AzFramework::EntityContextId::CreateNull();
  183. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  184. entityContextId, &AzToolsFramework::EditorEntityContextRequests::GetEditorEntityContextId);
  185. if (const auto prefabFocusInterface = AZ::Interface<AzToolsFramework::Prefab::PrefabFocusInterface>::Get();
  186. prefabFocusInterface && prefabFocusInterface->IsFocusedPrefabInstanceReadOnly(entityContextId))
  187. {
  188. return false;
  189. }
  190. return true;
  191. }
  192. );
  193. // Trigger update whenever entity selection changes.
  194. actionManagerInterface->AddActionToUpdater(EditorIdentifiers::LevelLoadedUpdaterIdentifier, actionIdentifier);
  195. actionManagerInterface->AddActionToUpdater(PrefabIdentifiers::PrefabFocusChangedUpdaterIdentifier, actionIdentifier);
  196. // This action is only accessible outside of Component Modes
  197. actionManagerInterface->AssignModeToAction(AzToolsFramework::DefaultActionContextModeIdentifier, actionIdentifier);
  198. }
  199. }
  200. void CameraEditorSystemComponent::OnMenuBindingHook()
  201. {
  202. auto menuManagerInterface = AZ::Interface<AzToolsFramework::MenuManagerInterface>::Get();
  203. if (!menuManagerInterface)
  204. {
  205. return;
  206. }
  207. menuManagerInterface->AddActionToMenu(EditorIdentifiers::ViewportContextMenuIdentifier, "o3de.action.camera.createFromView", 60100);
  208. }
  209. void CameraEditorSystemComponent::NotifyRegisterViews()
  210. {
  211. RegisterViewportCameraSelectorWindow();
  212. }
  213. }