EditorViewportWidget.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 PlayInEditorState
  115. {
  116. Editor,
  117. Starting,
  118. Started,
  119. Stopping
  120. };
  121. enum class KeyPressedState
  122. {
  123. AllUp,
  124. PressedThisFrame,
  125. PressedInPreviousFrame,
  126. };
  127. ////////////////////////////////////////////////////////////////////////
  128. // Method overrides ...
  129. // QWidget overrides ...
  130. void focusOutEvent(QFocusEvent* event) override;
  131. void keyPressEvent(QKeyEvent* event) override;
  132. bool event(QEvent* event) override;
  133. void resizeEvent(QResizeEvent* event) override;
  134. void paintEvent(QPaintEvent* event) override;
  135. void mousePressEvent(QMouseEvent* event) override;
  136. // QtViewport/IDisplayViewport/CViewport overrides ...
  137. EViewportType GetType() const override
  138. {
  139. return ET_ViewportCamera;
  140. }
  141. void SetType([[maybe_unused]] EViewportType type) override
  142. {
  143. assert(type == ET_ViewportCamera);
  144. };
  145. AzToolsFramework::ViewportInteraction::MouseInteraction BuildMouseInteraction(
  146. Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, const QPoint& point) override;
  147. void SetViewportId(int id) override;
  148. QPoint WorldToView(const Vec3& wp) const override;
  149. Vec3 WorldToView3D(const Vec3& wp, int nFlags = 0) const override;
  150. Vec3 ViewToWorld(
  151. const QPoint& vp,
  152. bool* collideWithTerrain = nullptr,
  153. bool onlyTerrain = false,
  154. bool bSkipVegetation = false,
  155. bool bTestRenderMesh = false,
  156. bool* collideWithObject = nullptr) const override;
  157. void ViewToWorldRay(const QPoint& vp, Vec3& raySrc, Vec3& rayDir) const override;
  158. Vec3 ViewToWorldNormal(const QPoint& vp, bool onlyTerrain, bool bTestRenderMesh = false) override;
  159. float GetScreenScaleFactor(const Vec3& worldPoint) const override;
  160. float GetAspectRatio() const override;
  161. bool HitTest(const QPoint& point, HitContext& hitInfo) override;
  162. bool IsBoundsVisible(const AABB& box) const override;
  163. void CenterOnAABB(const AABB& aabb) override;
  164. void OnTitleMenu(QMenu* menu) override;
  165. void SetViewTM(const Matrix34& tm) override;
  166. const Matrix34& GetViewTM() const override;
  167. void Update() override;
  168. void UpdateContent(int flags) override;
  169. // SceneNotificationBus overrides ...
  170. void OnBeginPrepareRender() override;
  171. // Camera::CameraNotificationBus overrides ...
  172. void OnActiveViewChanged(const AZ::EntityId&) override;
  173. // IEditorEventListener overrides ...
  174. void OnEditorNotifyEvent(EEditorNotifyEvent event) override;
  175. // Callback for setting modification
  176. void OnDefaultCameraNearFarChanged();
  177. // AzToolsFramework::EditorEntityContextNotificationBus overrides ...
  178. // note: handler moved to cpp to resolve link issues in unity builds
  179. void OnStartPlayInEditor();
  180. void OnStopPlayInEditor();
  181. void OnStartPlayInEditorBegin();
  182. // IUndoManagerListener
  183. void BeginUndoTransaction() override;
  184. void EndUndoTransaction() override;
  185. // AzFramework::InputSystemCursorConstraintRequestBus overrides ...
  186. void* GetSystemCursorConstraintWindow() const override;
  187. // AzToolsFramework::MainEditorViewportInteractionRequestBus overrides ...
  188. bool ShowingWorldSpace() override;
  189. QWidget* GetWidgetForViewportContextMenu() override;
  190. // EditorEntityViewportInteractionRequestBus overrides ...
  191. void FindVisibleEntities(AZStd::vector<AZ::EntityId>& visibleEntities) override;
  192. // Camera::EditorCameraRequestBus overrides ...
  193. void SetViewFromEntityPerspective(const AZ::EntityId& entityId) override;
  194. AZ::EntityId GetCurrentViewEntityId() override;
  195. bool GetActiveCameraPosition(AZ::Vector3& cameraPos) override;
  196. AZStd::optional<AZ::Transform> GetActiveCameraTransform() override;
  197. AZStd::optional<float> GetCameraFoV() override;
  198. bool GetActiveCameraState(AzFramework::CameraState& cameraState) override;
  199. // AzToolsFramework::Prefab::PrefabPublicNotificationBus overrides ...
  200. void OnRootPrefabInstanceLoaded() override;
  201. ////////////////////////////////////////////////////////////////////////
  202. // Private helpers...
  203. void SetDefaultCameraNearFar();
  204. void RenderAll();
  205. bool RayRenderMeshIntersection(IRenderMesh* pRenderMesh, const Vec3& vInPos, const Vec3& vInDir, Vec3& vOutPos, Vec3& vOutNormal) const;
  206. bool AddCameraMenuItems(QMenu* menu);
  207. void ResizeView(int width, int height);
  208. void HideCursor();
  209. void ShowCursor();
  210. double WidgetToViewportFactor() const;
  211. bool ShouldPreviewFullscreen() const;
  212. void StartFullscreenPreview();
  213. void StopFullscreenPreview();
  214. void OnMenuCreateCameraEntityFromCurrentView();
  215. // From a series of input primitives, compose a complete mouse interaction.
  216. AzToolsFramework::ViewportInteraction::MouseInteraction BuildMouseInteractionInternal(
  217. AzToolsFramework::ViewportInteraction::MouseButtons buttons,
  218. AzToolsFramework::ViewportInteraction::KeyboardModifiers modifiers,
  219. const AzToolsFramework::ViewportInteraction::MousePick& mousePick) const;
  220. // Given a point in the viewport, return the pick ray into the scene.
  221. // note: The argument passed to parameter **point**, originating
  222. // from a Qt event, must first be passed to WidgetToViewport before being
  223. // passed to BuildMousePick.
  224. AzToolsFramework::ViewportInteraction::MousePick BuildMousePick(const QPoint& point) const;
  225. bool CheckRespondToInput() const;
  226. void BuildDragDropContext(
  227. AzQtComponents::ViewportDragContext& context, AzFramework::ViewportId viewportId, const QPoint& point) override;
  228. void SetAsActiveViewport();
  229. void PushDisableRendering();
  230. void PopDisableRendering();
  231. bool IsRenderingDisabled() const;
  232. void RestoreViewportAfterGameMode();
  233. void UpdateScene();
  234. void SetDefaultCamera();
  235. void SetSelectedCamera();
  236. bool IsSelectedCamera() const;
  237. void SetEntityAsCamera(const AZ::EntityId& entityId);
  238. void SetFirstComponentCamera();
  239. void PostCameraSet();
  240. // This switches the active camera to the next one in the list of (default, all custom cams).
  241. void CycleCamera();
  242. QPoint WidgetToViewport(const QPoint& point) const;
  243. QPoint ViewportToWidget(const QPoint& point) const;
  244. QSize WidgetToViewport(const QSize& size) const;
  245. void UnProjectFromScreen(float sx, float sy, float* px, float* py, float* pz) const;
  246. void ProjectToScreen(float ptx, float pty, float ptz, float* sx, float* sy) const;
  247. AZ::RPI::ViewPtr GetCurrentAtomView() const;
  248. ////////////////////////////////////////////////////////////////////////
  249. // Members ...
  250. friend class AZ::ViewportHelpers::EditorEntityNotifications;
  251. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  252. // Singleton for the primary viewport
  253. static EditorViewportWidget* m_pPrimaryViewport;
  254. // The simulation (play-game in editor) state
  255. PlayInEditorState m_playInEditorState = PlayInEditorState::Editor;
  256. // Whether we are doing a full screen game preview (play-game in editor) or a regular one
  257. bool m_inFullscreenPreview = false;
  258. // The entity ID of the current camera for this viewport, or invalid if the default editor camera
  259. AZ::EntityId m_viewEntityId;
  260. // During play game in editor, holds the editor entity ID of the last
  261. AZ::EntityId m_viewEntityIdCachedForEditMode;
  262. // The editor camera TM before switching to game mode
  263. Matrix34 m_preGameModeViewTM;
  264. // Disables rendering during some periods of time, e.g. undo/redo, resize events
  265. uint m_disableRenderingCount = 0;
  266. // Determines if the viewport needs updating (false when out of focus for example)
  267. bool m_bUpdateViewport = false;
  268. // Avoid re-entering PostCameraSet->OnActiveViewChanged->PostCameraSet
  269. bool m_sendingOnActiveChanged = false;
  270. // Legacy...
  271. KeyPressedState m_pressedKeyState = KeyPressedState::AllUp;
  272. // The name to use for the default editor camera
  273. const QString m_defaultViewName;
  274. // Reentrancy guard for on paint events
  275. bool m_isOnPaint = false;
  276. // Guard against calling UpdateVisibility multiple times a frame
  277. bool m_hasUpdatedVisibility = false;
  278. // Aspect ratios available in the title bar
  279. CPredefinedAspectRatios m_predefinedAspectRatios;
  280. // Is the cursor hidden or displayed?
  281. bool m_bCursorHidden = false;
  282. // Shim for QtViewport, which used to be responsible for visibility queries in the editor,
  283. // these are now forwarded to EntityVisibilityQuery
  284. AzFramework::EntityVisibilityQuery m_entityVisibilityQuery;
  285. // Handlers for snapping/editor event callbacks
  286. SandboxEditor::AngleSnappingChangedEvent::Handler m_angleSnappingHandler;
  287. SandboxEditor::CameraSpeedScaleChangedEvent::Handler m_cameraSpeedScaleHandler;
  288. SandboxEditor::GridShowingChangedEvent::Handler m_gridShowingHandler;
  289. SandboxEditor::GridSnappingChangedEvent::Handler m_gridSnappingHandler;
  290. SandboxEditor::NearFarPlaneChangedEvent::Handler m_nearPlaneDistanceHandler;
  291. SandboxEditor::NearFarPlaneChangedEvent::Handler m_farPlaneDistanceHandler;
  292. SandboxEditor::PerspectiveChangedEvent::Handler m_perspectiveChangeHandler;
  293. AZStd::unique_ptr<SandboxEditor::EditorViewportSettingsCallbacks> m_editorViewportSettingsCallbacks;
  294. // Used for some legacy logic which lets the widget release a grabbed keyboard at the right times
  295. // Unclear if it's still necessary.
  296. QSet<int> m_keyDown;
  297. // This widget holds a reference to the manipulator manage because its responsible for drawing manipulators
  298. AZStd::shared_ptr<AzToolsFramework::ManipulatorManager> m_manipulatorManager;
  299. AZStd::unique_ptr<SandboxEditor::EditorModularViewportCameraComposer> m_editorModularViewportCameraComposer;
  300. // Helper for getting EditorEntityNotificationBus events
  301. AZStd::unique_ptr<AZ::ViewportHelpers::EditorEntityNotifications> m_editorEntityNotifications;
  302. // The widget to which Atom will actually render
  303. AtomToolsFramework::RenderViewportWidget* m_renderViewport = nullptr;
  304. // Atom debug display
  305. AzFramework::DebugDisplayRequests* m_debugDisplay = nullptr;
  306. // Type to return current state of editor viewport settings
  307. EditorViewportSettings m_editorViewportSettings;
  308. // DO NOT USE THIS! It exists only to satisfy the signature of the base class method GetViewTm
  309. mutable Matrix34 m_viewTmStorage;
  310. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  311. };