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