BsEditorApplication.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "BsEditorApplication.h"
  2. #include "BsEditorWindowManager.h"
  3. #include "BsEditorWidgetManager.h"
  4. #include "BsMainEditorWindow.h"
  5. #include "BsRenderWindow.h"
  6. #include "BsBuiltinEditorResources.h"
  7. #include "BsUndoRedo.h"
  8. #include "BsFileSerializer.h"
  9. #include "BsFileSystem.h"
  10. #include "BsEditorWidgetLayout.h"
  11. #include "BsScenePicking.h"
  12. #include "BsSelection.h"
  13. #include "BsGizmoManager.h"
  14. #include "BsCodeEditor.h"
  15. #include "BsBuildManager.h"
  16. #include "BsScriptCodeImporter.h"
  17. #include "BsShaderIncludeHandler.h"
  18. #include "BsDropDownWindowManager.h"
  19. #include "BsProjectLibrary.h"
  20. #include "BsProjectSettings.h"
  21. #include "BsEditorSettings.h"
  22. #include "BsScriptManager.h"
  23. #include "BsImporter.h"
  24. #include "BsVirtualInput.h"
  25. #include "BsResources.h"
  26. #include "BsCoreSceneManager.h"
  27. #include "BsSplashScreen.h"
  28. #include "BsDynLib.h"
  29. #include "BsSceneManager.h"
  30. namespace BansheeEngine
  31. {
  32. const Path EditorApplication::WIDGET_LAYOUT_PATH = PROJECT_INTERNAL_DIR + L"Layout.asset";
  33. const Path EditorApplication::BUILD_DATA_PATH = PROJECT_INTERNAL_DIR + L"BuildData.asset";
  34. const Path EditorApplication::PROJECT_SETTINGS_PATH = PROJECT_INTERNAL_DIR + L"Settings.asset";
  35. RENDER_WINDOW_DESC createRenderWindowDesc()
  36. {
  37. RENDER_WINDOW_DESC renderWindowDesc;
  38. renderWindowDesc.videoMode = VideoMode(1280, 720);
  39. renderWindowDesc.title = "BansheeEditor";
  40. renderWindowDesc.fullscreen = false;
  41. renderWindowDesc.border = WindowBorder::None;
  42. renderWindowDesc.hideUntilSwap = true;
  43. return renderWindowDesc;
  44. }
  45. Vector<String> getImporters()
  46. {
  47. Vector<String> importers;
  48. importers.push_back("BansheeFreeImgImporter");
  49. importers.push_back("BansheeFBXImporter");
  50. importers.push_back("BansheeFontImporter");
  51. importers.push_back("BansheeSL");
  52. return importers;
  53. }
  54. Path getEditorSettingsPath()
  55. {
  56. return Paths::getRuntimeDataPath() + L"Settings.asset";
  57. }
  58. EditorApplication::EditorApplication(EditorRenderAPI renderAPIPlugin)
  59. :Application(createRenderWindowDesc(), toEngineRenderAPI(renderAPIPlugin), RendererPlugin::Default, getImporters()),
  60. mActiveRAPIPlugin(toEngineRenderAPI(renderAPIPlugin)), mSBansheeEditorPlugin(nullptr), mIsProjectLoaded(false)
  61. {
  62. }
  63. EditorApplication::~EditorApplication()
  64. {
  65. ProjectLibrary::shutDown();
  66. BuiltinEditorResources::shutDown();
  67. }
  68. void EditorApplication::onStartUp()
  69. {
  70. SplashScreen::show();
  71. Application::onStartUp();
  72. // In editor we render game on a separate surface, handled in Game window
  73. SceneManager::instance().setMainRenderTarget(nullptr);
  74. loadEditorSettings();
  75. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  76. BuiltinEditorResources::startUp();
  77. {
  78. auto inputConfig = VirtualInput::instance().getConfiguration();
  79. inputConfig->registerButton("Rename", BC_F2);
  80. inputConfig->registerButton("Undo", BC_Z, ButtonModifier::Ctrl);
  81. inputConfig->registerButton("Redo", BC_Y, ButtonModifier::Ctrl);
  82. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  83. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  84. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  85. inputConfig->registerButton("Duplicate", BC_D, ButtonModifier::Ctrl);
  86. inputConfig->registerButton("Delete", BC_DELETE);
  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* mainWindow = 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::startUp(EditorRenderAPI renderAPI)
  125. {
  126. CoreApplication::startUp<EditorApplication>(renderAPI);
  127. }
  128. void EditorApplication::preUpdate()
  129. {
  130. Application::preUpdate();
  131. EditorWidgetManager::instance().update();
  132. DropDownWindowManager::instance().update();
  133. }
  134. void EditorApplication::postUpdate()
  135. {
  136. // Call update on editor widgets before parent's postUpdate because the parent will render the GUI and we need
  137. // to ensure editor widget's GUI is updated.
  138. EditorWindowManager::instance().update();
  139. Application::postUpdate();
  140. SplashScreen::hide();
  141. }
  142. void EditorApplication::quitRequested()
  143. {
  144. typedef void(*QuitRequestedFunc)();
  145. QuitRequestedFunc quitRequestedCall = (QuitRequestedFunc)mSBansheeEditorPlugin->getSymbol("quitRequested");
  146. if (quitRequestedCall != nullptr)
  147. quitRequestedCall();
  148. }
  149. Path EditorApplication::getEditorAssemblyPath() const
  150. {
  151. Path assemblyPath = getBuiltinAssemblyFolder();
  152. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  153. return assemblyPath;
  154. }
  155. Path EditorApplication::getEditorScriptAssemblyPath() const
  156. {
  157. Path assemblyPath = getScriptAssemblyFolder();
  158. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  159. return assemblyPath;
  160. }
  161. Path EditorApplication::getScriptAssemblyFolder() const
  162. {
  163. if (!isProjectLoaded())
  164. return Path::BLANK;
  165. Path assemblyFolder = getProjectPath();
  166. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  167. return assemblyFolder;
  168. }
  169. void EditorApplication::saveProject()
  170. {
  171. if (!isProjectLoaded())
  172. return;
  173. Path buildDataPath = getProjectPath();
  174. buildDataPath.append(BUILD_DATA_PATH);
  175. BuildManager::instance().save(buildDataPath);
  176. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  177. saveEditorSettings();
  178. saveProjectSettings();
  179. gProjectLibrary().saveLibrary();
  180. }
  181. void EditorApplication::unloadProject()
  182. {
  183. if (!isProjectLoaded())
  184. return;
  185. saveProject();
  186. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  187. BuildManager::instance().clear();
  188. UndoRedo::instance().clear();
  189. EditorWidgetManager::instance().closeAll();
  190. gProjectLibrary().unloadLibrary();
  191. Resources::instance().unloadAllUnused();
  192. gCoreSceneManager().clearScene();
  193. mProjectPath = Path::BLANK;
  194. mProjectName = StringUtil::WBLANK;
  195. mIsProjectLoaded = false;
  196. }
  197. void EditorApplication::loadProject(const Path& projectPath)
  198. {
  199. unloadProject();
  200. mProjectPath = projectPath;
  201. mProjectName = projectPath.getWTail();
  202. mIsProjectLoaded = true;
  203. loadProjectSettings();
  204. Path buildDataPath = getProjectPath();
  205. buildDataPath.append(BUILD_DATA_PATH);
  206. BuildManager::instance().load(buildDataPath);
  207. gProjectLibrary().loadLibrary();
  208. // Do this before restoring windows to ensure types are loaded
  209. ScriptManager::instance().reload();
  210. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  211. if (layout != nullptr)
  212. EditorWidgetManager::instance().setLayout(layout);
  213. }
  214. void EditorApplication::createProject(const Path& path)
  215. {
  216. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  217. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  218. if (!FileSystem::exists(resourceDir))
  219. FileSystem::createDir(resourceDir);
  220. if (!FileSystem::exists(internalResourcesDir))
  221. FileSystem::createDir(internalResourcesDir);
  222. Path defaultLayoutPath = FileSystem::getWorkingDirectoryPath();
  223. defaultLayoutPath.append(BuiltinEditorResources::getDefaultWidgetLayoutPath());
  224. if (FileSystem::exists(defaultLayoutPath))
  225. {
  226. Path projectLayoutPath = Path::combine(path, WIDGET_LAYOUT_PATH);
  227. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  228. }
  229. }
  230. bool EditorApplication::isValidProjectPath(const Path& path)
  231. {
  232. if (!path.isAbsolute())
  233. return false;
  234. if (!FileSystem::isDirectory(path))
  235. return false;
  236. return true;
  237. }
  238. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  239. {
  240. Path layoutPath = getProjectPath();
  241. layoutPath.append(WIDGET_LAYOUT_PATH);
  242. if(FileSystem::exists(layoutPath))
  243. {
  244. FileDecoder fs(layoutPath);
  245. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  246. }
  247. return nullptr;
  248. }
  249. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  250. {
  251. Path layoutPath = getProjectPath();
  252. layoutPath.append(WIDGET_LAYOUT_PATH);
  253. FileEncoder fs(layoutPath);
  254. fs.encode(layout.get());
  255. }
  256. void EditorApplication::loadEditorSettings()
  257. {
  258. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  259. absoluteDataPath.append(getEditorSettingsPath());
  260. if (FileSystem::exists(absoluteDataPath))
  261. {
  262. FileDecoder fs(absoluteDataPath);
  263. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  264. }
  265. if (mEditorSettings == nullptr)
  266. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  267. }
  268. void EditorApplication::saveEditorSettings()
  269. {
  270. if (mEditorSettings == nullptr)
  271. return;
  272. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  273. absoluteDataPath.append(getEditorSettingsPath());
  274. FileEncoder fs(absoluteDataPath);
  275. fs.encode(mEditorSettings.get());
  276. }
  277. void EditorApplication::loadProjectSettings()
  278. {
  279. if (isProjectLoaded())
  280. {
  281. Path absoluteDataPath = getProjectPath();
  282. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  283. if (FileSystem::exists(absoluteDataPath))
  284. {
  285. FileDecoder fs(absoluteDataPath);
  286. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  287. }
  288. }
  289. if (mProjectSettings == nullptr)
  290. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  291. }
  292. void EditorApplication::saveProjectSettings()
  293. {
  294. if (mProjectSettings == nullptr || !isProjectLoaded())
  295. return;
  296. Path absoluteDataPath = getProjectPath();
  297. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  298. FileEncoder fs(absoluteDataPath);
  299. fs.encode(mProjectSettings.get());
  300. }
  301. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  302. {
  303. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  304. }
  305. RenderAPIPlugin EditorApplication::toEngineRenderAPI(EditorRenderAPI renderAPI)
  306. {
  307. if (renderAPI == EditorRenderAPI::DX11)
  308. return RenderAPIPlugin::DX11;
  309. else if (renderAPI == EditorRenderAPI::OpenGL)
  310. return RenderAPIPlugin::OpenGL;
  311. BS_EXCEPT(InvalidStateException, "Unsupported render API.");
  312. }
  313. EditorApplication& gEditorApplication()
  314. {
  315. return static_cast<EditorApplication&>(EditorApplication::instance());
  316. }
  317. }