BsEditorApplication.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. // DEBUG ONLY
  30. #include "BsFileSystem.h"
  31. #include "BsSceneObject.h"
  32. #include "BsGpuProgram.h"
  33. #include "BsShader.h"
  34. #include "BsTexture.h"
  35. #include "BsMaterial.h"
  36. #include "BsTechnique.h"
  37. #include "BsPass.h"
  38. #include "BsCRenderable.h"
  39. #include "BsFolderMonitor.h"
  40. #include "BsCCamera.h"
  41. #include "BsCGUIWidget.h"
  42. #include "BsGUIButton.h"
  43. #include "BsGUILayout.h"
  44. #include "BsEvent.h"
  45. #include "BsCoreRenderer.h"
  46. #include "BsMesh.h"
  47. #include "BsMath.h"
  48. #include "BsDebug.h"
  49. #include "BsInput.h"
  50. namespace BansheeEngine
  51. {
  52. const Path EditorApplication::WIDGET_LAYOUT_PATH = PROJECT_INTERNAL_DIR + L"Layout.asset";
  53. const Path EditorApplication::BUILD_DATA_PATH = PROJECT_INTERNAL_DIR + L"BuildData.asset";
  54. const Path EditorApplication::EDITOR_SETTINGS_PATH = RUNTIME_DATA_PATH + L"Settings.asset";
  55. const Path EditorApplication::PROJECT_SETTINGS_PATH = PROJECT_INTERNAL_DIR + L"Settings.asset";
  56. RENDER_WINDOW_DESC createRenderWindowDesc()
  57. {
  58. RENDER_WINDOW_DESC renderWindowDesc;
  59. renderWindowDesc.videoMode = VideoMode(1280, 720);
  60. renderWindowDesc.title = "BansheeEditor";
  61. renderWindowDesc.fullscreen = false;
  62. renderWindowDesc.border = WindowBorder::None;
  63. renderWindowDesc.hideUntilSwap = true;
  64. return renderWindowDesc;
  65. }
  66. EditorApplication::EditorApplication(RenderAPIPlugin renderAPIPlugin)
  67. :Application(createRenderWindowDesc(), renderAPIPlugin, RendererPlugin::Default),
  68. mActiveRAPIPlugin(renderAPIPlugin), mSBansheeEditorPlugin(nullptr), mIsProjectLoaded(false)
  69. {
  70. }
  71. EditorApplication::~EditorApplication()
  72. {
  73. #if BS_DEBUG
  74. /************************************************************************/
  75. /* DEBUG CODE */
  76. /************************************************************************/
  77. gResources().unload(mTestTexRef);
  78. gResources().unload(mDbgMeshRef);
  79. gResources().unload(mTestShader);
  80. gResources().unload(mTestMaterial);
  81. mTestMaterial = nullptr;
  82. mTestTexRef = nullptr;
  83. mDbgMeshRef = nullptr;
  84. mTestShader = nullptr;
  85. /************************************************************************/
  86. /* END DEBUG CODE */
  87. /************************************************************************/
  88. #endif
  89. ProjectLibrary::shutDown();
  90. BuiltinEditorResources::shutDown();
  91. }
  92. void EditorApplication::onStartUp()
  93. {
  94. Application::onStartUp();
  95. loadEditorSettings();
  96. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  97. BuiltinEditorResources::startUp();
  98. {
  99. auto inputConfig = VirtualInput::instance().getConfiguration();
  100. inputConfig->registerButton("Rename", BC_F2);
  101. inputConfig->registerButton("Undo", BC_Z, ButtonModifier::Ctrl);
  102. inputConfig->registerButton("Redo", BC_Y, ButtonModifier::Ctrl);
  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. inputConfig->registerButton("Duplicate", BC_D, ButtonModifier::Ctrl);
  107. inputConfig->registerButton("Delete", BC_DELETE);
  108. }
  109. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  110. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  111. ResourceImporter* resourceImporter = bs_new<ResourceImporter>();
  112. Importer::instance()._registerAssetImporter(resourceImporter);
  113. PrefabImporter* prefabImporter = bs_new<PrefabImporter>();
  114. Importer::instance()._registerAssetImporter(prefabImporter);
  115. ProjectLibrary::startUp();
  116. UndoRedo::startUp();
  117. EditorWindowManager::startUp();
  118. EditorWidgetManager::startUp();
  119. DropDownWindowManager::startUp();
  120. ScenePicking::startUp();
  121. Selection::startUp();
  122. GizmoManager::startUp();
  123. BuildManager::startUp();
  124. CodeEditorManager::startUp();
  125. MainEditorWindow* mainWindow = MainEditorWindow::create(getPrimaryWindow());
  126. ScriptManager::instance().initialize();
  127. BS_EXCEPT(InternalErrorException, "Forced crash");
  128. #if BS_DEBUG
  129. /************************************************************************/
  130. /* DEBUG CODE */
  131. /************************************************************************/
  132. HShader dummyParsedShader = Importer::instance().import<Shader>(RUNTIME_DATA_PATH + "Raw\\Engine\\Shaders\\TestFX.bsl");
  133. assert(dummyParsedShader != nullptr); // Ad hoc unit test
  134. RenderAPICore* renderAPI = RenderAPICore::instancePtr();
  135. mTestModelGO = SceneObject::create("TestMesh");
  136. HRenderable testRenderable = mTestModelGO->addComponent<CRenderable>();
  137. Path testShaderLoc = RUNTIME_DATA_PATH + L"Test.bsl";;
  138. mTestShader = Importer::instance().import<Shader>(testShaderLoc);
  139. gResources().save(mTestShader, L"E:\\testShader.asset", true);
  140. gResources().unload(mTestShader);
  141. mTestShader = gResources().load<Shader>(L"E:\\testShader.asset");
  142. mTestMaterial = Material::create(mTestShader);
  143. mTestTexRef = static_resource_cast<Texture>(Importer::instance().import(RUNTIME_DATA_PATH + L"Examples\\Dragon.tga"));
  144. mDbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(RUNTIME_DATA_PATH + L"Examples\\Dragon.fbx"));
  145. gResources().save(mTestTexRef, L"E:\\ExportTest.tex", true);
  146. gResources().save(mDbgMeshRef, L"E:\\ExportMesh.mesh", true);
  147. gResources().unload(mTestTexRef);
  148. gResources().unload(mDbgMeshRef);
  149. mTestTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"E:\\ExportTest.tex"));
  150. mDbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"E:\\ExportMesh.mesh"));
  151. mDbgMeshRef.blockUntilLoaded();
  152. mDbgMeshRef->blockUntilCoreInitialized();
  153. mTestTexRef.blockUntilLoaded();
  154. mTestTexRef->blockUntilCoreInitialized();
  155. mTestMaterial->setTexture("tex", mTestTexRef);
  156. gResources().save(mTestShader, L"E:\\ExportShader.asset", true);
  157. gResources().save(mTestMaterial, L"E:\\ExportMaterial.mat", true);
  158. gResources().unload(mTestMaterial);
  159. gResources().unload(mTestShader);
  160. mTestShader = gResources().load<Shader>(L"E:\\ExportShader.asset");
  161. mTestMaterial = gResources().load<Material>(L"E:\\ExportMaterial.mat");
  162. testRenderable->setMesh(mDbgMeshRef);
  163. testRenderable->setMaterial(0, mTestMaterial);
  164. HSceneObject clone = mTestModelGO->clone();
  165. mTestModelGO->destroy();
  166. /************************************************************************/
  167. /* END DEBUG CODE */
  168. /************************************************************************/
  169. #endif
  170. }
  171. void EditorApplication::onShutDown()
  172. {
  173. unloadProject();
  174. CodeEditorManager::shutDown();
  175. BuildManager::shutDown();
  176. GizmoManager::shutDown();
  177. Selection::shutDown();
  178. ScenePicking::shutDown();
  179. saveEditorSettings();
  180. DropDownWindowManager::shutDown();
  181. EditorWidgetManager::shutDown();
  182. EditorWindowManager::shutDown();
  183. UndoRedo::shutDown();
  184. Application::onShutDown();
  185. }
  186. void EditorApplication::loadScriptSystem()
  187. {
  188. loadPlugin("BansheeMono", &mMonoPlugin);
  189. loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin);
  190. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin);
  191. }
  192. void EditorApplication::startUp(RenderAPIPlugin renderAPI)
  193. {
  194. CoreApplication::startUp<EditorApplication>(renderAPI);
  195. }
  196. void EditorApplication::preUpdate()
  197. {
  198. Application::preUpdate();
  199. EditorWidgetManager::instance().update();
  200. DropDownWindowManager::instance().update();
  201. }
  202. void EditorApplication::postUpdate()
  203. {
  204. Application::postUpdate();
  205. gProjectLibrary().update();
  206. EditorWindowManager::instance().update();
  207. }
  208. Path EditorApplication::getEditorAssemblyPath() const
  209. {
  210. Path assemblyPath = getBuiltinAssemblyFolder();
  211. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  212. return assemblyPath;
  213. }
  214. Path EditorApplication::getEditorScriptAssemblyPath() const
  215. {
  216. Path assemblyPath = getScriptAssemblyFolder();
  217. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  218. return assemblyPath;
  219. }
  220. Path EditorApplication::getScriptAssemblyFolder() const
  221. {
  222. if (!isProjectLoaded())
  223. return Path::BLANK;
  224. Path assemblyFolder = getProjectPath();
  225. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  226. return assemblyFolder;
  227. }
  228. void EditorApplication::saveProject()
  229. {
  230. if (!isProjectLoaded())
  231. return;
  232. Path buildDataPath = getProjectPath();
  233. buildDataPath.append(BUILD_DATA_PATH);
  234. BuildManager::instance().save(buildDataPath);
  235. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  236. saveEditorSettings();
  237. saveProjectSettings();
  238. gProjectLibrary().saveLibrary();
  239. }
  240. void EditorApplication::unloadProject()
  241. {
  242. if (!isProjectLoaded())
  243. return;
  244. saveProject();
  245. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  246. BuildManager::instance().clear();
  247. UndoRedo::instance().clear();
  248. EditorWidgetManager::instance().closeAll();
  249. gProjectLibrary().unloadLibrary();
  250. Resources::instance().unloadAllUnused();
  251. gCoreSceneManager().clearScene();
  252. mProjectPath = Path::BLANK;
  253. mProjectName = StringUtil::WBLANK;
  254. mIsProjectLoaded = false;
  255. }
  256. void EditorApplication::loadProject(const Path& projectPath)
  257. {
  258. unloadProject();
  259. mProjectPath = projectPath;
  260. mProjectName = projectPath.getWTail();
  261. mIsProjectLoaded = true;
  262. loadProjectSettings();
  263. Path buildDataPath = getProjectPath();
  264. buildDataPath.append(BUILD_DATA_PATH);
  265. BuildManager::instance().load(buildDataPath);
  266. // Do this before restoring windows and loading library to ensure types are loaded
  267. ScriptManager::instance().reload();
  268. gProjectLibrary().loadLibrary();
  269. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  270. if (layout != nullptr)
  271. EditorWidgetManager::instance().setLayout(layout);
  272. }
  273. void EditorApplication::createProject(const Path& path)
  274. {
  275. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  276. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  277. if (!FileSystem::exists(resourceDir))
  278. FileSystem::createDir(resourceDir);
  279. if (!FileSystem::exists(internalResourcesDir))
  280. FileSystem::createDir(internalResourcesDir);
  281. Path defaultLayoutPath = FileSystem::getWorkingDirectoryPath();
  282. defaultLayoutPath.append(BuiltinEditorResources::DefaultWidgetLayoutPath);
  283. if (FileSystem::exists(defaultLayoutPath))
  284. {
  285. Path projectLayoutPath = Path::combine(path, WIDGET_LAYOUT_PATH);
  286. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  287. }
  288. }
  289. bool EditorApplication::isValidProjectPath(const Path& path)
  290. {
  291. if (!path.isAbsolute())
  292. return false;
  293. if (!FileSystem::isDirectory(path))
  294. return false;
  295. return true;
  296. }
  297. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  298. {
  299. Path layoutPath = getProjectPath();
  300. layoutPath.append(WIDGET_LAYOUT_PATH);
  301. if(FileSystem::exists(layoutPath))
  302. {
  303. FileDecoder fs(layoutPath);
  304. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  305. }
  306. return nullptr;
  307. }
  308. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  309. {
  310. Path layoutPath = getProjectPath();
  311. layoutPath.append(WIDGET_LAYOUT_PATH);
  312. FileEncoder fs(layoutPath);
  313. fs.encode(layout.get());
  314. }
  315. void EditorApplication::loadEditorSettings()
  316. {
  317. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  318. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  319. if (FileSystem::exists(absoluteDataPath))
  320. {
  321. FileDecoder fs(absoluteDataPath);
  322. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  323. }
  324. if (mEditorSettings == nullptr)
  325. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  326. }
  327. void EditorApplication::saveEditorSettings()
  328. {
  329. if (mEditorSettings == nullptr)
  330. return;
  331. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  332. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  333. FileEncoder fs(absoluteDataPath);
  334. fs.encode(mEditorSettings.get());
  335. }
  336. void EditorApplication::loadProjectSettings()
  337. {
  338. if (isProjectLoaded())
  339. {
  340. Path absoluteDataPath = getProjectPath();
  341. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  342. if (FileSystem::exists(absoluteDataPath))
  343. {
  344. FileDecoder fs(absoluteDataPath);
  345. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  346. }
  347. }
  348. if (mProjectSettings == nullptr)
  349. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  350. }
  351. void EditorApplication::saveProjectSettings()
  352. {
  353. if (mProjectSettings == nullptr || !isProjectLoaded())
  354. return;
  355. Path absoluteDataPath = getProjectPath();
  356. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  357. FileEncoder fs(absoluteDataPath);
  358. fs.encode(mProjectSettings.get());
  359. }
  360. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  361. {
  362. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  363. }
  364. EditorApplication& gEditorApplication()
  365. {
  366. return static_cast<EditorApplication&>(EditorApplication::instance());
  367. }
  368. }