BsEditorApplication.cpp 15 KB

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