BsEditorApplication.cpp 10 KB

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