BsEditorApplication.cpp 12 KB

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