EditorViewportWidget.cpp 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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. // Description : implementation filefov
  9. #include "EditorDefs.h"
  10. #include "EditorViewportWidget.h"
  11. // Qt
  12. #include <QBoxLayout>
  13. #include <QCheckBox>
  14. #include <QMessageBox>
  15. #include <QPainter>
  16. #include <QScopedValueRollback>
  17. #include <QTimer>
  18. // AzCore
  19. #include <AzCore/Component/EntityId.h>
  20. #include <AzCore/Console/IConsole.h>
  21. #include <AzCore/Interface/Interface.h>
  22. #include <AzCore/Math/MathUtils.h>
  23. #include <AzCore/Math/VectorConversions.h>
  24. // AzFramework
  25. #include <AzFramework/Components/CameraBus.h>
  26. #if defined(AZ_PLATFORM_WINDOWS)
  27. #include <AzFramework/Input/Buses/Notifications/RawInputNotificationBus_Platform.h>
  28. #endif // defined(AZ_PLATFORM_WINDOWS)
  29. #include <AzFramework/Input/Devices/Mouse/InputDeviceMouse.h> // for AzFramework::InputDeviceMouse
  30. #include <AzFramework/Viewport/ViewportControllerList.h>
  31. // AzQtComponents
  32. #include <AzQtComponents/Utilities/QtWindowUtilities.h>
  33. // AzToolsFramework
  34. #include <AzToolsFramework/API/ComponentEntityObjectBus.h>
  35. #include <AzToolsFramework/API/EditorCameraBus.h>
  36. #include <AzToolsFramework/API/ViewportEditorModeTrackerInterface.h>
  37. #include <AzToolsFramework/Editor/ActionManagerUtils.h>
  38. #include <AzToolsFramework/Manipulators/ManipulatorManager.h>
  39. #include <AzToolsFramework/Viewport/ViewBookmarkLoaderInterface.h>
  40. #include <AzToolsFramework/Viewport/ViewportSettings.h>
  41. #include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h>
  42. #include <AzToolsFramework/ViewportSelection/EditorTransformComponentSelectionRequestBus.h>
  43. // AtomToolsFramework
  44. #include <AtomToolsFramework/Viewport/RenderViewportWidget.h>
  45. // AzFramework
  46. #include <AzFramework/Render/IntersectorInterface.h>
  47. // Editor
  48. #include "AnimationContext.h"
  49. #include "Core/QtEditorApplication.h"
  50. #include "CryCommon/MathConversion.h"
  51. #include "CryEditDoc.h"
  52. #include "CustomResolutionDlg.h"
  53. #include "DisplaySettings.h"
  54. #include "EditorPreferencesPageGeneral.h"
  55. #include "EditorViewportCamera.h"
  56. #include "EditorViewportSettings.h"
  57. #include "GameEngine.h"
  58. #include "Include/IDisplayViewport.h"
  59. #include "LayoutWnd.h"
  60. #include "MainWindow.h"
  61. #include "ProcessInfo.h"
  62. #include "Util/fastlib.h"
  63. #include "ViewManager.h"
  64. #include "ViewPane.h"
  65. #include "ViewportManipulatorController.h"
  66. // Atom
  67. #include <Atom/RPI.Public/RenderPipeline.h>
  68. #include <Atom/RPI.Public/View.h>
  69. #include <Atom/RPI.Public/ViewProviderBus.h>
  70. #include <Atom/RPI.Public/ViewportContextManager.h>
  71. #include <AzCore/Console/IConsole.h>
  72. #include <AzCore/Math/MatrixUtils.h>
  73. #include <QtGui/private/qhighdpiscaling_p.h>
  74. AZ_CVAR(
  75. bool, ed_visibility_logTiming, false, nullptr, AZ::ConsoleFunctorFlags::Null, "Output the timing of the new IVisibilitySystem query");
  76. EditorViewportWidget* EditorViewportWidget::m_pPrimaryViewport = nullptr;
  77. #if AZ_TRAIT_OS_PLATFORM_APPLE
  78. void StopFixedCursorMode();
  79. void StartFixedCursorMode(QObject* viewport);
  80. #endif
  81. #define RENDER_MESH_TEST_DISTANCE (0.2f)
  82. #define CURSOR_FONT_HEIGHT 8.0f
  83. namespace AZ::ViewportHelpers
  84. {
  85. static const char TextCantCreateCameraNoLevel[] = "Cannot create camera when no level is loaded.";
  86. class EditorEntityNotifications : public AzToolsFramework::EditorEntityContextNotificationBus::Handler
  87. {
  88. public:
  89. EditorEntityNotifications(EditorViewportWidget& editorViewportWidget)
  90. : m_editorViewportWidget(editorViewportWidget)
  91. {
  92. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  93. }
  94. ~EditorEntityNotifications() override
  95. {
  96. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  97. }
  98. // AzToolsFramework::EditorEntityContextNotificationBus overrides ...
  99. void OnStartPlayInEditor() override
  100. {
  101. m_editorViewportWidget.OnStartPlayInEditor();
  102. }
  103. void OnStopPlayInEditor() override
  104. {
  105. m_editorViewportWidget.OnStopPlayInEditor();
  106. }
  107. void OnStartPlayInEditorBegin() override
  108. {
  109. m_editorViewportWidget.OnStartPlayInEditorBegin();
  110. }
  111. private:
  112. EditorViewportWidget& m_editorViewportWidget;
  113. };
  114. } // namespace AZ::ViewportHelpers
  115. // helper to mark the view entity dirty that was moved using the 'Be this camera' functionality
  116. static void MarkCameraEntityDirty(const AZ::EntityId entityId)
  117. {
  118. using AzToolsFramework::ToolsApplicationRequests;
  119. AzToolsFramework::UndoSystem::URSequencePoint* undoBatch = nullptr;
  120. ToolsApplicationRequests::Bus::BroadcastResult(
  121. undoBatch, &ToolsApplicationRequests::Bus::Events::BeginUndoBatch, "EditorCameraComponentEntityChange");
  122. ToolsApplicationRequests::Bus::Broadcast(&ToolsApplicationRequests::Bus::Events::AddDirtyEntity, entityId);
  123. ToolsApplicationRequests::Bus::Broadcast(&ToolsApplicationRequests::Bus::Events::EndUndoBatch);
  124. }
  125. static void PopViewGroupForDefaultContext()
  126. {
  127. auto* atomViewportRequests = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  128. if (!atomViewportRequests)
  129. {
  130. return;
  131. }
  132. auto viewSystem = AZ::RPI::ViewportContextRequests::Get();
  133. if (!viewSystem)
  134. {
  135. return;
  136. }
  137. if (auto viewGroup = viewSystem->GetCurrentViewGroup(viewSystem->GetDefaultViewportContextName()))
  138. {
  139. const AZ::Name contextName = atomViewportRequests->GetDefaultViewportContextName();
  140. atomViewportRequests->PopViewGroup(contextName, viewGroup);
  141. }
  142. }
  143. static void PushViewGroupForDefaultContext()
  144. {
  145. auto* atomViewportRequests = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  146. if (!atomViewportRequests)
  147. {
  148. return;
  149. }
  150. auto viewSystem = AZ::RPI::ViewportContextRequests::Get();
  151. if (!viewSystem)
  152. {
  153. return;
  154. }
  155. if (auto viewGroup = viewSystem->GetCurrentViewGroup(viewSystem->GetDefaultViewportContextName()))
  156. {
  157. const AZ::Name contextName = atomViewportRequests->GetDefaultViewportContextName();
  158. atomViewportRequests->PushViewGroup(contextName, viewGroup);
  159. }
  160. }
  161. EditorViewportWidget::EditorViewportWidget(const QString& name, QWidget* parent)
  162. : QtViewport(parent)
  163. , m_defaultViewName(name)
  164. , m_renderViewport(nullptr) // m_renderViewport is initialized later, in SetViewportId
  165. {
  166. // need this to be set in order to allow for language switching on Windows
  167. setAttribute(Qt::WA_InputMethodEnabled);
  168. if (GetIEditor()->GetViewManager()->GetSelectedViewport() == nullptr)
  169. {
  170. GetIEditor()->GetViewManager()->SelectViewport(this);
  171. }
  172. GetIEditor()->RegisterNotifyListener(this);
  173. GetIEditor()->GetUndoManager()->AddListener(this);
  174. // The renderer requires something, so don't allow us to shrink to absolutely nothing
  175. // This won't in fact stop the viewport from being shrunk, when it's the centralWidget for
  176. // the MainWindow, but it will stop the viewport from getting resize events
  177. // once it's smaller than that, which from the renderer's perspective works out
  178. // to be the same thing.
  179. setMinimumSize(50, 50);
  180. setMouseTracking(true);
  181. Camera::EditorCameraRequestBus::Handler::BusConnect();
  182. Camera::CameraNotificationBus::Handler::BusConnect();
  183. m_editorEntityNotifications = AZStd::make_unique<AZ::ViewportHelpers::EditorEntityNotifications>(*this);
  184. AzFramework::AssetCatalogEventBus::Handler::BusConnect();
  185. AzToolsFramework::Prefab::PrefabPublicNotificationBus::Handler::BusConnect();
  186. m_manipulatorManager = GetIEditor()->GetViewManager()->GetManipulatorManager();
  187. if (!m_pPrimaryViewport)
  188. {
  189. SetAsActiveViewport();
  190. }
  191. }
  192. //////////////////////////////////////////////////////////////////////////
  193. EditorViewportWidget::~EditorViewportWidget()
  194. {
  195. if (m_pPrimaryViewport == this)
  196. {
  197. m_pPrimaryViewport = nullptr;
  198. }
  199. AzToolsFramework::Prefab::PrefabPublicNotificationBus::Handler::BusDisconnect();
  200. m_editorViewportSettings.Disconnect();
  201. DisconnectViewportInteractionRequestBus();
  202. m_editorEntityNotifications.reset();
  203. Camera::EditorCameraRequestBus::Handler::BusDisconnect();
  204. Camera::CameraNotificationBus::Handler::BusDisconnect();
  205. GetIEditor()->GetUndoManager()->RemoveListener(this);
  206. GetIEditor()->UnregisterNotifyListener(this);
  207. }
  208. //////////////////////////////////////////////////////////////////////////
  209. void EditorViewportWidget::resizeEvent(QResizeEvent* event)
  210. {
  211. // Call base class resize event while not rendering
  212. PushDisableRendering();
  213. QtViewport::resizeEvent(event);
  214. PopDisableRendering();
  215. // Emit Legacy system events about the viewport size change
  216. const QRect rcWindow = rect().translated(mapToGlobal(QPoint()));
  217. gEnv->pSystem->GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_MOVE, rcWindow.left(), rcWindow.top());
  218. m_rcClient = rect();
  219. m_rcClient.setBottomRight(WidgetToViewport(m_rcClient.bottomRight()));
  220. gEnv->pSystem->GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_RESIZE, width(), height());
  221. // In the case of the default viewport camera, we must re-set the FOV, which also updates the aspect ratio
  222. // Component cameras hand this themselves
  223. if (m_viewSourceType == ViewSourceType::None)
  224. {
  225. SetFOV(GetFOV());
  226. }
  227. }
  228. //////////////////////////////////////////////////////////////////////////
  229. void EditorViewportWidget::paintEvent([[maybe_unused]] QPaintEvent* event)
  230. {
  231. // Do not call CViewport::OnPaint() for painting messages
  232. // FIXME: paintEvent() isn't the best place for such logic. Should listen to proper eNotify events and to the stuff there instead.
  233. // (Repeats for other view port classes too).
  234. CGameEngine* ge = GetIEditor()->GetGameEngine();
  235. if ((ge && ge->IsLevelLoaded()) || (GetType() != ET_ViewportCamera))
  236. {
  237. setRenderOverlayVisible(true);
  238. }
  239. else
  240. {
  241. setRenderOverlayVisible(false);
  242. QPainter painter(this); // device context for painting
  243. // draw gradient background
  244. const QRect rc = rect();
  245. QLinearGradient gradient(rc.topLeft(), rc.bottomLeft());
  246. gradient.setColorAt(0, QColor(80, 80, 80));
  247. gradient.setColorAt(1, QColor(200, 200, 200));
  248. painter.fillRect(rc, gradient);
  249. // if we have some level loaded/loading/new
  250. // we draw a text
  251. if (!GetIEditor()->GetLevelFolder().isEmpty())
  252. {
  253. const int kFontSize = 200;
  254. const char* kFontName = "Arial";
  255. const QColor kTextColor(255, 255, 255);
  256. const QColor kTextShadowColor(0, 0, 0);
  257. const QFont font(kFontName, static_cast<int>(kFontSize / 10.0f));
  258. painter.setFont(font);
  259. QString friendlyName = QFileInfo(GetIEditor()->GetLevelName()).fileName();
  260. const QString strMsg = tr("Preparing level %1...").arg(friendlyName);
  261. // draw text shadow
  262. painter.setPen(kTextShadowColor);
  263. painter.drawText(rc, Qt::AlignCenter, strMsg);
  264. painter.setPen(kTextColor);
  265. // offset rect for normal text
  266. painter.drawText(rc.translated(-1, -1), Qt::AlignCenter, strMsg);
  267. }
  268. }
  269. }
  270. //////////////////////////////////////////////////////////////////////////
  271. void EditorViewportWidget::mousePressEvent(QMouseEvent* event)
  272. {
  273. GetIEditor()->GetViewManager()->SelectViewport(this);
  274. QtViewport::mousePressEvent(event);
  275. }
  276. AzToolsFramework::ViewportInteraction::MousePick EditorViewportWidget::BuildMousePick(const QPoint& point) const
  277. {
  278. AzToolsFramework::ViewportInteraction::MousePick mousePick;
  279. mousePick.m_screenCoordinates = AzToolsFramework::ViewportInteraction::ScreenPointFromQPoint(point * devicePixelRatioF());
  280. const auto [origin, direction] = m_renderViewport->ViewportScreenToWorldRay(mousePick.m_screenCoordinates);
  281. mousePick.m_rayOrigin = origin;
  282. mousePick.m_rayDirection = direction;
  283. return mousePick;
  284. }
  285. AzToolsFramework::ViewportInteraction::MouseInteraction EditorViewportWidget::BuildMouseInteractionInternal(
  286. const AzToolsFramework::ViewportInteraction::MouseButtons buttons,
  287. const AzToolsFramework::ViewportInteraction::KeyboardModifiers modifiers,
  288. const AzToolsFramework::ViewportInteraction::MousePick& mousePick) const
  289. {
  290. AzToolsFramework::ViewportInteraction::MouseInteraction mouse;
  291. mouse.m_interactionId.m_cameraId = m_viewEntityId;
  292. mouse.m_interactionId.m_viewportId = GetViewportId();
  293. mouse.m_mouseButtons = buttons;
  294. mouse.m_mousePick = mousePick;
  295. mouse.m_keyboardModifiers = modifiers;
  296. return mouse;
  297. }
  298. AzToolsFramework::ViewportInteraction::MouseInteraction EditorViewportWidget::BuildMouseInteraction(
  299. const Qt::MouseButtons buttons, const Qt::KeyboardModifiers modifiers, const QPoint& point)
  300. {
  301. namespace AztfVi = AzToolsFramework::ViewportInteraction;
  302. return BuildMouseInteractionInternal(
  303. AztfVi::BuildMouseButtons(buttons), AztfVi::BuildKeyboardModifiers(modifiers), BuildMousePick(WidgetToViewport(point)));
  304. }
  305. //////////////////////////////////////////////////////////////////////////
  306. bool EditorViewportWidget::event(QEvent* event)
  307. {
  308. switch (event->type())
  309. {
  310. case QEvent::WindowActivate:
  311. GetIEditor()->GetViewManager()->SelectViewport(this);
  312. // also kill the keys; if we alt-tab back to the viewport, or come back from the debugger, it's done (and there's no guarantee we'll
  313. // get the keyrelease event anyways)
  314. m_keyDown.clear();
  315. break;
  316. case QEvent::Shortcut:
  317. // a shortcut should immediately clear us, otherwise the release event never gets sent
  318. m_keyDown.clear();
  319. break;
  320. }
  321. return QtViewport::event(event);
  322. }
  323. //////////////////////////////////////////////////////////////////////////
  324. void EditorViewportWidget::UpdateContent(int flags)
  325. {
  326. QtViewport::UpdateContent(flags);
  327. if (flags & eUpdateObjects)
  328. {
  329. m_bUpdateViewport = true;
  330. }
  331. }
  332. //////////////////////////////////////////////////////////////////////////
  333. void EditorViewportWidget::Update()
  334. {
  335. if (Editor::EditorQtApplication::instance()->isMovingOrResizing())
  336. {
  337. return;
  338. }
  339. if (m_rcClient.isEmpty())
  340. {
  341. return;
  342. }
  343. if (!isVisible())
  344. {
  345. return;
  346. }
  347. // Don't wait for changes to update the focused viewport.
  348. if (CheckRespondToInput())
  349. {
  350. m_bUpdateViewport = true;
  351. }
  352. // While Renderer doesn't support fast rendering of the scene to more then 1 viewport
  353. // render only focused viewport if more then 1 are opened and always update is off.
  354. if (!m_isOnPaint && m_viewManager->GetNumberOfGameViewports() > 1 && GetType() == ET_ViewportCamera)
  355. {
  356. if (m_pPrimaryViewport != this)
  357. {
  358. if (CheckRespondToInput()) // If this is the focused window, set primary viewport.
  359. {
  360. SetAsActiveViewport();
  361. }
  362. else if (!m_bUpdateViewport) // Skip this viewport.
  363. {
  364. return;
  365. }
  366. }
  367. }
  368. const bool isGameMode = GetIEditor()->IsInGameMode();
  369. const bool isSimulationMode = GetIEditor()->GetGameEngine()->GetSimulationMode();
  370. // Allow debug visualization in both 'game' (Ctrl-G) and 'simulation' (Ctrl-P) modes
  371. if (isGameMode || isSimulationMode)
  372. {
  373. if (!IsRenderingDisabled())
  374. {
  375. // Disable rendering to avoid recursion into Update()
  376. PushDisableRendering();
  377. // get debug display interface for the viewport
  378. AzFramework::DebugDisplayRequestBus::BusPtr debugDisplayBus;
  379. AzFramework::DebugDisplayRequestBus::Bind(debugDisplayBus, GetViewportId());
  380. AZ_Assert(debugDisplayBus, "Invalid DebugDisplayRequestBus.");
  381. AzFramework::DebugDisplayRequests* debugDisplay = AzFramework::DebugDisplayRequestBus::FindFirstHandler(debugDisplayBus);
  382. // draw debug visualizations
  383. if (debugDisplay)
  384. {
  385. const AZ::u32 prevState = debugDisplay->GetState();
  386. debugDisplay->SetState(AzFramework::e_Mode3D | AzFramework::e_AlphaBlended | AzFramework::e_FillModeSolid | AzFramework::e_CullModeBack | AzFramework::e_DepthWriteOn | AzFramework::e_DepthTestOn);
  387. AzFramework::EntityDebugDisplayEventBus::Broadcast(
  388. &AzFramework::EntityDebugDisplayEvents::DisplayEntityViewport, AzFramework::ViewportInfo{ GetViewportId() },
  389. *debugDisplay);
  390. debugDisplay->SetState(prevState);
  391. }
  392. QtViewport::Update();
  393. PopDisableRendering();
  394. }
  395. // Game mode rendering is handled by CryAction
  396. if (isGameMode)
  397. {
  398. return;
  399. }
  400. }
  401. if (m_playInEditorState == PlayInEditorState::Stopping)
  402. {
  403. m_playInEditorState = PlayInEditorState::Editor;
  404. // Note that:
  405. // - this is assuming that the Atom camera components will share the same view ptr in editor as in game mode.
  406. // - if `m_viewEntityIdCachedForEditMode' is invalid, the camera before game mode was the default editor camera
  407. // - we must set the camera again when exiting game mode, because when rendering with track view, the editor camera gets set again
  408. SetViewFromEntityPerspective(m_viewEntityIdCachedForEditMode);
  409. m_viewEntityIdCachedForEditMode.SetInvalid();
  410. }
  411. // Prevents rendering recursion due to recursive Paint messages.
  412. if (IsRenderingDisabled())
  413. {
  414. return;
  415. }
  416. PushDisableRendering();
  417. // Render
  418. {
  419. // Post Render Callback
  420. {
  421. PostRenderers::iterator itr = m_postRenderers.begin();
  422. PostRenderers::iterator end = m_postRenderers.end();
  423. for (; itr != end; ++itr)
  424. {
  425. (*itr)->OnPostRender();
  426. }
  427. }
  428. }
  429. if(!m_hasUpdatedVisibility)
  430. {
  431. auto start = std::chrono::steady_clock::now();
  432. m_entityVisibilityQuery.UpdateVisibility(m_renderViewport->GetCameraState());
  433. if (ed_visibility_logTiming)
  434. {
  435. auto stop = std::chrono::steady_clock::now();
  436. std::chrono::duration<double> diff = stop - start;
  437. AZ_Printf("Visibility", "FindVisibleEntities (new) - Duration: %f", diff);
  438. }
  439. m_hasUpdatedVisibility = true;
  440. }
  441. QtViewport::Update();
  442. PopDisableRendering();
  443. m_bUpdateViewport = false;
  444. }
  445. //////////////////////////////////////////////////////////////////////////
  446. void EditorViewportWidget::PostCameraSet()
  447. {
  448. // CryLegacy notify
  449. GetIEditor()->Notify(eNotify_CameraChanged);
  450. // Special case in the editor; if the camera is the default editor camera,
  451. // notify that the active view changed. In game mode, it is a hard error to not have
  452. // any cameras on the view stack!
  453. if (m_viewSourceType == ViewSourceType::None)
  454. {
  455. m_sendingOnActiveChanged = true;
  456. Camera::CameraNotificationBus::Broadcast(&Camera::CameraNotificationBus::Events::OnActiveViewChanged, AZ::EntityId());
  457. m_sendingOnActiveChanged = false;
  458. }
  459. // Notify about editor camera change
  460. Camera::EditorCameraNotificationBus::Broadcast(
  461. &Camera::EditorCameraNotificationBus::Events::OnViewportViewEntityChanged, m_viewEntityId);
  462. // The editor view entity ID has changed, and the editor camera component "Be This Camera" text needs to be updated
  463. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(
  464. &AzToolsFramework::PropertyEditorGUIMessages::RequestRefresh,
  465. AzToolsFramework::PropertyModificationRefreshLevel::Refresh_AttributesAndValues);
  466. }
  467. //////////////////////////////////////////////////////////////////////////
  468. void EditorViewportWidget::OnEditorNotifyEvent(EEditorNotifyEvent event)
  469. {
  470. switch (event)
  471. {
  472. case eNotify_OnBeginGameMode:
  473. {
  474. if (GetIEditor()->GetViewManager()->GetGameViewport() == this)
  475. {
  476. if (m_viewEntityId.IsValid())
  477. {
  478. MarkCameraEntityDirty(m_viewEntityId);
  479. }
  480. m_preGameModeViewTM = GetViewTM();
  481. // this should only occur for the main viewport and no others.
  482. ShowCursor();
  483. SetCurrentCursor(STD_CURSOR_GAME);
  484. if (ShouldPreviewFullscreen())
  485. {
  486. StartFullscreenPreview();
  487. }
  488. }
  489. if (m_renderViewport)
  490. {
  491. m_renderViewport->SetInputProcessingEnabled(false);
  492. }
  493. }
  494. break;
  495. case eNotify_OnEndGameMode:
  496. if (GetIEditor()->GetViewManager()->GetGameViewport() == this)
  497. {
  498. SetCurrentCursor(STD_CURSOR_DEFAULT);
  499. if (m_inFullscreenPreview)
  500. {
  501. StopFullscreenPreview();
  502. }
  503. RestoreViewportAfterGameMode();
  504. }
  505. if (m_renderViewport)
  506. {
  507. m_renderViewport->SetInputProcessingEnabled(true);
  508. }
  509. break;
  510. case eNotify_OnCloseScene:
  511. // we restore the default viewport camera when closing the level to ensure if there is a pushed view group for a particular
  512. // editor camera component (view entity) it is popped/cleared to return to a default state when opening the next level
  513. SetDefaultCamera();
  514. m_renderViewport->SetScene(nullptr);
  515. break;
  516. case eNotify_OnEndLoad:
  517. case eNotify_OnEndCreate:
  518. UpdateScene();
  519. break;
  520. case eNotify_OnBeginNewScene:
  521. PushDisableRendering();
  522. break;
  523. case eNotify_OnEndNewScene:
  524. PopDisableRendering();
  525. UpdateScene();
  526. break;
  527. case eNotify_OnBeginLayerExport:
  528. case eNotify_OnBeginSceneSave:
  529. PushDisableRendering();
  530. break;
  531. case eNotify_OnEndLayerExport:
  532. case eNotify_OnEndSceneSave:
  533. PopDisableRendering();
  534. break;
  535. }
  536. }
  537. void EditorViewportWidget::OnBeginPrepareRender()
  538. {
  539. AZ_PROFILE_FUNCTION(Editor);
  540. m_hasUpdatedVisibility = false;
  541. if (!m_debugDisplay)
  542. {
  543. AzFramework::DebugDisplayRequestBus::BusPtr debugDisplayBus;
  544. AzFramework::DebugDisplayRequestBus::Bind(debugDisplayBus, GetViewportId());
  545. AZ_Assert(debugDisplayBus, "Invalid DebugDisplayRequestBus.");
  546. m_debugDisplay = AzFramework::DebugDisplayRequestBus::FindFirstHandler(debugDisplayBus);
  547. }
  548. if (!m_debugDisplay)
  549. {
  550. return;
  551. }
  552. m_isOnPaint = true;
  553. Update();
  554. m_isOnPaint = false;
  555. if (GetIEditor()->IsInGameMode())
  556. {
  557. return;
  558. }
  559. RenderAll();
  560. // Draw 2D helpers.
  561. m_debugDisplay->DepthTestOff();
  562. auto prevState = m_debugDisplay->GetState();
  563. m_debugDisplay->SetState(AzFramework::e_Mode3D | AzFramework::e_AlphaBlended | AzFramework::e_FillModeSolid | AzFramework::e_CullModeBack | AzFramework::e_DepthWriteOn | AzFramework::e_DepthTestOn);
  564. AzFramework::ViewportDebugDisplayEventBus::Event(
  565. AzToolsFramework::GetEntityContextId(), &AzFramework::ViewportDebugDisplayEvents::DisplayViewport2d,
  566. AzFramework::ViewportInfo{ GetViewportId() }, *m_debugDisplay);
  567. m_debugDisplay->SetState(prevState);
  568. m_debugDisplay->DepthTestOn();
  569. }
  570. //////////////////////////////////////////////////////////////////////////
  571. void EditorViewportWidget::RenderAll()
  572. {
  573. if (!m_debugDisplay)
  574. {
  575. return;
  576. }
  577. // allow the override of in-editor visualization
  578. AzFramework::ViewportDebugDisplayEventBus::Event(
  579. AzToolsFramework::GetEntityContextId(), &AzFramework::ViewportDebugDisplayEvents::DisplayViewport,
  580. AzFramework::ViewportInfo{ GetViewportId() }, *m_debugDisplay);
  581. m_entityVisibilityQuery.DisplayVisibility(*m_debugDisplay);
  582. if (m_manipulatorManager != nullptr)
  583. {
  584. namespace AztfVi = AzToolsFramework::ViewportInteraction;
  585. AztfVi::KeyboardModifiers keyboardModifiers;
  586. AztfVi::EditorModifierKeyRequestBus::BroadcastResult(
  587. keyboardModifiers, &AztfVi::EditorModifierKeyRequestBus::Events::QueryKeyboardModifiers);
  588. m_debugDisplay->DepthTestOff();
  589. m_manipulatorManager->DrawManipulators(
  590. *m_debugDisplay, m_renderViewport->GetCameraState(),
  591. BuildMouseInteractionInternal(
  592. AztfVi::MouseButtons(AztfVi::TranslateMouseButtons(QGuiApplication::mouseButtons())), keyboardModifiers,
  593. BuildMousePick(WidgetToViewport(mapFromGlobal(QCursor::pos())))));
  594. m_debugDisplay->DepthTestOn();
  595. }
  596. }
  597. //////////////////////////////////////////////////////////////////////////
  598. float EditorViewportWidget::GetAspectRatio() const
  599. {
  600. return gSettings.viewports.fDefaultAspectRatio;
  601. }
  602. //////////////////////////////////////////////////////////////////////////
  603. void EditorViewportWidget::OnMenuCreateCameraEntityFromCurrentView()
  604. {
  605. Camera::EditorCameraSystemRequestBus::Broadcast(&Camera::EditorCameraSystemRequests::CreateCameraEntityFromViewport);
  606. }
  607. void EditorViewportWidget::FindVisibleEntities(AZStd::vector<AZ::EntityId>& visibleEntitiesOut)
  608. {
  609. visibleEntitiesOut.assign(m_entityVisibilityQuery.Begin(), m_entityVisibilityQuery.End());
  610. }
  611. QWidget* EditorViewportWidget::GetWidgetForViewportContextMenu()
  612. {
  613. return this;
  614. }
  615. bool EditorViewportWidget::ShowingWorldSpace()
  616. {
  617. namespace AztfVi = AzToolsFramework::ViewportInteraction;
  618. AztfVi::KeyboardModifiers keyboardModifiers;
  619. AztfVi::EditorModifierKeyRequestBus::BroadcastResult(
  620. keyboardModifiers, &AztfVi::EditorModifierKeyRequestBus::Events::QueryKeyboardModifiers);
  621. return keyboardModifiers.Shift();
  622. }
  623. void EditorViewportWidget::SetViewportId(int id)
  624. {
  625. CViewport::SetViewportId(id);
  626. // Clear the cached DebugDisplay pointer. we're about to delete that render viewport, and deleting the render
  627. // viewport invalidates the DebugDisplay.
  628. m_debugDisplay = nullptr;
  629. // First delete any existing layout
  630. // This also deletes any existing render viewport widget (since it will be added to the layout)
  631. // Below is the typical method of clearing a QLayout, see e.g. https://doc.qt.io/qt-5/qlayout.html#takeAt
  632. if (QLayout* thisLayout = layout())
  633. {
  634. QLayoutItem* item;
  635. while ((item = thisLayout->takeAt(0)) != nullptr)
  636. {
  637. if (QWidget* widget = item->widget())
  638. {
  639. delete widget;
  640. }
  641. thisLayout->removeItem(item);
  642. delete item;
  643. }
  644. delete thisLayout;
  645. }
  646. // Now that we have an ID, we can initialize our viewport.
  647. m_renderViewport = new AtomToolsFramework::RenderViewportWidget(this, false);
  648. if (!m_renderViewport->InitializeViewportContext(id))
  649. {
  650. AZ_Warning("EditorViewportWidget", false, "Failed to initialize RenderViewportWidget's ViewportContext");
  651. delete m_renderViewport;
  652. m_renderViewport = nullptr;
  653. return;
  654. }
  655. QBoxLayout* layout = new QBoxLayout(QBoxLayout::Direction::TopToBottom, this);
  656. layout->setContentsMargins(QMargins());
  657. layout->addWidget(m_renderViewport);
  658. UpdateScene();
  659. if (m_pPrimaryViewport == this)
  660. {
  661. SetAsActiveViewport();
  662. }
  663. m_renderViewport->GetControllerList()->Add(AZStd::make_shared<SandboxEditor::ViewportManipulatorController>());
  664. m_editorModularViewportCameraComposer =
  665. AZStd::make_unique<SandboxEditor::EditorModularViewportCameraComposer>(AzFramework::ViewportId(id));
  666. m_renderViewport->GetControllerList()->Add(m_editorModularViewportCameraComposer->CreateModularViewportCameraController());
  667. m_editorViewportSettings.Connect(AzFramework::ViewportId(id));
  668. m_editorViewportSettingsCallbacks = SandboxEditor::CreateEditorViewportSettingsCallbacks();
  669. m_gridShowingHandler = SandboxEditor::GridShowingChangedEvent::Handler(
  670. [id](const bool showing)
  671. {
  672. AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Event(
  673. id, &AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Events::OnGridShowingChanged, showing);
  674. });
  675. m_editorViewportSettingsCallbacks->SetGridShowingChangedEvent(m_gridShowingHandler);
  676. m_gridSnappingHandler = SandboxEditor::GridSnappingChangedEvent::Handler(
  677. [id](const bool snapping)
  678. {
  679. AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Event(
  680. id, &AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Events::OnGridSnappingChanged, snapping);
  681. });
  682. m_editorViewportSettingsCallbacks->SetGridSnappingChangedEvent(m_gridSnappingHandler);
  683. m_angleSnappingHandler = SandboxEditor::AngleSnappingChangedEvent::Handler(
  684. [id](const bool snapping)
  685. {
  686. AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Event(
  687. id, &AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Events::OnAngleSnappingChanged, snapping);
  688. });
  689. m_editorViewportSettingsCallbacks->SetAngleSnappingChangedEvent(m_angleSnappingHandler);
  690. m_cameraSpeedScaleHandler = SandboxEditor::CameraSpeedScaleChangedEvent::Handler(
  691. [id](const float scale)
  692. {
  693. AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Event(
  694. id, &AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Events::OnCameraSpeedScaleChanged, scale);
  695. });
  696. m_editorViewportSettingsCallbacks->SetCameraSpeedScaleChangedEvent(m_cameraSpeedScaleHandler);
  697. m_perspectiveChangeHandler = SandboxEditor::PerspectiveChangedEvent::Handler(
  698. [this](const float fovRadians)
  699. {
  700. if (m_viewSourceType == ViewSourceType::None)
  701. {
  702. if (m_viewPane)
  703. {
  704. m_viewPane->OnFOVChanged(fovRadians);
  705. }
  706. SetFOV(fovRadians);
  707. }
  708. });
  709. m_editorViewportSettingsCallbacks->SetPerspectiveChangedEvent(m_perspectiveChangeHandler);
  710. m_nearPlaneDistanceHandler = SandboxEditor::NearFarPlaneChangedEvent::Handler(
  711. [this]([[maybe_unused]] float nearPlaneDistance)
  712. {
  713. OnDefaultCameraNearFarChanged();
  714. });
  715. m_editorViewportSettingsCallbacks->SetNearPlaneDistanceChangedEvent(m_nearPlaneDistanceHandler);
  716. m_farPlaneDistanceHandler = SandboxEditor::NearFarPlaneChangedEvent::Handler(
  717. [this]([[maybe_unused]] float farPlaneDistance)
  718. {
  719. OnDefaultCameraNearFarChanged();
  720. });
  721. m_editorViewportSettingsCallbacks->SetFarPlaneDistanceChangedEvent(m_farPlaneDistanceHandler);
  722. }
  723. void EditorViewportWidget::ConnectViewportInteractionRequestBus()
  724. {
  725. AzToolsFramework::ViewportInteraction::MainEditorViewportInteractionRequestBus::Handler::BusConnect(GetViewportId());
  726. AzToolsFramework::ViewportInteraction::EditorEntityViewportInteractionRequestBus::Handler::BusConnect(GetViewportId());
  727. m_viewportUi.ConnectViewportUiBus(GetViewportId());
  728. AzFramework::ViewportBorderRequestBus::Handler::BusConnect(GetViewportId());
  729. AzFramework::InputSystemCursorConstraintRequestBus::Handler::BusConnect();
  730. }
  731. void EditorViewportWidget::DisconnectViewportInteractionRequestBus()
  732. {
  733. AzFramework::InputSystemCursorConstraintRequestBus::Handler::BusDisconnect();
  734. AzFramework::ViewportBorderRequestBus::Handler::BusDisconnect();
  735. m_viewportUi.DisconnectViewportUiBus();
  736. AzToolsFramework::ViewportInteraction::EditorEntityViewportInteractionRequestBus::Handler::BusDisconnect();
  737. AzToolsFramework::ViewportInteraction::MainEditorViewportInteractionRequestBus::Handler::BusDisconnect();
  738. }
  739. namespace AZ::ViewportHelpers
  740. {
  741. void ToggleBool(bool* variable, bool* disableVariableIfOn)
  742. {
  743. *variable = !*variable;
  744. if (*variable && disableVariableIfOn)
  745. {
  746. *disableVariableIfOn = false;
  747. }
  748. }
  749. void ToggleInt(int* variable)
  750. {
  751. *variable = !*variable;
  752. }
  753. void AddCheckbox(QMenu* menu, const QString& text, bool* variable, bool* disableVariableIfOn = nullptr)
  754. {
  755. QAction* action = menu->addAction(text);
  756. QObject::connect(
  757. action, &QAction::triggered, action,
  758. [variable, disableVariableIfOn]
  759. {
  760. ToggleBool(variable, disableVariableIfOn);
  761. });
  762. action->setCheckable(true);
  763. action->setChecked(*variable);
  764. }
  765. void AddCheckbox(QMenu* menu, const QString& text, int* variable)
  766. {
  767. QAction* action = menu->addAction(text);
  768. QObject::connect(
  769. action, &QAction::triggered, action,
  770. [variable]
  771. {
  772. ToggleInt(variable);
  773. });
  774. action->setCheckable(true);
  775. action->setChecked(*variable);
  776. }
  777. } // namespace AZ::ViewportHelpers
  778. //////////////////////////////////////////////////////////////////////////
  779. void EditorViewportWidget::OnTitleMenu(QMenu* menu)
  780. {
  781. const bool bDisplayLabels = GetIEditor()->GetDisplaySettings()->IsDisplayLabels();
  782. QAction* action = menu->addAction(tr("Labels"));
  783. connect(
  784. action, &QAction::triggered, this,
  785. [bDisplayLabels]
  786. {
  787. GetIEditor()->GetDisplaySettings()->DisplayLabels(!bDisplayLabels);
  788. });
  789. action->setCheckable(true);
  790. action->setChecked(bDisplayLabels);
  791. AZ::ViewportHelpers::AddCheckbox(menu, tr("Show Construction Plane"), &gSettings.snap.constructPlaneDisplay);
  792. AZ::ViewportHelpers::AddCheckbox(menu, tr("Show Trigger Bounds"), &gSettings.viewports.bShowTriggerBounds);
  793. AZ::ViewportHelpers::AddCheckbox(menu, tr("Show Helpers of Frozen Objects"), &gSettings.viewports.nShowFrozenHelpers);
  794. if (!m_predefinedAspectRatios.IsEmpty())
  795. {
  796. QMenu* aspectRatiosMenu = menu->addMenu(tr("Target Aspect Ratio"));
  797. for (size_t i = 0; i < m_predefinedAspectRatios.GetCount(); ++i)
  798. {
  799. const QString& aspectRatioString = m_predefinedAspectRatios.GetName(i);
  800. QAction* aspectRatioAction = aspectRatiosMenu->addAction(aspectRatioString);
  801. connect(
  802. aspectRatioAction, &QAction::triggered, this,
  803. [i, this]
  804. {
  805. const float aspect = m_predefinedAspectRatios.GetValue(i);
  806. gSettings.viewports.fDefaultAspectRatio = aspect;
  807. });
  808. aspectRatioAction->setCheckable(true);
  809. aspectRatioAction->setChecked(m_predefinedAspectRatios.IsCurrent(i));
  810. }
  811. }
  812. // Set ourself as the active viewport so the following actions create a camera from this view
  813. GetIEditor()->GetViewManager()->SelectViewport(this);
  814. CGameEngine* gameEngine = GetIEditor()->GetGameEngine();
  815. if (Camera::EditorCameraSystemRequestBus::HasHandlers())
  816. {
  817. action = menu->addAction(tr("Create camera entity from current view"));
  818. connect(action, &QAction::triggered, this, &EditorViewportWidget::OnMenuCreateCameraEntityFromCurrentView);
  819. const auto prefabEditorEntityOwnershipInterface = AZ::Interface<AzToolsFramework::PrefabEditorEntityOwnershipInterface>::Get();
  820. if (!gameEngine || !gameEngine->IsLevelLoaded() ||
  821. (prefabEditorEntityOwnershipInterface && !prefabEditorEntityOwnershipInterface->IsRootPrefabAssigned()))
  822. {
  823. action->setEnabled(false);
  824. action->setToolTip(tr(AZ::ViewportHelpers::TextCantCreateCameraNoLevel));
  825. menu->setToolTipsVisible(true);
  826. }
  827. }
  828. if (!gameEngine || !gameEngine->IsLevelLoaded())
  829. {
  830. action->setEnabled(false);
  831. action->setToolTip(tr(AZ::ViewportHelpers::TextCantCreateCameraNoLevel));
  832. menu->setToolTipsVisible(true);
  833. }
  834. // Add Cameras.
  835. bool bHasCameras = AddCameraMenuItems(menu);
  836. EditorViewportWidget* pFloatingViewport = nullptr;
  837. if (GetIEditor()->GetViewManager()->GetViewCount() > 1)
  838. {
  839. for (int i = 0; i < GetIEditor()->GetViewManager()->GetViewCount(); ++i)
  840. {
  841. CViewport* vp = GetIEditor()->GetViewManager()->GetView(i);
  842. if (!vp)
  843. {
  844. continue;
  845. }
  846. if (viewport_cast<EditorViewportWidget*>(vp) == nullptr)
  847. {
  848. continue;
  849. }
  850. if (vp->GetViewportId() == MAX_NUM_VIEWPORTS - 1)
  851. {
  852. menu->addSeparator();
  853. QMenu* floatViewMenu = menu->addMenu(tr("Floating View"));
  854. pFloatingViewport = (EditorViewportWidget*)vp;
  855. pFloatingViewport->AddCameraMenuItems(floatViewMenu);
  856. if (bHasCameras)
  857. {
  858. floatViewMenu->addSeparator();
  859. }
  860. }
  861. }
  862. }
  863. }
  864. //////////////////////////////////////////////////////////////////////////
  865. bool EditorViewportWidget::AddCameraMenuItems(QMenu* menu)
  866. {
  867. if (!menu->isEmpty())
  868. {
  869. menu->addSeparator();
  870. }
  871. menu->addSeparator();
  872. // Camera Sub menu
  873. QMenu* customCameraMenu = menu->addMenu(tr("Camera"));
  874. QAction* action = customCameraMenu->addAction("Editor Camera");
  875. action->setCheckable(true);
  876. action->setChecked(m_viewSourceType == ViewSourceType::None);
  877. connect(action, &QAction::triggered, this, &EditorViewportWidget::SetDefaultCamera);
  878. AZ::EBusAggregateResults<AZ::EntityId> getCameraResults;
  879. Camera::CameraBus::BroadcastResult(getCameraResults, &Camera::CameraRequests::GetCameras);
  880. QVector<QAction*> additionalCameras;
  881. additionalCameras.reserve(static_cast<int>(getCameraResults.values.size()));
  882. for (const AZ::EntityId& entityId : getCameraResults.values)
  883. {
  884. AZStd::string entityName;
  885. AZ::ComponentApplicationBus::BroadcastResult(entityName, &AZ::ComponentApplicationRequests::GetEntityName, entityId);
  886. action = new QAction(QString(entityName.c_str()), nullptr);
  887. additionalCameras.append(action);
  888. action->setCheckable(true);
  889. action->setChecked(m_viewEntityId == entityId && m_viewSourceType == ViewSourceType::CameraComponent);
  890. connect(
  891. action, &QAction::triggered, this,
  892. [this, entityId](bool isChecked)
  893. {
  894. if (isChecked)
  895. {
  896. SetEntityAsCamera(entityId);
  897. }
  898. else
  899. {
  900. SetDefaultCamera();
  901. }
  902. });
  903. }
  904. std::sort(
  905. additionalCameras.begin(), additionalCameras.end(),
  906. [](QAction* a1, QAction* a2)
  907. {
  908. return QString::compare(a1->text(), a2->text(), Qt::CaseInsensitive) < 0;
  909. });
  910. for (QAction* cameraAction : additionalCameras)
  911. {
  912. customCameraMenu->addAction(cameraAction);
  913. }
  914. return true;
  915. }
  916. //////////////////////////////////////////////////////////////////////////
  917. void EditorViewportWidget::ResizeView(int width, int height)
  918. {
  919. const QRect rView = rect().translated(mapToGlobal(QPoint()));
  920. int deltaWidth = width - rView.width();
  921. int deltaHeight = height - rView.height();
  922. if (window()->isFullScreen())
  923. {
  924. setGeometry(rView.left(), rView.top(), rView.width() + deltaWidth, rView.height() + deltaHeight);
  925. }
  926. else
  927. {
  928. QWidget* window = this->window();
  929. if (window->isMaximized())
  930. {
  931. window->showNormal();
  932. }
  933. const QSize deltaSize = QSize(width, height) - size();
  934. window->move(0, 0);
  935. window->resize(window->size() + deltaSize);
  936. }
  937. }
  938. //////////////////////////////////////////////////////////////////////////
  939. EditorViewportWidget* EditorViewportWidget::GetPrimaryViewport()
  940. {
  941. return m_pPrimaryViewport;
  942. }
  943. //////////////////////////////////////////////////////////////////////////
  944. void EditorViewportWidget::focusOutEvent([[maybe_unused]] QFocusEvent* event)
  945. {
  946. // if we lose focus, the keyboard map needs to be cleared immediately
  947. if (!m_keyDown.isEmpty())
  948. {
  949. m_keyDown.clear();
  950. releaseKeyboard();
  951. }
  952. }
  953. void EditorViewportWidget::keyPressEvent(QKeyEvent* event)
  954. {
  955. // NOTE: we keep track of key presses and releases explicitly because the OS/Qt will insert a slight delay between sending
  956. // key events when the key is held down. This is standard, but makes responding to key events for game style input silly
  957. // because we want the movement to be butter smooth.
  958. if (!event->isAutoRepeat())
  959. {
  960. m_keyDown.insert(event->key());
  961. }
  962. QtViewport::keyPressEvent(event);
  963. #if defined(AZ_PLATFORM_WINDOWS)
  964. // In game mode on windows we need to forward raw text events to the input system.
  965. if (GetIEditor()->IsInGameMode() && GetType() == ET_ViewportCamera)
  966. {
  967. // Get the QString as a '\0'-terminated array of unsigned shorts.
  968. // The result remains valid until the string is modified.
  969. const ushort* codeUnitsUTF16 = event->text().utf16();
  970. while (ushort codeUnitUTF16 = *codeUnitsUTF16)
  971. {
  972. AzFramework::RawInputNotificationBusWindows::Broadcast(
  973. &AzFramework::RawInputNotificationsWindows::OnRawInputCodeUnitUTF16Event, codeUnitUTF16);
  974. ++codeUnitsUTF16;
  975. }
  976. }
  977. #endif // defined(AZ_PLATFORM_WINDOWS)
  978. }
  979. void EditorViewportWidget::SetViewTM(const Matrix34& camMatrix)
  980. {
  981. GetCurrentAtomView()->SetCameraTransform(LYTransformToAZMatrix3x4(camMatrix));
  982. if (m_pressedKeyState == KeyPressedState::PressedThisFrame)
  983. {
  984. m_pressedKeyState = KeyPressedState::PressedInPreviousFrame;
  985. }
  986. }
  987. const Matrix34& EditorViewportWidget::GetViewTM() const
  988. {
  989. // `m_viewTmStorage' is only required because we must return a reference
  990. m_viewTmStorage = AZTransformToLYTransform(GetCurrentAtomView()->GetCameraTransform());
  991. return m_viewTmStorage;
  992. };
  993. AZ::EntityId EditorViewportWidget::GetCurrentViewEntityId()
  994. {
  995. // Sanity check that this camera entity ID is actually the camera entity which owns the current active render view
  996. if (m_viewSourceType == ViewSourceType::CameraComponent)
  997. {
  998. // Check that the current view is the same view as the view entity view
  999. AZ::RPI::ViewPtr viewEntityView;
  1000. AZ::RPI::ViewProviderBus::EventResult(viewEntityView, m_viewEntityId, &AZ::RPI::ViewProviderBus::Events::GetView);
  1001. [[maybe_unused]] const bool isViewEntityCorrect = viewEntityView == GetCurrentAtomView();
  1002. AZ_Error(
  1003. "EditorViewportWidget", isViewEntityCorrect,
  1004. "GetCurrentViewEntityId called while the current view is being changed. "
  1005. "You may get inconsistent results if you make use of the returned entity ID. "
  1006. "This is an internal error, please report it as a bug.");
  1007. }
  1008. return m_viewEntityId;
  1009. }
  1010. Vec3 EditorViewportWidget::WorldToView3D(const Vec3& wp, [[maybe_unused]] int nFlags) const
  1011. {
  1012. Vec3 out(0, 0, 0);
  1013. float x, y;
  1014. ProjectToScreen(wp.x, wp.y, wp.z, &x, &y);
  1015. if (_finite(x) && _finite(y))
  1016. {
  1017. out.x = (x / 100) * m_rcClient.width();
  1018. out.y = (y / 100) * m_rcClient.height();
  1019. out.x /= static_cast<float>(QHighDpiScaling::factor(windowHandle()->screen()));
  1020. out.y /= static_cast<float>(QHighDpiScaling::factor(windowHandle()->screen()));
  1021. }
  1022. return out;
  1023. }
  1024. //////////////////////////////////////////////////////////////////////////
  1025. QPoint EditorViewportWidget::WorldToView(const Vec3& wp) const
  1026. {
  1027. return AzToolsFramework::ViewportInteraction::QPointFromScreenPoint(m_renderViewport->ViewportWorldToScreen(LYVec3ToAZVec3(wp)));
  1028. }
  1029. //////////////////////////////////////////////////////////////////////////
  1030. Vec3 EditorViewportWidget::ViewToWorld(
  1031. const QPoint& vp, bool* collideWithTerrain, bool onlyTerrain, bool bSkipVegetation, bool bTestRenderMesh, bool* collideWithObject) const
  1032. {
  1033. AZ_PROFILE_FUNCTION(Editor);
  1034. AZ_UNUSED(collideWithTerrain);
  1035. AZ_UNUSED(onlyTerrain);
  1036. AZ_UNUSED(bTestRenderMesh);
  1037. AZ_UNUSED(bSkipVegetation);
  1038. AZ_UNUSED(bSkipVegetation);
  1039. AZ_UNUSED(collideWithObject);
  1040. auto ray =
  1041. m_renderViewport->ViewportScreenToWorldRay(AzToolsFramework::ViewportInteraction::ScreenPointFromQPoint(vp * devicePixelRatioF()));
  1042. const float maxDistance = 10000.f;
  1043. Vec3 v = AZVec3ToLYVec3(ray.m_direction) * maxDistance;
  1044. if (!_finite(v.x) || !_finite(v.y) || !_finite(v.z))
  1045. {
  1046. return Vec3(0, 0, 0);
  1047. }
  1048. Vec3 colp = AZVec3ToLYVec3(ray.m_origin) + 0.002f * v;
  1049. return colp;
  1050. }
  1051. //////////////////////////////////////////////////////////////////////////
  1052. Vec3 EditorViewportWidget::ViewToWorldNormal(const QPoint& vp, bool onlyTerrain, bool bTestRenderMesh)
  1053. {
  1054. AZ_UNUSED(vp);
  1055. AZ_UNUSED(onlyTerrain);
  1056. AZ_UNUSED(bTestRenderMesh);
  1057. AZ_PROFILE_FUNCTION(Editor);
  1058. return Vec3(0, 0, 1);
  1059. }
  1060. //////////////////////////////////////////////////////////////////////////
  1061. bool EditorViewportWidget::RayRenderMeshIntersection(
  1062. IRenderMesh* pRenderMesh, const Vec3& vInPos, const Vec3& vInDir, Vec3& vOutPos, Vec3& vOutNormal) const
  1063. {
  1064. AZ_UNUSED(pRenderMesh);
  1065. AZ_UNUSED(vInPos);
  1066. AZ_UNUSED(vInDir);
  1067. AZ_UNUSED(vOutPos);
  1068. AZ_UNUSED(vOutNormal);
  1069. return false;
  1070. /*SRayHitInfo hitInfo;
  1071. hitInfo.bUseCache = false;
  1072. hitInfo.bInFirstHit = false;
  1073. hitInfo.inRay.origin = vInPos;
  1074. hitInfo.inRay.direction = vInDir.GetNormalized();
  1075. hitInfo.inReferencePoint = vInPos;
  1076. hitInfo.fMaxHitDistance = 0;
  1077. bool bRes = ???->RenderMeshRayIntersection(pRenderMesh, hitInfo, nullptr);
  1078. vOutPos = hitInfo.vHitPos;
  1079. vOutNormal = hitInfo.vHitNormal;
  1080. return bRes;*/
  1081. }
  1082. void EditorViewportWidget::UnProjectFromScreen(float sx, float sy, float* px, float* py, float* pz) const
  1083. {
  1084. const AZ::Vector3 wp = m_renderViewport->ViewportScreenToWorld(AzFramework::ScreenPoint{ (int)sx, m_rcClient.bottom() - ((int)sy) });
  1085. *px = wp.GetX();
  1086. *py = wp.GetY();
  1087. *pz = wp.GetZ();
  1088. }
  1089. void EditorViewportWidget::ProjectToScreen(float ptx, float pty, float ptz, float* sx, float* sy) const
  1090. {
  1091. AzFramework::ScreenPoint screenPosition = m_renderViewport->ViewportWorldToScreen(AZ::Vector3{ ptx, pty, ptz });
  1092. *sx = static_cast<float>(screenPosition.m_x);
  1093. *sy = static_cast<float>(screenPosition.m_y);
  1094. }
  1095. //////////////////////////////////////////////////////////////////////////
  1096. void EditorViewportWidget::ViewToWorldRay(const QPoint& vp, Vec3& raySrc, Vec3& rayDir) const
  1097. {
  1098. QRect rc = m_rcClient;
  1099. Vec3 pos0, pos1;
  1100. float wx, wy, wz;
  1101. UnProjectFromScreen(static_cast<float>(vp.x()), static_cast<float>(rc.bottom() - vp.y()), &wx, &wy, &wz);
  1102. if (!_finite(wx) || !_finite(wy) || !_finite(wz))
  1103. {
  1104. return;
  1105. }
  1106. if (fabs(wx) > 1000000 || fabs(wy) > 1000000 || fabs(wz) > 1000000)
  1107. {
  1108. return;
  1109. }
  1110. pos0(wx, wy, wz);
  1111. raySrc = pos0;
  1112. rayDir = (pos0 - AZVec3ToLYVec3(m_renderViewport->GetCameraState().m_position)).GetNormalized();
  1113. }
  1114. //////////////////////////////////////////////////////////////////////////
  1115. float EditorViewportWidget::GetScreenScaleFactor([[maybe_unused]] const Vec3& worldPoint) const
  1116. {
  1117. AZ_Error("CryLegacy", false, "EditorViewportWidget::GetScreenScaleFactor not implemented");
  1118. return 1.f;
  1119. }
  1120. //////////////////////////////////////////////////////////////////////////
  1121. bool EditorViewportWidget::CheckRespondToInput() const
  1122. {
  1123. if (!Editor::EditorQtApplication::IsActive())
  1124. {
  1125. return false;
  1126. }
  1127. if (!hasFocus() && !m_renderViewport->hasFocus())
  1128. {
  1129. return false;
  1130. }
  1131. return true;
  1132. }
  1133. //////////////////////////////////////////////////////////////////////////
  1134. bool EditorViewportWidget::HitTest(const QPoint& point, HitContext& hitInfo)
  1135. {
  1136. return QtViewport::HitTest(point, hitInfo);
  1137. }
  1138. //////////////////////////////////////////////////////////////////////////
  1139. bool EditorViewportWidget::IsBoundsVisible(const AABB&) const
  1140. {
  1141. AZ_Assert(false, "Not supported");
  1142. return false;
  1143. }
  1144. //////////////////////////////////////////////////////////////////////////
  1145. void EditorViewportWidget::CenterOnAABB(const AABB& aabb)
  1146. {
  1147. Vec3 selectionCenter = aabb.GetCenter();
  1148. // Minimum center size is 40cm
  1149. const float minSelectionRadius = 0.4f;
  1150. const float selectionSize = std::max(minSelectionRadius, aabb.GetRadius());
  1151. // Move camera 25% further back than required
  1152. const float centerScale = 1.25f;
  1153. // Decompose original transform matrix
  1154. const Matrix34& originalTM = GetViewTM();
  1155. AffineParts affineParts;
  1156. affineParts.SpectralDecompose(originalTM);
  1157. // Forward vector is y component of rotation matrix
  1158. Matrix33 rotationMatrix(affineParts.rot);
  1159. const Vec3 viewDirection = rotationMatrix.GetColumn1().GetNormalized();
  1160. // Compute adjustment required by FOV != 90 degrees
  1161. const float fov = GetFOV();
  1162. const float fovScale = (1.0f / tan(fov * 0.5f));
  1163. // Compute new transform matrix
  1164. const float distanceToTarget = selectionSize * fovScale * centerScale;
  1165. const Vec3 newPosition = selectionCenter - (viewDirection * distanceToTarget);
  1166. Matrix34 newTM = Matrix34(rotationMatrix, newPosition);
  1167. // Set new orbit distance
  1168. float orbitDistance = distanceToTarget;
  1169. orbitDistance = fabs(orbitDistance);
  1170. SetViewTM(newTM);
  1171. }
  1172. //////////////////////////////////////////////////////////////////////////
  1173. void EditorViewportWidget::SetFOV(const float fov)
  1174. {
  1175. if (m_viewEntityId.IsValid())
  1176. {
  1177. Camera::CameraRequestBus::Event(m_viewEntityId, &Camera::CameraComponentRequests::SetFovRadians, fov);
  1178. }
  1179. else
  1180. {
  1181. auto viewSystem = AZ::RPI::ViewportContextRequests::Get();
  1182. if (!viewSystem)
  1183. {
  1184. return;
  1185. }
  1186. if (auto viewGroup = viewSystem->GetCurrentViewGroup(viewSystem->GetDefaultViewportContextName()))
  1187. {
  1188. auto viewToClip = viewGroup->GetView()->GetViewToClipMatrix();
  1189. AZ::SetPerspectiveMatrixFOV(viewToClip, fov, aznumeric_cast<float>(width()) / aznumeric_cast<float>(height()));
  1190. viewGroup->GetView()->SetViewToClipMatrix(viewToClip);
  1191. }
  1192. }
  1193. }
  1194. //////////////////////////////////////////////////////////////////////////
  1195. float EditorViewportWidget::GetFOV() const
  1196. {
  1197. if (m_viewEntityId.IsValid())
  1198. {
  1199. float fov = 0.f;
  1200. Camera::CameraRequestBus::EventResult(fov, m_viewEntityId, &Camera::CameraComponentRequests::GetFovRadians);
  1201. return fov;
  1202. }
  1203. else
  1204. {
  1205. auto viewSystem = AZ::RPI::ViewportContextRequests::Get();
  1206. if (!viewSystem)
  1207. {
  1208. return AZ::Constants::HalfPi; // 90 degrees (default)
  1209. }
  1210. if (auto viewGroup = viewSystem->GetCurrentViewGroup(viewSystem->GetDefaultViewportContextName()))
  1211. {
  1212. return AZ::GetPerspectiveMatrixFOV(viewGroup->GetView()->GetViewToClipMatrix());
  1213. }
  1214. }
  1215. return AZ::Constants::HalfPi; // 90 degrees (default)
  1216. }
  1217. void EditorViewportWidget::OnActiveViewChanged(const AZ::EntityId& viewEntityId)
  1218. {
  1219. // Avoid re-entry
  1220. if (m_sendingOnActiveChanged)
  1221. {
  1222. return;
  1223. }
  1224. // Ignore any changes in simulation mode
  1225. if (m_playInEditorState != PlayInEditorState::Editor)
  1226. {
  1227. return;
  1228. }
  1229. // if they've picked the same camera, then that means they want to toggle
  1230. if (viewEntityId.IsValid())
  1231. {
  1232. // Any such events for game entities should be filtered out by the check above
  1233. AZ_Error(
  1234. "EditorViewportWidget", Camera::EditorCameraViewRequestBus::FindFirstHandler(viewEntityId) != nullptr,
  1235. "Internal logic error - active view changed to an entity which is not an editor camera. "
  1236. "Please report this as a bug.");
  1237. m_viewEntityId = viewEntityId;
  1238. m_viewSourceType = ViewSourceType::CameraComponent;
  1239. AZStd::string entityName;
  1240. AZ::ComponentApplicationBus::BroadcastResult(entityName, &AZ::ComponentApplicationRequests::GetEntityName, viewEntityId);
  1241. SetName(QString("Camera entity: %1").arg(entityName.c_str()));
  1242. PostCameraSet();
  1243. }
  1244. else
  1245. {
  1246. SetDefaultCamera();
  1247. }
  1248. }
  1249. //////////////////////////////////////////////////////////////////////////
  1250. void EditorViewportWidget::SetDefaultCamera()
  1251. {
  1252. if (m_viewEntityId.IsValid())
  1253. {
  1254. // remove pushed view group for view entity (editor camera component in 'Be this camera' mode
  1255. PopViewGroupForDefaultContext();
  1256. }
  1257. m_viewEntityId.SetInvalid();
  1258. m_viewSourceType = ViewSourceType::None;
  1259. SetName(m_defaultViewName);
  1260. // synchronize the configured editor viewport FOV to the default camera
  1261. if (m_viewPane)
  1262. {
  1263. const float fovRadians = SandboxEditor::CameraDefaultFovRadians();
  1264. m_viewPane->OnFOVChanged(fovRadians);
  1265. SetFOV(fovRadians);
  1266. }
  1267. // update camera matrix according to near / far values
  1268. SetDefaultCameraNearFar();
  1269. PushViewGroupForDefaultContext();
  1270. PostCameraSet();
  1271. }
  1272. void EditorViewportWidget::SetDefaultCameraNearFar()
  1273. {
  1274. auto viewSystem = AZ::RPI::ViewportContextRequests::Get();
  1275. if (!viewSystem)
  1276. {
  1277. return;
  1278. }
  1279. if (auto viewGroup = viewSystem->GetCurrentViewGroup(viewSystem->GetDefaultViewportContextName()))
  1280. {
  1281. auto viewToClip = viewGroup->GetView()->GetViewToClipMatrix();
  1282. AZ::SetPerspectiveMatrixNearFar(viewToClip, SandboxEditor::CameraDefaultNearPlaneDistance(), SandboxEditor::CameraDefaultFarPlaneDistance());
  1283. viewGroup->GetView()->SetViewToClipMatrix(viewToClip);
  1284. }
  1285. }
  1286. void EditorViewportWidget::OnDefaultCameraNearFarChanged()
  1287. {
  1288. if (m_viewSourceType == ViewSourceType::None)
  1289. {
  1290. SetDefaultCameraNearFar();
  1291. }
  1292. }
  1293. //////////////////////////////////////////////////////////////////////////
  1294. AZ::RPI::ViewPtr EditorViewportWidget::GetCurrentAtomView() const
  1295. {
  1296. if (m_renderViewport && m_renderViewport->GetViewportContext())
  1297. {
  1298. return m_renderViewport->GetViewportContext()->GetDefaultView();
  1299. }
  1300. else
  1301. {
  1302. return nullptr;
  1303. }
  1304. }
  1305. //////////////////////////////////////////////////////////////////////////
  1306. void EditorViewportWidget::SetEntityAsCamera(const AZ::EntityId& entityId)
  1307. {
  1308. SetViewFromEntityPerspective(entityId);
  1309. }
  1310. void EditorViewportWidget::SetFirstComponentCamera()
  1311. {
  1312. AZ::EBusAggregateResults<AZ::EntityId> results;
  1313. Camera::CameraBus::BroadcastResult(results, &Camera::CameraRequests::GetCameras);
  1314. AZStd::sort_heap(results.values.begin(), results.values.end());
  1315. AZ::EntityId entityId;
  1316. if (!results.values.empty())
  1317. {
  1318. entityId = results.values[0];
  1319. }
  1320. SetEntityAsCamera(entityId);
  1321. }
  1322. //////////////////////////////////////////////////////////////////////////
  1323. void EditorViewportWidget::SetSelectedCamera()
  1324. {
  1325. AZ::EBusAggregateResults<AZ::EntityId> cameraList;
  1326. Camera::CameraBus::BroadcastResult(cameraList, &Camera::CameraRequests::GetCameras);
  1327. if (cameraList.values.size() > 0)
  1328. {
  1329. AzToolsFramework::EntityIdList selectedEntityList;
  1330. AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(
  1331. selectedEntityList, &AzToolsFramework::ToolsApplicationRequests::GetSelectedEntities);
  1332. for (const AZ::EntityId& entityId : selectedEntityList)
  1333. {
  1334. if (AZStd::find(cameraList.values.begin(), cameraList.values.end(), entityId) != cameraList.values.end())
  1335. {
  1336. SetEntityAsCamera(entityId);
  1337. }
  1338. }
  1339. }
  1340. }
  1341. //////////////////////////////////////////////////////////////////////////
  1342. bool EditorViewportWidget::IsSelectedCamera() const
  1343. {
  1344. AzToolsFramework::EntityIdList selectedEntityList;
  1345. AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(
  1346. selectedEntityList, &AzToolsFramework::ToolsApplicationRequests::GetSelectedEntities);
  1347. if ((m_viewSourceType == ViewSourceType::CameraComponent) && !selectedEntityList.empty() &&
  1348. AZStd::find(selectedEntityList.begin(), selectedEntityList.end(), m_viewEntityId) != selectedEntityList.end())
  1349. {
  1350. return true;
  1351. }
  1352. return false;
  1353. }
  1354. //////////////////////////////////////////////////////////////////////////
  1355. void EditorViewportWidget::CycleCamera()
  1356. {
  1357. // None -> Sequence -> LegacyCamera -> ... LegacyCamera -> CameraComponent -> ... CameraComponent -> None
  1358. // AZ_Entity has been intentionally left out of the cycle for now.
  1359. switch (m_viewSourceType)
  1360. {
  1361. case EditorViewportWidget::ViewSourceType::None:
  1362. {
  1363. SetFirstComponentCamera();
  1364. break;
  1365. }
  1366. case EditorViewportWidget::ViewSourceType::CameraComponent:
  1367. {
  1368. AZ::EBusAggregateResults<AZ::EntityId> results;
  1369. Camera::CameraBus::BroadcastResult(results, &Camera::CameraRequests::GetCameras);
  1370. AZStd::sort_heap(results.values.begin(), results.values.end());
  1371. auto&& currentCameraIterator = AZStd::find(results.values.begin(), results.values.end(), m_viewEntityId);
  1372. if (currentCameraIterator != results.values.end())
  1373. {
  1374. ++currentCameraIterator;
  1375. if (currentCameraIterator != results.values.end())
  1376. {
  1377. SetEntityAsCamera(*currentCameraIterator);
  1378. break;
  1379. }
  1380. }
  1381. SetDefaultCamera();
  1382. break;
  1383. }
  1384. default:
  1385. {
  1386. SetDefaultCamera();
  1387. break;
  1388. }
  1389. }
  1390. }
  1391. void EditorViewportWidget::SetViewFromEntityPerspective(const AZ::EntityId& entityId)
  1392. {
  1393. // This is an editor event, so is only serviced during edit mode, not play game mode
  1394. if (m_playInEditorState != PlayInEditorState::Editor)
  1395. {
  1396. AZ_Warning(
  1397. "EditorViewportWidget", false, "Tried to change the editor camera during play game in editor; this is currently unsupported");
  1398. return;
  1399. }
  1400. // when changing view, if an editor camera component (view entity) was in use, ensure we attempt to record an undo operation
  1401. // in case the transform of the entity changed (if no changes occurred then no undo operation will be stored)
  1402. if (m_viewEntityId.IsValid())
  1403. {
  1404. MarkCameraEntityDirty(m_viewEntityId);
  1405. }
  1406. if (entityId.IsValid())
  1407. {
  1408. // if we are switching between editor camera components (view entities) in the scene, if one is currently
  1409. // assigned, ensure we pop it from the stack before assigning a new one
  1410. if (m_viewEntityId.IsValid())
  1411. {
  1412. PopViewGroupForDefaultContext();
  1413. }
  1414. Camera::CameraRequestBus::Event(entityId, &Camera::CameraRequestBus::Events::MakeActiveView);
  1415. }
  1416. else
  1417. {
  1418. // note: SetDefaultCamera internally handles popping the current view group if an editor camera component
  1419. // (view entity) is assigned
  1420. SetDefaultCamera();
  1421. }
  1422. }
  1423. bool EditorViewportWidget::GetActiveCameraPosition(AZ::Vector3& cameraPos)
  1424. {
  1425. if (m_pPrimaryViewport == this)
  1426. {
  1427. if (GetIEditor()->IsInGameMode())
  1428. {
  1429. cameraPos = m_renderViewport->GetViewportContext()->GetCameraTransform().GetTranslation();
  1430. }
  1431. else
  1432. {
  1433. // Use viewTM, which is synced with the camera and guaranteed to be up-to-date
  1434. cameraPos = LYVec3ToAZVec3(GetViewTM().GetTranslation());
  1435. }
  1436. return true;
  1437. }
  1438. return false;
  1439. }
  1440. AZStd::optional<AZ::Transform> EditorViewportWidget::GetActiveCameraTransform()
  1441. {
  1442. if (m_pPrimaryViewport == this)
  1443. {
  1444. if (GetIEditor()->IsInGameMode())
  1445. {
  1446. return m_renderViewport->GetViewportContext()->GetCameraTransform();
  1447. }
  1448. else
  1449. {
  1450. // Use viewTM, which is synced with the camera and guaranteed to be up-to-date
  1451. return GetCurrentAtomView()->GetCameraTransform();
  1452. }
  1453. }
  1454. return AZStd::nullopt;
  1455. }
  1456. AZStd::optional<float> EditorViewportWidget::GetCameraFoV()
  1457. {
  1458. if (m_pPrimaryViewport == this)
  1459. {
  1460. return GetFOV();
  1461. }
  1462. return AZStd::nullopt;
  1463. }
  1464. bool EditorViewportWidget::GetActiveCameraState(AzFramework::CameraState& cameraState)
  1465. {
  1466. if (m_pPrimaryViewport == this)
  1467. {
  1468. cameraState = m_renderViewport->GetCameraState();
  1469. return true;
  1470. }
  1471. return false;
  1472. }
  1473. void EditorViewportWidget::OnStartPlayInEditorBegin()
  1474. {
  1475. m_playInEditorState = PlayInEditorState::Starting;
  1476. }
  1477. void EditorViewportWidget::OnRootPrefabInstanceLoaded()
  1478. {
  1479. // set the default camera on level/prefab load
  1480. SetDefaultCamera();
  1481. // set the camera position once we know the entire scene (level) has finished loading
  1482. Matrix34 defaultView = Matrix34::CreateIdentity();
  1483. // check to see if we have an existing last known location for this level
  1484. auto* viewBookmarkInterface = AZ::Interface<AzToolsFramework::ViewBookmarkInterface>::Get();
  1485. if (const AZStd::optional<AzToolsFramework::ViewBookmark> lastKnownLocationBookmark = viewBookmarkInterface->LoadLastKnownLocation();
  1486. lastKnownLocationBookmark.has_value())
  1487. {
  1488. defaultView.SetTranslation(Vec3(lastKnownLocationBookmark->m_position));
  1489. defaultView.SetRotation33(AZMatrix3x3ToLYMatrix3x3(AZ::Matrix3x3::CreateFromQuaternion(SandboxEditor::CameraRotation(
  1490. AZ::DegToRad(lastKnownLocationBookmark->m_rotation.GetX()), AZ::DegToRad(lastKnownLocationBookmark->m_rotation.GetZ())))));
  1491. }
  1492. else
  1493. {
  1494. // set the default editor camera position and orientation if there was no last known location
  1495. const AZ::Vector2 pitchYawDegrees = m_editorViewportSettings.DefaultEditorCameraOrientation();
  1496. defaultView.SetTranslation(Vec3(m_editorViewportSettings.DefaultEditorCameraPosition()));
  1497. defaultView.SetRotation33(AZMatrix3x3ToLYMatrix3x3(AZ::Matrix3x3::CreateFromQuaternion(
  1498. SandboxEditor::CameraRotation(AZ::DegToRad(pitchYawDegrees.GetX()), AZ::DegToRad(pitchYawDegrees.GetY())))));
  1499. }
  1500. SetViewTM(defaultView);
  1501. }
  1502. void EditorViewportWidget::OnStartPlayInEditor()
  1503. {
  1504. m_playInEditorState = PlayInEditorState::Started;
  1505. if (m_viewEntityId.IsValid())
  1506. {
  1507. // Note that this is assuming that the Atom camera components will share the same view ptr
  1508. // in editor as in game mode
  1509. m_viewEntityIdCachedForEditMode = m_viewEntityId;
  1510. AZ::EntityId runtimeEntityId;
  1511. AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
  1512. &AzToolsFramework::EditorEntityContextRequestBus::Events::MapEditorIdToRuntimeId, m_viewEntityId, runtimeEntityId);
  1513. m_viewEntityId = runtimeEntityId;
  1514. }
  1515. }
  1516. void EditorViewportWidget::OnStopPlayInEditor()
  1517. {
  1518. m_playInEditorState = PlayInEditorState::Stopping;
  1519. }
  1520. //////////////////////////////////////////////////////////////////////////
  1521. void EditorViewportWidget::HideCursor()
  1522. {
  1523. if (m_bCursorHidden || !gSettings.viewports.bHideMouseCursorWhenCaptured)
  1524. {
  1525. return;
  1526. }
  1527. qApp->setOverrideCursor(Qt::BlankCursor);
  1528. #if AZ_TRAIT_OS_PLATFORM_APPLE
  1529. StartFixedCursorMode(this);
  1530. #endif
  1531. m_bCursorHidden = true;
  1532. }
  1533. //////////////////////////////////////////////////////////////////////////
  1534. void EditorViewportWidget::ShowCursor()
  1535. {
  1536. if (!m_bCursorHidden || !gSettings.viewports.bHideMouseCursorWhenCaptured)
  1537. {
  1538. return;
  1539. }
  1540. #if AZ_TRAIT_OS_PLATFORM_APPLE
  1541. StopFixedCursorMode();
  1542. #endif
  1543. qApp->restoreOverrideCursor();
  1544. m_bCursorHidden = false;
  1545. }
  1546. //////////////////////////////////////////////////////////////////////////
  1547. void EditorViewportWidget::PushDisableRendering()
  1548. {
  1549. ++m_disableRenderingCount;
  1550. }
  1551. //////////////////////////////////////////////////////////////////////////
  1552. void EditorViewportWidget::PopDisableRendering()
  1553. {
  1554. assert(m_disableRenderingCount >= 1);
  1555. --m_disableRenderingCount;
  1556. }
  1557. //////////////////////////////////////////////////////////////////////////
  1558. bool EditorViewportWidget::IsRenderingDisabled() const
  1559. {
  1560. return m_disableRenderingCount > 0;
  1561. }
  1562. //////////////////////////////////////////////////////////////////////////
  1563. QPoint EditorViewportWidget::WidgetToViewport(const QPoint& point) const
  1564. {
  1565. return point * WidgetToViewportFactor();
  1566. }
  1567. QPoint EditorViewportWidget::ViewportToWidget(const QPoint& point) const
  1568. {
  1569. return point / WidgetToViewportFactor();
  1570. }
  1571. //////////////////////////////////////////////////////////////////////////
  1572. QSize EditorViewportWidget::WidgetToViewport(const QSize& size) const
  1573. {
  1574. return size * WidgetToViewportFactor();
  1575. }
  1576. //////////////////////////////////////////////////////////////////////////
  1577. double EditorViewportWidget::WidgetToViewportFactor() const
  1578. {
  1579. #if defined(AZ_PLATFORM_WINDOWS)
  1580. // Needed for high DPI mode on windows
  1581. return devicePixelRatioF();
  1582. #else
  1583. return 1.0;
  1584. #endif
  1585. }
  1586. //////////////////////////////////////////////////////////////////////////
  1587. void EditorViewportWidget::BeginUndoTransaction()
  1588. {
  1589. PushDisableRendering();
  1590. }
  1591. //////////////////////////////////////////////////////////////////////////
  1592. void EditorViewportWidget::EndUndoTransaction()
  1593. {
  1594. PopDisableRendering();
  1595. Update();
  1596. }
  1597. void* EditorViewportWidget::GetSystemCursorConstraintWindow() const
  1598. {
  1599. // Even when the mouse cursor is not in a constrained mode, we still return the viewport as the constraint window,
  1600. // so that the engine's mouse coordinates will be normalized to the editor viewport rather than the entire application window.
  1601. // This ensures that viewport mouse interactions are in the correct 2D coordinate space, for example when using ImGuiManager's
  1602. // debug tools.
  1603. return renderOverlayHWND();
  1604. }
  1605. void EditorViewportWidget::BuildDragDropContext(
  1606. AzQtComponents::ViewportDragContext& context, const AzFramework::ViewportId viewportId, const QPoint& point)
  1607. {
  1608. QtViewport::BuildDragDropContext(context, viewportId, point);
  1609. }
  1610. void EditorViewportWidget::RestoreViewportAfterGameMode()
  1611. {
  1612. Matrix34 preGameModeViewTM = m_preGameModeViewTM;
  1613. QString text = QString(
  1614. tr("When leaving \" Game Mode \" the engine will automatically restore your camera position to the default position before you "
  1615. "had entered Game mode.<br/><br/><small>If you dislike this setting you can always change this anytime in the global "
  1616. "preferences.</small><br/><br/>"));
  1617. QString restoreOnExitGameModePopupDisabledRegKey("Editor/AutoHide/ViewportCameraRestoreOnExitGameMode");
  1618. // Read the popup disabled registry value
  1619. QSettings settings;
  1620. QVariant restoreOnExitGameModePopupDisabledRegValue = settings.value(restoreOnExitGameModePopupDisabledRegKey);
  1621. // Has the user previously disabled being asked about restoring the camera on exiting game mode?
  1622. if (restoreOnExitGameModePopupDisabledRegValue.isNull())
  1623. {
  1624. // No, ask them now
  1625. QMessageBox messageBox(QMessageBox::Question, "O3DE", text, QMessageBox::StandardButtons(QMessageBox::No | QMessageBox::Yes), this);
  1626. messageBox.setDefaultButton(QMessageBox::Yes);
  1627. QCheckBox* checkBox = new QCheckBox(QStringLiteral("Do not show this message again"));
  1628. checkBox->setChecked(true);
  1629. messageBox.setCheckBox(checkBox);
  1630. // Unconstrain the system cursor and make it visible before we show the dialog box, otherwise the user can't see the cursor.
  1631. AzFramework::InputSystemCursorRequestBus::Event(
  1632. AzFramework::InputDeviceMouse::Id, &AzFramework::InputSystemCursorRequests::SetSystemCursorState,
  1633. AzFramework::SystemCursorState::UnconstrainedAndVisible);
  1634. int response = messageBox.exec();
  1635. if (checkBox->isChecked())
  1636. {
  1637. settings.setValue(restoreOnExitGameModePopupDisabledRegKey, response);
  1638. }
  1639. // Update the value only if the popup hasn't previously been disabled and the value has changed
  1640. bool newSetting = (response == QMessageBox::Yes);
  1641. if (newSetting != GetIEditor()->GetEditorSettings()->restoreViewportCamera)
  1642. {
  1643. GetIEditor()->GetEditorSettings()->restoreViewportCamera = newSetting;
  1644. GetIEditor()->GetEditorSettings()->Save();
  1645. }
  1646. }
  1647. bool restoreViewportCamera = GetIEditor()->GetEditorSettings()->restoreViewportCamera;
  1648. if (restoreViewportCamera)
  1649. {
  1650. SetViewTM(preGameModeViewTM);
  1651. }
  1652. else
  1653. {
  1654. AZ_Warning("CryLegacy", false, "Not restoring the editor viewport camera is currently unsupported");
  1655. SetViewTM(preGameModeViewTM);
  1656. }
  1657. }
  1658. void EditorViewportWidget::UpdateScene()
  1659. {
  1660. auto sceneSystem = AzFramework::SceneSystemInterface::Get();
  1661. if (sceneSystem)
  1662. {
  1663. AZStd::shared_ptr<AzFramework::Scene> mainScene = sceneSystem->GetScene(AzFramework::Scene::MainSceneName);
  1664. if (mainScene)
  1665. {
  1666. AZ::RPI::SceneNotificationBus::Handler::BusDisconnect();
  1667. m_renderViewport->SetScene(mainScene);
  1668. auto viewportContext = m_renderViewport->GetViewportContext();
  1669. AZ::RPI::SceneNotificationBus::Handler::BusConnect(viewportContext->GetRenderScene()->GetId());
  1670. // Don't enable the render pipeline until a level has been loaded
  1671. // Also show/hide the RenderViewportWidget accordingly so that we get the
  1672. // expected gradient background when no level is loaded
  1673. auto renderPipeline = viewportContext->GetCurrentPipeline();
  1674. if (renderPipeline)
  1675. {
  1676. if (GetIEditor()->IsLevelLoaded())
  1677. {
  1678. m_renderViewport->show();
  1679. renderPipeline->AddToRenderTick();
  1680. }
  1681. else
  1682. {
  1683. m_renderViewport->hide();
  1684. renderPipeline->RemoveFromRenderTick();
  1685. }
  1686. }
  1687. }
  1688. }
  1689. }
  1690. void EditorViewportWidget::SetAsActiveViewport()
  1691. {
  1692. auto viewportContextManager = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  1693. const AZ::Name defaultContextName = viewportContextManager->GetDefaultViewportContextName();
  1694. // If another viewport was active before, restore its name to its per-ID one.
  1695. if (m_pPrimaryViewport && m_pPrimaryViewport != this && m_pPrimaryViewport->m_renderViewport)
  1696. {
  1697. auto viewportContext = m_pPrimaryViewport->m_renderViewport->GetViewportContext();
  1698. if (viewportContext)
  1699. {
  1700. // Remove the old viewport's camera from the stack, as it's no longer the owning viewport
  1701. viewportContextManager->PopViewGroup(defaultContextName, viewportContext->GetViewGroup());
  1702. viewportContextManager->RenameViewportContext(viewportContext, defaultContextName);
  1703. }
  1704. }
  1705. m_pPrimaryViewport = this;
  1706. if (m_renderViewport)
  1707. {
  1708. auto viewportContext = m_renderViewport->GetViewportContext();
  1709. if (viewportContext)
  1710. {
  1711. // Push our camera onto the default viewport's view stack to preserve camera state continuity
  1712. // Other views can still be pushed on top of our view for e.g. game mode
  1713. viewportContextManager->RenameViewportContext(viewportContext, defaultContextName);
  1714. viewportContextManager->PushViewGroup(defaultContextName, viewportContext->GetViewGroup());
  1715. }
  1716. }
  1717. }
  1718. void EditorViewportSettings::Connect(const AzFramework::ViewportId viewportId)
  1719. {
  1720. AzToolsFramework::ViewportInteraction::ViewportSettingsRequestBus::Handler::BusConnect(viewportId);
  1721. }
  1722. void EditorViewportSettings::Disconnect()
  1723. {
  1724. AzToolsFramework::ViewportInteraction::ViewportSettingsRequestBus::Handler::BusDisconnect();
  1725. }
  1726. bool EditorViewportSettings::GridSnappingEnabled() const
  1727. {
  1728. return SandboxEditor::GridSnappingEnabled();
  1729. }
  1730. float EditorViewportSettings::GridSize() const
  1731. {
  1732. return SandboxEditor::GridSnappingSize();
  1733. }
  1734. bool EditorViewportSettings::ShowGrid() const
  1735. {
  1736. return SandboxEditor::ShowingGrid();
  1737. }
  1738. bool EditorViewportSettings::AngleSnappingEnabled() const
  1739. {
  1740. return SandboxEditor::AngleSnappingEnabled();
  1741. }
  1742. float EditorViewportSettings::AngleStep() const
  1743. {
  1744. return SandboxEditor::AngleSnappingSize();
  1745. }
  1746. float EditorViewportSettings::ManipulatorLineBoundWidth() const
  1747. {
  1748. return SandboxEditor::ManipulatorLineBoundWidth();
  1749. }
  1750. float EditorViewportSettings::ManipulatorCircleBoundWidth() const
  1751. {
  1752. return SandboxEditor::ManipulatorCircleBoundWidth();
  1753. }
  1754. bool EditorViewportSettings::StickySelectEnabled() const
  1755. {
  1756. return SandboxEditor::StickySelectEnabled();
  1757. }
  1758. AZ::Vector3 EditorViewportSettings::DefaultEditorCameraPosition() const
  1759. {
  1760. return SandboxEditor::CameraDefaultEditorPosition();
  1761. }
  1762. AZ::Vector2 EditorViewportSettings::DefaultEditorCameraOrientation() const
  1763. {
  1764. return SandboxEditor::CameraDefaultEditorOrientation();
  1765. }
  1766. bool EditorViewportSettings::IconsVisible() const
  1767. {
  1768. return AzToolsFramework::IconsVisible();
  1769. }
  1770. bool EditorViewportSettings::HelpersVisible() const
  1771. {
  1772. return AzToolsFramework::HelpersVisible();
  1773. }
  1774. bool EditorViewportSettings::OnlyShowHelpersForSelectedEntities() const
  1775. {
  1776. return AzToolsFramework::OnlyShowHelpersForSelectedEntities();
  1777. }
  1778. AZ_CVAR_EXTERNED(bool, ed_previewGameInFullscreen_once);
  1779. bool EditorViewportWidget::ShouldPreviewFullscreen() const
  1780. {
  1781. CLayoutWnd* layout = GetIEditor()->GetViewManager()->GetLayout();
  1782. if (!layout)
  1783. {
  1784. AZ_Assert(false, "CRenderViewport: No View Manager layout");
  1785. return false;
  1786. }
  1787. // Doesn't work with split layout
  1788. if (layout->GetLayout() != EViewLayout::ET_Layout0)
  1789. {
  1790. return false;
  1791. }
  1792. // If level not loaded, don't preview in fullscreen (preview shouldn't work at all without a level, but it does)
  1793. if (auto ge = GetIEditor()->GetGameEngine())
  1794. {
  1795. if (!ge->IsLevelLoaded())
  1796. {
  1797. return false;
  1798. }
  1799. }
  1800. // Check 'ed_previewGameInFullscreen_once'
  1801. if (ed_previewGameInFullscreen_once)
  1802. {
  1803. ed_previewGameInFullscreen_once = false;
  1804. return true;
  1805. }
  1806. else
  1807. {
  1808. return false;
  1809. }
  1810. }
  1811. void EditorViewportWidget::StartFullscreenPreview()
  1812. {
  1813. AZ_Assert(!m_inFullscreenPreview, "EditorViewportWidget::StartFullscreenPreview called when already in full screen preview");
  1814. m_inFullscreenPreview = true;
  1815. // Pick the screen on which the main window lies to use as the screen for the full screen preview
  1816. const QScreen* screen = MainWindow::instance()->screen();
  1817. const QRect screenGeometry = screen->geometry();
  1818. // Unparent this and show it, which turns it into a free floating window
  1819. // Also set style to frameless and disable resizing by user
  1820. setParent(nullptr);
  1821. setWindowFlag(Qt::FramelessWindowHint, true);
  1822. setWindowFlag(Qt::MSWindowsFixedSizeDialogHint, true);
  1823. setFixedSize(screenGeometry.size());
  1824. move(QPoint(screenGeometry.x(), screenGeometry.y()));
  1825. showMaximized();
  1826. // This must be done after unparenting this widget above
  1827. MainWindow::instance()->hide();
  1828. }
  1829. void EditorViewportWidget::StopFullscreenPreview()
  1830. {
  1831. AZ_Assert(m_inFullscreenPreview, "EditorViewportWidget::StartFullscreenPreview called when not in full screen preview");
  1832. m_inFullscreenPreview = false;
  1833. // Unset frameless window flags
  1834. setWindowFlag(Qt::FramelessWindowHint, false);
  1835. setWindowFlag(Qt::MSWindowsFixedSizeDialogHint, false);
  1836. // Unset fixed size (note that 50x50 is the minimum set in the constructor)
  1837. setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
  1838. setMinimumSize(50, 50);
  1839. // Attach this viewport to the primary view pane (whose index is 0).
  1840. if (CLayoutWnd* layout = GetIEditor()->GetViewManager()->GetLayout())
  1841. {
  1842. if (CLayoutViewPane* viewPane = layout->GetViewPaneByIndex(0))
  1843. {
  1844. // Force-reattach this viewport to its view pane by first detaching
  1845. viewPane->DetachViewport();
  1846. viewPane->AttachViewport(this);
  1847. // Set the main widget of the layout, which causes this widgets size to be bound to the layout
  1848. // and the viewport title bar to be displayed
  1849. layout->SetMainWidget(viewPane);
  1850. }
  1851. else
  1852. {
  1853. AZ_Assert(false, "CRenderViewport: No view pane with ID 0 (primary view pane)");
  1854. }
  1855. }
  1856. else
  1857. {
  1858. AZ_Assert(false, "CRenderViewport: No View Manager layout");
  1859. }
  1860. // Set this as the selected viewport
  1861. GetIEditor()->GetViewManager()->SelectViewport(this);
  1862. // Show this widget (setting flags may hide it)
  1863. showNormal();
  1864. // Show the main window
  1865. MainWindow::instance()->show();
  1866. }
  1867. AZStd::optional<AzFramework::ViewportBorderPadding> EditorViewportWidget::GetViewportBorderPadding() const
  1868. {
  1869. if (auto viewportEditorModeTracker = AZ::Interface<AzToolsFramework::ViewportEditorModeTrackerInterface>::Get())
  1870. {
  1871. auto viewportEditorModes = viewportEditorModeTracker->GetViewportEditorModes({ AzToolsFramework::GetEntityContextId() });
  1872. if (viewportEditorModes->IsModeActive(AzToolsFramework::ViewportEditorMode::Focus) ||
  1873. viewportEditorModes->IsModeActive(AzToolsFramework::ViewportEditorMode::Component))
  1874. {
  1875. AzFramework::ViewportBorderPadding viewportBorderPadding = {};
  1876. viewportBorderPadding.m_top = AzToolsFramework::ViewportUi::ViewportUiTopBorderSize;
  1877. viewportBorderPadding.m_left = AzToolsFramework::ViewportUi::ViewportUiLeftRightBottomBorderSize;
  1878. viewportBorderPadding.m_right = AzToolsFramework::ViewportUi::ViewportUiLeftRightBottomBorderSize;
  1879. viewportBorderPadding.m_bottom = AzToolsFramework::ViewportUi::ViewportUiLeftRightBottomBorderSize;
  1880. return viewportBorderPadding;
  1881. }
  1882. }
  1883. return AZStd::nullopt;
  1884. }
  1885. #include <moc_EditorViewportWidget.cpp>