BsEditorApplication.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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(RenderAPIPlugin renderAPIPlugin)
  59. :Application(createRenderWindowDesc(), renderAPIPlugin, RendererPlugin::Default),
  60. mActiveRAPIPlugin(renderAPIPlugin), 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_new<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("Duplicate", BC_D, ButtonModifier::Ctrl);
  98. inputConfig->registerButton("Delete", BC_DELETE);
  99. }
  100. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  101. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  102. ResourceImporter* resourceImporter = bs_new<ResourceImporter>();
  103. Importer::instance()._registerAssetImporter(resourceImporter);
  104. ProjectLibrary::startUp(getProjectPath());
  105. UndoRedo::startUp();
  106. EditorWindowManager::startUp();
  107. EditorWidgetManager::startUp();
  108. DropDownWindowManager::startUp();
  109. ScenePicking::startUp();
  110. Selection::startUp();
  111. GizmoManager::startUp();
  112. BuildManager::startUp();
  113. CodeEditorManager::startUp();
  114. MainEditorWindow* mainWindow = MainEditorWindow::create(getPrimaryWindow());
  115. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin); // Managed part of the editor
  116. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  117. if (layout != nullptr)
  118. EditorWidgetManager::instance().setLayout(layout);
  119. BuildManager::instance().load(BUILD_DATA_PATH);
  120. /************************************************************************/
  121. /* DEBUG CODE */
  122. /************************************************************************/
  123. HShader dummyParsedShader = Importer::instance().import<Shader>("..\\..\\..\\..\\Data\\Raw\\Engine\\Shaders\\TestFX.bsl");
  124. assert(dummyParsedShader != nullptr); // Ad hoc unit test
  125. RenderAPICore* renderAPI = RenderAPICore::instancePtr();
  126. HSceneObject testModelGO = SceneObject::create("TestMesh");
  127. HRenderable testRenderable = testModelGO->addComponent<Renderable>();
  128. WString testShaderLoc = L"..\\..\\..\\..\\Data\\Test.bsl";;
  129. mTestShader = Importer::instance().import<Shader>(testShaderLoc);
  130. gResources().save(mTestShader, L"C:\\testShader.asset", true);
  131. gResources().unload(mTestShader);
  132. mTestShader = gResources().load<Shader>(L"C:\\testShader.asset");
  133. mTestMaterial = Material::create(mTestShader);
  134. mTestTexRef = static_resource_cast<Texture>(Importer::instance().import(L"..\\..\\..\\..\\Data\\Examples\\Dragon.tga"));
  135. mDbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(L"..\\..\\..\\..\\Data\\Examples\\Dragon.fbx"));
  136. gResources().save(mTestTexRef, L"C:\\ExportTest.tex", true);
  137. gResources().save(mDbgMeshRef, L"C:\\ExportMesh.mesh", true);
  138. gResources().unload(mTestTexRef);
  139. gResources().unload(mDbgMeshRef);
  140. mTestTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"C:\\ExportTest.tex"));
  141. mDbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"C:\\ExportMesh.mesh"));
  142. mDbgMeshRef.blockUntilLoaded();
  143. mDbgMeshRef->blockUntilCoreInitialized();
  144. mTestTexRef.blockUntilLoaded();
  145. mTestTexRef->blockUntilCoreInitialized();
  146. mTestMaterial->setTexture("tex", mTestTexRef);
  147. gResources().save(mTestShader, L"C:\\ExportShader.asset", true);
  148. gResources().save(mTestMaterial, L"C:\\ExportMaterial.mat", true);
  149. gResources().unload(mTestMaterial);
  150. gResources().unload(mTestShader);
  151. mTestShader = gResources().load<Shader>(L"C:\\ExportShader.asset");
  152. mTestMaterial = gResources().load<Material>(L"C:\\ExportMaterial.mat");
  153. testRenderable->setMesh(mDbgMeshRef);
  154. testRenderable->setMaterial(0, mTestMaterial);
  155. HSceneObject clone = testModelGO->clone();
  156. testModelGO->destroy();
  157. /************************************************************************/
  158. /* END DEBUG CODE */
  159. /************************************************************************/
  160. }
  161. void EditorApplication::onShutDown()
  162. {
  163. BuildManager::instance().save(BUILD_DATA_PATH);
  164. CodeEditorManager::shutDown();
  165. BuildManager::shutDown();
  166. GizmoManager::shutDown();
  167. Selection::shutDown();
  168. ScenePicking::shutDown();
  169. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  170. // TODO - Save project settings
  171. DropDownWindowManager::shutDown();
  172. EditorWidgetManager::shutDown();
  173. EditorWindowManager::shutDown();
  174. UndoRedo::shutDown();
  175. Application::onShutDown();
  176. }
  177. void EditorApplication::startUp(RenderAPIPlugin renderAPI)
  178. {
  179. CoreApplication::startUp<EditorApplication>(renderAPI);
  180. }
  181. void EditorApplication::preUpdate()
  182. {
  183. Application::preUpdate();
  184. EditorWidgetManager::instance().update();
  185. DropDownWindowManager::instance().update();
  186. }
  187. void EditorApplication::postUpdate()
  188. {
  189. Application::postUpdate();
  190. ProjectLibrary::instance().update();
  191. EditorWindowManager::instance().update();
  192. }
  193. bool EditorApplication::isProjectLoaded() const
  194. {
  195. return true; // TODO - DEBUG ONLY
  196. }
  197. const Path& EditorApplication::getProjectPath() const
  198. {
  199. static Path dummyProjectPath = L"D:\\DummyBansheeProject\\";
  200. return dummyProjectPath;
  201. }
  202. const WString& EditorApplication::getProjectName() const
  203. {
  204. static WString dummyProjectName = L"DummyBansheeProject";
  205. return dummyProjectName;
  206. }
  207. Path EditorApplication::getEditorAssemblyPath() const
  208. {
  209. Path assemblyPath = getBuiltinAssemblyFolder();
  210. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  211. return assemblyPath;
  212. }
  213. Path EditorApplication::getEditorScriptAssemblyPath() const
  214. {
  215. Path assemblyPath = getScriptAssemblyFolder();
  216. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  217. return assemblyPath;
  218. }
  219. Path EditorApplication::getScriptAssemblyFolder() const
  220. {
  221. Path assemblyFolder = getProjectPath();
  222. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  223. return assemblyFolder;
  224. }
  225. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  226. {
  227. Path layoutPath = getProjectPath();
  228. layoutPath.append(WIDGET_LAYOUT_PATH);
  229. if(FileSystem::exists(layoutPath))
  230. {
  231. FileDecoder fs(layoutPath);
  232. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  233. }
  234. return nullptr;
  235. }
  236. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  237. {
  238. Path layoutPath = getProjectPath();
  239. layoutPath.append(WIDGET_LAYOUT_PATH);
  240. FileEncoder fs(layoutPath);
  241. fs.encode(layout.get());
  242. }
  243. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  244. {
  245. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  246. }
  247. EditorApplication& gEditorApplication()
  248. {
  249. return static_cast<EditorApplication&>(EditorApplication::instance());
  250. }
  251. }