BsEditorApplication.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 "BsRenderable.h"
  37. #include "BsVirtualInput.h"
  38. #include "BsFolderMonitor.h"
  39. #include "BsCamera.h"
  40. #include "BsGUIWidget.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::PROJECT_INTERNAL_DIR = L"Internal\\";
  51. const Path EditorApplication::WIDGET_LAYOUT_PATH = PROJECT_INTERNAL_DIR + L"Layout.asset";
  52. const Path EditorApplication::BUILD_DATA_PATH = PROJECT_INTERNAL_DIR + L"BuildData.asset";
  53. const Path EditorApplication::EDITOR_SETTINGS_PATH = RUNTIME_DATA_PATH + L"Settings.asset";
  54. const Path EditorApplication::PROJECT_SETTINGS_PATH = PROJECT_INTERNAL_DIR + L"Settings.asset";
  55. RENDER_WINDOW_DESC createRenderWindowDesc()
  56. {
  57. RENDER_WINDOW_DESC renderWindowDesc;
  58. renderWindowDesc.videoMode = VideoMode(1280, 720);
  59. renderWindowDesc.title = "BansheeEditor";
  60. renderWindowDesc.fullscreen = false;
  61. renderWindowDesc.border = WindowBorder::None;
  62. renderWindowDesc.hideUntilSwap = true;
  63. return renderWindowDesc;
  64. }
  65. EditorApplication::EditorApplication(RenderAPIPlugin renderAPIPlugin)
  66. :Application(createRenderWindowDesc(), renderAPIPlugin, RendererPlugin::Default),
  67. mActiveRAPIPlugin(renderAPIPlugin), mSBansheeEditorPlugin(nullptr), mIsProjectLoaded(false)
  68. {
  69. }
  70. EditorApplication::~EditorApplication()
  71. {
  72. /************************************************************************/
  73. /* DEBUG CODE */
  74. /************************************************************************/
  75. gResources().unload(mTestTexRef);
  76. gResources().unload(mDbgMeshRef);
  77. gResources().unload(mTestShader);
  78. gResources().unload(mTestMaterial);
  79. mTestMaterial = nullptr;
  80. mTestTexRef = nullptr;
  81. mDbgMeshRef = nullptr;
  82. mTestShader = nullptr;
  83. /************************************************************************/
  84. /* END DEBUG CODE */
  85. /************************************************************************/
  86. ProjectLibrary::shutDown();
  87. BuiltinEditorResources::shutDown();
  88. }
  89. void EditorApplication::onStartUp()
  90. {
  91. Application::onStartUp();
  92. loadEditorSettings();
  93. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  94. BuiltinEditorResources::startUp();
  95. {
  96. auto inputConfig = VirtualInput::instance().getConfiguration();
  97. inputConfig->registerButton("Rename", BC_F2);
  98. inputConfig->registerButton("Undo", BC_Z, ButtonModifier::Ctrl);
  99. inputConfig->registerButton("Redo", BC_Y, ButtonModifier::Ctrl);
  100. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  101. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  102. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  103. inputConfig->registerButton("Duplicate", BC_D, ButtonModifier::Ctrl);
  104. inputConfig->registerButton("Delete", BC_DELETE);
  105. }
  106. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  107. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  108. ResourceImporter* resourceImporter = bs_new<ResourceImporter>();
  109. Importer::instance()._registerAssetImporter(resourceImporter);
  110. PrefabImporter* prefabImporter = bs_new<PrefabImporter>();
  111. Importer::instance()._registerAssetImporter(prefabImporter);
  112. ProjectLibrary::startUp();
  113. UndoRedo::startUp();
  114. EditorWindowManager::startUp();
  115. EditorWidgetManager::startUp();
  116. DropDownWindowManager::startUp();
  117. ScenePicking::startUp();
  118. Selection::startUp();
  119. GizmoManager::startUp();
  120. BuildManager::startUp();
  121. CodeEditorManager::startUp();
  122. MainEditorWindow* mainWindow = MainEditorWindow::create(getPrimaryWindow());
  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<Renderable>();
  131. Path testShaderLoc = RUNTIME_DATA_PATH + L"Test.bsl";;
  132. mTestShader = Importer::instance().import<Shader>(testShaderLoc);
  133. gResources().save(mTestShader, L"C:\\testShader.asset", true);
  134. gResources().unload(mTestShader);
  135. mTestShader = gResources().load<Shader>(L"C:\\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"C:\\ExportTest.tex", true);
  140. gResources().save(mDbgMeshRef, L"C:\\ExportMesh.mesh", true);
  141. gResources().unload(mTestTexRef);
  142. gResources().unload(mDbgMeshRef);
  143. mTestTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"C:\\ExportTest.tex"));
  144. mDbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"C:\\ExportMesh.mesh"));
  145. mDbgMeshRef.blockUntilLoaded();
  146. mDbgMeshRef->blockUntilCoreInitialized();
  147. mTestTexRef.blockUntilLoaded();
  148. mTestTexRef->blockUntilCoreInitialized();
  149. mTestMaterial->setTexture("tex", mTestTexRef);
  150. gResources().save(mTestShader, L"C:\\ExportShader.asset", true);
  151. gResources().save(mTestMaterial, L"C:\\ExportMaterial.mat", true);
  152. gResources().unload(mTestMaterial);
  153. gResources().unload(mTestShader);
  154. mTestShader = gResources().load<Shader>(L"C:\\ExportShader.asset");
  155. mTestMaterial = gResources().load<Material>(L"C:\\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. Application::loadScriptSystem();
  182. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin); // Managed part of the editor
  183. }
  184. void EditorApplication::startUp(RenderAPIPlugin renderAPI)
  185. {
  186. CoreApplication::startUp<EditorApplication>(renderAPI);
  187. }
  188. void EditorApplication::preUpdate()
  189. {
  190. Application::preUpdate();
  191. EditorWidgetManager::instance().update();
  192. DropDownWindowManager::instance().update();
  193. }
  194. void EditorApplication::postUpdate()
  195. {
  196. Application::postUpdate();
  197. ProjectLibrary::instance().update();
  198. EditorWindowManager::instance().update();
  199. }
  200. Path EditorApplication::getEditorAssemblyPath() const
  201. {
  202. Path assemblyPath = getBuiltinAssemblyFolder();
  203. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  204. return assemblyPath;
  205. }
  206. Path EditorApplication::getEditorScriptAssemblyPath() const
  207. {
  208. Path assemblyPath = getScriptAssemblyFolder();
  209. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  210. return assemblyPath;
  211. }
  212. Path EditorApplication::getScriptAssemblyFolder() const
  213. {
  214. if (!isProjectLoaded())
  215. return Path::BLANK;
  216. Path assemblyFolder = getProjectPath();
  217. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  218. return assemblyFolder;
  219. }
  220. void EditorApplication::saveProject()
  221. {
  222. if (!isProjectLoaded())
  223. return;
  224. BuildManager::instance().save(BUILD_DATA_PATH);
  225. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  226. saveEditorSettings();
  227. saveProjectSettings();
  228. ProjectLibrary::instance().saveLibrary();
  229. }
  230. void EditorApplication::unloadProject()
  231. {
  232. if (!isProjectLoaded())
  233. return;
  234. saveProject();
  235. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  236. BuildManager::instance().clear();
  237. UndoRedo::instance().clear();
  238. ProjectLibrary::instance().unloadLibrary();
  239. Resources::instance().unloadAllUnused();
  240. }
  241. void EditorApplication::loadProject(const Path& projectPath)
  242. {
  243. unloadProject();
  244. mProjectPath = projectPath;
  245. mProjectName = projectPath.getWTail();
  246. mIsProjectLoaded = true;
  247. loadProjectSettings();
  248. BuildManager::instance().load(BUILD_DATA_PATH);
  249. ProjectLibrary::instance().loadLibrary();
  250. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  251. if (layout != nullptr)
  252. EditorWidgetManager::instance().setLayout(layout);
  253. ScriptManager::instance().reload();
  254. }
  255. void EditorApplication::createProject(const Path& path)
  256. {
  257. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  258. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  259. if (!FileSystem::exists(resourceDir))
  260. FileSystem::createDir(resourceDir);
  261. if (!FileSystem::exists(internalResourcesDir))
  262. FileSystem::createDir(internalResourcesDir);
  263. Path defaultLayoutPath = FileSystem::getWorkingDirectoryPath();
  264. defaultLayoutPath.append(BuiltinEditorResources::DefaultWidgetLayoutPath);
  265. if (FileSystem::exists(defaultLayoutPath))
  266. {
  267. Path projectLayoutPath = Path::combine(path, WIDGET_LAYOUT_PATH);
  268. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  269. }
  270. }
  271. bool EditorApplication::isValidProjectPath(const Path& path)
  272. {
  273. if (!path.isAbsolute())
  274. return false;
  275. if (!FileSystem::isDirectory(path))
  276. return false;
  277. return true;
  278. }
  279. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  280. {
  281. Path layoutPath = getProjectPath();
  282. layoutPath.append(WIDGET_LAYOUT_PATH);
  283. if(FileSystem::exists(layoutPath))
  284. {
  285. FileDecoder fs(layoutPath);
  286. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  287. }
  288. return nullptr;
  289. }
  290. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  291. {
  292. Path layoutPath = getProjectPath();
  293. layoutPath.append(WIDGET_LAYOUT_PATH);
  294. FileEncoder fs(layoutPath);
  295. fs.encode(layout.get());
  296. }
  297. void EditorApplication::loadEditorSettings()
  298. {
  299. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  300. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  301. if (FileSystem::exists(absoluteDataPath))
  302. {
  303. FileDecoder fs(absoluteDataPath);
  304. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  305. }
  306. if (mEditorSettings == nullptr)
  307. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  308. }
  309. void EditorApplication::saveEditorSettings()
  310. {
  311. if (mEditorSettings == nullptr)
  312. return;
  313. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  314. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  315. FileEncoder fs(absoluteDataPath);
  316. fs.encode(mEditorSettings.get());
  317. }
  318. void EditorApplication::loadProjectSettings()
  319. {
  320. if (isProjectLoaded())
  321. {
  322. Path absoluteDataPath = getProjectPath();
  323. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  324. if (FileSystem::exists(absoluteDataPath))
  325. {
  326. FileDecoder fs(absoluteDataPath);
  327. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  328. }
  329. }
  330. if (mProjectSettings == nullptr)
  331. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  332. }
  333. void EditorApplication::saveProjectSettings()
  334. {
  335. if (mProjectSettings == nullptr || !isProjectLoaded())
  336. return;
  337. Path absoluteDataPath = getProjectPath();
  338. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  339. FileEncoder fs(absoluteDataPath);
  340. fs.encode(mProjectSettings.get());
  341. }
  342. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  343. {
  344. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  345. }
  346. EditorApplication& gEditorApplication()
  347. {
  348. return static_cast<EditorApplication&>(EditorApplication::instance());
  349. }
  350. }