EditorToolsApplication.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #include "EditorDefs.h"
  9. #include "EditorToolsApplication.h"
  10. // Qt
  11. #include <QMessageBox>
  12. // AzToolsFramework
  13. #include <AzToolsFramework/UI/UICore/WidgetHelpers.h>
  14. #include <AzToolsFramework/Thumbnails/ThumbnailerComponent.h>
  15. #include <AzToolsFramework/AssetBrowser/AssetBrowserComponent.h>
  16. // Editor
  17. #include "MainWindow.h"
  18. #include "Controls/ReflectedPropertyControl/ReflectedVar.h"
  19. #include "CryEdit.h"
  20. #include "DisplaySettingsPythonFuncs.h"
  21. #include "GameEngine.h"
  22. #include "PythonEditorFuncs.h"
  23. #include "TrackView/TrackViewPythonFuncs.h"
  24. #include "Include/IObjectManager.h"
  25. #include "Objects/ObjectManager.h"
  26. namespace EditorInternal
  27. {
  28. EditorToolsApplication::EditorToolsApplication(AZ::ComponentApplicationSettings componentAppSettings)
  29. : EditorToolsApplication(nullptr, nullptr, AZStd::move(componentAppSettings))
  30. {
  31. }
  32. EditorToolsApplication::EditorToolsApplication(int* argc, char*** argv)
  33. : EditorToolsApplication(argc, argv, {})
  34. {
  35. }
  36. EditorToolsApplication::EditorToolsApplication(int* argc, char*** argv, AZ::ComponentApplicationSettings componentAppSettings)
  37. : ToolsApplication(argc, argv, AZStd::move(componentAppSettings))
  38. {
  39. EditorToolsApplicationRequests::Bus::Handler::BusConnect();
  40. AzToolsFramework::ViewportInteraction::EditorModifierKeyRequestBus::Handler::BusConnect();
  41. AzToolsFramework::ViewportInteraction::EditorViewportInputTimeNowRequestBus::Handler::BusConnect();
  42. }
  43. EditorToolsApplication::~EditorToolsApplication()
  44. {
  45. AzToolsFramework::ViewportInteraction::EditorViewportInputTimeNowRequestBus::Handler::BusDisconnect();
  46. AzToolsFramework::ViewportInteraction::EditorModifierKeyRequestBus::Handler::BusDisconnect();
  47. EditorToolsApplicationRequests::Bus::Handler::BusDisconnect();
  48. Stop();
  49. }
  50. bool EditorToolsApplication::IsStartupAborted() const
  51. {
  52. return m_StartupAborted;
  53. }
  54. void EditorToolsApplication::RegisterCoreComponents()
  55. {
  56. AzToolsFramework::ToolsApplication::RegisterCoreComponents();
  57. // Expose Legacy Python Bindings to Behavior Context for EditorPythonBindings Gem
  58. RegisterComponentDescriptor(AzToolsFramework::CryEditPythonHandler::CreateDescriptor());
  59. RegisterComponentDescriptor(AzToolsFramework::CryEditDocFuncsHandler::CreateDescriptor());
  60. RegisterComponentDescriptor(AzToolsFramework::DisplaySettingsPythonFuncsHandler::CreateDescriptor());
  61. RegisterComponentDescriptor(AzToolsFramework::MainWindowEditorFuncsHandler::CreateDescriptor());
  62. RegisterComponentDescriptor(AzToolsFramework::PythonEditorComponent::CreateDescriptor());
  63. RegisterComponentDescriptor(AzToolsFramework::PythonEditorFuncsHandler::CreateDescriptor());
  64. RegisterComponentDescriptor(AzToolsFramework::DisplaySettingsComponent::CreateDescriptor());
  65. RegisterComponentDescriptor(AzToolsFramework::TrackViewComponent::CreateDescriptor());
  66. RegisterComponentDescriptor(AzToolsFramework::TrackViewFuncsHandler::CreateDescriptor());
  67. RegisterComponentDescriptor(AzToolsFramework::ViewPanePythonFuncsHandler::CreateDescriptor());
  68. RegisterComponentDescriptor(AzToolsFramework::ViewportTitleDlgPythonFuncsHandler::CreateDescriptor());
  69. }
  70. AZ::ComponentTypeList EditorToolsApplication::GetRequiredSystemComponents() const
  71. {
  72. AZ::ComponentTypeList components = AzToolsFramework::ToolsApplication::GetRequiredSystemComponents();
  73. components.emplace_back(azrtti_typeid<AzToolsFramework::Thumbnailer::ThumbnailerComponent>());
  74. components.emplace_back(azrtti_typeid<AzToolsFramework::AssetBrowser::AssetBrowserComponent>());
  75. // Add new Bus-based Python Bindings
  76. components.emplace_back(azrtti_typeid<AzToolsFramework::DisplaySettingsComponent>());
  77. components.emplace_back(azrtti_typeid<AzToolsFramework::PythonEditorComponent>());
  78. components.emplace_back(azrtti_typeid<AzToolsFramework::TrackViewComponent>());
  79. return components;
  80. }
  81. void EditorToolsApplication::StartCommon(AZ::Entity* systemEntity)
  82. {
  83. AzToolsFramework::ToolsApplication::StartCommon(systemEntity);
  84. m_StartupAborted = m_moduleManager->m_quitRequested;
  85. if (systemEntity->GetState() != AZ::Entity::State::Active)
  86. {
  87. m_StartupAborted = true;
  88. }
  89. }
  90. bool EditorToolsApplication::Start()
  91. {
  92. AzFramework::Application::StartupParameters params;
  93. // Must be done before creating QApplication, otherwise asserts when we alloc
  94. AzToolsFramework::ToolsApplication::Start({}, params);
  95. if (IsStartupAborted() || !m_systemEntity)
  96. {
  97. AzToolsFramework::ToolsApplication::Stop();
  98. return false;
  99. }
  100. return true;
  101. }
  102. void EditorToolsApplication::QueryApplicationType(AZ::ApplicationTypeQuery& appType) const
  103. {
  104. appType.m_maskValue = AZ::ApplicationTypeQuery::Masks::Editor | AZ::ApplicationTypeQuery::Masks::Tool;
  105. };
  106. void EditorToolsApplication::CreateReflectionManager()
  107. {
  108. ToolsApplication::CreateReflectionManager();
  109. GetSerializeContext()->CreateEditContext();
  110. }
  111. void EditorToolsApplication::Reflect(AZ::ReflectContext* context)
  112. {
  113. ToolsApplication::Reflect(context);
  114. // Reflect property control classes to the serialize context...
  115. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  116. {
  117. ReflectedVarInit::setupReflection(serializeContext);
  118. }
  119. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  120. {
  121. behaviorContext->EBus<EditorToolsApplicationRequestBus>("EditorToolsApplicationRequestBus")
  122. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  123. ->Attribute(AZ::Script::Attributes::Category, "Editor")
  124. ->Attribute(AZ::Script::Attributes::Module, "editor")
  125. ->Event("OpenLevel", &EditorToolsApplicationRequests::OpenLevel)
  126. ->Event("OpenLevelNoPrompt", &EditorToolsApplicationRequests::OpenLevelNoPrompt)
  127. ->Event("CreateLevel", &EditorToolsApplicationRequests::CreateLevel)
  128. ->Event("CreateLevelNoPrompt", &EditorToolsApplicationRequests::CreateLevelNoPrompt)
  129. ->Event("GetGameFolder", &EditorToolsApplicationRequests::GetGameFolder)
  130. ->Event("GetCurrentLevelName", &EditorToolsApplicationRequests::GetCurrentLevelName)
  131. ->Event("GetCurrentLevelPath", &EditorToolsApplicationRequests::GetCurrentLevelPath)
  132. ->Event("Exit", &EditorToolsApplicationRequests::Exit)
  133. ->Event("ExitNoPrompt", &EditorToolsApplicationRequests::ExitNoPrompt)
  134. ;
  135. }
  136. }
  137. AZStd::string EditorToolsApplication::GetGameFolder() const
  138. {
  139. return Path::GetEditingGameDataFolder();
  140. }
  141. bool EditorToolsApplication::OpenLevel(AZStd::string_view levelName)
  142. {
  143. AZStd::string levelPath = levelName;
  144. if (!AZ::IO::SystemFile::Exists(levelPath.c_str()))
  145. {
  146. // now let's check if they pre-pended directories (Samples/SomeLevelName)
  147. AZStd::string levelFileName;
  148. {
  149. AZStd::size_t found = levelPath.find_last_of("/\\");
  150. if (found == AZStd::string::npos)
  151. {
  152. levelFileName = levelPath.substr(found + 1);
  153. }
  154. else
  155. {
  156. levelFileName = levelPath;
  157. }
  158. }
  159. // if the input path can't be found, let's automatically add on the game folder and the levels
  160. AzFramework::StringFunc::Path::Join(levelPath.c_str(), levelFileName.c_str(), levelPath);
  161. AzFramework::StringFunc::Path::Join(DefaultLevelFolder, levelPath.c_str(), levelPath);
  162. AzFramework::StringFunc::Path::Join(GetGameFolder().c_str(), levelPath.c_str(), levelPath);
  163. // make sure the level path includes the cry extension, if needed
  164. if (!levelFileName.ends_with(GetOldCryLevelExtension()) && !levelFileName.ends_with(GetLevelExtension()))
  165. {
  166. AZStd::size_t levelPathLength = levelPath.length();
  167. levelPath += GetOldCryLevelExtension();
  168. // Check if there is a .cry file, otherwise assume it is a new .ly file
  169. if (!AZ::IO::SystemFile::Exists(levelPath.c_str()))
  170. {
  171. levelPath.replace(levelPathLength, sizeof(GetOldCryLevelExtension()) - 1, GetLevelExtension());
  172. }
  173. }
  174. if (!AZ::IO::SystemFile::Exists(levelPath.c_str()))
  175. {
  176. return false;
  177. }
  178. }
  179. auto previousDocument = GetIEditor()->GetDocument();
  180. QString previousPathName = (previousDocument != nullptr) ? previousDocument->GetLevelPathName() : "";
  181. auto newDocument = CCryEditApp::instance()->OpenDocumentFile(levelPath.c_str(), true, COpenSameLevelOptions::ReopenLevelIfSame);
  182. // the underlying document pointer doesn't change, so we can't check that; use the path name's instead
  183. return newDocument && !newDocument->IsLevelLoadFailed();
  184. }
  185. bool EditorToolsApplication::OpenLevelNoPrompt(AZStd::string_view levelName)
  186. {
  187. GetIEditor()->GetDocument()->SetModifiedFlag(false);
  188. return OpenLevel(levelName);
  189. }
  190. int EditorToolsApplication::CreateLevel(AZStd::string_view templateName, AZStd::string_view levelName, bool /*bUseTerrain*/)
  191. {
  192. // Clang warns about a temporary being created in a function's argument list, so fullyQualifiedLevelName before the call
  193. QString fullyQualifiedLevelName;
  194. return CCryEditApp::instance()->CreateLevel(QString::fromUtf8(templateName.data(), static_cast<int>(templateName.size())),
  195. QString::fromUtf8(levelName.data(), static_cast<int>(levelName.size())),
  196. fullyQualifiedLevelName);
  197. }
  198. int EditorToolsApplication::CreateLevelNoPrompt(AZStd::string_view templateName, AZStd::string_view levelName, int /*terrainExportTextureSize*/, bool /*useTerrain*/)
  199. {
  200. // If a level was open, ignore any unsaved changes if it had been modified
  201. if (GetIEditor()->IsLevelLoaded())
  202. {
  203. GetIEditor()->GetDocument()->SetModifiedFlag(false);
  204. }
  205. // Clang warns about a temporary being created in a function's argument list, so fullyQualifiedLevelName before the call
  206. QString fullyQualifiedLevelName;
  207. return CCryEditApp::instance()->CreateLevel(QString::fromUtf8(templateName.data(), static_cast<int>(templateName.size())),
  208. QString::fromUtf8(levelName.data(), static_cast<int>(levelName.size())),
  209. fullyQualifiedLevelName);
  210. }
  211. AZStd::string EditorToolsApplication::GetCurrentLevelName() const
  212. {
  213. return AZStd::string(GetIEditor()->GetGameEngine()->GetLevelName().toUtf8().data());
  214. }
  215. AZStd::string EditorToolsApplication::GetCurrentLevelPath() const
  216. {
  217. return AZStd::string(GetIEditor()->GetGameEngine()->GetLevelPath().toUtf8().data());
  218. }
  219. const char* EditorToolsApplication::GetLevelExtension() const
  220. {
  221. return ".prefab";
  222. }
  223. const char* EditorToolsApplication::GetOldCryLevelExtension() const
  224. {
  225. return ".cry";
  226. }
  227. void EditorToolsApplication::Exit()
  228. {
  229. // Adding a single-shot QTimer to PyExit delays the QApplication::closeAllWindows call until
  230. // all the events in the event queue have been processed. Calling QApplication::closeAllWindows instead
  231. // of MainWindow::close ensures the Metal render window is cleaned up on macOS.
  232. QTimer::singleShot(0, qApp, &QApplication::closeAllWindows);
  233. }
  234. void EditorToolsApplication::ExitNoPrompt()
  235. {
  236. // Set the level to "unmodified" so that it doesn't prompt to save on exit.
  237. GetIEditor()->GetDocument()->SetModifiedFlag(false);
  238. Exit();
  239. }
  240. AzToolsFramework::ViewportInteraction::KeyboardModifiers EditorToolsApplication::QueryKeyboardModifiers()
  241. {
  242. return AzToolsFramework::ViewportInteraction::BuildKeyboardModifiers(QGuiApplication::queryKeyboardModifiers());
  243. }
  244. AZStd::chrono::milliseconds EditorToolsApplication::EditorViewportInputTimeNow()
  245. {
  246. const auto now = AZStd::chrono::steady_clock::now();
  247. return AZStd::chrono::time_point_cast<AZStd::chrono::milliseconds>(now).time_since_epoch();
  248. }
  249. } // namespace EditorInternal