BsEditorApplication.cpp 15 KB

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