BsEditorApplication.cpp 10 KB

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