BsEditorApplication.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. }
  245. void EditorApplication::loadProject(const Path& projectPath)
  246. {
  247. unloadProject();
  248. mProjectPath = projectPath;
  249. mProjectName = projectPath.getWTail();
  250. mIsProjectLoaded = true;
  251. loadProjectSettings();
  252. Path buildDataPath = getProjectPath();
  253. buildDataPath.append(BUILD_DATA_PATH);
  254. BuildManager::instance().load(buildDataPath);
  255. // Do this before restoring windows and loading library to ensure types are loaded
  256. ScriptManager::instance().reload();
  257. ProjectLibrary::instance().loadLibrary();
  258. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  259. if (layout != nullptr)
  260. EditorWidgetManager::instance().setLayout(layout);
  261. }
  262. void EditorApplication::createProject(const Path& path)
  263. {
  264. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  265. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  266. if (!FileSystem::exists(resourceDir))
  267. FileSystem::createDir(resourceDir);
  268. if (!FileSystem::exists(internalResourcesDir))
  269. FileSystem::createDir(internalResourcesDir);
  270. Path defaultLayoutPath = FileSystem::getWorkingDirectoryPath();
  271. defaultLayoutPath.append(BuiltinEditorResources::DefaultWidgetLayoutPath);
  272. if (FileSystem::exists(defaultLayoutPath))
  273. {
  274. Path projectLayoutPath = Path::combine(path, WIDGET_LAYOUT_PATH);
  275. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  276. }
  277. }
  278. bool EditorApplication::isValidProjectPath(const Path& path)
  279. {
  280. if (!path.isAbsolute())
  281. return false;
  282. if (!FileSystem::isDirectory(path))
  283. return false;
  284. return true;
  285. }
  286. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  287. {
  288. Path layoutPath = getProjectPath();
  289. layoutPath.append(WIDGET_LAYOUT_PATH);
  290. if(FileSystem::exists(layoutPath))
  291. {
  292. FileDecoder fs(layoutPath);
  293. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  294. }
  295. return nullptr;
  296. }
  297. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  298. {
  299. Path layoutPath = getProjectPath();
  300. layoutPath.append(WIDGET_LAYOUT_PATH);
  301. FileEncoder fs(layoutPath);
  302. fs.encode(layout.get());
  303. }
  304. void EditorApplication::loadEditorSettings()
  305. {
  306. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  307. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  308. if (FileSystem::exists(absoluteDataPath))
  309. {
  310. FileDecoder fs(absoluteDataPath);
  311. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  312. }
  313. if (mEditorSettings == nullptr)
  314. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  315. }
  316. void EditorApplication::saveEditorSettings()
  317. {
  318. if (mEditorSettings == nullptr)
  319. return;
  320. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  321. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  322. FileEncoder fs(absoluteDataPath);
  323. fs.encode(mEditorSettings.get());
  324. }
  325. void EditorApplication::loadProjectSettings()
  326. {
  327. if (isProjectLoaded())
  328. {
  329. Path absoluteDataPath = getProjectPath();
  330. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  331. if (FileSystem::exists(absoluteDataPath))
  332. {
  333. FileDecoder fs(absoluteDataPath);
  334. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  335. }
  336. }
  337. if (mProjectSettings == nullptr)
  338. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  339. }
  340. void EditorApplication::saveProjectSettings()
  341. {
  342. if (mProjectSettings == nullptr || !isProjectLoaded())
  343. return;
  344. Path absoluteDataPath = getProjectPath();
  345. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  346. FileEncoder fs(absoluteDataPath);
  347. fs.encode(mProjectSettings.get());
  348. }
  349. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  350. {
  351. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  352. }
  353. EditorApplication& gEditorApplication()
  354. {
  355. return static_cast<EditorApplication&>(EditorApplication::instance());
  356. }
  357. }