BsEditorApplication.cpp 10.0 KB

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