BsEditorApplication.cpp 12 KB

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