EditorViewportWidget.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #if !defined(Q_MOC_RUN)
  10. #include <QSet>
  11. #include "EditorModularViewportCameraComposer.h"
  12. #include "EditorViewportSettings.h"
  13. #include "Undo/Undo.h"
  14. #include "Util/PredefinedAspectRatios.h"
  15. #include "Viewport.h"
  16. #include <Atom/RPI.Public/SceneBus.h>
  17. #include <Atom/RPI.Public/ViewportContext.h>
  18. #include <AzCore/Component/EntityId.h>
  19. #include <AzCore/std/optional.h>
  20. #include <AzFramework/Asset/AssetCatalogBus.h>
  21. #include <AzFramework/Components/CameraBus.h>
  22. #include <AzFramework/Input/Buses/Requests/InputSystemCursorRequestBus.h>
  23. #include <AzFramework/Scene/SceneSystemInterface.h>
  24. #include <AzFramework/Viewport/ViewportBus.h>
  25. #include <AzFramework/Visibility/EntityVisibilityQuery.h>
  26. #include <AzFramework/Windowing/WindowBus.h>
  27. #include <AzToolsFramework/API/EditorCameraBus.h>
  28. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  29. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  30. #include <AzToolsFramework/Prefab/PrefabPublicNotificationBus.h>
  31. #include <AzToolsFramework/Viewport/ViewportMessages.h>
  32. #include <MathConversion.h>
  33. #endif
  34. // forward declarations.
  35. class QMenu;
  36. class QKeyEvent;
  37. struct ray_hit;
  38. struct IRenderMesh;
  39. struct IVariable;
  40. namespace AZ::ViewportHelpers
  41. {
  42. class EditorEntityNotifications;
  43. } // namespace AZ::ViewportHelpers
  44. namespace AtomToolsFramework
  45. {
  46. class RenderViewportWidget;
  47. class ModularViewportCameraController;
  48. } // namespace AtomToolsFramework
  49. namespace AzToolsFramework
  50. {
  51. class ManipulatorManager;
  52. }
  53. //! Viewport settings for the EditorViewportWidget
  54. struct EditorViewportSettings : public AzToolsFramework::ViewportInteraction::ViewportSettingsRequestBus::Handler
  55. {
  56. void Connect(AzFramework::ViewportId viewportId);
  57. void Disconnect();
  58. // ViewportSettingsRequestBus overrides ...
  59. bool GridSnappingEnabled() const override;
  60. float GridSize() const override;
  61. bool ShowGrid() const override;
  62. bool AngleSnappingEnabled() const override;
  63. float AngleStep() const override;
  64. float ManipulatorLineBoundWidth() const override;
  65. float ManipulatorCircleBoundWidth() const override;
  66. bool StickySelectEnabled() const override;
  67. AZ::Vector3 DefaultEditorCameraPosition() const override;
  68. AZ::Vector2 DefaultEditorCameraOrientation() const override;
  69. bool IconsVisible() const override;
  70. bool HelpersVisible() const override;
  71. bool OnlyShowHelpersForSelectedEntities() const override;
  72. };
  73. //! EditorViewportWidget window
  74. AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  75. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  76. class SANDBOX_API EditorViewportWidget final
  77. : public QtViewport
  78. , public AzFramework::ViewportBorderRequestBus::Handler
  79. , private IEditorNotifyListener
  80. , private IUndoManagerListener
  81. , private Camera::EditorCameraRequestBus::Handler
  82. , private Camera::CameraNotificationBus::Handler
  83. , private AzFramework::InputSystemCursorConstraintRequestBus::Handler
  84. , private AzToolsFramework::ViewportInteraction::MainEditorViewportInteractionRequestBus::Handler
  85. , private AzToolsFramework::ViewportInteraction::EditorEntityViewportInteractionRequestBus::Handler
  86. , private AzFramework::AssetCatalogEventBus::Handler
  87. , private AZ::RPI::SceneNotificationBus::Handler
  88. , private AzToolsFramework::Prefab::PrefabPublicNotificationBus::Handler
  89. {
  90. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  91. AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  92. Q_OBJECT
  93. public:
  94. EditorViewportWidget(const QString& name, QWidget* parent = nullptr);
  95. ~EditorViewportWidget() override;
  96. static const GUID& GetClassID()
  97. {
  98. return QtViewport::GetClassID<EditorViewportWidget>();
  99. }
  100. static EditorViewportWidget* GetPrimaryViewport();
  101. // Used by ViewPan in some circumstances
  102. void ConnectViewportInteractionRequestBus();
  103. void DisconnectViewportInteractionRequestBus();
  104. // QtViewport/IDisplayViewport/CViewport
  105. // These methods are made public in the derived class because they are called with an object whose static type is known to be this class
  106. // type.
  107. void SetFOV(float fov) override;
  108. float GetFOV() const override;
  109. // AzFramework::ViewportBorderRequestBus overrides ...
  110. AZStd::optional<AzFramework::ViewportBorderPadding> GetViewportBorderPadding() const override;
  111. private:
  112. ////////////////////////////////////////////////////////////////////////
  113. // Private types ...
  114. enum class ViewSourceType
  115. {
  116. None,
  117. CameraComponent,
  118. ViewSourceTypesCount,
  119. };
  120. enum class PlayInEditorState
  121. {
  122. Editor,
  123. Starting,
  124. Started,
  125. Stopping
  126. };
  127. enum class KeyPressedState
  128. {
  129. AllUp,
  130. PressedThisFrame,
  131. PressedInPreviousFrame,
  132. };
  133. ////////////////////////////////////////////////////////////////////////
  134. // Method overrides ...
  135. // QWidget overrides ...
  136. void focusOutEvent(QFocusEvent* event) override;
  137. void keyPressEvent(QKeyEvent* event) override;
  138. bool event(QEvent* event) override;
  139. void resizeEvent(QResizeEvent* event) override;
  140. void paintEvent(QPaintEvent* event) override;
  141. void mousePressEvent(QMouseEvent* event) override;
  142. // QtViewport/IDisplayViewport/CViewport overrides ...
  143. EViewportType GetType() const override
  144. {
  145. return ET_ViewportCamera;
  146. }
  147. void SetType([[maybe_unused]] EViewportType type) override
  148. {
  149. assert(type == ET_ViewportCamera);
  150. };
  151. AzToolsFramework::ViewportInteraction::MouseInteraction BuildMouseInteraction(
  152. Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, const QPoint& point) override;
  153. void SetViewportId(int id) override;
  154. QPoint WorldToView(const Vec3& wp) const override;
  155. Vec3 WorldToView3D(const Vec3& wp, int nFlags = 0) const override;
  156. Vec3 ViewToWorld(
  157. const QPoint& vp,
  158. bool* collideWithTerrain = nullptr,
  159. bool onlyTerrain = false,
  160. bool bSkipVegetation = false,
  161. bool bTestRenderMesh = false,
  162. bool* collideWithObject = nullptr) const override;
  163. void ViewToWorldRay(const QPoint& vp, Vec3& raySrc, Vec3& rayDir) const override;
  164. Vec3 ViewToWorldNormal(const QPoint& vp, bool onlyTerrain, bool bTestRenderMesh = false) override;
  165. float GetScreenScaleFactor(const Vec3& worldPoint) const override;
  166. float GetAspectRatio() const override;
  167. bool HitTest(const QPoint& point, HitContext& hitInfo) override;
  168. bool IsBoundsVisible(const AABB& box) const override;
  169. void CenterOnAABB(const AABB& aabb) override;
  170. void OnTitleMenu(QMenu* menu) override;
  171. void SetViewTM(const Matrix34& tm) override;
  172. const Matrix34& GetViewTM() const override;
  173. void Update() override;
  174. void UpdateContent(int flags) override;
  175. // SceneNotificationBus overrides ...
  176. void OnBeginPrepareRender() override;
  177. // Camera::CameraNotificationBus overrides ...
  178. void OnActiveViewChanged(const AZ::EntityId&) override;
  179. // IEditorEventListener overrides ...
  180. void OnEditorNotifyEvent(EEditorNotifyEvent event) override;
  181. // Callback for setting modification
  182. void OnDefaultCameraNearFarChanged();
  183. // AzToolsFramework::EditorEntityContextNotificationBus overrides ...
  184. // note: handler moved to cpp to resolve link issues in unity builds
  185. void OnStartPlayInEditor();
  186. void OnStopPlayInEditor();
  187. void OnStartPlayInEditorBegin();
  188. // IUndoManagerListener
  189. void BeginUndoTransaction() override;
  190. void EndUndoTransaction() override;
  191. // AzFramework::InputSystemCursorConstraintRequestBus overrides ...
  192. void* GetSystemCursorConstraintWindow() const override;
  193. // AzToolsFramework::MainEditorViewportInteractionRequestBus overrides ...
  194. bool ShowingWorldSpace() override;
  195. QWidget* GetWidgetForViewportContextMenu() override;
  196. // EditorEntityViewportInteractionRequestBus overrides ...
  197. void FindVisibleEntities(AZStd::vector<AZ::EntityId>& visibleEntities) override;
  198. // Camera::EditorCameraRequestBus overrides ...
  199. void SetViewFromEntityPerspective(const AZ::EntityId& entityId) override;
  200. AZ::EntityId GetCurrentViewEntityId() override;
  201. bool GetActiveCameraPosition(AZ::Vector3& cameraPos) override;
  202. AZStd::optional<AZ::Transform> GetActiveCameraTransform() override;
  203. AZStd::optional<float> GetCameraFoV() override;
  204. bool GetActiveCameraState(AzFramework::CameraState& cameraState) override;
  205. // AzToolsFramework::Prefab::PrefabPublicNotificationBus overrides ...
  206. void OnRootPrefabInstanceLoaded() override;
  207. ////////////////////////////////////////////////////////////////////////
  208. // Private helpers...
  209. void SetDefaultCameraNearFar();
  210. void RenderAll();
  211. bool RayRenderMeshIntersection(IRenderMesh* pRenderMesh, const Vec3& vInPos, const Vec3& vInDir, Vec3& vOutPos, Vec3& vOutNormal) const;
  212. bool AddCameraMenuItems(QMenu* menu);
  213. void ResizeView(int width, int height);
  214. void HideCursor();
  215. void ShowCursor();
  216. double WidgetToViewportFactor() const;
  217. bool ShouldPreviewFullscreen() const;
  218. void StartFullscreenPreview();
  219. void StopFullscreenPreview();
  220. void OnMenuCreateCameraEntityFromCurrentView();
  221. // From a series of input primitives, compose a complete mouse interaction.
  222. AzToolsFramework::ViewportInteraction::MouseInteraction BuildMouseInteractionInternal(
  223. AzToolsFramework::ViewportInteraction::MouseButtons buttons,
  224. AzToolsFramework::ViewportInteraction::KeyboardModifiers modifiers,
  225. const AzToolsFramework::ViewportInteraction::MousePick& mousePick) const;
  226. // Given a point in the viewport, return the pick ray into the scene.
  227. // note: The argument passed to parameter **point**, originating
  228. // from a Qt event, must first be passed to WidgetToViewport before being
  229. // passed to BuildMousePick.
  230. AzToolsFramework::ViewportInteraction::MousePick BuildMousePick(const QPoint& point) const;
  231. bool CheckRespondToInput() const;
  232. void BuildDragDropContext(
  233. AzQtComponents::ViewportDragContext& context, AzFramework::ViewportId viewportId, const QPoint& point) override;
  234. void SetAsActiveViewport();
  235. void PushDisableRendering();
  236. void PopDisableRendering();
  237. bool IsRenderingDisabled() const;
  238. void RestoreViewportAfterGameMode();
  239. void UpdateScene();
  240. void SetDefaultCamera();
  241. void SetSelectedCamera();
  242. bool IsSelectedCamera() const;
  243. void SetEntityAsCamera(const AZ::EntityId& entityId);
  244. void SetFirstComponentCamera();
  245. void PostCameraSet();
  246. // This switches the active camera to the next one in the list of (default, all custom cams).
  247. void CycleCamera();
  248. QPoint WidgetToViewport(const QPoint& point) const;
  249. QPoint ViewportToWidget(const QPoint& point) const;
  250. QSize WidgetToViewport(const QSize& size) const;
  251. void UnProjectFromScreen(float sx, float sy, float* px, float* py, float* pz) const;
  252. void ProjectToScreen(float ptx, float pty, float ptz, float* sx, float* sy) const;
  253. AZ::RPI::ViewPtr GetCurrentAtomView() const;
  254. ////////////////////////////////////////////////////////////////////////
  255. // Members ...
  256. friend class AZ::ViewportHelpers::EditorEntityNotifications;
  257. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  258. // Singleton for the primary viewport
  259. static EditorViewportWidget* m_pPrimaryViewport;
  260. // The simulation (play-game in editor) state
  261. PlayInEditorState m_playInEditorState = PlayInEditorState::Editor;
  262. // Whether we are doing a full screen game preview (play-game in editor) or a regular one
  263. bool m_inFullscreenPreview = false;
  264. // The entity ID of the current camera for this viewport, or invalid if the default editor camera
  265. AZ::EntityId m_viewEntityId;
  266. // Determines also if the current camera for this viewport is default editor camera
  267. ViewSourceType m_viewSourceType = ViewSourceType::None;
  268. // During play game in editor, holds the editor entity ID of the last
  269. AZ::EntityId m_viewEntityIdCachedForEditMode;
  270. // The editor camera TM before switching to game mode
  271. Matrix34 m_preGameModeViewTM;
  272. // Disables rendering during some periods of time, e.g. undo/redo, resize events
  273. uint m_disableRenderingCount = 0;
  274. // Determines if the viewport needs updating (false when out of focus for example)
  275. bool m_bUpdateViewport = false;
  276. // Avoid re-entering PostCameraSet->OnActiveViewChanged->PostCameraSet
  277. bool m_sendingOnActiveChanged = false;
  278. // Legacy...
  279. KeyPressedState m_pressedKeyState = KeyPressedState::AllUp;
  280. // The name to use for the default editor camera
  281. const QString m_defaultViewName;
  282. // Reentrancy guard for on paint events
  283. bool m_isOnPaint = false;
  284. // Guard against calling UpdateVisibility multiple times a frame
  285. bool m_hasUpdatedVisibility = false;
  286. // Aspect ratios available in the title bar
  287. CPredefinedAspectRatios m_predefinedAspectRatios;
  288. // Is the cursor hidden or displayed?
  289. bool m_bCursorHidden = false;
  290. // Shim for QtViewport, which used to be responsible for visibility queries in the editor,
  291. // these are now forwarded to EntityVisibilityQuery
  292. AzFramework::EntityVisibilityQuery m_entityVisibilityQuery;
  293. // Handlers for snapping/editor event callbacks
  294. SandboxEditor::AngleSnappingChangedEvent::Handler m_angleSnappingHandler;
  295. SandboxEditor::CameraSpeedScaleChangedEvent::Handler m_cameraSpeedScaleHandler;
  296. SandboxEditor::GridShowingChangedEvent::Handler m_gridShowingHandler;
  297. SandboxEditor::GridSnappingChangedEvent::Handler m_gridSnappingHandler;
  298. SandboxEditor::NearFarPlaneChangedEvent::Handler m_nearPlaneDistanceHandler;
  299. SandboxEditor::NearFarPlaneChangedEvent::Handler m_farPlaneDistanceHandler;
  300. SandboxEditor::PerspectiveChangedEvent::Handler m_perspectiveChangeHandler;
  301. AZStd::unique_ptr<SandboxEditor::EditorViewportSettingsCallbacks> m_editorViewportSettingsCallbacks;
  302. // Used for some legacy logic which lets the widget release a grabbed keyboard at the right times
  303. // Unclear if it's still necessary.
  304. QSet<int> m_keyDown;
  305. // This widget holds a reference to the manipulator manage because its responsible for drawing manipulators
  306. AZStd::shared_ptr<AzToolsFramework::ManipulatorManager> m_manipulatorManager;
  307. AZStd::unique_ptr<SandboxEditor::EditorModularViewportCameraComposer> m_editorModularViewportCameraComposer;
  308. // Helper for getting EditorEntityNotificationBus events
  309. AZStd::unique_ptr<AZ::ViewportHelpers::EditorEntityNotifications> m_editorEntityNotifications;
  310. // The widget to which Atom will actually render
  311. AtomToolsFramework::RenderViewportWidget* m_renderViewport = nullptr;
  312. // Atom debug display
  313. AzFramework::DebugDisplayRequests* m_debugDisplay = nullptr;
  314. // Type to return current state of editor viewport settings
  315. EditorViewportSettings m_editorViewportSettings;
  316. // DO NOT USE THIS! It exists only to satisfy the signature of the base class method GetViewTm
  317. mutable Matrix34 m_viewTmStorage;
  318. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  319. };