BsEditorApplication.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "BsEditorApplication.h"
  2. #include "BsEditorWindowManager.h"
  3. #include "BsEditorWidgetManager.h"
  4. #include "BsMainEditorWindow.h"
  5. #include "BsRenderWindow.h"
  6. #include "BsBuiltinEditorResources.h"
  7. #include "BsUndoRedo.h"
  8. #include "BsFileSerializer.h"
  9. #include "BsFileSystem.h"
  10. #include "BsResourceImporter.h"
  11. #include "BsEditorWidgetLayout.h"
  12. #include "BsScenePicking.h"
  13. #include "BsSelection.h"
  14. #include "BsGizmoManager.h"
  15. #include "BsCodeEditor.h"
  16. #include "BsBuildManager.h"
  17. #include "BsScriptCodeImporter.h"
  18. #include "BsShaderIncludeHandler.h"
  19. #include "BsDropDownWindowManager.h"
  20. // DEBUG ONLY
  21. #include "BsResources.h"
  22. #include "BsSceneObject.h"
  23. #include "BsImporter.h"
  24. #include "BsGpuProgram.h"
  25. #include "BsShader.h"
  26. #include "BsTexture.h"
  27. #include "BsMaterial.h"
  28. #include "BsTechnique.h"
  29. #include "BsPass.h"
  30. #include "BsRenderable.h"
  31. #include "BsVirtualInput.h"
  32. #include "BsFolderMonitor.h"
  33. #include "BsProjectLibrary.h"
  34. #include "BsCamera.h"
  35. #include "BsGUIWidget.h"
  36. #include "BsGUIButton.h"
  37. #include "BsGUILayout.h"
  38. #include "BsEvent.h"
  39. #include "BsCoreRenderer.h"
  40. #include "BsEditorSettings.h"
  41. #include "BsMesh.h"
  42. #include "BsMath.h"
  43. #include "BsDebug.h"
  44. namespace BansheeEngine
  45. {
  46. const Path EditorApplication::WIDGET_LAYOUT_PATH = L"Internal\\Layout.asset";
  47. const Path EditorApplication::BUILD_DATA_PATH = L"Internal\\BuildData.asset";
  48. RENDER_WINDOW_DESC createRenderWindowDesc()
  49. {
  50. RENDER_WINDOW_DESC renderWindowDesc;
  51. renderWindowDesc.videoMode = VideoMode(1280, 720);
  52. renderWindowDesc.title = "BansheeEditor";
  53. renderWindowDesc.fullscreen = false;
  54. renderWindowDesc.border = WindowBorder::None;
  55. renderWindowDesc.hideUntilSwap = true;
  56. return renderWindowDesc;
  57. }
  58. EditorApplication::EditorApplication(RenderSystemPlugin renderSystemPlugin)
  59. :Application(createRenderWindowDesc(), renderSystemPlugin, RendererPlugin::Default),
  60. mActiveRSPlugin(renderSystemPlugin), mSBansheeEditorPlugin(nullptr)
  61. {
  62. }
  63. EditorApplication::~EditorApplication()
  64. {
  65. shutdownPlugin(mSBansheeEditorPlugin);
  66. /************************************************************************/
  67. /* DEBUG CODE */
  68. /************************************************************************/
  69. gResources().unload(mTestTexRef);
  70. gResources().unload(mDbgMeshRef);
  71. gResources().unload(mTestShader);
  72. gResources().unload(mTestMaterial);
  73. mTestMaterial = nullptr;
  74. mTestTexRef = nullptr;
  75. mDbgMeshRef = nullptr;
  76. mTestShader = nullptr;
  77. /************************************************************************/
  78. /* END DEBUG CODE */
  79. /************************************************************************/
  80. ProjectLibrary::shutDown();
  81. BuiltinEditorResources::shutDown();
  82. }
  83. void EditorApplication::onStartUp()
  84. {
  85. Application::onStartUp();
  86. // TODO - Load project settings
  87. mEditorSettings = bs_shared_ptr<EditorSettings>();
  88. BuiltinEditorResources::startUp();
  89. {
  90. auto inputConfig = VirtualInput::instance().getConfiguration();
  91. inputConfig->registerButton("Rename", BC_F2);
  92. inputConfig->registerButton("Undo", BC_Z, ButtonModifier::Ctrl);
  93. inputConfig->registerButton("Redo", BC_Y, ButtonModifier::Ctrl);
  94. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  95. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  96. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  97. inputConfig->registerButton("Delete", BC_DELETE);
  98. }
  99. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  100. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  101. ResourceImporter* resourceImporter = bs_new<ResourceImporter>();
  102. Importer::instance()._registerAssetImporter(resourceImporter);
  103. ProjectLibrary::startUp(getProjectPath());
  104. UndoRedo::startUp();
  105. EditorWindowManager::startUp();
  106. EditorWidgetManager::startUp();
  107. DropDownWindowManager::startUp();
  108. ScenePicking::startUp();
  109. Selection::startUp();
  110. GizmoManager::startUp();
  111. BuildManager::startUp();
  112. CodeEditorManager::startUp();
  113. MainEditorWindow* mainWindow = MainEditorWindow::create(getPrimaryWindow());
  114. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin); // Managed part of the editor
  115. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  116. if (layout != nullptr)
  117. EditorWidgetManager::instance().setLayout(layout);
  118. BuildManager::instance().load(BUILD_DATA_PATH);
  119. /************************************************************************/
  120. /* DEBUG CODE */
  121. /************************************************************************/
  122. HShader dummyParsedShader = Importer::instance().import<Shader>("..\\..\\..\\..\\Data\\Raw\\Engine\\Shaders\\TestFX.bsl");
  123. assert(dummyParsedShader != nullptr); // Ad hoc unit test
  124. RenderAPICore* renderSystem = RenderAPICore::instancePtr();
  125. HSceneObject testModelGO = SceneObject::create("TestMesh");
  126. HRenderable testRenderable = testModelGO->addComponent<Renderable>();
  127. WString testShaderLoc = L"..\\..\\..\\..\\Data\\Test.bsl";;
  128. mTestShader = Importer::instance().import<Shader>(testShaderLoc);
  129. gResources().save(mTestShader, L"C:\\testShader.asset", true);
  130. gResources().unload(mTestShader);
  131. mTestShader = gResources().load<Shader>(L"C:\\testShader.asset");
  132. mTestMaterial = Material::create(mTestShader);
  133. mTestTexRef = static_resource_cast<Texture>(Importer::instance().import(L"..\\..\\..\\..\\Data\\Examples\\Dragon.tga"));
  134. mDbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(L"..\\..\\..\\..\\Data\\Examples\\Dragon.fbx"));
  135. gResources().save(mTestTexRef, L"C:\\ExportTest.tex", true);
  136. gResources().save(mDbgMeshRef, L"C:\\ExportMesh.mesh", true);
  137. gResources().unload(mTestTexRef);
  138. gResources().unload(mDbgMeshRef);
  139. mTestTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"C:\\ExportTest.tex"));
  140. mDbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"C:\\ExportMesh.mesh"));
  141. mDbgMeshRef.blockUntilLoaded();
  142. mDbgMeshRef->blockUntilCoreInitialized();
  143. mTestTexRef.blockUntilLoaded();
  144. mTestTexRef->blockUntilCoreInitialized();
  145. mTestMaterial->setTexture("tex", mTestTexRef);
  146. gResources().save(mTestShader, L"C:\\ExportShader.asset", true);
  147. gResources().save(mTestMaterial, L"C:\\ExportMaterial.mat", true);
  148. gResources().unload(mTestMaterial);
  149. gResources().unload(mTestShader);
  150. mTestShader = gResources().load<Shader>(L"C:\\ExportShader.asset");
  151. mTestMaterial = gResources().load<Material>(L"C:\\ExportMaterial.mat");
  152. testRenderable->setMesh(mDbgMeshRef);
  153. testRenderable->setMaterial(0, mTestMaterial);
  154. HSceneObject clone = testModelGO->clone();
  155. testModelGO->destroy();
  156. /************************************************************************/
  157. /* END DEBUG CODE */
  158. /************************************************************************/
  159. }
  160. void EditorApplication::onShutDown()
  161. {
  162. BuildManager::instance().save(BUILD_DATA_PATH);
  163. CodeEditorManager::shutDown();
  164. BuildManager::shutDown();
  165. GizmoManager::shutDown();
  166. Selection::shutDown();
  167. ScenePicking::shutDown();
  168. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  169. // TODO - Save project settings
  170. DropDownWindowManager::shutDown();
  171. EditorWidgetManager::shutDown();
  172. EditorWindowManager::shutDown();
  173. UndoRedo::shutDown();
  174. Application::onShutDown();
  175. }
  176. void EditorApplication::startUp(RenderSystemPlugin renderSystemPlugin)
  177. {
  178. CoreApplication::startUp<EditorApplication>(renderSystemPlugin);
  179. }
  180. void EditorApplication::preUpdate()
  181. {
  182. Application::preUpdate();
  183. EditorWidgetManager::instance().update();
  184. DropDownWindowManager::instance().update();
  185. }
  186. void EditorApplication::postUpdate()
  187. {
  188. Application::postUpdate();
  189. ProjectLibrary::instance().update();
  190. EditorWindowManager::instance().update();
  191. }
  192. bool EditorApplication::isProjectLoaded() const
  193. {
  194. return true; // TODO - DEBUG ONLY
  195. }
  196. const Path& EditorApplication::getProjectPath() const
  197. {
  198. static Path dummyProjectPath = L"D:\\DummyBansheeProject\\";
  199. return dummyProjectPath;
  200. }
  201. const WString& EditorApplication::getProjectName() const
  202. {
  203. static WString dummyProjectName = L"DummyBansheeProject";
  204. return dummyProjectName;
  205. }
  206. Path EditorApplication::getEditorAssemblyPath() const
  207. {
  208. Path assemblyPath = getBuiltinAssemblyFolder();
  209. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  210. return assemblyPath;
  211. }
  212. Path EditorApplication::getEditorScriptAssemblyPath() const
  213. {
  214. Path assemblyPath = getScriptAssemblyFolder();
  215. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  216. return assemblyPath;
  217. }
  218. Path EditorApplication::getScriptAssemblyFolder() const
  219. {
  220. Path assemblyFolder = getProjectPath();
  221. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  222. return assemblyFolder;
  223. }
  224. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  225. {
  226. Path layoutPath = getProjectPath();
  227. layoutPath.append(WIDGET_LAYOUT_PATH);
  228. if(FileSystem::exists(layoutPath))
  229. {
  230. FileDecoder fs(layoutPath);
  231. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  232. }
  233. return nullptr;
  234. }
  235. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  236. {
  237. Path layoutPath = getProjectPath();
  238. layoutPath.append(WIDGET_LAYOUT_PATH);
  239. FileEncoder fs(layoutPath);
  240. fs.encode(layout.get());
  241. }
  242. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  243. {
  244. return bs_shared_ptr<EditorShaderIncludeHandler>();
  245. }
  246. EditorApplication& gEditorApplication()
  247. {
  248. return static_cast<EditorApplication&>(EditorApplication::instance());
  249. }
  250. }