BsEditorApplication.cpp 11 KB

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