BsEditorApplication.cpp 13 KB

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