BsEditorApplication.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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.scripting = true;
  47. startUpDesc.physicsCooking = true;
  48. startUpDesc.primaryWindowDesc.videoMode = VideoMode(1920, 1080);
  49. startUpDesc.primaryWindowDesc.title = "BansheeEditor";
  50. startUpDesc.primaryWindowDesc.fullscreen = false;
  51. startUpDesc.primaryWindowDesc.showTitleBar = false;
  52. startUpDesc.primaryWindowDesc.showBorder = false;
  53. startUpDesc.primaryWindowDesc.hideUntilSwap = true;
  54. startUpDesc.primaryWindowDesc.depthBuffer = false;
  55. startUpDesc.primaryWindowDesc.hidden = true;
  56. startUpDesc.importers.push_back("bsfFreeImgImporter");
  57. startUpDesc.importers.push_back("bsfFBXImporter");
  58. startUpDesc.importers.push_back("bsfFontImporter");
  59. startUpDesc.importers.push_back("bsfSL");
  60. return startUpDesc;
  61. }
  62. Path getEditorSettingsPath()
  63. {
  64. return gEditorApplication().getDataPath() + "Settings.asset";
  65. }
  66. EditorApplication::EditorApplication()
  67. :Application(createStartupDesc()), mIsProjectLoaded(false), mSBansheeEditorPlugin(nullptr)
  68. {
  69. }
  70. EditorApplication::~EditorApplication()
  71. {
  72. ProjectLibrary::shutDown();
  73. BuiltinEditorResources::shutDown();
  74. }
  75. void EditorApplication::onStartUp()
  76. {
  77. // Find the path to data files
  78. //// First, look for EditorData folder in the direct descendant of the working directory
  79. if (FileSystem::exists("EditorData"))
  80. {
  81. mBuiltinDataPath = FileSystem::getWorkingDirectoryPath();
  82. mBuiltinDataPath.append("EditorData/");
  83. }
  84. else
  85. {
  86. // Then check the source distribution itself, in case we're running directly from the build directory
  87. mBuiltinDataPath = Path(RAW_APP_ROOT) + Path("Data/");
  88. if (!FileSystem::exists(mBuiltinDataPath))
  89. LOGERR("Cannot find builtin assets for the editor at path '" + mBuiltinDataPath.toString() + "'.");
  90. }
  91. Application::onStartUp();
  92. // In editor we render game on a separate surface, handled in Game window
  93. SceneManager::instance().setMainRenderTarget(nullptr);
  94. loadEditorSettings();
  95. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  96. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  97. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  98. // Hidden dependency: Needs to be done before BuiltinEditorResources import as shader include lookup requires it
  99. ProjectLibrary::startUp();
  100. BuiltinEditorResources::startUp();
  101. {
  102. auto inputConfig = VirtualInput::instance().getConfiguration();
  103. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  104. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  105. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  106. }
  107. UndoRedo::startUp();
  108. EditorWindowManager::startUp();
  109. EditorWidgetManager::startUp();
  110. DropDownWindowManager::startUp();
  111. ScenePicking::startUp();
  112. Selection::startUp();
  113. GizmoManager::startUp();
  114. BuildManager::startUp();
  115. CodeEditorManager::startUp();
  116. MainEditorWindow::create(getPrimaryWindow());
  117. ScriptManager::instance().initialize();
  118. }
  119. void EditorApplication::onShutDown()
  120. {
  121. unloadProject();
  122. CodeEditorManager::shutDown();
  123. BuildManager::shutDown();
  124. GizmoManager::shutDown();
  125. Selection::shutDown();
  126. ScenePicking::shutDown();
  127. saveEditorSettings();
  128. DropDownWindowManager::shutDown();
  129. EditorWidgetManager::shutDown();
  130. EditorWindowManager::shutDown();
  131. UndoRedo::shutDown();
  132. Application::onShutDown();
  133. }
  134. void EditorApplication::loadScriptSystem()
  135. {
  136. loadPlugin("bsfMono", &mMonoPlugin);
  137. loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin);
  138. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin);
  139. }
  140. void EditorApplication::unloadScriptSystem()
  141. {
  142. // These plugins must be unloaded before any other script plugins, because
  143. // they will cause finalizers to trigger and various modules those finalizers
  144. // might reference must still be active
  145. if(mSBansheeEditorPlugin != nullptr)
  146. unloadPlugin(mSBansheeEditorPlugin);
  147. if(mSBansheeEnginePlugin != nullptr)
  148. unloadPlugin(mSBansheeEnginePlugin);
  149. if(mMonoPlugin != nullptr)
  150. unloadPlugin(mMonoPlugin);
  151. }
  152. void EditorApplication::startUp()
  153. {
  154. CoreApplication::startUp<EditorApplication>();
  155. }
  156. void EditorApplication::startUpRenderer()
  157. {
  158. mSplashScreenTimer.reset();
  159. SplashScreen::show();
  160. }
  161. void EditorApplication::preUpdate()
  162. {
  163. Application::preUpdate();
  164. EditorWidgetManager::instance().update();
  165. DropDownWindowManager::instance().update();
  166. }
  167. void EditorApplication::postUpdate()
  168. {
  169. // Call update on editor widgets before parent's postUpdate because the parent will render the GUI and we need
  170. // to ensure editor widget's GUI is updated.
  171. EditorWindowManager::instance().update();
  172. Application::postUpdate();
  173. if(mSplashScreenShown)
  174. {
  175. UINT64 currentTime = mSplashScreenTimer.getMilliseconds();
  176. if (currentTime >= SPLASH_SCREEN_DURATION_MS)
  177. {
  178. SplashScreen::hide();
  179. EditorWindowManager::instance().showWindows();
  180. mSplashScreenShown = false;
  181. }
  182. }
  183. setFPSLimit(mEditorSettings->getFPSLimit());
  184. }
  185. void EditorApplication::quitRequested()
  186. {
  187. typedef void(*QuitRequestedFunc)();
  188. QuitRequestedFunc quitRequestedCall = (QuitRequestedFunc)mSBansheeEditorPlugin->getSymbol("quitRequested");
  189. if (quitRequestedCall != nullptr)
  190. quitRequestedCall();
  191. }
  192. Path EditorApplication::getEditorAssemblyPath() const
  193. {
  194. Path assemblyPath = getBuiltinAssemblyFolder();
  195. assemblyPath.append(String(EDITOR_ASSEMBLY) + ".dll");
  196. return assemblyPath;
  197. }
  198. Path EditorApplication::getEditorScriptAssemblyPath() const
  199. {
  200. Path assemblyPath = getScriptAssemblyFolder();
  201. assemblyPath.append(String(SCRIPT_EDITOR_ASSEMBLY) + ".dll");
  202. return assemblyPath;
  203. }
  204. Path EditorApplication::getScriptAssemblyFolder() const
  205. {
  206. if (!isProjectLoaded())
  207. return Path::BLANK;
  208. Path assemblyFolder = getProjectPath();
  209. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  210. return assemblyFolder;
  211. }
  212. void EditorApplication::saveProject()
  213. {
  214. if (!isProjectLoaded())
  215. return;
  216. Path buildDataPath = getProjectPath();
  217. buildDataPath.append(BUILD_DATA_PATH);
  218. BuildManager::instance().save(buildDataPath);
  219. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  220. saveEditorSettings();
  221. saveProjectSettings();
  222. gProjectLibrary().saveLibrary();
  223. }
  224. void EditorApplication::unloadProject()
  225. {
  226. if (!isProjectLoaded())
  227. return;
  228. saveProject();
  229. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  230. BuildManager::instance().clear();
  231. UndoRedo::instance().clear();
  232. EditorWidgetManager::instance().closeAll();
  233. gProjectLibrary().unloadLibrary();
  234. Resources::instance().unloadAllUnused();
  235. gSceneManager().clearScene();
  236. mProjectPath = Path::BLANK;
  237. mProjectName = StringUtil::BLANK;
  238. mIsProjectLoaded = false;
  239. }
  240. void EditorApplication::loadProject(const Path& projectPath)
  241. {
  242. unloadProject();
  243. mProjectPath = projectPath;
  244. mProjectName = projectPath.getTail();
  245. mIsProjectLoaded = true;
  246. loadProjectSettings();
  247. Path buildDataPath = getProjectPath();
  248. buildDataPath.append(BUILD_DATA_PATH);
  249. BuildManager::instance().load(buildDataPath);
  250. gProjectLibrary().loadLibrary();
  251. // Do this before restoring windows to ensure types are loaded
  252. ScriptManager::instance().reload();
  253. SPtr<EditorWidgetLayout> layout = loadWidgetLayout();
  254. if (layout != nullptr)
  255. EditorWidgetManager::instance().setLayout(layout);
  256. }
  257. void EditorApplication::createProject(const Path& path)
  258. {
  259. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  260. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  261. if (!FileSystem::exists(resourceDir))
  262. FileSystem::createDir(resourceDir);
  263. if (!FileSystem::exists(internalResourcesDir))
  264. FileSystem::createDir(internalResourcesDir);
  265. saveDefaultWidgetLayout(path);
  266. }
  267. bool EditorApplication::isValidProjectPath(const Path& path)
  268. {
  269. if (!path.isAbsolute())
  270. return false;
  271. if (!FileSystem::isDirectory(path))
  272. return false;
  273. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  274. if (!FileSystem::exists(resourceDir))
  275. return false;
  276. return true;
  277. }
  278. SPtr<EditorWidgetLayout> EditorApplication::loadWidgetLayout()
  279. {
  280. Path layoutPath = getProjectPath();
  281. layoutPath.append(WIDGET_LAYOUT_PATH);
  282. if (!FileSystem::exists(layoutPath))
  283. saveDefaultWidgetLayout(getProjectPath());
  284. if(FileSystem::exists(layoutPath))
  285. {
  286. FileDecoder fs(layoutPath);
  287. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  288. }
  289. return nullptr;
  290. }
  291. void EditorApplication::saveWidgetLayout(const SPtr<EditorWidgetLayout>& layout)
  292. {
  293. Path layoutPath = getProjectPath();
  294. layoutPath.append(WIDGET_LAYOUT_PATH);
  295. FileEncoder fs(layoutPath);
  296. fs.encode(layout.get());
  297. }
  298. void EditorApplication::saveDefaultWidgetLayout(const Path& folder)
  299. {
  300. Path internalResourcesDir = Path::combine(folder, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  301. if (!FileSystem::exists(internalResourcesDir))
  302. FileSystem::createDir(internalResourcesDir);
  303. Path defaultLayoutPath = BuiltinEditorResources::getDefaultWidgetLayoutPath();
  304. if (FileSystem::exists(defaultLayoutPath))
  305. {
  306. Path projectLayoutPath = Path::combine(folder, WIDGET_LAYOUT_PATH);
  307. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  308. }
  309. }
  310. void EditorApplication::loadEditorSettings()
  311. {
  312. Path settingsPath = getEditorSettingsPath();
  313. if (FileSystem::exists(settingsPath))
  314. {
  315. FileDecoder fs(settingsPath);
  316. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  317. }
  318. if (mEditorSettings == nullptr)
  319. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  320. }
  321. void EditorApplication::saveEditorSettings()
  322. {
  323. if (mEditorSettings == nullptr)
  324. return;
  325. Path settingsPath = getEditorSettingsPath();
  326. FileEncoder fs(settingsPath);
  327. fs.encode(mEditorSettings.get());
  328. }
  329. void EditorApplication::loadProjectSettings()
  330. {
  331. if (isProjectLoaded())
  332. {
  333. Path absoluteDataPath = getProjectPath();
  334. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  335. if (FileSystem::exists(absoluteDataPath))
  336. {
  337. FileDecoder fs(absoluteDataPath);
  338. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  339. }
  340. }
  341. if (mProjectSettings == nullptr)
  342. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  343. }
  344. void EditorApplication::saveProjectSettings()
  345. {
  346. if (mProjectSettings == nullptr || !isProjectLoaded())
  347. return;
  348. Path absoluteDataPath = getProjectPath();
  349. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  350. FileEncoder fs(absoluteDataPath);
  351. fs.encode(mProjectSettings.get());
  352. }
  353. SPtr<IShaderIncludeHandler> EditorApplication::getShaderIncludeHandler() const
  354. {
  355. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  356. }
  357. EditorApplication& gEditorApplication()
  358. {
  359. return static_cast<EditorApplication&>(EditorApplication::instance());
  360. }
  361. }