BsEditorApplication.cpp 14 KB

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