BsEditorApplication.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsEditorApplication.h"
  4. #include "EditorWindow/BsEditorWindowManager.h"
  5. #include "EditorWindow/BsEditorWidgetManager.h"
  6. #include "EditorWindow/BsMainEditorWindow.h"
  7. #include "RenderAPI/BsRenderWindow.h"
  8. #include "Utility/BsBuiltinEditorResources.h"
  9. #include "UndoRedo/BsUndoRedo.h"
  10. #include "Serialization/BsFileSerializer.h"
  11. #include "FileSystem/BsFileSystem.h"
  12. #include "EditorWindow/BsEditorWidgetLayout.h"
  13. #include "Scene/BsScenePicking.h"
  14. #include "Scene/BsSelection.h"
  15. #include "Scene/BsGizmoManager.h"
  16. #include "CodeEditor/BsCodeEditor.h"
  17. #include "Build/BsBuildManager.h"
  18. #include "Resources/BsScriptCodeImporter.h"
  19. #include "Library/BsEditorShaderIncludeHandler.h"
  20. #include "EditorWindow/BsDropDownWindowManager.h"
  21. #include "Library/BsProjectLibrary.h"
  22. #include "Settings/BsProjectSettings.h"
  23. #include "Settings/BsEditorSettings.h"
  24. #include "Script/BsScriptManager.h"
  25. #include "Importer/BsImporter.h"
  26. #include "Input/BsVirtualInput.h"
  27. #include "Resources/BsResources.h"
  28. #include "Scene/BsSceneManager.h"
  29. #include "Utility/BsSplashScreen.h"
  30. #include "Utility/BsDynLib.h"
  31. #include "Scene/BsSceneManager.h"
  32. #include "BsEngineConfig.h"
  33. namespace bs
  34. {
  35. constexpr UINT32 SPLASH_SCREEN_DURATION_MS = 1000;
  36. const Path EditorApplication::WIDGET_LAYOUT_PATH = PROJECT_INTERNAL_DIR + "Layout.asset";
  37. const Path EditorApplication::BUILD_DATA_PATH = PROJECT_INTERNAL_DIR + "BuildData.asset";
  38. const Path EditorApplication::PROJECT_SETTINGS_PATH = PROJECT_INTERNAL_DIR + "Settings.asset";
  39. START_UP_DESC createStartupDesc()
  40. {
  41. START_UP_DESC startUpDesc;
  42. startUpDesc.renderAPI = BS_RENDER_API_MODULE;
  43. startUpDesc.renderer = BS_RENDERER_MODULE;
  44. startUpDesc.audio = BS_AUDIO_MODULE;
  45. startUpDesc.physics = BS_PHYSICS_MODULE;
  46. startUpDesc.physicsCooking = true;
  47. startUpDesc.primaryWindowDesc.videoMode = VideoMode(1920, 1080);
  48. startUpDesc.primaryWindowDesc.title = "BansheeEditor";
  49. startUpDesc.primaryWindowDesc.fullscreen = false;
  50. startUpDesc.primaryWindowDesc.showTitleBar = false;
  51. startUpDesc.primaryWindowDesc.showBorder = false;
  52. startUpDesc.primaryWindowDesc.hideUntilSwap = true;
  53. startUpDesc.primaryWindowDesc.depthBuffer = false;
  54. startUpDesc.primaryWindowDesc.hidden = true;
  55. startUpDesc.importers.push_back("bsfFreeImgImporter");
  56. startUpDesc.importers.push_back("bsfFBXImporter");
  57. startUpDesc.importers.push_back("bsfFontImporter");
  58. startUpDesc.importers.push_back("bsfSL");
  59. return startUpDesc;
  60. }
  61. Path getEditorSettingsPath()
  62. {
  63. return Paths::getEditorDataPath() + "Settings.asset";
  64. }
  65. EditorApplication::EditorApplication()
  66. :Application(createStartupDesc()), mIsProjectLoaded(false)
  67. {
  68. }
  69. EditorApplication::~EditorApplication()
  70. {
  71. ProjectLibrary::shutDown();
  72. BuiltinEditorResources::shutDown();
  73. }
  74. void EditorApplication::onStartUp()
  75. {
  76. Application::onStartUp();
  77. // In editor we render game on a separate surface, handled in Game window
  78. SceneManager::instance().setMainRenderTarget(nullptr);
  79. loadEditorSettings();
  80. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  81. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  82. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  83. // Hidden dependency: Needs to be done before BuiltinEditorResources import as shader include lookup requires it
  84. ProjectLibrary::startUp();
  85. BuiltinEditorResources::startUp();
  86. {
  87. auto inputConfig = VirtualInput::instance().getConfiguration();
  88. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  89. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  90. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  91. }
  92. UndoRedo::startUp();
  93. EditorWindowManager::startUp();
  94. EditorWidgetManager::startUp();
  95. DropDownWindowManager::startUp();
  96. ScenePicking::startUp();
  97. Selection::startUp();
  98. GizmoManager::startUp();
  99. BuildManager::startUp();
  100. CodeEditorManager::startUp();
  101. MainEditorWindow::create(getPrimaryWindow());
  102. ScriptManager::startUp();
  103. }
  104. void EditorApplication::onShutDown()
  105. {
  106. unloadProject();
  107. CodeEditorManager::shutDown();
  108. BuildManager::shutDown();
  109. GizmoManager::shutDown();
  110. Selection::shutDown();
  111. ScenePicking::shutDown();
  112. saveEditorSettings();
  113. DropDownWindowManager::shutDown();
  114. EditorWidgetManager::shutDown();
  115. EditorWindowManager::shutDown();
  116. UndoRedo::shutDown();
  117. Application::onShutDown();
  118. }
  119. void EditorApplication::startUp()
  120. {
  121. CoreApplication::startUp<EditorApplication>();
  122. }
  123. void EditorApplication::startUpRenderer()
  124. {
  125. mSplashScreenTimer.reset();
  126. SplashScreen::show();
  127. }
  128. void EditorApplication::startUpScriptManager()
  129. {
  130. // Do nothing, we initialize the script manager at a later stage
  131. }
  132. void EditorApplication::updateScriptManager()
  133. {
  134. // Do nothing, we need to perform script update before editor window update, but Application::postUpdate (where this
  135. // method is normally called from) needs to execute after editor window update.
  136. }
  137. void EditorApplication::preUpdate()
  138. {
  139. Application::preUpdate();
  140. EditorWidgetManager::instance().update();
  141. DropDownWindowManager::instance().update();
  142. }
  143. void EditorApplication::postUpdate()
  144. {
  145. ScriptManager::instance().update();
  146. // Call update on editor widgets before parent's postUpdate because the parent will render the GUI and we need
  147. // to ensure editor widget's GUI is updated.
  148. EditorWindowManager::instance().update();
  149. Application::postUpdate();
  150. if(mSplashScreenShown)
  151. {
  152. UINT64 currentTime = mSplashScreenTimer.getMilliseconds();
  153. if (currentTime >= SPLASH_SCREEN_DURATION_MS)
  154. {
  155. SplashScreen::hide();
  156. EditorWindowManager::instance().showWindows();
  157. mSplashScreenShown = false;
  158. }
  159. }
  160. setFPSLimit(mEditorSettings->getFPSLimit());
  161. }
  162. void EditorApplication::quitRequested()
  163. {
  164. onQuitRequested();
  165. }
  166. void EditorApplication::saveProject()
  167. {
  168. if (!isProjectLoaded())
  169. return;
  170. Path buildDataPath = getProjectPath();
  171. buildDataPath.append(BUILD_DATA_PATH);
  172. BuildManager::instance().save(buildDataPath);
  173. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  174. saveEditorSettings();
  175. saveProjectSettings();
  176. gProjectLibrary().saveLibrary();
  177. }
  178. void EditorApplication::unloadProject()
  179. {
  180. if (!isProjectLoaded())
  181. return;
  182. saveProject();
  183. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  184. BuildManager::instance().clear();
  185. UndoRedo::instance().clear();
  186. EditorWidgetManager::instance().closeAll();
  187. gProjectLibrary().unloadLibrary();
  188. Resources::instance().unloadAllUnused();
  189. gSceneManager().clearScene();
  190. mProjectPath = Path::BLANK;
  191. mProjectName = StringUtil::BLANK;
  192. mIsProjectLoaded = false;
  193. }
  194. void EditorApplication::loadProject(const Path& projectPath)
  195. {
  196. unloadProject();
  197. mProjectPath = projectPath;
  198. mProjectName = projectPath.getTail();
  199. mIsProjectLoaded = true;
  200. loadProjectSettings();
  201. Path buildDataPath = getProjectPath();
  202. buildDataPath.append(BUILD_DATA_PATH);
  203. BuildManager::instance().load(buildDataPath);
  204. gProjectLibrary().loadLibrary();
  205. // Do this before restoring windows to ensure types are loaded
  206. ScriptManager::instance().reload();
  207. SPtr<EditorWidgetLayout> layout = loadWidgetLayout();
  208. if (layout != nullptr)
  209. EditorWidgetManager::instance().setLayout(layout);
  210. }
  211. void EditorApplication::createProject(const Path& path)
  212. {
  213. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  214. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  215. if (!FileSystem::exists(resourceDir))
  216. FileSystem::createDir(resourceDir);
  217. if (!FileSystem::exists(internalResourcesDir))
  218. FileSystem::createDir(internalResourcesDir);
  219. saveDefaultWidgetLayout(path);
  220. }
  221. bool EditorApplication::isValidProjectPath(const Path& path)
  222. {
  223. if (!path.isAbsolute())
  224. return false;
  225. if (!FileSystem::isDirectory(path))
  226. return false;
  227. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  228. if (!FileSystem::exists(resourceDir))
  229. return false;
  230. return true;
  231. }
  232. SPtr<EditorWidgetLayout> EditorApplication::loadWidgetLayout()
  233. {
  234. Path layoutPath = getProjectPath();
  235. layoutPath.append(WIDGET_LAYOUT_PATH);
  236. if (!FileSystem::exists(layoutPath))
  237. saveDefaultWidgetLayout(getProjectPath());
  238. if(FileSystem::exists(layoutPath))
  239. {
  240. FileDecoder fs(layoutPath);
  241. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  242. }
  243. return nullptr;
  244. }
  245. void EditorApplication::saveWidgetLayout(const SPtr<EditorWidgetLayout>& layout)
  246. {
  247. Path layoutPath = getProjectPath();
  248. layoutPath.append(WIDGET_LAYOUT_PATH);
  249. FileEncoder fs(layoutPath);
  250. fs.encode(layout.get());
  251. }
  252. void EditorApplication::saveDefaultWidgetLayout(const Path& folder)
  253. {
  254. Path internalResourcesDir = Path::combine(folder, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  255. if (!FileSystem::exists(internalResourcesDir))
  256. FileSystem::createDir(internalResourcesDir);
  257. Path defaultLayoutPath = BuiltinEditorResources::getDefaultWidgetLayoutPath();
  258. if (FileSystem::exists(defaultLayoutPath))
  259. {
  260. Path projectLayoutPath = Path::combine(folder, WIDGET_LAYOUT_PATH);
  261. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  262. }
  263. }
  264. void EditorApplication::loadEditorSettings()
  265. {
  266. Path settingsPath = getEditorSettingsPath();
  267. if (FileSystem::exists(settingsPath))
  268. {
  269. FileDecoder fs(settingsPath);
  270. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  271. }
  272. if (mEditorSettings == nullptr)
  273. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  274. }
  275. void EditorApplication::saveEditorSettings()
  276. {
  277. if (mEditorSettings == nullptr)
  278. return;
  279. Path settingsPath = getEditorSettingsPath();
  280. FileEncoder fs(settingsPath);
  281. fs.encode(mEditorSettings.get());
  282. }
  283. void EditorApplication::loadProjectSettings()
  284. {
  285. if (isProjectLoaded())
  286. {
  287. Path absoluteDataPath = getProjectPath();
  288. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  289. if (FileSystem::exists(absoluteDataPath))
  290. {
  291. FileDecoder fs(absoluteDataPath);
  292. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  293. }
  294. }
  295. if (mProjectSettings == nullptr)
  296. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  297. }
  298. void EditorApplication::saveProjectSettings()
  299. {
  300. if (mProjectSettings == nullptr || !isProjectLoaded())
  301. return;
  302. Path absoluteDataPath = getProjectPath();
  303. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  304. FileEncoder fs(absoluteDataPath);
  305. fs.encode(mProjectSettings.get());
  306. }
  307. SPtr<IShaderIncludeHandler> EditorApplication::getShaderIncludeHandler() const
  308. {
  309. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  310. }
  311. EditorApplication& gEditorApplication()
  312. {
  313. return static_cast<EditorApplication&>(EditorApplication::instance());
  314. }
  315. }