BsEditorApplication.cpp 11 KB

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