BsEditorApplication.cpp 15 KB

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