BsEditorApplication.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. // DEBUG ONLY
  19. #include "DbgEditorWidget1.h"
  20. #include "DbgEditorWidget2.h"
  21. #include "BsResources.h"
  22. #include "BsSceneObject.h"
  23. #include "BsImporter.h"
  24. #include "BsGpuProgram.h"
  25. #include "BsGpuProgramImportOptions.h"
  26. #include "BsShader.h"
  27. #include "BsTexture.h"
  28. #include "BsMaterial.h"
  29. #include "BsTechnique.h"
  30. #include "BsPass.h"
  31. #include "BsRenderable.h"
  32. #include "BsDbgTestGameObjectRef.h"
  33. #include "BsVirtualInput.h"
  34. #include "BsFolderMonitor.h"
  35. #include "BsProjectLibrary.h"
  36. #include "BsCamera.h"
  37. #include "BsGUIWidget.h"
  38. #include "BsGUIArea.h"
  39. #include "BsGUIButton.h"
  40. #include "BsGUILayout.h"
  41. #include "BsEvent.h"
  42. #include "BsCoreRenderer.h"
  43. #include "BsEditorSettings.h"
  44. #include "BsMesh.h"
  45. #include "BsMath.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. RENDER_WINDOW_DESC createRenderWindowDesc()
  51. {
  52. RENDER_WINDOW_DESC renderWindowDesc;
  53. renderWindowDesc.videoMode = VideoMode(1280, 720);
  54. renderWindowDesc.title = "BansheeEditor";
  55. renderWindowDesc.fullscreen = false;
  56. renderWindowDesc.border = WindowBorder::None;
  57. return renderWindowDesc;
  58. }
  59. EditorApplication::EditorApplication(RenderSystemPlugin renderSystemPlugin)
  60. :Application(createRenderWindowDesc(), renderSystemPlugin, RendererPlugin::Default),
  61. mActiveRSPlugin(renderSystemPlugin), mSBansheeEditorPlugin(nullptr)
  62. {
  63. // TODO - Load project settings
  64. mEditorSettings = bs_shared_ptr<EditorSettings>();
  65. BuiltinEditorResources::startUp(renderSystemPlugin);
  66. {
  67. auto inputConfig = VirtualInput::instance().getConfiguration();
  68. inputConfig->registerButton("Rename", BC_F2);
  69. inputConfig->registerButton("Undo", BC_Z, ButtonModifier::Ctrl);
  70. inputConfig->registerButton("Redo", BC_Y, ButtonModifier::Ctrl);
  71. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  72. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  73. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  74. inputConfig->registerButton("Delete", BC_DELETE);
  75. }
  76. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  77. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  78. ResourceImporter* resourceImporter = bs_new<ResourceImporter>();
  79. Importer::instance()._registerAssetImporter(resourceImporter);
  80. ProjectLibrary::startUp(getProjectPath());
  81. UndoRedo::startUp();
  82. EditorWindowManager::startUp();
  83. EditorWidgetManager::startUp();
  84. ScenePicking::startUp();
  85. Selection::startUp();
  86. GizmoManager::startUp();
  87. BuildManager::startUp();
  88. CodeEditorManager::startUp();
  89. }
  90. EditorApplication::~EditorApplication()
  91. {
  92. BuildManager::instance().save(BUILD_DATA_PATH);
  93. CodeEditorManager::shutDown();
  94. BuildManager::shutDown();
  95. GizmoManager::shutDown();
  96. Selection::shutDown();
  97. ScenePicking::shutDown();
  98. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  99. // TODO - Save project settings
  100. EditorWidgetManager::shutDown();
  101. EditorWindowManager::shutDown();
  102. UndoRedo::shutDown();
  103. // We purposely don't unload this plugin, it needs to be unloaded after
  104. // all mono assemblies have been unloaded (since their finalizers will call
  105. // into the plugin). So we leave it to be unloaded automatically on app exit
  106. shutdownPlugin(mSBansheeEditorPlugin);
  107. /************************************************************************/
  108. /* DEBUG CODE */
  109. /************************************************************************/
  110. gResources().unload(mTestTexRef);
  111. gResources().unload(mDbgMeshRef);
  112. gResources().unload(mFragProgRef);
  113. gResources().unload(mVertProgRef);
  114. gResources().unload(mTestMaterial);
  115. mTestMaterial = nullptr;
  116. mTestTexRef = nullptr;
  117. mDbgMeshRef = nullptr;
  118. mFragProgRef = nullptr;
  119. mVertProgRef = nullptr;
  120. mNewPassGL = nullptr;
  121. mNewTechniqueGL = nullptr;
  122. mNewPassDX = nullptr;
  123. mNewTechniqueDX = nullptr;
  124. mNewPassDX11 = nullptr;
  125. mNewTechniqueDX11 = nullptr;
  126. mTestShader = nullptr;
  127. /************************************************************************/
  128. /* END DEBUG CODE */
  129. /************************************************************************/
  130. ProjectLibrary::shutDown();
  131. BuiltinEditorResources::shutDown();
  132. }
  133. void EditorApplication::onStartUp()
  134. {
  135. Application::onStartUp();
  136. MainEditorWindow* mainWindow = MainEditorWindow::create(getPrimaryWindow());
  137. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin); // Managed part of the editor
  138. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  139. if (layout != nullptr)
  140. EditorWidgetManager::instance().setLayout(layout);
  141. BuildManager::instance().load(BUILD_DATA_PATH);
  142. /************************************************************************/
  143. /* DEBUG CODE */
  144. /************************************************************************/
  145. HShader dummyParsedShader = Importer::instance().import<Shader>("..\\..\\..\\..\\Data\\Raw\\Engine\\Shaders\\DummyFX.bsl");
  146. assert(dummyParsedShader != nullptr);
  147. RenderAPICore* renderSystem = RenderAPICore::instancePtr();
  148. HSceneObject testModelGO = SceneObject::create("TestMesh");
  149. HRenderable testRenderable = testModelGO->addComponent<Renderable>();
  150. WString psLoc;
  151. WString vsLoc;
  152. GpuProgramProfile psProfile;
  153. GpuProgramProfile vsProfile;
  154. String psEntry;
  155. String vsEntry;
  156. String language;
  157. switch (mActiveRSPlugin)
  158. {
  159. case RenderSystemPlugin::DX11:
  160. {
  161. psLoc = L"..\\..\\..\\..\\Data\\hlsl11_ps.gpuprog";
  162. vsLoc = L"..\\..\\..\\..\\Data\\hlsl11_vs.gpuprog";
  163. language = "hlsl";
  164. psProfile = GPP_FS_4_0;
  165. vsProfile = GPP_VS_4_0;
  166. psEntry = "ps_main";
  167. vsEntry = "vs_main";
  168. break;
  169. }
  170. case RenderSystemPlugin::DX9:
  171. {
  172. psLoc = L"..\\..\\..\\..\\Data\\hlsl9_ps.gpuprog";
  173. vsLoc = L"..\\..\\..\\..\\Data\\hlsl9_vs.gpuprog";
  174. language = "hlsl";
  175. psProfile = GPP_FS_2_0;
  176. vsProfile = GPP_VS_2_0;
  177. psEntry = "ps_main";
  178. vsEntry = "vs_main";
  179. break;
  180. }
  181. case RenderSystemPlugin::OpenGL:
  182. {
  183. psLoc = L"..\\..\\..\\..\\Data\\glsl_ps.gpuprog";
  184. vsLoc = L"..\\..\\..\\..\\Data\\glsl_vs.gpuprog";
  185. language = "glsl";
  186. psProfile = GPP_FS_2_0;
  187. vsProfile = GPP_VS_2_0;
  188. psEntry = "main";
  189. vsEntry = "main";
  190. break;
  191. }
  192. }
  193. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(psLoc);
  194. if (rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  195. {
  196. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  197. importOptions->setEntryPoint(psEntry);
  198. importOptions->setLanguage(language);
  199. importOptions->setProfile(psProfile);
  200. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  201. }
  202. mFragProgRef = Importer::instance().import<GpuProgram>(psLoc, gpuProgImportOptions);
  203. gpuProgImportOptions = Importer::instance().createImportOptions(vsLoc);
  204. if (rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  205. {
  206. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  207. importOptions->setEntryPoint(vsEntry);
  208. importOptions->setLanguage(language);
  209. importOptions->setProfile(vsProfile);
  210. importOptions->setType(GPT_VERTEX_PROGRAM);
  211. }
  212. mVertProgRef = Importer::instance().import<GpuProgram>(vsLoc, gpuProgImportOptions);
  213. gResources().save(mVertProgRef, L"C:\\vertProgCg.vprog", true);
  214. gResources().unload(mVertProgRef);
  215. mVertProgRef = gResources().load<GpuProgram>(L"C:\\vertProgCg.vprog");
  216. gResources().save(mFragProgRef, L"C:\\fragProgCg.vprog", true);
  217. gResources().unload(mFragProgRef);
  218. mFragProgRef = gResources().load<GpuProgram>(L"C:\\fragProgCg.vprog");
  219. PASS_DESC passDesc;
  220. passDesc.vertexProgram = mVertProgRef;
  221. passDesc.fragmentProgram = mFragProgRef;
  222. mNewPassGL = Pass::create(passDesc);
  223. mNewTechniqueGL = Technique::create(RenderAPIOpenGL, "BansheeRenderer", { mNewPassGL });
  224. // TODO - I need to create different techniques for different render systems (and renderers, if there were any),
  225. // which is redundant as some techniques can be reused. I should add a functionality that supports multiple
  226. // render systems/renderers per technique
  227. mNewPassDX = Pass::create(passDesc);
  228. mNewTechniqueDX = Technique::create(RenderAPIDX9, "BansheeRenderer", { mNewPassDX });
  229. mNewPassDX11 = Pass::create(passDesc);
  230. mNewTechniqueDX11 = Technique::create(RenderAPIDX11, "BansheeRenderer", { mNewPassDX11 });
  231. SHADER_DESC shaderDesc;
  232. shaderDesc.addParameter("matWorldViewProj", "matWorldViewProj", GPDT_MATRIX_4X4, RPS_WorldViewProjTfrm);
  233. shaderDesc.addParameter("samp", "samp", GPOT_SAMPLER2D);
  234. shaderDesc.addParameter("tex", "tex", GPOT_TEXTURE2D);
  235. shaderDesc.setParamBlockAttribs("PerObject", true, GPBU_DYNAMIC, RBS_PerObject);
  236. mTestShader = Shader::create("TestShader", shaderDesc, { mNewTechniqueGL, mNewTechniqueDX, mNewTechniqueDX11 });
  237. mTestMaterial = Material::create(mTestShader);
  238. mTestTexRef = static_resource_cast<Texture>(Importer::instance().import(L"..\\..\\..\\..\\Data\\Examples\\Dragon.tga"));
  239. mDbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(L"..\\..\\..\\..\\Data\\Examples\\Dragon.fbx"));
  240. gResources().save(mTestTexRef, L"C:\\ExportTest.tex", true);
  241. gResources().save(mDbgMeshRef, L"C:\\ExportMesh.mesh", true);
  242. gResources().unload(mTestTexRef);
  243. gResources().unload(mDbgMeshRef);
  244. mTestTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"C:\\ExportTest.tex"));
  245. mDbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"C:\\ExportMesh.mesh"));
  246. mDbgMeshRef.blockUntilLoaded();
  247. mDbgMeshRef->blockUntilCoreInitialized();
  248. mTestTexRef.blockUntilLoaded();
  249. mTestTexRef->blockUntilCoreInitialized();
  250. mTestMaterial->setTexture("tex", mTestTexRef);
  251. gResources().save(mTestShader, L"C:\\ExportShader.asset", true);
  252. gResources().save(mTestMaterial, L"C:\\ExportMaterial.mat", true);
  253. gResources().unload(mTestMaterial);
  254. gResources().unload(mTestShader);
  255. mTestShader = gResources().load<Shader>(L"C:\\ExportShader.asset");
  256. mTestMaterial = gResources().load<Material>(L"C:\\ExportMaterial.mat");
  257. testRenderable->setMesh(mDbgMeshRef);
  258. testRenderable->setMaterial(0, mTestMaterial);
  259. GameObjectHandle<DbgTestGameObjectRef> dbgTestGameObjectRef = testModelGO->addComponent<DbgTestGameObjectRef>();
  260. dbgTestGameObjectRef->mRenderable = testRenderable;
  261. HSceneObject clone = testModelGO->clone();
  262. GameObjectHandle<DbgTestGameObjectRef> clonedDbgTestGameObjectRef = clone->getComponent<DbgTestGameObjectRef>();
  263. clone->setScale(Vector3::ONE * 0.01f);
  264. testModelGO->destroy();
  265. //Win32FolderMonitor* folderMonitor = bs_new<Win32FolderMonitor>();
  266. //FolderChange folderChanges = (FolderChange)((UINT32)FolderChange::FileName | (UINT32)FolderChange::DirName |
  267. // (UINT32)FolderChange::Creation | (UINT32)FolderChange::LastWrite);
  268. //folderMonitor->startMonitor(L"D:\\TestDetect", true, folderChanges);
  269. //HTexture dbgCursor = static_resource_cast<Texture>(Importer::instance().import(L"C:\\CursorDbg.psd"));
  270. //PixelDataPtr cursorPixelData = dbgCursor->allocateSubresourceBuffer(0);
  271. //gMainSyncedCA().readSubresource(dbgCursor.getInternalPtr(), 0, cursorPixelData);
  272. //gMainSyncedCA().submitToCoreThread(true);
  273. /************************************************************************/
  274. /* MODAL WINDOW */
  275. /************************************************************************/
  276. //RENDER_WINDOW_DESC modalWindowDesc;
  277. //modalWindowDesc.width = 200;
  278. //modalWindowDesc.height = 200;
  279. //modalWindowDesc.left = 0;
  280. //modalWindowDesc.top = 0;
  281. //modalWindowDesc.title = "ModalWindow";
  282. //modalWindowDesc.fullscreen = false;
  283. //modalWindowDesc.border = WindowBorder::None;
  284. //modalWindowDesc.toolWindow = true;
  285. //modalWindowDesc.modal = true;
  286. //RenderWindowPtr modalWindow = RenderWindow::create(modalWindowDesc, gApplication().getPrimaryWindow());
  287. //HSceneObject modalSceneObject = SceneObject::create("ModalWindow");
  288. //HCamera modalCamera = modalSceneObject->addComponent<Camera>();
  289. //modalCamera->initialize(modalWindow, 0.0f, 0.0f, 1.0f, 1.0f);
  290. //modalCamera->setNearClipDistance(5);
  291. //modalCamera->setAspectRatio(1.0f);
  292. //modalCamera->setIgnoreSceneRenderables(true);
  293. //HGUIWidget modalGUI = modalSceneObject->addComponent<GUIWidget>(modalCamera->getViewport().get());
  294. //modalGUI->setDepth(128);
  295. //modalGUI->setSkin(EditorGUI::instance().getSkin());
  296. //GUIArea* modalArea = GUIArea::createStretchedXY(*modalGUI, 0, 0, 0, 0, 500);
  297. //GUIButton* modalButton = GUIButton::create(*modalGUI, HString(L"Close"));
  298. //modalButton->onClick.connect(std::bind(&EditorApplication::closeModalWindow, modalWindow, modalSceneObject));
  299. //modalArea->getLayout().addElement(modalButton);
  300. /************************************************************************/
  301. /* END DEBUG CODE */
  302. /************************************************************************/
  303. DbgEditorWidget1::open(); // DEBUG ONLY
  304. DbgEditorWidget2::open(); // DEBUG ONLY
  305. }
  306. void EditorApplication::onShutDown()
  307. {
  308. Application::onShutDown();
  309. }
  310. void EditorApplication::startUp(RenderSystemPlugin renderSystemPlugin)
  311. {
  312. CoreApplication::startUp<EditorApplication>(renderSystemPlugin);
  313. }
  314. void EditorApplication::closeModalWindow(RenderWindowPtr window, HSceneObject sceneObject)
  315. {
  316. //sceneObject->destroy();
  317. window->destroy();
  318. }
  319. void EditorApplication::preUpdate()
  320. {
  321. Application::preUpdate();
  322. EditorWidgetManager::instance().update();
  323. }
  324. void EditorApplication::postUpdate()
  325. {
  326. Application::postUpdate();
  327. ProjectLibrary::instance().update();
  328. EditorWindowManager::instance().update();
  329. }
  330. bool EditorApplication::isProjectLoaded() const
  331. {
  332. return true; // TODO - DEBUG ONLY
  333. }
  334. bool EditorApplication::isGameViewFocused() const
  335. {
  336. return false; // TODO
  337. }
  338. bool EditorApplication::isSceneViewFocused() const
  339. {
  340. return true; // TODO
  341. }
  342. const Path& EditorApplication::getProjectPath() const
  343. {
  344. static Path dummyProjectPath = L"D:\\DummyBansheeProject\\";
  345. return dummyProjectPath;
  346. }
  347. const WString& EditorApplication::getProjectName() const
  348. {
  349. static WString dummyProjectName = L"DummyBansheeProject";
  350. return dummyProjectName;
  351. }
  352. Path EditorApplication::getEditorAssemblyPath() const
  353. {
  354. Path assemblyPath = getBuiltinAssemblyFolder();
  355. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  356. return assemblyPath;
  357. }
  358. Path EditorApplication::getEditorScriptAssemblyPath() const
  359. {
  360. Path assemblyPath = getScriptAssemblyFolder();
  361. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  362. return assemblyPath;
  363. }
  364. Path EditorApplication::getScriptAssemblyFolder() const
  365. {
  366. Path assemblyFolder = getProjectPath();
  367. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  368. return assemblyFolder;
  369. }
  370. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  371. {
  372. Path layoutPath = getProjectPath();
  373. layoutPath.append(WIDGET_LAYOUT_PATH);
  374. if(FileSystem::exists(layoutPath))
  375. {
  376. FileDecoder fs(layoutPath);
  377. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  378. }
  379. return nullptr;
  380. }
  381. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  382. {
  383. Path layoutPath = getProjectPath();
  384. layoutPath.append(WIDGET_LAYOUT_PATH);
  385. FileEncoder fs(layoutPath);
  386. fs.encode(layout.get());
  387. }
  388. EditorApplication& gEditorApplication()
  389. {
  390. return static_cast<EditorApplication&>(EditorApplication::instance());
  391. }
  392. }