BsEditorApplication.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 "Platform/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 + L"Layout.asset";
  37. const Path EditorApplication::BUILD_DATA_PATH = PROJECT_INTERNAL_DIR + L"BuildData.asset";
  38. const Path EditorApplication::PROJECT_SETTINGS_PATH = PROJECT_INTERNAL_DIR + L"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.scripting = 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("BansheeFreeImgImporter");
  56. startUpDesc.importers.push_back("BansheeFBXImporter");
  57. startUpDesc.importers.push_back("BansheeFontImporter");
  58. startUpDesc.importers.push_back("BansheeSL");
  59. return startUpDesc;
  60. }
  61. Path getEditorSettingsPath()
  62. {
  63. return Paths::getRuntimeDataPath() + L"Settings.asset";
  64. }
  65. EditorApplication::EditorApplication()
  66. :Application(createStartupDesc()), mIsProjectLoaded(false), mSBansheeEditorPlugin(nullptr)
  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. BuiltinEditorResources::startUp();
  82. {
  83. auto inputConfig = VirtualInput::instance().getConfiguration();
  84. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  85. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  86. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  87. }
  88. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  89. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  90. ProjectLibrary::startUp();
  91. UndoRedo::startUp();
  92. EditorWindowManager::startUp();
  93. EditorWidgetManager::startUp();
  94. DropDownWindowManager::startUp();
  95. ScenePicking::startUp();
  96. Selection::startUp();
  97. GizmoManager::startUp();
  98. BuildManager::startUp();
  99. CodeEditorManager::startUp();
  100. MainEditorWindow::create(getPrimaryWindow());
  101. ScriptManager::instance().initialize();
  102. }
  103. void EditorApplication::onShutDown()
  104. {
  105. unloadProject();
  106. CodeEditorManager::shutDown();
  107. BuildManager::shutDown();
  108. GizmoManager::shutDown();
  109. Selection::shutDown();
  110. ScenePicking::shutDown();
  111. saveEditorSettings();
  112. DropDownWindowManager::shutDown();
  113. EditorWidgetManager::shutDown();
  114. EditorWindowManager::shutDown();
  115. UndoRedo::shutDown();
  116. Application::onShutDown();
  117. }
  118. void EditorApplication::loadScriptSystem()
  119. {
  120. loadPlugin("BansheeMono", &mMonoPlugin);
  121. loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin);
  122. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin);
  123. }
  124. void EditorApplication::unloadScriptSystem()
  125. {
  126. // These plugins must be unloaded before any other script plugins, because
  127. // they will cause finalizers to trigger and various modules those finalizers
  128. // might reference must still be active
  129. if(mSBansheeEditorPlugin != nullptr)
  130. unloadPlugin(mSBansheeEditorPlugin);
  131. if(mSBansheeEnginePlugin != nullptr)
  132. unloadPlugin(mSBansheeEnginePlugin);
  133. if(mMonoPlugin != nullptr)
  134. unloadPlugin(mMonoPlugin);
  135. }
  136. void EditorApplication::startUp()
  137. {
  138. CoreApplication::startUp<EditorApplication>();
  139. }
  140. void EditorApplication::startUpRenderer()
  141. {
  142. mSplashScreenTimer.reset();
  143. SplashScreen::show();
  144. }
  145. void EditorApplication::preUpdate()
  146. {
  147. Application::preUpdate();
  148. EditorWidgetManager::instance().update();
  149. DropDownWindowManager::instance().update();
  150. }
  151. void EditorApplication::postUpdate()
  152. {
  153. // Call update on editor widgets before parent's postUpdate because the parent will render the GUI and we need
  154. // to ensure editor widget's GUI is updated.
  155. EditorWindowManager::instance().update();
  156. Application::postUpdate();
  157. if(mSplashScreenShown)
  158. {
  159. UINT64 currentTime = mSplashScreenTimer.getMilliseconds();
  160. if (currentTime >= SPLASH_SCREEN_DURATION_MS)
  161. {
  162. SplashScreen::hide();
  163. EditorWindowManager::instance().showWindows();
  164. mSplashScreenShown = false;
  165. }
  166. }
  167. setFPSLimit(mEditorSettings->getFPSLimit());
  168. }
  169. void EditorApplication::quitRequested()
  170. {
  171. typedef void(*QuitRequestedFunc)();
  172. QuitRequestedFunc quitRequestedCall = (QuitRequestedFunc)mSBansheeEditorPlugin->getSymbol("quitRequested");
  173. if (quitRequestedCall != nullptr)
  174. quitRequestedCall();
  175. }
  176. Path EditorApplication::getEditorAssemblyPath() const
  177. {
  178. Path assemblyPath = getBuiltinAssemblyFolder();
  179. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  180. return assemblyPath;
  181. }
  182. Path EditorApplication::getEditorScriptAssemblyPath() const
  183. {
  184. Path assemblyPath = getScriptAssemblyFolder();
  185. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  186. return assemblyPath;
  187. }
  188. Path EditorApplication::getScriptAssemblyFolder() const
  189. {
  190. if (!isProjectLoaded())
  191. return Path::BLANK;
  192. Path assemblyFolder = getProjectPath();
  193. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  194. return assemblyFolder;
  195. }
  196. void EditorApplication::saveProject()
  197. {
  198. if (!isProjectLoaded())
  199. return;
  200. Path buildDataPath = getProjectPath();
  201. buildDataPath.append(BUILD_DATA_PATH);
  202. BuildManager::instance().save(buildDataPath);
  203. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  204. saveEditorSettings();
  205. saveProjectSettings();
  206. gProjectLibrary().saveLibrary();
  207. }
  208. void EditorApplication::unloadProject()
  209. {
  210. if (!isProjectLoaded())
  211. return;
  212. saveProject();
  213. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  214. BuildManager::instance().clear();
  215. UndoRedo::instance().clear();
  216. EditorWidgetManager::instance().closeAll();
  217. gProjectLibrary().unloadLibrary();
  218. Resources::instance().unloadAllUnused();
  219. gSceneManager().clearScene();
  220. mProjectPath = Path::BLANK;
  221. mProjectName = StringUtil::WBLANK;
  222. mIsProjectLoaded = false;
  223. }
  224. void EditorApplication::loadProject(const Path& projectPath)
  225. {
  226. unloadProject();
  227. mProjectPath = projectPath;
  228. mProjectName = projectPath.getWTail();
  229. mIsProjectLoaded = true;
  230. loadProjectSettings();
  231. Path buildDataPath = getProjectPath();
  232. buildDataPath.append(BUILD_DATA_PATH);
  233. BuildManager::instance().load(buildDataPath);
  234. gProjectLibrary().loadLibrary();
  235. // Do this before restoring windows to ensure types are loaded
  236. ScriptManager::instance().reload();
  237. SPtr<EditorWidgetLayout> layout = loadWidgetLayout();
  238. if (layout != nullptr)
  239. EditorWidgetManager::instance().setLayout(layout);
  240. }
  241. void EditorApplication::createProject(const Path& path)
  242. {
  243. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  244. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  245. if (!FileSystem::exists(resourceDir))
  246. FileSystem::createDir(resourceDir);
  247. if (!FileSystem::exists(internalResourcesDir))
  248. FileSystem::createDir(internalResourcesDir);
  249. saveDefaultWidgetLayout(path);
  250. }
  251. bool EditorApplication::isValidProjectPath(const Path& path)
  252. {
  253. if (!path.isAbsolute())
  254. return false;
  255. if (!FileSystem::isDirectory(path))
  256. return false;
  257. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  258. if (!FileSystem::exists(resourceDir))
  259. return false;
  260. return true;
  261. }
  262. SPtr<EditorWidgetLayout> EditorApplication::loadWidgetLayout()
  263. {
  264. Path layoutPath = getProjectPath();
  265. layoutPath.append(WIDGET_LAYOUT_PATH);
  266. if (!FileSystem::exists(layoutPath))
  267. saveDefaultWidgetLayout(getProjectPath());
  268. if(FileSystem::exists(layoutPath))
  269. {
  270. FileDecoder fs(layoutPath);
  271. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  272. }
  273. return nullptr;
  274. }
  275. void EditorApplication::saveWidgetLayout(const SPtr<EditorWidgetLayout>& layout)
  276. {
  277. Path layoutPath = getProjectPath();
  278. layoutPath.append(WIDGET_LAYOUT_PATH);
  279. FileEncoder fs(layoutPath);
  280. fs.encode(layout.get());
  281. }
  282. void EditorApplication::saveDefaultWidgetLayout(const Path& folder)
  283. {
  284. Path internalResourcesDir = Path::combine(folder, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  285. if (!FileSystem::exists(internalResourcesDir))
  286. FileSystem::createDir(internalResourcesDir);
  287. Path defaultLayoutPath = BuiltinEditorResources::getDefaultWidgetLayoutPath();
  288. if (FileSystem::exists(defaultLayoutPath))
  289. {
  290. Path projectLayoutPath = Path::combine(folder, WIDGET_LAYOUT_PATH);
  291. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  292. }
  293. }
  294. void EditorApplication::loadEditorSettings()
  295. {
  296. Path settingsPath = getEditorSettingsPath();
  297. if (FileSystem::exists(settingsPath))
  298. {
  299. FileDecoder fs(settingsPath);
  300. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  301. }
  302. if (mEditorSettings == nullptr)
  303. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  304. }
  305. void EditorApplication::saveEditorSettings()
  306. {
  307. if (mEditorSettings == nullptr)
  308. return;
  309. Path settingsPath = getEditorSettingsPath();
  310. FileEncoder fs(settingsPath);
  311. fs.encode(mEditorSettings.get());
  312. }
  313. void EditorApplication::loadProjectSettings()
  314. {
  315. if (isProjectLoaded())
  316. {
  317. Path absoluteDataPath = getProjectPath();
  318. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  319. if (FileSystem::exists(absoluteDataPath))
  320. {
  321. FileDecoder fs(absoluteDataPath);
  322. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  323. }
  324. }
  325. if (mProjectSettings == nullptr)
  326. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  327. }
  328. void EditorApplication::saveProjectSettings()
  329. {
  330. if (mProjectSettings == nullptr || !isProjectLoaded())
  331. return;
  332. Path absoluteDataPath = getProjectPath();
  333. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  334. FileEncoder fs(absoluteDataPath);
  335. fs.encode(mProjectSettings.get());
  336. }
  337. SPtr<IShaderIncludeHandler> EditorApplication::getShaderIncludeHandler() const
  338. {
  339. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  340. }
  341. EditorApplication& gEditorApplication()
  342. {
  343. return static_cast<EditorApplication&>(EditorApplication::instance());
  344. }
  345. }