BsEditorApplication.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include "BsEditorApplication.h"
  2. #include "BsEditorWindowManager.h"
  3. #include "BsEditorWidgetManager.h"
  4. #include "BsMainEditorWindow.h"
  5. #include "BsApplication.h"
  6. #include "CmApplication.h"
  7. #include "CmRenderWindow.h"
  8. #include "BsEditorGUI.h"
  9. #include "BsUndoRedo.h"
  10. #include "CmFileSerializer.h"
  11. #include "CmFileSystem.h"
  12. #include "CmPath.h"
  13. #include "BsEditorWidgetLayout.h"
  14. // DEBUG ONLY
  15. #include "DbgEditorWidget1.h"
  16. #include "DbgEditorWidget2.h"
  17. #include "CmResources.h"
  18. #include "CmSceneObject.h"
  19. #include "CmImporter.h"
  20. #include "CmGpuProgram.h"
  21. #include "CmGpuProgramImportOptions.h"
  22. #include "CmShader.h"
  23. #include "CmTexture.h"
  24. #include "CmMaterial.h"
  25. #include "CmTechnique.h"
  26. #include "CmPass.h"
  27. #include "BsRenderable.h"
  28. #include "BsDbgTestGameObjectRef.h"
  29. #include "BsVirtualInput.h"
  30. #include "CmWin32FolderMonitor.h"
  31. #include "BsProjectLibrary.h"
  32. #include "BsCamera.h"
  33. #include "BsGUIWidget.h"
  34. #include "BsGUIArea.h"
  35. #include "BsGUIButton.h"
  36. #include "BsGUILayout.h"
  37. #include "BsEvent.h"
  38. namespace BansheeEngine
  39. {
  40. const Path EditorApplication::WIDGET_LAYOUT_PATH = L"Internal\\Layout.asset";
  41. EditorApplication::EditorApplication(RenderSystemPlugin renderSystemPlugin)
  42. :mActiveRSPlugin(renderSystemPlugin)
  43. {
  44. RENDER_WINDOW_DESC renderWindowDesc;
  45. renderWindowDesc.width = 1280;
  46. renderWindowDesc.height = 720;
  47. renderWindowDesc.title = "BansheeEditor";
  48. renderWindowDesc.fullscreen = false;
  49. renderWindowDesc.border = WindowBorder::None;
  50. const String& renderSystemLibraryName = getLibraryNameForRenderSystem(renderSystemPlugin);
  51. gBansheeApp().startUp(renderWindowDesc, renderSystemLibraryName, "BansheeForwardRenderer"); // TODO - Make renderer and resource cache dir customizable
  52. EditorGUI::startUp();
  53. {
  54. auto inputConfig = VirtualInput::instance().getConfiguration();
  55. inputConfig->registerButton("Rename", BC_F2);
  56. inputConfig->registerButton("Undo", BC_Z, VButtonModifier::Ctrl);
  57. inputConfig->registerButton("Redo", BC_Y, VButtonModifier::Ctrl);
  58. inputConfig->registerButton("Copy", BC_C, VButtonModifier::Ctrl);
  59. inputConfig->registerButton("Cut", BC_X, VButtonModifier::Ctrl);
  60. inputConfig->registerButton("Paste", BC_V, VButtonModifier::Ctrl);
  61. inputConfig->registerButton("Delete", BC_DELETE);
  62. }
  63. ProjectLibrary::startUp(getActiveProjectPath());
  64. UndoRedo::startUp();
  65. EditorWindowManager::startUp();
  66. MainEditorWindow* mainWindow = MainEditorWindow::create(gApplication().getPrimaryWindow());
  67. EditorWidgetManager::startUp();
  68. gApplication().loadPlugin("SBansheeEditor"); // Managed part of the editor
  69. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  70. if(layout != nullptr)
  71. EditorWidgetManager::instance().setLayout(layout);
  72. /************************************************************************/
  73. /* DEBUG CODE */
  74. /************************************************************************/
  75. RenderSystem* renderSystem = RenderSystem::instancePtr();
  76. RenderWindowPtr renderWindow = gApplication().getPrimaryWindow();
  77. HSceneObject testModelGO = SceneObject::create("TestMesh");
  78. HRenderable testRenderable = testModelGO->addComponent<Renderable>();
  79. WString psLoc;
  80. WString vsLoc;
  81. GpuProgramProfile psProfile;
  82. GpuProgramProfile vsProfile;
  83. String psEntry;
  84. String vsEntry;
  85. String language;
  86. switch (renderSystemPlugin)
  87. {
  88. case RenderSystemPlugin::DX11:
  89. {
  90. psLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl11_ps.gpuprog";
  91. vsLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl11_vs.gpuprog";
  92. language = "hlsl";
  93. psProfile = GPP_PS_4_0;
  94. vsProfile = GPP_VS_4_0;
  95. psEntry = "ps_main";
  96. vsEntry = "vs_main";
  97. break;
  98. }
  99. case RenderSystemPlugin::DX9:
  100. {
  101. psLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl9_ps.gpuprog";
  102. vsLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl9_vs.gpuprog";
  103. language = "hlsl";
  104. psProfile = GPP_PS_2_0;
  105. vsProfile = GPP_VS_2_0;
  106. psEntry = "ps_main";
  107. vsEntry = "vs_main";
  108. break;
  109. }
  110. case RenderSystemPlugin::OpenGL:
  111. {
  112. psLoc = L"C:\\Projects\\BansheeEngine\\Data\\glsl_ps.gpuprog";
  113. vsLoc = L"C:\\Projects\\BansheeEngine\\Data\\glsl_vs.gpuprog";
  114. language = "glsl";
  115. psProfile = GPP_PS_2_0;
  116. vsProfile = GPP_VS_2_0;
  117. psEntry = "main";
  118. vsEntry = "main";
  119. break;
  120. }
  121. }
  122. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(psLoc);
  123. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  124. {
  125. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  126. importOptions->setEntryPoint(psEntry);
  127. importOptions->setLanguage(language);
  128. importOptions->setProfile(psProfile);
  129. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  130. }
  131. HGpuProgram fragProgRef = Importer::instance().import(psLoc, gpuProgImportOptions);
  132. gpuProgImportOptions = Importer::instance().createImportOptions(vsLoc);
  133. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  134. {
  135. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  136. importOptions->setEntryPoint(vsEntry);
  137. importOptions->setLanguage(language);
  138. importOptions->setProfile(vsProfile);
  139. importOptions->setType(GPT_VERTEX_PROGRAM);
  140. }
  141. HGpuProgram vertProgRef = Importer::instance().import(vsLoc, gpuProgImportOptions);
  142. gResources().save(vertProgRef, L"C:\\vertProgCg.vprog", true);
  143. gResources().unload(vertProgRef);
  144. vertProgRef = gResources().load(L"C:\\vertProgCg.vprog");
  145. gResources().save(fragProgRef, L"C:\\fragProgCg.vprog", true);
  146. gResources().unload(fragProgRef);
  147. fragProgRef = gResources().load(L"C:\\fragProgCg.vprog");
  148. ShaderPtr testShader = Shader::create("TestShader");
  149. testShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
  150. if(renderSystemPlugin == RenderSystemPlugin::DX11)
  151. testShader->addParameter("input", "input", GPDT_STRUCT, 2, 8);
  152. testShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
  153. testShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
  154. TechniquePtr newTechniqueGL = testShader->addTechnique("GLRenderSystem", "ForwardRenderer");
  155. PassPtr newPassGL = newTechniqueGL->addPass();
  156. newPassGL->setVertexProgram(vertProgRef);
  157. newPassGL->setFragmentProgram(fragProgRef);
  158. // TODO - I need to create different techniques for different render systems (and renderers, if there were any),
  159. // which is redundant as some techniques can be reused. I should add a functionality that supports multiple
  160. // render systems/renderers per technique
  161. TechniquePtr newTechniqueDX = testShader->addTechnique("D3D9RenderSystem", "ForwardRenderer");
  162. PassPtr newPassDX = newTechniqueDX->addPass();
  163. newPassDX->setVertexProgram(vertProgRef);
  164. newPassDX->setFragmentProgram(fragProgRef);
  165. TechniquePtr newTechniqueDX11 = testShader->addTechnique("D3D11RenderSystem", "ForwardRenderer");
  166. PassPtr newPassDX11 = newTechniqueDX11->addPass();
  167. newPassDX11->setVertexProgram(vertProgRef);
  168. newPassDX11->setFragmentProgram(fragProgRef);
  169. HMaterial testMaterial = Material::create();
  170. testMaterial->setShader(testShader);
  171. testMaterial->setMat4("matViewProjection", Matrix4::IDENTITY);
  172. if(renderSystemPlugin == RenderSystemPlugin::DX11)
  173. {
  174. float dbgMultipliers1[2];
  175. dbgMultipliers1[0] = 0.0f;
  176. dbgMultipliers1[1] = 0.0f;
  177. float dbgMultipliers2[2];
  178. dbgMultipliers2[0] = 1.0f;
  179. dbgMultipliers2[1] = 1.0f;
  180. testMaterial->setStructData("input", dbgMultipliers1, sizeof(dbgMultipliers1), 0);
  181. testMaterial->setStructData("input", dbgMultipliers2, sizeof(dbgMultipliers2), 1);
  182. }
  183. HTexture testTexRef = static_resource_cast<Texture>(Importer::instance().import(L"C:\\ArenaTowerDFS.psd"));
  184. HMesh dbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(L"C:\\X_Arena_Tower.FBX"));
  185. gResources().save(testTexRef, L"C:\\ExportTest.tex", true);
  186. gResources().save(dbgMeshRef, L"C:\\ExportMesh.mesh", true);
  187. gResources().unload(testTexRef);
  188. gResources().unload(dbgMeshRef);
  189. testTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"C:\\ExportTest.tex"));
  190. dbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"C:\\ExportMesh.mesh"));
  191. dbgMeshRef.synchronize();
  192. testTexRef.synchronize();
  193. testMaterial->setTexture("tex", testTexRef);
  194. gResources().save(testMaterial, L"C:\\ExportMaterial.mat", true);
  195. gResources().unload(testMaterial);
  196. testMaterial = gResources().load(L"C:\\ExportMaterial.mat");
  197. testRenderable->setMesh(dbgMeshRef);
  198. testRenderable->setMaterial(0, testMaterial);
  199. GameObjectHandle<DbgTestGameObjectRef> dbgTestGameObjectRef = testModelGO->addComponent<DbgTestGameObjectRef>();
  200. dbgTestGameObjectRef->mRenderable = testRenderable;
  201. HSceneObject clone = testModelGO->clone();
  202. GameObjectHandle<DbgTestGameObjectRef> clonedDbgTestGameObjectRef = clone->getComponent<DbgTestGameObjectRef>();
  203. testModelGO->destroy();
  204. //Win32FolderMonitor* folderMonitor = cm_new<Win32FolderMonitor>();
  205. //FolderChange folderChanges = (FolderChange)((UINT32)FolderChange::FileName | (UINT32)FolderChange::DirName |
  206. // (UINT32)FolderChange::Creation | (UINT32)FolderChange::LastWrite);
  207. //folderMonitor->startMonitor(L"D:\\TestDetect", true, folderChanges);
  208. //HTexture dbgCursor = static_resource_cast<Texture>(Importer::instance().import(L"C:\\CursorDbg.psd"));
  209. //PixelDataPtr cursorPixelData = dbgCursor->allocateSubresourceBuffer(0);
  210. //gMainSyncedCA().readSubresource(dbgCursor.getInternalPtr(), 0, cursorPixelData);
  211. //gMainSyncedCA().submitToCoreThread(true);
  212. /************************************************************************/
  213. /* MODAL WINDOW */
  214. /************************************************************************/
  215. //RENDER_WINDOW_DESC modalWindowDesc;
  216. //modalWindowDesc.width = 200;
  217. //modalWindowDesc.height = 200;
  218. //modalWindowDesc.left = 0;
  219. //modalWindowDesc.top = 0;
  220. //modalWindowDesc.title = "ModalWindow";
  221. //modalWindowDesc.fullscreen = false;
  222. //modalWindowDesc.border = WindowBorder::None;
  223. //modalWindowDesc.toolWindow = true;
  224. //modalWindowDesc.modal = true;
  225. //RenderWindowPtr modalWindow = RenderWindow::create(modalWindowDesc, gApplication().getPrimaryWindow());
  226. //HSceneObject modalSceneObject = SceneObject::create("ModalWindow");
  227. //HCamera modalCamera = modalSceneObject->addComponent<Camera>();
  228. //modalCamera->initialize(modalWindow, 0.0f, 0.0f, 1.0f, 1.0f);
  229. //modalCamera->setNearClipDistance(5);
  230. //modalCamera->setAspectRatio(1.0f);
  231. //modalCamera->setIgnoreSceneRenderables(true);
  232. //HGUIWidget modalGUI = modalSceneObject->addComponent<GUIWidget>(modalCamera->getViewport().get());
  233. //modalGUI->setDepth(128);
  234. //modalGUI->setSkin(EditorGUI::instance().getSkin());
  235. //GUIArea* modalArea = GUIArea::createStretchedXY(*modalGUI, 0, 0, 0, 0, 500);
  236. //GUIButton* modalButton = GUIButton::create(*modalGUI, HString(L"Close"));
  237. //modalButton->onClick.connect(std::bind(&EditorApplication::closeModalWindow, modalWindow, modalSceneObject));
  238. //modalArea->getLayout().addElement(modalButton);
  239. /************************************************************************/
  240. /* END DEBUG CODE */
  241. /************************************************************************/
  242. gApplication().mainLoopCallback.connect(std::bind(&EditorApplication::update, this));
  243. DbgEditorWidget1::open(); // DEBUG ONLY
  244. DbgEditorWidget2::open(); // DEBUG ONLY
  245. gBansheeApp().runMainLoop();
  246. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  247. EditorWidgetManager::shutDown();
  248. EditorWindowManager::shutDown();
  249. UndoRedo::shutDown();
  250. /************************************************************************/
  251. /* DEBUG CODE */
  252. /************************************************************************/
  253. gResources().unload(testTexRef);
  254. gResources().unload(dbgMeshRef);
  255. gResources().unload(fragProgRef);
  256. gResources().unload(vertProgRef);
  257. gResources().unload(testMaterial);
  258. testMaterial = nullptr;
  259. testTexRef = nullptr;
  260. dbgMeshRef = nullptr;
  261. fragProgRef = nullptr;
  262. vertProgRef = nullptr;
  263. newPassGL = nullptr;
  264. newTechniqueGL = nullptr;
  265. newPassDX = nullptr;
  266. newTechniqueDX = nullptr;
  267. newPassDX11 = nullptr;
  268. newTechniqueDX11 = nullptr;
  269. testShader = nullptr;
  270. renderWindow = nullptr;
  271. /************************************************************************/
  272. /* END DEBUG CODE */
  273. /************************************************************************/
  274. ProjectLibrary::shutDown();
  275. EditorGUI::shutDown();
  276. gBansheeApp().shutDown();
  277. }
  278. void EditorApplication::closeModalWindow(RenderWindowPtr window, HSceneObject sceneObject)
  279. {
  280. //sceneObject->destroy();
  281. window->destroy();
  282. }
  283. EditorApplication::~EditorApplication()
  284. {
  285. // TODO - Move shutdown code from constructor to here. Right now I don't care because cleanup
  286. // isn't working as intended and if I move stuff I will probably break it even more
  287. }
  288. void EditorApplication::runMainLoop()
  289. {
  290. // TODO - Move "runMainLoop" code from constructor to here. Right now I don't care because cleanup
  291. // isn't working as intended and if I move stuff I will probably break it even more
  292. }
  293. void EditorApplication::update()
  294. {
  295. ProjectLibrary::instance().update();
  296. EditorWindowManager::instance().update();
  297. }
  298. bool EditorApplication::isProjectLoaded() const
  299. {
  300. return true; // TODO - DEBUG ONLY
  301. }
  302. const Path& EditorApplication::getActiveProjectPath() const
  303. {
  304. static Path dummyProjectPath = L"D:\\DummyBansheeProject\\";
  305. return dummyProjectPath;
  306. }
  307. const String& EditorApplication::getLibraryNameForRenderSystem(RenderSystemPlugin plugin)
  308. {
  309. static String DX11Name = "CamelotD3D11RenderSystem";
  310. static String DX9Name = "CamelotD3D9RenderSystem";
  311. static String OpenGLName = "CamelotGLRenderSystem";
  312. switch(plugin)
  313. {
  314. case RenderSystemPlugin::DX11:
  315. return DX11Name;
  316. case RenderSystemPlugin::DX9:
  317. return DX9Name;
  318. case RenderSystemPlugin::OpenGL:
  319. return OpenGLName;
  320. }
  321. return StringUtil::BLANK;
  322. }
  323. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  324. {
  325. Path layoutPath = getActiveProjectPath();
  326. layoutPath.append(WIDGET_LAYOUT_PATH);
  327. if(FileSystem::exists(layoutPath))
  328. {
  329. FileSerializer fs;
  330. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode(layoutPath));
  331. }
  332. return nullptr;
  333. }
  334. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  335. {
  336. Path layoutPath = getActiveProjectPath();
  337. layoutPath.append(WIDGET_LAYOUT_PATH);
  338. FileSerializer fs;
  339. fs.encode(layout.get(), layoutPath);
  340. }
  341. }