BsEditorApplication.cpp 15 KB

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