BsEditorApplication.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "BsResourceImporter.h"
  11. #include "BsEditorWidgetLayout.h"
  12. #include "BsScenePicking.h"
  13. #include "BsSelection.h"
  14. #include "BsGizmoManager.h"
  15. #include "BsCodeEditor.h"
  16. #include "BsBuildManager.h"
  17. #include "BsScriptCodeImporter.h"
  18. #include "BsShaderIncludeHandler.h"
  19. #include "BsDropDownWindowManager.h"
  20. #include "BsPrefabImporter.h"
  21. #include "BsProjectLibrary.h"
  22. #include "BsProjectSettings.h"
  23. #include "BsEditorSettings.h"
  24. #include "BsScriptManager.h"
  25. #include "BsImporter.h"
  26. #include "BsVirtualInput.h"
  27. #include "BsResources.h"
  28. #include "BsCoreSceneManager.h"
  29. #include "BsSplashScreen.h"
  30. // DEBUG ONLY
  31. #include "BsShader.h"
  32. namespace BansheeEngine
  33. {
  34. const Path EditorApplication::WIDGET_LAYOUT_PATH = PROJECT_INTERNAL_DIR + L"Layout.asset";
  35. const Path EditorApplication::BUILD_DATA_PATH = PROJECT_INTERNAL_DIR + L"BuildData.asset";
  36. const Path EditorApplication::EDITOR_SETTINGS_PATH = RUNTIME_DATA_PATH + L"Settings.asset";
  37. const Path EditorApplication::PROJECT_SETTINGS_PATH = PROJECT_INTERNAL_DIR + L"Settings.asset";
  38. RENDER_WINDOW_DESC createRenderWindowDesc()
  39. {
  40. RENDER_WINDOW_DESC renderWindowDesc;
  41. renderWindowDesc.videoMode = VideoMode(1280, 720);
  42. renderWindowDesc.title = "BansheeEditor";
  43. renderWindowDesc.fullscreen = false;
  44. renderWindowDesc.border = WindowBorder::None;
  45. renderWindowDesc.hideUntilSwap = true;
  46. return renderWindowDesc;
  47. }
  48. EditorApplication::EditorApplication(RenderAPIPlugin renderAPIPlugin)
  49. :Application(createRenderWindowDesc(), renderAPIPlugin, RendererPlugin::Default),
  50. mActiveRAPIPlugin(renderAPIPlugin), mSBansheeEditorPlugin(nullptr), mIsProjectLoaded(false)
  51. {
  52. }
  53. EditorApplication::~EditorApplication()
  54. {
  55. ProjectLibrary::shutDown();
  56. BuiltinEditorResources::shutDown();
  57. }
  58. void EditorApplication::onStartUp()
  59. {
  60. SplashScreen::show();
  61. Application::onStartUp();
  62. loadEditorSettings();
  63. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  64. BuiltinEditorResources::startUp();
  65. {
  66. auto inputConfig = VirtualInput::instance().getConfiguration();
  67. inputConfig->registerButton("Rename", BC_F2);
  68. inputConfig->registerButton("Undo", BC_Z, ButtonModifier::Ctrl);
  69. inputConfig->registerButton("Redo", BC_Y, ButtonModifier::Ctrl);
  70. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  71. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  72. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  73. inputConfig->registerButton("Duplicate", BC_D, ButtonModifier::Ctrl);
  74. inputConfig->registerButton("Delete", BC_DELETE);
  75. }
  76. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  77. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  78. ResourceImporter* resourceImporter = bs_new<ResourceImporter>();
  79. Importer::instance()._registerAssetImporter(resourceImporter);
  80. PrefabImporter* prefabImporter = bs_new<PrefabImporter>();
  81. Importer::instance()._registerAssetImporter(prefabImporter);
  82. ProjectLibrary::startUp();
  83. UndoRedo::startUp();
  84. EditorWindowManager::startUp();
  85. EditorWidgetManager::startUp();
  86. DropDownWindowManager::startUp();
  87. ScenePicking::startUp();
  88. Selection::startUp();
  89. GizmoManager::startUp();
  90. BuildManager::startUp();
  91. CodeEditorManager::startUp();
  92. MainEditorWindow* mainWindow = MainEditorWindow::create(getPrimaryWindow());
  93. ScriptManager::instance().initialize();
  94. #if BS_DEBUG_MODE
  95. HShader dummyParsedShader = Importer::instance().import<Shader>(RUNTIME_DATA_PATH + "Raw\\Engine\\Shaders\\TestFX.bsl");
  96. assert(dummyParsedShader != nullptr); // Ad hoc unit test
  97. #endif
  98. }
  99. void EditorApplication::onShutDown()
  100. {
  101. unloadProject();
  102. CodeEditorManager::shutDown();
  103. BuildManager::shutDown();
  104. GizmoManager::shutDown();
  105. Selection::shutDown();
  106. ScenePicking::shutDown();
  107. saveEditorSettings();
  108. DropDownWindowManager::shutDown();
  109. EditorWidgetManager::shutDown();
  110. EditorWindowManager::shutDown();
  111. UndoRedo::shutDown();
  112. Application::onShutDown();
  113. }
  114. void EditorApplication::loadScriptSystem()
  115. {
  116. loadPlugin("BansheeMono", &mMonoPlugin);
  117. loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin);
  118. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin);
  119. }
  120. void EditorApplication::startUp(RenderAPIPlugin renderAPI)
  121. {
  122. CoreApplication::startUp<EditorApplication>(renderAPI);
  123. }
  124. void EditorApplication::preUpdate()
  125. {
  126. Application::preUpdate();
  127. EditorWidgetManager::instance().update();
  128. DropDownWindowManager::instance().update();
  129. }
  130. void EditorApplication::postUpdate()
  131. {
  132. Application::postUpdate();
  133. gProjectLibrary().update();
  134. EditorWindowManager::instance().update();
  135. SplashScreen::hide();
  136. }
  137. Path EditorApplication::getEditorAssemblyPath() const
  138. {
  139. Path assemblyPath = getBuiltinAssemblyFolder();
  140. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  141. return assemblyPath;
  142. }
  143. Path EditorApplication::getEditorScriptAssemblyPath() const
  144. {
  145. Path assemblyPath = getScriptAssemblyFolder();
  146. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  147. return assemblyPath;
  148. }
  149. Path EditorApplication::getScriptAssemblyFolder() const
  150. {
  151. if (!isProjectLoaded())
  152. return Path::BLANK;
  153. Path assemblyFolder = getProjectPath();
  154. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  155. return assemblyFolder;
  156. }
  157. void EditorApplication::saveProject()
  158. {
  159. if (!isProjectLoaded())
  160. return;
  161. Path buildDataPath = getProjectPath();
  162. buildDataPath.append(BUILD_DATA_PATH);
  163. BuildManager::instance().save(buildDataPath);
  164. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  165. saveEditorSettings();
  166. saveProjectSettings();
  167. gProjectLibrary().saveLibrary();
  168. }
  169. void EditorApplication::unloadProject()
  170. {
  171. if (!isProjectLoaded())
  172. return;
  173. saveProject();
  174. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  175. BuildManager::instance().clear();
  176. UndoRedo::instance().clear();
  177. EditorWidgetManager::instance().closeAll();
  178. gProjectLibrary().unloadLibrary();
  179. Resources::instance().unloadAllUnused();
  180. gCoreSceneManager().clearScene();
  181. mProjectPath = Path::BLANK;
  182. mProjectName = StringUtil::WBLANK;
  183. mIsProjectLoaded = false;
  184. }
  185. void EditorApplication::loadProject(const Path& projectPath)
  186. {
  187. unloadProject();
  188. mProjectPath = projectPath;
  189. mProjectName = projectPath.getWTail();
  190. mIsProjectLoaded = true;
  191. loadProjectSettings();
  192. Path buildDataPath = getProjectPath();
  193. buildDataPath.append(BUILD_DATA_PATH);
  194. BuildManager::instance().load(buildDataPath);
  195. // Do this before restoring windows and loading library to ensure types are loaded
  196. ScriptManager::instance().reload();
  197. gProjectLibrary().loadLibrary();
  198. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  199. if (layout != nullptr)
  200. EditorWidgetManager::instance().setLayout(layout);
  201. }
  202. void EditorApplication::createProject(const Path& path)
  203. {
  204. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  205. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  206. if (!FileSystem::exists(resourceDir))
  207. FileSystem::createDir(resourceDir);
  208. if (!FileSystem::exists(internalResourcesDir))
  209. FileSystem::createDir(internalResourcesDir);
  210. Path defaultLayoutPath = FileSystem::getWorkingDirectoryPath();
  211. defaultLayoutPath.append(BuiltinEditorResources::DefaultWidgetLayoutPath);
  212. if (FileSystem::exists(defaultLayoutPath))
  213. {
  214. Path projectLayoutPath = Path::combine(path, WIDGET_LAYOUT_PATH);
  215. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  216. }
  217. }
  218. bool EditorApplication::isValidProjectPath(const Path& path)
  219. {
  220. if (!path.isAbsolute())
  221. return false;
  222. if (!FileSystem::isDirectory(path))
  223. return false;
  224. return true;
  225. }
  226. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  227. {
  228. Path layoutPath = getProjectPath();
  229. layoutPath.append(WIDGET_LAYOUT_PATH);
  230. if(FileSystem::exists(layoutPath))
  231. {
  232. FileDecoder fs(layoutPath);
  233. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  234. }
  235. return nullptr;
  236. }
  237. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  238. {
  239. Path layoutPath = getProjectPath();
  240. layoutPath.append(WIDGET_LAYOUT_PATH);
  241. FileEncoder fs(layoutPath);
  242. fs.encode(layout.get());
  243. }
  244. void EditorApplication::loadEditorSettings()
  245. {
  246. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  247. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  248. if (FileSystem::exists(absoluteDataPath))
  249. {
  250. FileDecoder fs(absoluteDataPath);
  251. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  252. }
  253. if (mEditorSettings == nullptr)
  254. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  255. }
  256. void EditorApplication::saveEditorSettings()
  257. {
  258. if (mEditorSettings == nullptr)
  259. return;
  260. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  261. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  262. FileEncoder fs(absoluteDataPath);
  263. fs.encode(mEditorSettings.get());
  264. }
  265. void EditorApplication::loadProjectSettings()
  266. {
  267. if (isProjectLoaded())
  268. {
  269. Path absoluteDataPath = getProjectPath();
  270. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  271. if (FileSystem::exists(absoluteDataPath))
  272. {
  273. FileDecoder fs(absoluteDataPath);
  274. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  275. }
  276. }
  277. if (mProjectSettings == nullptr)
  278. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  279. }
  280. void EditorApplication::saveProjectSettings()
  281. {
  282. if (mProjectSettings == nullptr || !isProjectLoaded())
  283. return;
  284. Path absoluteDataPath = getProjectPath();
  285. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  286. FileEncoder fs(absoluteDataPath);
  287. fs.encode(mProjectSettings.get());
  288. }
  289. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  290. {
  291. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  292. }
  293. EditorApplication& gEditorApplication()
  294. {
  295. return static_cast<EditorApplication&>(EditorApplication::instance());
  296. }
  297. }