BsEditorApplication.cpp 11 KB

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