IEditorImpl.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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 : CEditorImpl class implementation.
  9. #include "EditorDefs.h"
  10. #include "IEditorImpl.h"
  11. #include <EditorCommonAPI.h>
  12. // Qt
  13. #include <QByteArray>
  14. // AzCore
  15. #include <AzCore/IO/Path/Path.h>
  16. #include <AzCore/JSON/document.h>
  17. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  18. #include <AzCore/Utils/Utils.h>
  19. #if defined(AZ_PLATFORM_MAC)
  20. #include <AzCore/Utils/SystemUtilsApple_Platform.h>
  21. #endif
  22. // AzFramework
  23. #include <AzFramework/Terrain/TerrainDataRequestBus.h>
  24. // AzToolsFramework
  25. #include <AzToolsFramework/UI/UICore/WidgetHelpers.h>
  26. #include <AzToolsFramework/API/EditorPythonRunnerRequestsBus.h>
  27. // AzQtComponents
  28. #include <AzQtComponents/Components/Widgets/ColorPicker.h>
  29. #include <AzQtComponents/Utilities/Conversions.h>
  30. // Editor
  31. #include "CryEdit.h"
  32. #include "PluginManager.h"
  33. #include "ViewManager.h"
  34. #include "DisplaySettings.h"
  35. #include "LevelIndependentFileMan.h"
  36. #include "TrackView/TrackViewSequenceManager.h"
  37. #include "AnimationContext.h"
  38. #include "GameEngine.h"
  39. #include "ToolBox.h"
  40. #include "MainWindow.h"
  41. #include "Settings.h"
  42. #include "EditorFileMonitor.h"
  43. #include "MainStatusBar.h"
  44. #include "Util/FileUtil_impl.h"
  45. #include "Editor/AssetDatabase/AssetDatabaseLocationListener.h"
  46. #include "Editor/AzAssetBrowser/AzAssetBrowserRequestHandler.h"
  47. #include "Editor/AssetEditor/AssetEditorRequestsHandler.h"
  48. #include "Core/QtEditorApplication.h" // for Editor::EditorQtApplication
  49. static CCryEditDoc * theDocument;
  50. #include <QMimeData>
  51. #include <QMessageBox>
  52. #include <QProcess>
  53. #if defined(EXTERNAL_CRASH_REPORTING)
  54. #include <ToolsCrashHandler.h>
  55. #endif
  56. #ifndef VERIFY
  57. #define VERIFY(EXPRESSION) { auto e = EXPRESSION; assert(e); }
  58. #endif
  59. const char* CEditorImpl::m_crashLogFileName = "SessionStatus/editor_statuses.json";
  60. CEditorImpl::CEditorImpl()
  61. : m_pSystem(nullptr)
  62. , m_pFileUtil(nullptr)
  63. , m_pCommandManager(nullptr)
  64. , m_pPluginManager(nullptr)
  65. , m_pViewManager(nullptr)
  66. , m_pUndoManager(nullptr)
  67. , m_selectedAxis(AXIS_TERRAIN)
  68. , m_bUpdates(true)
  69. , m_bTerrainAxisIgnoreObjects(false)
  70. , m_pDisplaySettings(nullptr)
  71. , m_bSelectionLocked(true)
  72. , m_pGameEngine(nullptr)
  73. , m_pAnimationContext(nullptr)
  74. , m_pSequenceManager(nullptr)
  75. , m_pToolBoxManager(nullptr)
  76. , m_pMusicManager(nullptr)
  77. , m_pErrorReport(nullptr)
  78. , m_pLasLoadedLevelErrorReport(nullptr)
  79. , m_pSelectionTreeManager(nullptr)
  80. , m_pConsoleSync(nullptr)
  81. , m_pSettingsManager(nullptr)
  82. , m_pLevelIndependentFileMan(nullptr)
  83. , m_bShowStatusText(true)
  84. , m_bInitialized(false)
  85. , m_bExiting(false)
  86. , m_QtApplication(static_cast<Editor::EditorQtApplication*>(qApp))
  87. {
  88. // note that this is a call into EditorCore.dll, which stores the g_pEditorPointer for all shared modules that share EditorCore.dll
  89. // this means that they don't need to do SetIEditor(...) themselves and its available immediately
  90. SetIEditor(this);
  91. m_pFileUtil = new CFileUtil_impl();
  92. m_pLevelIndependentFileMan = new CLevelIndependentFileMan;
  93. SetPrimaryCDFolder();
  94. gSettings.Load();
  95. m_pErrorReport = new CErrorReport;
  96. m_pCommandManager = new CEditorCommandManager;
  97. m_pEditorFileMonitor.reset(new CEditorFileMonitor());
  98. m_pDisplaySettings = new CDisplaySettings;
  99. m_pDisplaySettings->LoadRegistry();
  100. m_pPluginManager = new CPluginManager;
  101. m_pViewManager = new CViewManager;
  102. m_pUndoManager = new CUndoManager;
  103. m_pToolBoxManager = new CToolBoxManager;
  104. m_pSequenceManager = new CTrackViewSequenceManager;
  105. m_pAnimationContext = new CAnimationContext;
  106. DetectVersion();
  107. RegisterTools();
  108. m_pAssetDatabaseLocationListener = nullptr;
  109. m_pAssetBrowserRequestHandler = nullptr;
  110. m_assetEditorRequestsHandler = nullptr;
  111. if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
  112. {
  113. if (AZ::IO::FixedMaxPath crashLogPath; settingsRegistry->Get(crashLogPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_ProjectUserPath))
  114. {
  115. crashLogPath /= m_crashLogFileName;
  116. AZ::IO::SystemFile::CreateDir(crashLogPath.ParentPath().FixedMaxPathString().c_str());
  117. QFile::setPermissions(crashLogPath.c_str(), QFileDevice::ReadOther | QFileDevice::WriteOther);
  118. }
  119. }
  120. }
  121. void CEditorImpl::Initialize()
  122. {
  123. #if defined(EXTERNAL_CRASH_REPORTING)
  124. CrashHandler::ToolsCrashHandler::InitCrashHandler("Editor", {});
  125. #endif
  126. // Must be set before QApplication is initialized, so that we support HighDpi monitors, like the Retina displays
  127. // on Windows 10
  128. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  129. QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
  130. // Prevents (native) sibling widgets from causing problems with docked QOpenGLWidgets on Windows
  131. // The problem is due to native widgets ending up with pixel formats that are incompatible with the GL pixel format
  132. // (generally due to a lack of an alpha channel). This blocks the creation of a shared GL context.
  133. // And on macOS it prevents all kinds of bugs related to native widgets, specially regarding toolbars (duplicate toolbars, artifacts, crashes).
  134. QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
  135. // Activate QT immediately so that its available as soon as CEditorImpl is (and thus GetIEditor())
  136. InitializeEditorCommon(GetIEditor());
  137. }
  138. //The only purpose of that function is to be called at the very begining of the shutdown sequence so that we can instrument and track
  139. //how many crashes occur while shutting down
  140. void CEditorImpl::OnBeginShutdownSequence()
  141. {
  142. }
  143. void CEditorImpl::OnEarlyExitShutdownSequence()
  144. {
  145. }
  146. void CEditorImpl::Uninitialize()
  147. {
  148. if (m_pSystem)
  149. {
  150. UninitializeEditorCommonISystem(m_pSystem);
  151. }
  152. UninitializeEditorCommon();
  153. }
  154. void CEditorImpl::UnloadPlugins()
  155. {
  156. AZStd::scoped_lock lock(m_pluginMutex);
  157. // Flush core buses. We're about to unload DLLs and need to ensure we don't have module-owned functions left behind.
  158. AZ::Data::AssetBus::ExecuteQueuedEvents();
  159. AZ::TickBus::ExecuteQueuedEvents();
  160. // Send this message to ensure that any widgets queued for deletion will get deleted before their
  161. // plugin containing their vtable is unloaded. If not, access violations can occur
  162. QCoreApplication::sendPostedEvents(Q_NULLPTR, QEvent::DeferredDelete);
  163. GetPluginManager()->ReleaseAllPlugins();
  164. GetPluginManager()->UnloadAllPlugins();
  165. }
  166. void CEditorImpl::LoadPlugins()
  167. {
  168. AZStd::scoped_lock lock(m_pluginMutex);
  169. constexpr const char* editorPluginFolder = "EditorPlugins";
  170. AZ::IO::FixedMaxPath pluginsPath;
  171. #if defined(AZ_PLATFORM_MAC)
  172. char maxPathBuffer[AZ::IO::MaxPathLength];
  173. if (auto appBundlePathOutcome = AZ::SystemUtilsApple::GetPathToApplicationBundle(maxPathBuffer);
  174. appBundlePathOutcome)
  175. {
  176. AZ::IO::FixedMaxPath bundleRootDirectory = appBundlePathOutcome.GetValue();
  177. // the bundle directory includes Editor.app so we want the parent directory
  178. bundleRootDirectory = (bundleRootDirectory / "..").LexicallyNormal();
  179. pluginsPath = bundleRootDirectory / editorPluginFolder;
  180. }
  181. #endif
  182. if (pluginsPath.empty())
  183. {
  184. // Use the executable directory as the starting point for the EditorPlugins path
  185. AZ::IO::FixedMaxPath executableDirectory = AZ::Utils::GetExecutableDirectory();
  186. pluginsPath = executableDirectory / editorPluginFolder;
  187. }
  188. // error handling for invalid paths is handled in LoadPlugins
  189. GetPluginManager()->LoadPlugins(pluginsPath.c_str());
  190. }
  191. CEditorImpl::~CEditorImpl()
  192. {
  193. gSettings.Save();
  194. m_bExiting = true; // Can't save level after this point (while Crash)
  195. SAFE_DELETE(m_pViewManager)
  196. SAFE_DELETE(m_pPluginManager)
  197. SAFE_DELETE(m_pAnimationContext) // relies on undo manager
  198. SAFE_DELETE(m_pUndoManager)
  199. if (m_pDisplaySettings)
  200. {
  201. m_pDisplaySettings->SaveRegistry();
  202. }
  203. SAFE_DELETE(m_pDisplaySettings)
  204. SAFE_DELETE(m_pToolBoxManager)
  205. SAFE_DELETE(m_pCommandManager)
  206. SAFE_DELETE(m_pLasLoadedLevelErrorReport)
  207. SAFE_DELETE(m_pSettingsManager);
  208. SAFE_DELETE(m_pAssetDatabaseLocationListener);
  209. SAFE_DELETE(m_pAssetBrowserRequestHandler);
  210. SAFE_DELETE(m_assetEditorRequestsHandler);
  211. // Game engine should be among the last things to be destroyed.
  212. SAFE_DELETE(m_pLevelIndependentFileMan);
  213. SAFE_DELETE(m_pGameEngine);
  214. // The error report must be destroyed after the game, as the engine
  215. // refers to the error report and the game destroys the engine.
  216. SAFE_DELETE(m_pErrorReport);
  217. SAFE_DELETE(m_pFileUtil); // Vladimir@Conffx
  218. }
  219. void CEditorImpl::SetPrimaryCDFolder()
  220. {
  221. QString szFolder = qApp->applicationDirPath();
  222. QDir::setCurrent(szFolder);
  223. }
  224. void CEditorImpl::SetGameEngine(CGameEngine* ge)
  225. {
  226. m_pAssetDatabaseLocationListener = new AssetDatabase::AssetDatabaseLocationListener();
  227. m_pAssetBrowserRequestHandler = new AzAssetBrowserRequestHandler();
  228. m_assetEditorRequestsHandler = aznew AssetEditorRequestsHandler();
  229. m_pSystem = ge->GetSystem();
  230. m_pGameEngine = ge;
  231. InitializeEditorCommonISystem(m_pSystem);
  232. m_templateRegistry.LoadTemplates("Editor");
  233. m_pAnimationContext->Init();
  234. }
  235. void CEditorImpl::RegisterTools()
  236. {
  237. }
  238. void CEditorImpl::ExecuteCommand(const char* sCommand, ...)
  239. {
  240. va_list args;
  241. va_start(args, sCommand);
  242. ExecuteCommand(QString::asprintf(sCommand, args));
  243. va_end(args);
  244. }
  245. void CEditorImpl::ExecuteCommand(const QString& command)
  246. {
  247. m_pCommandManager->Execute(command.toUtf8().data());
  248. }
  249. void CEditorImpl::Update()
  250. {
  251. if (!m_bUpdates)
  252. {
  253. return;
  254. }
  255. // Make sure this is not called recursively
  256. m_bUpdates = false;
  257. if (IsInPreviewMode())
  258. {
  259. SetModifiedFlag(false);
  260. SetModifiedModule(eModifiedNothing);
  261. }
  262. m_bUpdates = true;
  263. }
  264. ISystem* CEditorImpl::GetSystem()
  265. {
  266. return m_pSystem;
  267. }
  268. CCryEditDoc* CEditorImpl::GetDocument() const
  269. {
  270. return theDocument;
  271. }
  272. bool CEditorImpl::IsLevelLoaded() const
  273. {
  274. return GetDocument() && GetDocument()->IsDocumentReady();
  275. }
  276. void CEditorImpl::SetDocument(CCryEditDoc* pDoc)
  277. {
  278. theDocument = pDoc;
  279. }
  280. void CEditorImpl::SetModifiedFlag(bool modified)
  281. {
  282. if (GetDocument() && GetDocument()->IsDocumentReady())
  283. {
  284. GetDocument()->SetModifiedFlag(modified);
  285. if (modified)
  286. {
  287. GetDocument()->SetLevelExported(false);
  288. }
  289. }
  290. }
  291. void CEditorImpl::SetModifiedModule(EModifiedModule eModifiedModule, bool boSet)
  292. {
  293. if (GetDocument())
  294. {
  295. GetDocument()->SetModifiedModules(eModifiedModule, boSet);
  296. }
  297. }
  298. bool CEditorImpl::IsLevelExported() const
  299. {
  300. CCryEditDoc* pDoc = GetDocument();
  301. if (pDoc)
  302. {
  303. return pDoc->IsLevelExported();
  304. }
  305. return false;
  306. }
  307. bool CEditorImpl::SetLevelExported(bool boExported)
  308. {
  309. if (GetDocument())
  310. {
  311. GetDocument()->SetLevelExported(boExported);
  312. return true;
  313. }
  314. return false;
  315. }
  316. bool CEditorImpl::IsModified()
  317. {
  318. if (GetDocument())
  319. {
  320. return GetDocument()->IsModified();
  321. }
  322. return false;
  323. }
  324. bool CEditorImpl::SaveDocument()
  325. {
  326. if (m_bExiting)
  327. {
  328. return false;
  329. }
  330. if (GetDocument())
  331. {
  332. return GetDocument()->Save();
  333. }
  334. else
  335. {
  336. return false;
  337. }
  338. }
  339. QString CEditorImpl::GetPrimaryCDFolder()
  340. {
  341. return m_primaryCDFolder;
  342. }
  343. QString CEditorImpl::GetLevelFolder()
  344. {
  345. return GetGameEngine()->GetLevelPath();
  346. }
  347. QString CEditorImpl::GetLevelName()
  348. {
  349. m_levelNameBuffer = GetGameEngine()->GetLevelName();
  350. return m_levelNameBuffer;
  351. }
  352. QString CEditorImpl::GetLevelDataFolder()
  353. {
  354. return Path::AddPathSlash(Path::AddPathSlash(GetGameEngine()->GetLevelPath()) + "LevelData");
  355. }
  356. QString CEditorImpl::GetSearchPath(EEditorPathName path)
  357. {
  358. return gSettings.searchPaths[path][0];
  359. }
  360. QString CEditorImpl::GetResolvedUserFolder()
  361. {
  362. m_userFolder = Path::GetResolvedUserSandboxFolder();
  363. return m_userFolder;
  364. }
  365. void CEditorImpl::SetDataModified()
  366. {
  367. GetDocument()->SetModifiedFlag(true);
  368. }
  369. void CEditorImpl::SetStatusText(const QString& pszString)
  370. {
  371. if (m_bShowStatusText && GetMainStatusBar())
  372. {
  373. GetMainStatusBar()->SetStatusText(pszString);
  374. }
  375. }
  376. IMainStatusBar* CEditorImpl::GetMainStatusBar()
  377. {
  378. return MainWindow::instance()->StatusBar();
  379. }
  380. void CEditorImpl::SetAxisConstraints(AxisConstrains axisFlags)
  381. {
  382. m_selectedAxis = axisFlags;
  383. m_pViewManager->SetAxisConstrain(axisFlags);
  384. SetTerrainAxisIgnoreObjects(false);
  385. // Update all views.
  386. UpdateViews(eUpdateObjects, nullptr);
  387. }
  388. AxisConstrains CEditorImpl::GetAxisConstrains()
  389. {
  390. return m_selectedAxis;
  391. }
  392. void CEditorImpl::SetTerrainAxisIgnoreObjects(bool bIgnore)
  393. {
  394. m_bTerrainAxisIgnoreObjects = bIgnore;
  395. }
  396. bool CEditorImpl::IsTerrainAxisIgnoreObjects()
  397. {
  398. return m_bTerrainAxisIgnoreObjects;
  399. }
  400. CSettingsManager* CEditorImpl::GetSettingsManager()
  401. {
  402. // Do not go any further before XML class is ready to use
  403. if (!gEnv)
  404. {
  405. return nullptr;
  406. }
  407. if (!GetISystem())
  408. {
  409. return nullptr;
  410. }
  411. if (!m_pSettingsManager)
  412. {
  413. m_pSettingsManager = new CSettingsManager(eSettingsManagerMemoryStorage);
  414. }
  415. return m_pSettingsManager;
  416. }
  417. CViewManager* CEditorImpl::GetViewManager()
  418. {
  419. return m_pViewManager;
  420. }
  421. CViewport* CEditorImpl::GetActiveView()
  422. {
  423. MainWindow* mainWindow = MainWindow::instance();
  424. if (mainWindow)
  425. {
  426. CLayoutViewPane* viewPane = mainWindow->GetActiveView();
  427. if (viewPane)
  428. {
  429. return qobject_cast<QtViewport*>(viewPane->GetViewport());
  430. }
  431. }
  432. return nullptr;
  433. }
  434. void CEditorImpl::SetActiveView(CViewport* viewport)
  435. {
  436. m_pViewManager->SelectViewport(viewport);
  437. }
  438. void CEditorImpl::UpdateViews(int flags, const AABB* updateRegion)
  439. {
  440. AABB prevRegion = m_pViewManager->GetUpdateRegion();
  441. if (updateRegion)
  442. {
  443. m_pViewManager->SetUpdateRegion(*updateRegion);
  444. }
  445. m_pViewManager->UpdateViews(flags);
  446. if (updateRegion)
  447. {
  448. m_pViewManager->SetUpdateRegion(prevRegion);
  449. }
  450. }
  451. void CEditorImpl::ReloadTrackView()
  452. {
  453. Notify(eNotify_OnReloadTrackView);
  454. }
  455. void CEditorImpl::ResetViews()
  456. {
  457. m_pViewManager->ResetViews();
  458. m_pDisplaySettings->SetRenderFlags(m_pDisplaySettings->GetRenderFlags());
  459. }
  460. IEditorFileMonitor* CEditorImpl::GetFileMonitor()
  461. {
  462. return m_pEditorFileMonitor.get();
  463. }
  464. float CEditorImpl::GetTerrainElevation(float x, float y)
  465. {
  466. float terrainElevation = AzFramework::Terrain::TerrainDataRequests::GetDefaultTerrainHeight();
  467. AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(terrainElevation
  468. , &AzFramework::Terrain::TerrainDataRequests::GetHeightFromFloats, x, y,
  469. AzFramework::Terrain::TerrainDataRequests::Sampler::BILINEAR, nullptr);
  470. return terrainElevation;
  471. }
  472. const QColor& CEditorImpl::GetColorByName(const QString& name)
  473. {
  474. return m_QtApplication->GetColorByName(name);
  475. }
  476. const QtViewPane* CEditorImpl::OpenView(QString sViewClassName, bool reuseOpened)
  477. {
  478. auto openMode = reuseOpened ? QtViewPane::OpenMode::None : QtViewPane::OpenMode::MultiplePanes;
  479. return QtViewPaneManager::instance()->OpenPane(sViewClassName, openMode);
  480. }
  481. QWidget* CEditorImpl::FindView(QString viewClassName)
  482. {
  483. return QtViewPaneManager::instance()->GetView(viewClassName);
  484. }
  485. // Intended to give a window focus only if it is currently open
  486. bool CEditorImpl::SetViewFocus(const char* sViewClassName)
  487. {
  488. QWidget* findWindow = FindView(sViewClassName);
  489. if (findWindow)
  490. {
  491. findWindow->setFocus(Qt::OtherFocusReason);
  492. return true;
  493. }
  494. return false;
  495. }
  496. bool CEditorImpl::SelectColor(QColor& color, QWidget* parent)
  497. {
  498. const AZ::Color c = AzQtComponents::fromQColor(color);
  499. AzQtComponents::ColorPicker dlg(AzQtComponents::ColorPicker::Configuration::RGB, tr("Select Color"), parent);
  500. dlg.setCurrentColor(c);
  501. dlg.setSelectedColor(c);
  502. if (dlg.exec() == QDialog::Accepted)
  503. {
  504. color = AzQtComponents::toQColor(dlg.currentColor());
  505. return true;
  506. }
  507. return false;
  508. }
  509. void CEditorImpl::SetInGameMode(bool inGame)
  510. {
  511. if (IsInSimulationMode())
  512. {
  513. return;
  514. }
  515. if (m_pGameEngine)
  516. {
  517. m_pGameEngine->RequestSetGameMode(inGame);
  518. }
  519. }
  520. bool CEditorImpl::IsInGameMode()
  521. {
  522. if (m_pGameEngine)
  523. {
  524. return m_pGameEngine->IsInGameMode();
  525. }
  526. return false;
  527. }
  528. bool CEditorImpl::IsInSimulationMode()
  529. {
  530. if (m_pGameEngine)
  531. {
  532. return m_pGameEngine->GetSimulationMode();
  533. }
  534. return false;
  535. }
  536. bool CEditorImpl::IsInTestMode()
  537. {
  538. return CCryEditApp::instance()->IsInTestMode();
  539. }
  540. bool CEditorImpl::IsInConsolewMode()
  541. {
  542. return CCryEditApp::instance()->IsInConsoleMode();
  543. }
  544. bool CEditorImpl::IsInLevelLoadTestMode()
  545. {
  546. return CCryEditApp::instance()->IsInLevelLoadTestMode();
  547. }
  548. bool CEditorImpl::IsInPreviewMode()
  549. {
  550. return CCryEditApp::instance()->IsInPreviewMode();
  551. }
  552. static AZStd::string SafeGetStringFromDocument(rapidjson::Document& projectCfg, const char* memberName)
  553. {
  554. if (projectCfg.HasMember(memberName) && projectCfg[memberName].IsString())
  555. {
  556. return projectCfg[memberName].GetString();
  557. }
  558. return "";
  559. }
  560. AZStd::string CEditorImpl::LoadProjectIdFromProjectData()
  561. {
  562. const char* MissingProjectId = "";
  563. // get the full path of the project.json
  564. AZStd::string fullPath;
  565. AZStd::string relPath("project.json");
  566. bool fullPathFound = false;
  567. using namespace AzToolsFramework;
  568. AssetSystemRequestBus::BroadcastResult(fullPathFound, &AssetSystemRequestBus::Events::GetFullSourcePathFromRelativeProductPath, relPath, fullPath);
  569. if (!fullPathFound)
  570. {
  571. return MissingProjectId;
  572. }
  573. QFile file(fullPath.c_str());
  574. if (!file.open(QIODevice::ReadOnly))
  575. {
  576. return MissingProjectId;
  577. }
  578. // Read the project.json file using its full path
  579. QByteArray fileContents = file.readAll();
  580. file.close();
  581. rapidjson::Document projectCfg;
  582. projectCfg.Parse(fileContents);
  583. if (!projectCfg.IsObject())
  584. {
  585. return MissingProjectId;
  586. }
  587. AZStd::string projectId = SafeGetStringFromDocument(projectCfg, "project_id");
  588. // if we don't have a valid projectId by now, it's not happening
  589. if (projectId.empty() || projectId[0] == '\0')
  590. {
  591. return MissingProjectId;
  592. }
  593. // get the project Id and project name from the project.json file
  594. QString projectName(SafeGetStringFromDocument(projectCfg, "project_name").data());
  595. QFileInfo fileInfo(fullPath.c_str());
  596. QDir folderDirectory = fileInfo.dir();
  597. // get the project name from the folder directory
  598. QString editorProjectName = folderDirectory.dirName();
  599. // if the project name in the file doesn't match the directory name, it probably means that this is
  600. // a copied project, and not safe to put any plain text into the projectId string
  601. if (editorProjectName.compare(projectName, Qt::CaseInsensitive) != 0)
  602. {
  603. return projectId;
  604. }
  605. // get the project Id generated by using the project name from the folder directory
  606. QByteArray editorProjectNameUtf8 = editorProjectName.toUtf8();
  607. AZ::Uuid id = AZ::Uuid::CreateName(editorProjectNameUtf8.constData());
  608. // The projects that Open 3D Engine ships with had their project IDs hand-generated based on the name of the level.
  609. // Therefore, if the UUID from the project name is the same as the UUID in the file, it's one of our projects
  610. // and we can therefore send the name back, making it easier for Metrics to determine which level it was.
  611. // We are checking to see if this is a project we ship with Open 3D Engine, and therefore we can unobfuscate non-customer information.
  612. if (id != AZ::Uuid(projectId.data()))
  613. {
  614. return projectId;
  615. }
  616. QByteArray projectNameUtf8 = projectName.toUtf8();
  617. projectId += " [";
  618. projectId += projectNameUtf8.constData();
  619. projectId += "]";
  620. return projectId;
  621. }
  622. void CEditorImpl::DetectVersion()
  623. {
  624. #if defined(AZ_PLATFORM_WINDOWS)
  625. char exe[_MAX_PATH];
  626. DWORD dwHandle;
  627. UINT len;
  628. wchar_t ver[1024 * 8];
  629. AZ::Utils::GetExecutablePath(exe, _MAX_PATH);
  630. AZStd::wstring exeW;
  631. AZStd::to_wstring(exeW, exe);
  632. int verSize = GetFileVersionInfoSizeW(exeW.c_str(), &dwHandle);
  633. if (verSize > 0)
  634. {
  635. GetFileVersionInfoW(exeW.c_str(), dwHandle, 1024 * 8, ver);
  636. VS_FIXEDFILEINFO* vinfo;
  637. VerQueryValueW(ver, L"\\", (void**)&vinfo, &len);
  638. m_fileVersion.v[0] = vinfo->dwFileVersionLS & 0xFFFF;
  639. m_fileVersion.v[1] = vinfo->dwFileVersionLS >> 16;
  640. m_fileVersion.v[2] = vinfo->dwFileVersionMS & 0xFFFF;
  641. m_fileVersion.v[3] = vinfo->dwFileVersionMS >> 16;
  642. m_productVersion.v[0] = vinfo->dwProductVersionLS & 0xFFFF;
  643. m_productVersion.v[1] = vinfo->dwProductVersionLS >> 16;
  644. m_productVersion.v[2] = vinfo->dwProductVersionMS & 0xFFFF;
  645. m_productVersion.v[3] = vinfo->dwProductVersionMS >> 16;
  646. }
  647. #else
  648. // This requires the application version to be set using QCoreApplication::setApplicationVersion, which isn't done yet.
  649. const QString version = qApp->applicationVersion();
  650. if (!version.isEmpty())
  651. {
  652. QByteArray versionBytes = version.toUtf8();
  653. m_fileVersion.Set(versionBytes.data());
  654. m_productVersion.Set(versionBytes.data());
  655. }
  656. #endif
  657. }
  658. XmlNodeRef CEditorImpl::FindTemplate(const QString& templateName)
  659. {
  660. return m_templateRegistry.FindTemplate(templateName);
  661. }
  662. void CEditorImpl::AddTemplate(const QString& templateName, XmlNodeRef& tmpl)
  663. {
  664. m_templateRegistry.AddTemplate(templateName, tmpl);
  665. }
  666. bool CEditorImpl::ExecuteConsoleApp(const QString& CommandLine, QString& OutputText, [[maybe_unused]] bool bNoTimeOut, bool bShowWindow)
  667. {
  668. CLogFile::FormatLine("Executing console application '%s'", CommandLine.toUtf8().data());
  669. QProcess process;
  670. if (bShowWindow)
  671. {
  672. #if defined(AZ_PLATFORM_WINDOWS)
  673. process.start("cmd.exe", { QString("/C %1").arg(CommandLine) });
  674. #elif defined(AZ_PLATFORM_LINUX)
  675. //KDAB_TODO
  676. #elif defined(AZ_PLATFORM_MAC)
  677. process.start("/usr/bin/osascript", { QString("-e 'tell application \"Terminal\" to do script \"%1\"'").arg(QString(CommandLine).replace("\"", "\\\"")) });
  678. #else
  679. process.start("/usr/bin/csh", { QString("-c \"%1\"'").arg(QString(CommandLine).replace("\"", "\\\"")) } );
  680. #endif
  681. }
  682. else
  683. {
  684. process.start(CommandLine, QStringList());
  685. }
  686. if (!process.waitForStarted())
  687. {
  688. return false;
  689. }
  690. // Wait for the process to finish
  691. process.waitForFinished();
  692. OutputText += process.readAllStandardOutput();
  693. OutputText += process.readAllStandardError();
  694. return true;
  695. }
  696. void CEditorImpl::BeginUndo()
  697. {
  698. if (m_pUndoManager)
  699. {
  700. m_pUndoManager->Begin();
  701. }
  702. }
  703. void CEditorImpl::RestoreUndo(bool undo)
  704. {
  705. if (m_pUndoManager)
  706. {
  707. m_pUndoManager->Restore(undo);
  708. }
  709. }
  710. void CEditorImpl::AcceptUndo(const QString& name)
  711. {
  712. if (m_pUndoManager)
  713. {
  714. m_pUndoManager->Accept(name);
  715. }
  716. }
  717. void CEditorImpl::CancelUndo()
  718. {
  719. if (m_pUndoManager)
  720. {
  721. m_pUndoManager->Cancel();
  722. }
  723. }
  724. void CEditorImpl::SuperBeginUndo()
  725. {
  726. if (m_pUndoManager)
  727. {
  728. m_pUndoManager->SuperBegin();
  729. }
  730. }
  731. void CEditorImpl::SuperAcceptUndo(const QString& name)
  732. {
  733. if (m_pUndoManager)
  734. {
  735. m_pUndoManager->SuperAccept(name);
  736. }
  737. }
  738. void CEditorImpl::SuperCancelUndo()
  739. {
  740. if (m_pUndoManager)
  741. {
  742. m_pUndoManager->SuperCancel();
  743. }
  744. }
  745. void CEditorImpl::SuspendUndo()
  746. {
  747. if (m_pUndoManager)
  748. {
  749. m_pUndoManager->Suspend();
  750. }
  751. }
  752. void CEditorImpl::ResumeUndo()
  753. {
  754. if (m_pUndoManager)
  755. {
  756. m_pUndoManager->Resume();
  757. }
  758. }
  759. void CEditorImpl::Undo()
  760. {
  761. if (m_pUndoManager)
  762. {
  763. m_pUndoManager->Undo();
  764. }
  765. }
  766. void CEditorImpl::Redo()
  767. {
  768. if (m_pUndoManager)
  769. {
  770. m_pUndoManager->Redo();
  771. }
  772. }
  773. bool CEditorImpl::IsUndoRecording()
  774. {
  775. if (m_pUndoManager)
  776. {
  777. return m_pUndoManager->IsUndoRecording();
  778. }
  779. return false;
  780. }
  781. bool CEditorImpl::IsUndoSuspended()
  782. {
  783. if (m_pUndoManager)
  784. {
  785. return m_pUndoManager->IsUndoSuspended();
  786. }
  787. return false;
  788. }
  789. void CEditorImpl::RecordUndo(IUndoObject* obj)
  790. {
  791. if (m_pUndoManager)
  792. {
  793. m_pUndoManager->RecordUndo(obj);
  794. }
  795. }
  796. bool CEditorImpl::FlushUndo(bool isShowMessage)
  797. {
  798. if (isShowMessage && m_pUndoManager && m_pUndoManager->IsHaveUndo() && QMessageBox::question(AzToolsFramework::GetActiveWindow(), QObject::tr("Flush Undo"), QObject::tr("After this operation undo will not be available! Are you sure you want to continue?")) != QMessageBox::Yes)
  799. {
  800. return false;
  801. }
  802. if (m_pUndoManager)
  803. {
  804. m_pUndoManager->Flush();
  805. }
  806. return true;
  807. }
  808. bool CEditorImpl::ClearLastUndoSteps(int steps)
  809. {
  810. if (!m_pUndoManager || !m_pUndoManager->IsHaveUndo())
  811. {
  812. return false;
  813. }
  814. m_pUndoManager->ClearUndoStack(steps);
  815. return true;
  816. }
  817. bool CEditorImpl::ClearRedoStack()
  818. {
  819. if (!m_pUndoManager || !m_pUndoManager->IsHaveRedo())
  820. {
  821. return false;
  822. }
  823. m_pUndoManager->ClearRedoStack();
  824. return true;
  825. }
  826. void CEditorImpl::SetConsoleVar(const char* var, float value)
  827. {
  828. ICVar* ivar = GetSystem()->GetIConsole()->GetCVar(var);
  829. if (ivar)
  830. {
  831. ivar->Set(value);
  832. }
  833. }
  834. float CEditorImpl::GetConsoleVar(const char* var)
  835. {
  836. ICVar* ivar = GetSystem()->GetIConsole()->GetCVar(var);
  837. if (ivar)
  838. {
  839. return ivar->GetFVal();
  840. }
  841. return 0;
  842. }
  843. CAnimationContext* CEditorImpl::GetAnimation()
  844. {
  845. return m_pAnimationContext;
  846. }
  847. CTrackViewSequenceManager* CEditorImpl::GetSequenceManager()
  848. {
  849. return m_pSequenceManager;
  850. }
  851. ITrackViewSequenceManager* CEditorImpl::GetSequenceManagerInterface()
  852. {
  853. return GetSequenceManager();
  854. }
  855. void CEditorImpl::StartLevelErrorReportRecording()
  856. {
  857. IErrorReport* errorReport = GetErrorReport();
  858. if (errorReport)
  859. {
  860. errorReport->Clear();
  861. errorReport->SetImmediateMode(false);
  862. errorReport->SetShowErrors(true);
  863. }
  864. }
  865. // Confetti Start: Leroy Sikkes
  866. void CEditorImpl::Notify(EEditorNotifyEvent event)
  867. {
  868. NotifyExcept(event, nullptr);
  869. }
  870. void CEditorImpl::NotifyExcept(EEditorNotifyEvent event, IEditorNotifyListener* listener)
  871. {
  872. if (m_bExiting)
  873. {
  874. return;
  875. }
  876. std::list<IEditorNotifyListener*>::iterator it = m_listeners.begin();
  877. while (it != m_listeners.end())
  878. {
  879. if (*it == listener)
  880. {
  881. it++;
  882. continue; // skip "except" listener
  883. }
  884. (*it++)->OnEditorNotifyEvent(event);
  885. }
  886. if (event == eNotify_OnInit)
  887. {
  888. REGISTER_COMMAND("py", CmdPy, 0, "Execute a Python code snippet.");
  889. }
  890. GetPluginManager()->NotifyPlugins(event);
  891. }
  892. // Confetti end: Leroy Sikkes
  893. void CEditorImpl::RegisterNotifyListener(IEditorNotifyListener* listener)
  894. {
  895. listener->m_bIsRegistered = true;
  896. stl::push_back_unique(m_listeners, listener);
  897. }
  898. void CEditorImpl::UnregisterNotifyListener(IEditorNotifyListener* listener)
  899. {
  900. m_listeners.remove(listener);
  901. listener->m_bIsRegistered = false;
  902. }
  903. void CEditorImpl::ShowStatusText(bool bEnable)
  904. {
  905. m_bShowStatusText = bEnable;
  906. }
  907. void CEditorImpl::ReduceMemory()
  908. {
  909. GetIEditor()->GetUndoManager()->ClearRedoStack();
  910. GetIEditor()->GetUndoManager()->ClearUndoStack();
  911. #if defined(AZ_PLATFORM_WINDOWS)
  912. HANDLE hHeap = GetProcessHeap();
  913. if (hHeap)
  914. {
  915. uint64 maxsize = (uint64)HeapCompact(hHeap, 0);
  916. CryLogAlways("Max Free Memory Block = %I64d Kb", maxsize / 1024);
  917. }
  918. #endif
  919. }
  920. ESystemConfigPlatform CEditorImpl::GetEditorConfigPlatform() const
  921. {
  922. return m_pSystem->GetConfigPlatform();
  923. }
  924. void CEditorImpl::InitFinished()
  925. {
  926. if (!m_bInitialized)
  927. {
  928. m_bInitialized = true;
  929. Notify(eNotify_OnInit);
  930. // Let system wide listeners know about this as well.
  931. GetISystem()->GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_EDITOR_ON_INIT, 0, 0);
  932. }
  933. }
  934. void CEditorImpl::ReloadTemplates()
  935. {
  936. m_templateRegistry.LoadTemplates("Editor");
  937. }
  938. void CEditorImpl::CmdPy(IConsoleCmdArgs* pArgs)
  939. {
  940. if (AzToolsFramework::EditorPythonRunnerRequestBus::HasHandlers())
  941. {
  942. // Execute the given script command.
  943. QString scriptCmd = pArgs->GetCommandLine();
  944. scriptCmd = scriptCmd.right(scriptCmd.length() - 2); // The part of the text after the 'py'
  945. scriptCmd = scriptCmd.trimmed();
  946. AzToolsFramework::EditorPythonRunnerRequestBus::Broadcast(&AzToolsFramework::EditorPythonRunnerRequestBus::Events::ExecuteByString, scriptCmd.toUtf8().data(), false);
  947. }
  948. else
  949. {
  950. AZ_Warning("python", false, "EditorPythonRunnerRequestBus has no handlers");
  951. }
  952. }
  953. // Vladimir@Conffx
  954. SSystemGlobalEnvironment* CEditorImpl::GetEnv()
  955. {
  956. assert(gEnv);
  957. return gEnv;
  958. }
  959. // Leroy@Conffx
  960. SEditorSettings* CEditorImpl::GetEditorSettings()
  961. {
  962. return &gSettings;
  963. }