BsEditorApplication.cpp 14 KB

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