BsEditorApplication.cpp 12 KB

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