BsEditorApplication.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. #if BS_DEBUG
  128. /************************************************************************/
  129. /* DEBUG CODE */
  130. /************************************************************************/
  131. HShader dummyParsedShader = Importer::instance().import<Shader>(RUNTIME_DATA_PATH + "Raw\\Engine\\Shaders\\TestFX.bsl");
  132. assert(dummyParsedShader != nullptr); // Ad hoc unit test
  133. RenderAPICore* renderAPI = RenderAPICore::instancePtr();
  134. mTestModelGO = SceneObject::create("TestMesh");
  135. HRenderable testRenderable = mTestModelGO->addComponent<CRenderable>();
  136. Path testShaderLoc = RUNTIME_DATA_PATH + L"Test.bsl";;
  137. mTestShader = Importer::instance().import<Shader>(testShaderLoc);
  138. gResources().save(mTestShader, L"E:\\testShader.asset", true);
  139. gResources().unload(mTestShader);
  140. mTestShader = gResources().load<Shader>(L"E:\\testShader.asset");
  141. mTestMaterial = Material::create(mTestShader);
  142. mTestTexRef = static_resource_cast<Texture>(Importer::instance().import(RUNTIME_DATA_PATH + L"Examples\\Dragon.tga"));
  143. mDbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(RUNTIME_DATA_PATH + L"Examples\\Dragon.fbx"));
  144. gResources().save(mTestTexRef, L"E:\\ExportTest.tex", true);
  145. gResources().save(mDbgMeshRef, L"E:\\ExportMesh.mesh", true);
  146. gResources().unload(mTestTexRef);
  147. gResources().unload(mDbgMeshRef);
  148. mTestTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"E:\\ExportTest.tex"));
  149. mDbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"E:\\ExportMesh.mesh"));
  150. mDbgMeshRef.blockUntilLoaded();
  151. mDbgMeshRef->blockUntilCoreInitialized();
  152. mTestTexRef.blockUntilLoaded();
  153. mTestTexRef->blockUntilCoreInitialized();
  154. mTestMaterial->setTexture("tex", mTestTexRef);
  155. gResources().save(mTestShader, L"E:\\ExportShader.asset", true);
  156. gResources().save(mTestMaterial, L"E:\\ExportMaterial.mat", true);
  157. gResources().unload(mTestMaterial);
  158. gResources().unload(mTestShader);
  159. mTestShader = gResources().load<Shader>(L"E:\\ExportShader.asset");
  160. mTestMaterial = gResources().load<Material>(L"E:\\ExportMaterial.mat");
  161. testRenderable->setMesh(mDbgMeshRef);
  162. testRenderable->setMaterial(0, mTestMaterial);
  163. HSceneObject clone = mTestModelGO->clone();
  164. mTestModelGO->destroy();
  165. /************************************************************************/
  166. /* END DEBUG CODE */
  167. /************************************************************************/
  168. #endif
  169. }
  170. void EditorApplication::onShutDown()
  171. {
  172. unloadProject();
  173. CodeEditorManager::shutDown();
  174. BuildManager::shutDown();
  175. GizmoManager::shutDown();
  176. Selection::shutDown();
  177. ScenePicking::shutDown();
  178. saveEditorSettings();
  179. DropDownWindowManager::shutDown();
  180. EditorWidgetManager::shutDown();
  181. EditorWindowManager::shutDown();
  182. UndoRedo::shutDown();
  183. Application::onShutDown();
  184. }
  185. void EditorApplication::loadScriptSystem()
  186. {
  187. loadPlugin("BansheeMono", &mMonoPlugin);
  188. loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin);
  189. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin);
  190. }
  191. void EditorApplication::startUp(RenderAPIPlugin renderAPI)
  192. {
  193. CoreApplication::startUp<EditorApplication>(renderAPI);
  194. }
  195. void EditorApplication::preUpdate()
  196. {
  197. Application::preUpdate();
  198. EditorWidgetManager::instance().update();
  199. DropDownWindowManager::instance().update();
  200. }
  201. void EditorApplication::postUpdate()
  202. {
  203. Application::postUpdate();
  204. gProjectLibrary().update();
  205. EditorWindowManager::instance().update();
  206. }
  207. Path EditorApplication::getEditorAssemblyPath() const
  208. {
  209. Path assemblyPath = getBuiltinAssemblyFolder();
  210. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  211. return assemblyPath;
  212. }
  213. Path EditorApplication::getEditorScriptAssemblyPath() const
  214. {
  215. Path assemblyPath = getScriptAssemblyFolder();
  216. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  217. return assemblyPath;
  218. }
  219. Path EditorApplication::getScriptAssemblyFolder() const
  220. {
  221. if (!isProjectLoaded())
  222. return Path::BLANK;
  223. Path assemblyFolder = getProjectPath();
  224. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  225. return assemblyFolder;
  226. }
  227. void EditorApplication::saveProject()
  228. {
  229. if (!isProjectLoaded())
  230. return;
  231. Path buildDataPath = getProjectPath();
  232. buildDataPath.append(BUILD_DATA_PATH);
  233. BuildManager::instance().save(buildDataPath);
  234. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  235. saveEditorSettings();
  236. saveProjectSettings();
  237. gProjectLibrary().saveLibrary();
  238. }
  239. void EditorApplication::unloadProject()
  240. {
  241. if (!isProjectLoaded())
  242. return;
  243. saveProject();
  244. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  245. BuildManager::instance().clear();
  246. UndoRedo::instance().clear();
  247. EditorWidgetManager::instance().closeAll();
  248. gProjectLibrary().unloadLibrary();
  249. Resources::instance().unloadAllUnused();
  250. gCoreSceneManager().clearScene();
  251. mProjectPath = Path::BLANK;
  252. mProjectName = StringUtil::WBLANK;
  253. mIsProjectLoaded = false;
  254. }
  255. void EditorApplication::loadProject(const Path& projectPath)
  256. {
  257. unloadProject();
  258. mProjectPath = projectPath;
  259. mProjectName = projectPath.getWTail();
  260. mIsProjectLoaded = true;
  261. loadProjectSettings();
  262. Path buildDataPath = getProjectPath();
  263. buildDataPath.append(BUILD_DATA_PATH);
  264. BuildManager::instance().load(buildDataPath);
  265. // Do this before restoring windows and loading library to ensure types are loaded
  266. ScriptManager::instance().reload();
  267. gProjectLibrary().loadLibrary();
  268. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  269. if (layout != nullptr)
  270. EditorWidgetManager::instance().setLayout(layout);
  271. }
  272. void EditorApplication::createProject(const Path& path)
  273. {
  274. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  275. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  276. if (!FileSystem::exists(resourceDir))
  277. FileSystem::createDir(resourceDir);
  278. if (!FileSystem::exists(internalResourcesDir))
  279. FileSystem::createDir(internalResourcesDir);
  280. Path defaultLayoutPath = FileSystem::getWorkingDirectoryPath();
  281. defaultLayoutPath.append(BuiltinEditorResources::DefaultWidgetLayoutPath);
  282. if (FileSystem::exists(defaultLayoutPath))
  283. {
  284. Path projectLayoutPath = Path::combine(path, WIDGET_LAYOUT_PATH);
  285. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  286. }
  287. }
  288. bool EditorApplication::isValidProjectPath(const Path& path)
  289. {
  290. if (!path.isAbsolute())
  291. return false;
  292. if (!FileSystem::isDirectory(path))
  293. return false;
  294. return true;
  295. }
  296. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  297. {
  298. Path layoutPath = getProjectPath();
  299. layoutPath.append(WIDGET_LAYOUT_PATH);
  300. if(FileSystem::exists(layoutPath))
  301. {
  302. FileDecoder fs(layoutPath);
  303. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  304. }
  305. return nullptr;
  306. }
  307. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  308. {
  309. Path layoutPath = getProjectPath();
  310. layoutPath.append(WIDGET_LAYOUT_PATH);
  311. FileEncoder fs(layoutPath);
  312. fs.encode(layout.get());
  313. }
  314. void EditorApplication::loadEditorSettings()
  315. {
  316. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  317. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  318. if (FileSystem::exists(absoluteDataPath))
  319. {
  320. FileDecoder fs(absoluteDataPath);
  321. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  322. }
  323. if (mEditorSettings == nullptr)
  324. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  325. }
  326. void EditorApplication::saveEditorSettings()
  327. {
  328. if (mEditorSettings == nullptr)
  329. return;
  330. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  331. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  332. FileEncoder fs(absoluteDataPath);
  333. fs.encode(mEditorSettings.get());
  334. }
  335. void EditorApplication::loadProjectSettings()
  336. {
  337. if (isProjectLoaded())
  338. {
  339. Path absoluteDataPath = getProjectPath();
  340. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  341. if (FileSystem::exists(absoluteDataPath))
  342. {
  343. FileDecoder fs(absoluteDataPath);
  344. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  345. }
  346. }
  347. if (mProjectSettings == nullptr)
  348. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  349. }
  350. void EditorApplication::saveProjectSettings()
  351. {
  352. if (mProjectSettings == nullptr || !isProjectLoaded())
  353. return;
  354. Path absoluteDataPath = getProjectPath();
  355. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  356. FileEncoder fs(absoluteDataPath);
  357. fs.encode(mProjectSettings.get());
  358. }
  359. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  360. {
  361. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  362. }
  363. EditorApplication& gEditorApplication()
  364. {
  365. return static_cast<EditorApplication&>(EditorApplication::instance());
  366. }
  367. }