ViewportWidget.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "EditorCommon.h"
  11. #include "LyShinePassDataBus.h"
  12. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  13. #include <AtomToolsFramework/Viewport/RenderViewportWidget.h>
  14. #include <Atom/RPI.Public/ViewportContextBus.h>
  15. #include <IFont.h>
  16. #include <QTimer>
  17. #endif
  18. class RulerWidget;
  19. class QMimeData;
  20. class UiRenderer;
  21. class CDraw2d;
  22. class ViewportWidget
  23. : public AtomToolsFramework::RenderViewportWidget
  24. , private AzToolsFramework::EditorPickModeNotificationBus::Handler
  25. , private FontNotificationBus::Handler
  26. , private LyShinePassDataRequestBus::Handler
  27. , public AZ::RPI::ViewportContextNotificationBus::Handler
  28. {
  29. Q_OBJECT
  30. public: // types
  31. enum DrawElementBorders
  32. {
  33. DrawElementBorders_Unselected = 0x1,
  34. DrawElementBorders_Visual = 0x2,
  35. DrawElementBorders_Parent = 0x4,
  36. DrawElementBorders_Hidden = 0x8,
  37. };
  38. public: // member functions
  39. explicit ViewportWidget(EditorWindow* parent);
  40. virtual ~ViewportWidget();
  41. ViewportInteraction* GetViewportInteraction();
  42. bool IsDrawingElementBorders(uint32 flags) const;
  43. void ToggleDrawElementBorders(uint32 flags);
  44. void ActiveCanvasChanged();
  45. void EntityContextChanged();
  46. //! Flags the viewport display as needing a refresh
  47. void Refresh();
  48. //! Used to clear the viewport and prevent rendering until the viewport layout updates
  49. void ClearUntilSafeToRedraw();
  50. //! Set whether to render the canvas
  51. void SetRedrawEnabled(bool enabled);
  52. //! Get the canvas scale factor being used for the preview mode
  53. float GetPreviewCanvasScale() { return m_previewCanvasScale; }
  54. //! Used by ViewportInteraction for drawing
  55. ViewportHighlight* GetViewportHighlight() { return m_viewportHighlight.get(); }
  56. //! Get the size of the RPI render viewport
  57. AZ::Vector2 GetRenderViewportSize() const;
  58. //! Get the widget to viewport scale factor
  59. float WidgetToViewportFactor() const;
  60. bool IsInObjectPickMode() { return m_inObjectPickMode; }
  61. void PickItem(AZ::EntityId entityId);
  62. QWidget* CreateViewportWithRulersWidget(QWidget* parent);
  63. void ShowRulers(bool show);
  64. bool AreRulersShown() { return m_rulersVisible; }
  65. void RefreshRulers();
  66. void SetRulerCursorPositions(const QPoint& globalPos);
  67. void ShowGuides(bool show);
  68. bool AreGuidesShown() { return m_guidesVisible; }
  69. void InitUiRenderer();
  70. protected:
  71. void contextMenuEvent(QContextMenuEvent* e) override;
  72. private slots:
  73. void UserSelectionChanged(HierarchyItemRawPtrList* items);
  74. void EnableCanvasRender();
  75. //! Called by a timer at the max frequency that we want to refresh the display
  76. void RefreshTick();
  77. protected:
  78. //! Forwards mouse press events to ViewportInteraction.
  79. //!
  80. //! Event is NOT propagated to parent class.
  81. void mousePressEvent(QMouseEvent* ev) override;
  82. //! Forwards mouse move events to ViewportInteraction.
  83. //!
  84. //! Event is NOT propagated to parent class.
  85. void mouseMoveEvent(QMouseEvent* ev) override;
  86. //! Forwards mouse release events to ViewportInteraction.
  87. //!
  88. //! Event is NOT propagated to parent class.
  89. void mouseReleaseEvent(QMouseEvent* ev) override;
  90. //! Forwards mouse wheel events to ViewportInteraction.
  91. //!
  92. //! Event is propagated to parent class.
  93. void wheelEvent(QWheelEvent* ev) override;
  94. //! Prevents shortcuts from interfering with preview mode.
  95. bool eventFilter(QObject* watched, QEvent* event) override;
  96. //! Handle events from Qt.
  97. bool event(QEvent* ev) override;
  98. //! Key press event from Qt.
  99. void keyPressEvent(QKeyEvent* event) override;
  100. //! Key release event from Qt.
  101. void keyReleaseEvent(QKeyEvent* event) override;
  102. void focusOutEvent(QFocusEvent* ev) override;
  103. private: // member functions
  104. // EditorPickModeNotificationBus
  105. void OnEntityPickModeStarted() override;
  106. void OnEntityPickModeStopped() override;
  107. // FontNotifications
  108. void OnFontsReloaded() override;
  109. void OnFontTextureUpdated(IFFont* font) override;
  110. // ~FontNotifications
  111. // LyShinePassDataRequestBus
  112. LyShine::AttachmentImagesAndDependencies GetRenderTargets() override;
  113. // ~LyShinePassDataRequestBus
  114. // AZ::TickBus::Handler
  115. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  116. int GetTickOrder() override;
  117. // ~AZ::TickBus::Handler
  118. // AZ::RPI::ViewportContextNotificationBus::Handler overrides...
  119. void OnRenderTick() override;
  120. void OnViewportDpiScalingChanged(float dpiScale) override;
  121. //! Update UI canvases when in edit mode
  122. void UpdateEditMode(float deltaTime);
  123. //! Render the viewport when in edit mode
  124. void RenderEditMode();
  125. //! Update UI canvases when in preview mode
  126. void UpdatePreviewMode(float deltaTime);
  127. //! Render the viewport when in preview mode
  128. void RenderPreviewMode();
  129. //! Fill the entire viewport area with a background color
  130. void RenderViewportBackground();
  131. //! Create shortcuts for manipulating the viewport
  132. void SetupShortcuts();
  133. //! Do the Qt stuff to hide/show the rulers
  134. void ApplyRulerVisibility();
  135. private: // data
  136. void resizeEvent(QResizeEvent* ev) override;
  137. void dragEnterEvent(QDragEnterEvent* event) override;
  138. void dropEvent(QDropEvent* event) override;
  139. bool AcceptsMimeData(const QMimeData* mimeData);
  140. QPointF WidgetToViewport(const QPointF &point) const;
  141. EditorWindow* m_editorWindow;
  142. std::unique_ptr< ViewportInteraction > m_viewportInteraction;
  143. std::unique_ptr< ViewportAnchor > m_viewportAnchor;
  144. std::unique_ptr< ViewportHighlight > m_viewportHighlight;
  145. std::unique_ptr< ViewportCanvasBackground > m_viewportBackground;
  146. std::unique_ptr< ViewportPivot > m_viewportPivot;
  147. uint32 m_drawElementBordersFlags;
  148. bool m_refreshRequested;
  149. bool m_canvasRenderIsEnabled;
  150. QTimer m_updateTimer;
  151. float m_previewCanvasScale;
  152. bool m_inObjectPickMode = false;
  153. RulerWidget* m_rulerHorizontal = nullptr;
  154. RulerWidget* m_rulerVertical = nullptr;
  155. QWidget* m_rulerCorner = nullptr;
  156. bool m_rulersVisible;
  157. bool m_guidesVisible;
  158. bool m_fontTextureHasChanged = false;
  159. AZStd::shared_ptr<UiRenderer> m_uiRenderer;
  160. AZStd::shared_ptr<CDraw2d> m_draw2d;
  161. };